xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 231168c7e77777f69a323bcd80a7956527d34548)
16b4cac81SBjoern A. Zeeb /*-
2ec6185c5SBjoern A. Zeeb  * Copyright (c) 2020-2025 The FreeBSD Foundation
3c272abc5SBjoern A. Zeeb  * Copyright (c) 2020-2025 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
4211db70b6SBjoern A. Zeeb /*
4311db70b6SBjoern A. Zeeb  * TODO:
4411db70b6SBjoern A. Zeeb  * - lots :)
4511db70b6SBjoern A. Zeeb  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
4611db70b6SBjoern A. Zeeb  */
4711db70b6SBjoern A. Zeeb 
486b4cac81SBjoern A. Zeeb #include <sys/param.h>
496b4cac81SBjoern A. Zeeb #include <sys/types.h>
506b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
516b4cac81SBjoern A. Zeeb #include <sys/errno.h>
526b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
536b4cac81SBjoern A. Zeeb #include <sys/module.h>
546b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
5540839418SBjoern A. Zeeb #include <sys/sbuf.h>
566b4cac81SBjoern A. Zeeb #include <sys/socket.h>
576b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
586b4cac81SBjoern A. Zeeb #include <sys/queue.h>
596b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
60527687a9SBjoern A. Zeeb #include <sys/libkern.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net/if.h>
636b4cac81SBjoern A. Zeeb #include <net/if_var.h>
646b4cac81SBjoern A. Zeeb #include <net/if_media.h>
656b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
686b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
696b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
706b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
719fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
746b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
756b4cac81SBjoern A. Zeeb 
766b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
77a8f735a6SBjoern A. Zeeb #include <linux/rculist.h>
786b4cac81SBjoern A. Zeeb #include "linux_80211.h"
796b4cac81SBjoern A. Zeeb 
804a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
8111db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
8275d23d88SBjoern A. Zeeb #define	LKPI_80211_HT
832c44f1ffSBjoern A. Zeeb #define	LKPI_80211_VHT
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
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;
4043e022a91SBjoern A. Zeeb 	else
4053e022a91SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
406f9c7a07dSBjoern A. Zeeb 
407f9c7a07dSBjoern A. Zeeb 	/*
408f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
409f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
410f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
411f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
412f9c7a07dSBjoern A. Zeeb 	 */
4139fb91463SBjoern A. Zeeb 	rx_nss = 0;
414f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4159fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4169fb91463SBjoern A. Zeeb 			rx_nss++;
4179fb91463SBjoern A. Zeeb 	}
418f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
419f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
4209fb91463SBjoern A. Zeeb 
421f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
422f9c7a07dSBjoern A. Zeeb 
423f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
424f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
425f9c7a07dSBjoern A. Zeeb 	else
426f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
427f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
428f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
429f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
430f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
431f9c7a07dSBjoern A. Zeeb 	}
432f9c7a07dSBjoern A. Zeeb #endif
4339fb91463SBjoern A. Zeeb }
4349fb91463SBjoern A. Zeeb #endif
4359fb91463SBjoern A. Zeeb 
4369fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4379fb91463SBjoern A. Zeeb static void
438f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
4399fb91463SBjoern A. Zeeb {
440f9c7a07dSBjoern A. Zeeb 	uint32_t width;
441f9c7a07dSBjoern A. Zeeb 	int rx_nss;
442f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
443f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
4449fb91463SBjoern A. Zeeb 
445f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) {
446f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
4479fb91463SBjoern A. Zeeb 		return;
448f9c7a07dSBjoern A. Zeeb 	}
4499fb91463SBjoern A. Zeeb 
450f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
451f9c7a07dSBjoern A. Zeeb 
452f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
453f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
454f9c7a07dSBjoern A. Zeeb 
4553e022a91SBjoern A. Zeeb 	/*
4563e022a91SBjoern A. Zeeb 	 * If VHT20/40 are selected do not update the bandwidth
4573e022a91SBjoern A. Zeeb 	 * from HT but stya on VHT.
4583e022a91SBjoern A. Zeeb 	 */
4593e022a91SBjoern A. Zeeb 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
4603e022a91SBjoern A. Zeeb 		goto skip_bw;
4613e022a91SBjoern A. Zeeb 
462f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
463f9c7a07dSBjoern A. Zeeb 	switch (width) {
464f9c7a07dSBjoern A. Zeeb #if 0
465f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
466f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
4679fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
468f9c7a07dSBjoern A. Zeeb 		break;
469f9c7a07dSBjoern A. Zeeb #endif
470f9c7a07dSBjoern A. Zeeb 	default:
471f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
472f9c7a07dSBjoern A. Zeeb #if 0
473f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
474f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
475f9c7a07dSBjoern A. Zeeb 		else
476f9c7a07dSBjoern A. Zeeb #endif
4779fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4789fb91463SBjoern A. Zeeb 	}
4793e022a91SBjoern A. Zeeb skip_bw:
480f9c7a07dSBjoern A. Zeeb 
481f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
482f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
483f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
484f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
485f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
486f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
487f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
488f9c7a07dSBjoern A. Zeeb 			break;
489f9c7a07dSBjoern A. Zeeb 		}
490f9c7a07dSBjoern A. Zeeb 	}
491f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
492f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
493f9c7a07dSBjoern A. Zeeb 
494f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
495f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
496f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
497f9c7a07dSBjoern A. Zeeb 		break;
498f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
499f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
500f9c7a07dSBjoern A. Zeeb 		break;
501f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
502f9c7a07dSBjoern A. Zeeb 	default:
503f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
504f9c7a07dSBjoern A. Zeeb 		break;
505f9c7a07dSBjoern A. Zeeb 	}
5069fb91463SBjoern A. Zeeb }
5079fb91463SBjoern A. Zeeb #endif
5089fb91463SBjoern A. Zeeb 
509d9f59799SBjoern A. Zeeb static void
510f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
511f9c7a07dSBjoern A. Zeeb {
512f9c7a07dSBjoern A. Zeeb 
513f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
514f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni);
515f9c7a07dSBjoern A. Zeeb #endif
516f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
517f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni);
518f9c7a07dSBjoern A. Zeeb #endif
519f9c7a07dSBjoern A. Zeeb }
520f9c7a07dSBjoern A. Zeeb 
521389265e3SBjoern A. Zeeb static uint8_t
522389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
523389265e3SBjoern A. Zeeb {
524389265e3SBjoern A. Zeeb 	uint8_t chains;
525389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
526389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
527389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
528389265e3SBjoern A. Zeeb 
529389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
530389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
531389265e3SBjoern A. Zeeb #endif
532389265e3SBjoern A. Zeeb 
533389265e3SBjoern A. Zeeb 	chains = 1;
534389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
535389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
536389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
537389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
538389265e3SBjoern A. Zeeb #endif
539389265e3SBjoern A. Zeeb 
540389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
541389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
542389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
543389265e3SBjoern A. Zeeb #endif
544389265e3SBjoern A. Zeeb 
545389265e3SBjoern A. Zeeb 	return (chains);
546389265e3SBjoern A. Zeeb }
547389265e3SBjoern A. Zeeb 
548f9c7a07dSBjoern A. Zeeb static void
54967674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
55067674c1cSBjoern A. Zeeb     const char *_f, int _l)
551d9f59799SBjoern A. Zeeb {
552d9f59799SBjoern A. Zeeb 
5539d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5549d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
555d9f59799SBjoern A. Zeeb 		return;
556d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
557d9f59799SBjoern A. Zeeb 		return;
558d9f59799SBjoern A. Zeeb 
559d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
56067674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
56167674c1cSBjoern A. Zeeb 	if (ni != NULL)
56267674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
563d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
564d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
56511db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
5669d9ba2b7SBjoern A. Zeeb #endif
567d9f59799SBjoern A. Zeeb }
568d9f59799SBjoern A. Zeeb 
569d9f59799SBjoern A. Zeeb static void
570d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
571d9f59799SBjoern A. Zeeb {
572d9f59799SBjoern A. Zeeb 
573d9f59799SBjoern A. Zeeb 
574a8f735a6SBjoern A. Zeeb 	wiphy_lock(lsta->hw->wiphy);
575a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
576a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
577a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
578a8f735a6SBjoern A. Zeeb 	wiphy_unlock(lsta->hw->wiphy);
579d9f59799SBjoern A. Zeeb }
580d9f59799SBjoern A. Zeeb 
5814f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
5824f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
5834f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
5844f61ef8bSBjoern A. Zeeb {
5854f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
5864f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
5874f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
5884f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
589b6b352e4SBjoern A. Zeeb 	int band, i, tid;
5904f61ef8bSBjoern A. Zeeb 
5914f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
5924f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
5934f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
5944f61ef8bSBjoern A. Zeeb 		return (NULL);
5954f61ef8bSBjoern A. Zeeb 
596a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
5974f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
5984f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
5994f61ef8bSBjoern A. Zeeb 	/*
6000936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
6010936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
6020936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
6030936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
6040936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
6050936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
6060936c648SBjoern A. Zeeb 	 * two separate allocations.
6074f61ef8bSBjoern A. Zeeb 	 */
6080936c648SBjoern A. Zeeb 	lsta->ni = ni;
6094f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
6104f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
6114f61ef8bSBjoern A. Zeeb 
6124f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
6134f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
6144f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
6154f61ef8bSBjoern A. Zeeb 
6164f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
617b6b352e4SBjoern A. Zeeb 
618b6b352e4SBjoern A. Zeeb 	/* TXQ */
6194f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
6204f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
6214f61ef8bSBjoern A. Zeeb 
622d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
6234f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
6244f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
6254f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
6264f61ef8bSBjoern A. Zeeb 			goto cleanup;
6274f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
6284f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
629d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
630d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
631d0d29110SBjoern A. Zeeb 				continue;
632d0d29110SBjoern A. Zeeb 			}
633d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
6344f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
6354f61ef8bSBjoern A. Zeeb 		} else {
636fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
6374f61ef8bSBjoern A. Zeeb 		}
638d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
6390cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
640d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
6414f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
6424f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
6430cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
644d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
645eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
6464f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
6474f61ef8bSBjoern A. Zeeb 	}
6484f61ef8bSBjoern A. Zeeb 
649b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
650b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
651b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
652b6b352e4SBjoern A. Zeeb 
653b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
654b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
655b6b352e4SBjoern A. Zeeb 			continue;
656b6b352e4SBjoern A. Zeeb 
657b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
65873cd1c5dSBjoern A. Zeeb 			switch (band) {
65973cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
66073cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
66173cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
66273cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
66373cd1c5dSBjoern A. Zeeb 				case 110:
66473cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
66573cd1c5dSBjoern A. Zeeb 				case 55:
66673cd1c5dSBjoern A. Zeeb 				case 20:
66773cd1c5dSBjoern A. Zeeb 				case 10:
668b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
66973cd1c5dSBjoern A. Zeeb 					break;
67073cd1c5dSBjoern A. Zeeb 				}
67173cd1c5dSBjoern A. Zeeb 				break;
67273cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
67373cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
67473cd1c5dSBjoern A. Zeeb 				case 240:
67573cd1c5dSBjoern A. Zeeb 				case 120:
67673cd1c5dSBjoern A. Zeeb 				case 60:
67773cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
67873cd1c5dSBjoern A. Zeeb 					break;
67973cd1c5dSBjoern A. Zeeb 				}
68073cd1c5dSBjoern A. Zeeb 				break;
68173cd1c5dSBjoern A. Zeeb 			}
682b6b352e4SBjoern A. Zeeb 		}
683b6b352e4SBjoern A. Zeeb 	}
6849fb91463SBjoern A. Zeeb 
6856ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
6869fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
687f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
6889fb91463SBjoern A. Zeeb 
689f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
6909fb91463SBjoern A. Zeeb 
691f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
692b6b352e4SBjoern A. Zeeb 
6936ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
6946ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
6956ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
6966ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
6976ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
6986ffb7bd4SBjoern A. Zeeb 	}
6996ffb7bd4SBjoern A. Zeeb 
7004f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
701fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
7024f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
7034f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
7040936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
7054f61ef8bSBjoern A. Zeeb 
7064f61ef8bSBjoern A. Zeeb 	return (lsta);
7074f61ef8bSBjoern A. Zeeb 
7084f61ef8bSBjoern A. Zeeb cleanup:
709eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
710eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
711eac3646fSBjoern A. Zeeb 
712eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
713eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
7144f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
715eac3646fSBjoern A. Zeeb 	}
7164f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
7174f61ef8bSBjoern A. Zeeb 	return (NULL);
7184f61ef8bSBjoern A. Zeeb }
7194f61ef8bSBjoern A. Zeeb 
7200936c648SBjoern A. Zeeb static void
7210936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
7220936c648SBjoern A. Zeeb {
7230936c648SBjoern A. Zeeb 	struct mbuf *m;
7240936c648SBjoern A. Zeeb 
7250936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
7260936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
7270936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
7280936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
7290936c648SBjoern A. Zeeb 
7300936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
7310936c648SBjoern A. Zeeb 	IMPROVE();
7320936c648SBjoern A. Zeeb 
733fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
734fa4e4257SBjoern A. Zeeb 
735fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
7360936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
737fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
7380936c648SBjoern A. Zeeb 
7390936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
7400936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
7410936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
7420936c648SBjoern A. Zeeb 
7430936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
7440936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
7450936c648SBjoern A. Zeeb 	while (m != NULL) {
7460936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
7470936c648SBjoern A. Zeeb 
7480936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
7490936c648SBjoern A. Zeeb 		if (nim != NULL)
7500936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
7510936c648SBjoern A. Zeeb 		m_freem(m);
7520936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
7530936c648SBjoern A. Zeeb 	}
7540936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
7550936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
756fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
7570936c648SBjoern A. Zeeb 
7580936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
7590936c648SBjoern A. Zeeb 
7600936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
7610936c648SBjoern A. Zeeb 
7620936c648SBjoern A. Zeeb 	/* Free lsta. */
7630936c648SBjoern A. Zeeb 	lsta->ni = NULL;
7640936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
7650936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
7660936c648SBjoern A. Zeeb }
7670936c648SBjoern A. Zeeb 
7680936c648SBjoern A. Zeeb 
7696b4cac81SBjoern A. Zeeb static enum nl80211_band
7706b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
7716b4cac81SBjoern A. Zeeb {
7726b4cac81SBjoern A. Zeeb 
7736b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
7746b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
7756b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
7766b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
7776b4cac81SBjoern A. Zeeb #ifdef __notyet__
7786b4cac81SBjoern A. Zeeb 	else if ()
7796b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
7806b4cac81SBjoern A. Zeeb 	else if ()
7816b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
7826b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
7836b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
7846b4cac81SBjoern A. Zeeb #endif
7856b4cac81SBjoern A. Zeeb 	else
7866b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
7876b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
7886b4cac81SBjoern A. Zeeb }
7896b4cac81SBjoern A. Zeeb 
7906b4cac81SBjoern A. Zeeb static uint32_t
7916b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
7926b4cac81SBjoern A. Zeeb {
7936b4cac81SBjoern A. Zeeb 
7946b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
7956b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
7966b4cac81SBjoern A. Zeeb 	switch (band) {
7976b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
7986b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
7996b4cac81SBjoern A. Zeeb 		break;
8006b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
8016b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
8026b4cac81SBjoern A. Zeeb 		break;
8036b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
8046b4cac81SBjoern A. Zeeb 		break;
8056b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
8066b4cac81SBjoern A. Zeeb 		break;
8076b4cac81SBjoern A. Zeeb 	default:
8086b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
8096b4cac81SBjoern A. Zeeb 		break;
8106b4cac81SBjoern A. Zeeb 	}
8116b4cac81SBjoern A. Zeeb 
8126b4cac81SBjoern A. Zeeb 	IMPROVE();
8136b4cac81SBjoern A. Zeeb 	return (0x00);
8146b4cac81SBjoern A. Zeeb }
8156b4cac81SBjoern A. Zeeb 
816e3a0b120SBjoern A. Zeeb #if 0
8176b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
8186b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
8196b4cac81SBjoern A. Zeeb {
8206b4cac81SBjoern A. Zeeb 
8216b4cac81SBjoern A. Zeeb 	switch (ac) {
8226b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
8236b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
8246b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
8256b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
8266b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
8276b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
8286b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
8296b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
8306b4cac81SBjoern A. Zeeb 	default:
8316b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
8326b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
8336b4cac81SBjoern A. Zeeb 	}
8346b4cac81SBjoern A. Zeeb }
835e3a0b120SBjoern A. Zeeb #endif
8366b4cac81SBjoern A. Zeeb 
8376b4cac81SBjoern A. Zeeb static enum nl80211_iftype
8386b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
8396b4cac81SBjoern A. Zeeb {
8406b4cac81SBjoern A. Zeeb 
8416b4cac81SBjoern A. Zeeb 	switch (opmode) {
8426b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
8436b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
8446b4cac81SBjoern A. Zeeb 		break;
8456b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
8466b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
8476b4cac81SBjoern A. Zeeb 		break;
8486b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
8496b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
8506b4cac81SBjoern A. Zeeb 		break;
8516b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
8526b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
8536b4cac81SBjoern A. Zeeb 		break;
8546b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
8556b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
8566b4cac81SBjoern A. Zeeb 		break;
8576b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
8586b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
8596b4cac81SBjoern A. Zeeb 		break;
8606b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
8616b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8626b4cac81SBjoern A. Zeeb 	default:
8636b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
8646b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8656b4cac81SBjoern A. Zeeb 	}
8666b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
8676b4cac81SBjoern A. Zeeb }
8686b4cac81SBjoern A. Zeeb 
869b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
87012a511c8SBjoern A. Zeeb static const char *
87112a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
87212a511c8SBjoern A. Zeeb {
87312a511c8SBjoern A. Zeeb 
87412a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
87512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
87612a511c8SBjoern A. Zeeb 		return ("WEP40");
87712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
87812a511c8SBjoern A. Zeeb 		return ("TKIP");
87912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
88012a511c8SBjoern A. Zeeb 		return ("CCMP");
88112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
88212a511c8SBjoern A. Zeeb 		return ("WEP104");
88312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
88412a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
88512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
88612a511c8SBjoern A. Zeeb 		return ("GCMP");
88712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
88812a511c8SBjoern A. Zeeb 		return ("GCMP_256");
88912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
89012a511c8SBjoern A. Zeeb 		return ("CCMP_256");
89112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
89212a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
89312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
89412a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
89512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
89612a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
89712a511c8SBjoern A. Zeeb 	default:
89812a511c8SBjoern A. Zeeb 		return ("??");
89912a511c8SBjoern A. Zeeb 	}
90012a511c8SBjoern A. Zeeb }
90112a511c8SBjoern A. Zeeb 
9026b4cac81SBjoern A. Zeeb static uint32_t
9036b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
9046b4cac81SBjoern A. Zeeb {
9056b4cac81SBjoern A. Zeeb 
9066b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
9076b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
9086b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
9096b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
9106b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
9116b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
912906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
9136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
9146b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
9156b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
9166b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
9176b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
9186b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
9196b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
9206b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
9216b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
92212a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
92312a511c8SBjoern A. Zeeb 		    __func__,
92412a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
92512a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
9266b4cac81SBjoern A. Zeeb 		break;
9276b4cac81SBjoern A. Zeeb 	default:
92812a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
92912a511c8SBjoern A. Zeeb 		    __func__,
93012a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
93112a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
9326b4cac81SBjoern A. Zeeb 	}
9336b4cac81SBjoern A. Zeeb 
9346b4cac81SBjoern A. Zeeb 	return (0);
9356b4cac81SBjoern A. Zeeb }
9366b4cac81SBjoern A. Zeeb 
9376b4cac81SBjoern A. Zeeb static uint32_t
9386b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
9396b4cac81SBjoern A. Zeeb {
9406b4cac81SBjoern A. Zeeb 
9416b4cac81SBjoern A. Zeeb 	switch (cipher) {
9426b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
9436b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
9446b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
9456b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
9466b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
9476b4cac81SBjoern A. Zeeb 		if (keylen < 8)
9486b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
9496b4cac81SBjoern A. Zeeb 		else
9506b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
9516b4cac81SBjoern A. Zeeb 		break;
9526b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
9536b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
9546b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
9556b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
9566b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
9576b4cac81SBjoern A. Zeeb 		break;
9586b4cac81SBjoern A. Zeeb 	default:
9596b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
9606b4cac81SBjoern A. Zeeb 	};
9616b4cac81SBjoern A. Zeeb 	return (0);
9626b4cac81SBjoern A. Zeeb }
9636b4cac81SBjoern A. Zeeb #endif
9646b4cac81SBjoern A. Zeeb 
9656b4cac81SBjoern A. Zeeb #ifdef __notyet__
9666b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
9676b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
9686b4cac81SBjoern A. Zeeb {
9696b4cac81SBjoern A. Zeeb 
9706b4cac81SBjoern A. Zeeb 	/*
9716b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
9726b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
9736b4cac81SBjoern A. Zeeb 	 */
9746b4cac81SBjoern A. Zeeb 	switch (state) {
9756b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
9766b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
9776b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
9786b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
9796b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
9806b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
9816b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
9826b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
9836b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
9846b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
9856b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
9866b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
9876b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
9886b4cac81SBjoern A. Zeeb 	default:
9896b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
9906b4cac81SBjoern A. Zeeb 	};
9916b4cac81SBjoern A. Zeeb 
9926b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
9936b4cac81SBjoern A. Zeeb }
9946b4cac81SBjoern A. Zeeb #endif
9956b4cac81SBjoern A. Zeeb 
9966b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9976b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
9986b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
9996b4cac81SBjoern A. Zeeb {
10006b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10016b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
10026b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
10036b4cac81SBjoern A. Zeeb 	int i, nchans;
10046b4cac81SBjoern A. Zeeb 
10056b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10066b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
10076b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
10086b4cac81SBjoern A. Zeeb 		return (NULL);
10096b4cac81SBjoern A. Zeeb 
10106b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
10116b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
10126b4cac81SBjoern A. Zeeb 		return (NULL);
10136b4cac81SBjoern A. Zeeb 
10146b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
10156b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
10166b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
10176b4cac81SBjoern A. Zeeb 			return (&channels[i]);
10186b4cac81SBjoern A. Zeeb 	}
10196b4cac81SBjoern A. Zeeb 
10206b4cac81SBjoern A. Zeeb 	return (NULL);
10216b4cac81SBjoern A. Zeeb }
10226b4cac81SBjoern A. Zeeb 
10232ac8a218SBjoern A. Zeeb #if 0
10246b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
10256b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
10266b4cac81SBjoern A. Zeeb {
10276b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
10286b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
10296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10306b4cac81SBjoern A. Zeeb 
10316b4cac81SBjoern A. Zeeb 	chan = NULL;
10326b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
10336b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
10346b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
10356b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
10366b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
10376b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
10386b4cac81SBjoern A. Zeeb 	else
10396b4cac81SBjoern A. Zeeb 		c = NULL;
10406b4cac81SBjoern A. Zeeb 
10416b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
10426b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
10436b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
10446b4cac81SBjoern A. Zeeb 	}
10456b4cac81SBjoern A. Zeeb 
10466b4cac81SBjoern A. Zeeb 	return (chan);
10476b4cac81SBjoern A. Zeeb }
10482ac8a218SBjoern A. Zeeb #endif
10496b4cac81SBjoern A. Zeeb 
10502e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
10512e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
10522e183d99SBjoern A. Zeeb {
10532e183d99SBjoern A. Zeeb 	enum nl80211_band band;
10542e183d99SBjoern A. Zeeb 
10552e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
10562e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
10572e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
10582e183d99SBjoern A. Zeeb 		int i;
10592e183d99SBjoern A. Zeeb 
10602e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
10612e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
10622e183d99SBjoern A. Zeeb 			continue;
10632e183d99SBjoern A. Zeeb 
10642e183d99SBjoern A. Zeeb 		channels = supband->channels;
10652e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
10662e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
10672e183d99SBjoern A. Zeeb 				return (&channels[i]);
10682e183d99SBjoern A. Zeeb 		}
10692e183d99SBjoern A. Zeeb 	}
10702e183d99SBjoern A. Zeeb 
10712e183d99SBjoern A. Zeeb 	return (NULL);
10722e183d99SBjoern A. Zeeb }
10732e183d99SBjoern A. Zeeb 
1074b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
10756b4cac81SBjoern A. Zeeb static int
107611db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
107711db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
107811db70b6SBjoern A. Zeeb {
107911db70b6SBjoern A. Zeeb 	int error;
108011db70b6SBjoern A. Zeeb 
108111db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
108211db70b6SBjoern A. Zeeb 		return (0);
108311db70b6SBjoern A. Zeeb 
108411db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
108511db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
108611db70b6SBjoern A. Zeeb 
108711db70b6SBjoern A. Zeeb 	error = 0;
108811db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
108911db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
109011db70b6SBjoern A. Zeeb 		int err;
109111db70b6SBjoern A. Zeeb 
109211db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
109311db70b6SBjoern A. Zeeb 			continue;
109411db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
109511db70b6SBjoern A. Zeeb 
109611db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
109711db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
109811db70b6SBjoern A. Zeeb 		if (err != 0) {
109911db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
110011db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
110111db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
110211db70b6SBjoern A. Zeeb 			error++;
110311db70b6SBjoern A. Zeeb 
110411db70b6SBjoern A. Zeeb 			/*
110511db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
110611db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
110711db70b6SBjoern A. Zeeb 			 * crash (firmware).
110811db70b6SBjoern A. Zeeb 			 */
110911db70b6SBjoern A. Zeeb 			continue;
111011db70b6SBjoern A. Zeeb 		}
111111db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
111211db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
111311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
111411db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
111511db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
111611db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
111711db70b6SBjoern A. Zeeb #endif
111811db70b6SBjoern A. Zeeb 
111911db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
112011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
112111db70b6SBjoern A. Zeeb 	}
112211db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
112311db70b6SBjoern A. Zeeb 	return (error);
112411db70b6SBjoern A. Zeeb }
112511db70b6SBjoern A. Zeeb 
112611db70b6SBjoern A. Zeeb static int
112711db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
11286b4cac81SBjoern A. Zeeb {
11296b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
11306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11326b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
113311db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11346b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11356b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
11366b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11376b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
11386b4cac81SBjoern A. Zeeb 	int error;
11396b4cac81SBjoern A. Zeeb 
11406b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
11416b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
11426b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11436b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11446b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11456b4cac81SBjoern A. Zeeb 
114611db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
114711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
114811db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
114911db70b6SBjoern A. Zeeb 		return (0);
115011db70b6SBjoern A. Zeeb 	}
115111db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
115211db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
115311db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
115411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
115511db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
115611db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
115711db70b6SBjoern A. Zeeb 		return (0);
115811db70b6SBjoern A. Zeeb 	}
115911db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
116011db70b6SBjoern A. Zeeb 
116111db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
116211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
116311db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
116411db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
116511db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
116611db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
116711db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
116811db70b6SBjoern A. Zeeb #endif
116911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
117011db70b6SBjoern A. Zeeb 		return (1);
117111db70b6SBjoern A. Zeeb 	}
117211db70b6SBjoern A. Zeeb 
117311db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
117411db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
117511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
117611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
117711db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
117811db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
117911db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
118011db70b6SBjoern A. Zeeb #endif
118111db70b6SBjoern A. Zeeb 		error = 1;
118211db70b6SBjoern A. Zeeb 		goto out;
118311db70b6SBjoern A. Zeeb 	}
118411db70b6SBjoern A. Zeeb 
118511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
118611db70b6SBjoern A. Zeeb 	if (error != 0) {
118711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
118811db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
118911db70b6SBjoern A. Zeeb 		error = 0;
119011db70b6SBjoern A. Zeeb 		goto out;
119111db70b6SBjoern A. Zeeb 	}
119211db70b6SBjoern A. Zeeb 
119311db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
119411db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
119511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
119611db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
119711db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
119811db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
119911db70b6SBjoern A. Zeeb #endif
120011db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
120111db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
120211db70b6SBjoern A. Zeeb 	error = 1;
120311db70b6SBjoern A. Zeeb out:
120411db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
120511db70b6SBjoern A. Zeeb 	return (error);
120611db70b6SBjoern A. Zeeb }
120711db70b6SBjoern A. Zeeb 
120811db70b6SBjoern A. Zeeb static int
120911db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
121011db70b6SBjoern A. Zeeb {
121111db70b6SBjoern A. Zeeb 
121211db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
121311db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
121411db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
121511db70b6SBjoern A. Zeeb }
121611db70b6SBjoern A. Zeeb 
121711db70b6SBjoern A. Zeeb static int
121811db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
121911db70b6SBjoern A. Zeeb {
122011db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
122111db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
122211db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
122311db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
122411db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
122511db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
122611db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
122711db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
122811db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
122911db70b6SBjoern A. Zeeb 	uint32_t lcipher;
123011db70b6SBjoern A. Zeeb 	int error;
123111db70b6SBjoern A. Zeeb 
123211db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
123311db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
123411db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
123511db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
123611db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
123711db70b6SBjoern A. Zeeb 
123811db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
123911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
124011db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
124111db70b6SBjoern A. Zeeb 		return (0);
124211db70b6SBjoern A. Zeeb 	}
124311db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
124411db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
124511db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
124611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
124711db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
124811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
124911db70b6SBjoern A. Zeeb 		return (0);
125011db70b6SBjoern A. Zeeb 	}
125111db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
125211db70b6SBjoern A. Zeeb 
125311db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
125411db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
125511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
125611db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
125711db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
125811db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
125911db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
126011db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
126111db70b6SBjoern A. Zeeb 	}
126211db70b6SBjoern A. Zeeb 
126311db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
12646b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
126511db70b6SBjoern A. Zeeb 	switch (lcipher) {
126611db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
126711db70b6SBjoern A. Zeeb 		break;
126811db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
126911db70b6SBjoern A. Zeeb 	default:
127012a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
127112a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
127211db70b6SBjoern A. Zeeb 		IMPROVE();
127311db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
127411db70b6SBjoern A. Zeeb 		return (0);
127511db70b6SBjoern A. Zeeb 	}
127611db70b6SBjoern A. Zeeb 
127711db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
127811db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
127911db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
128011db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
12816b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
12826b4cac81SBjoern A. Zeeb #if 0
12836b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
12846b4cac81SBjoern A. Zeeb #endif
12856b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
12866b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
12876b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
12886b4cac81SBjoern A. Zeeb 
128911db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
129011db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
129111db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
129211db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
129311db70b6SBjoern A. Zeeb 
12946b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
12956b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
12966b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
12976b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
12986b4cac81SBjoern A. Zeeb 		break;
12996b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
13006b4cac81SBjoern A. Zeeb 	default:
130111db70b6SBjoern A. Zeeb 		/* currently UNREACH */
13026b4cac81SBjoern A. Zeeb 		IMPROVE();
130311db70b6SBjoern A. Zeeb 		break;
13046b4cac81SBjoern A. Zeeb 	};
130511db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
13066b4cac81SBjoern A. Zeeb 
130711db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
130811db70b6SBjoern A. Zeeb 	if (error != 0) {
130911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
131011db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
131111db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
131211db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
131311db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
131411db70b6SBjoern A. Zeeb 		return (0);
13156b4cac81SBjoern A. Zeeb 	}
13166b4cac81SBjoern A. Zeeb 
131711db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
131811db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
131911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
132011db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
132111db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
132211db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
132311db70b6SBjoern A. Zeeb #endif
132411db70b6SBjoern A. Zeeb 
132511db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
13266b4cac81SBjoern A. Zeeb 	return (1);
13276b4cac81SBjoern A. Zeeb }
13286b4cac81SBjoern A. Zeeb 
13296b4cac81SBjoern A. Zeeb static  int
13306b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
13316b4cac81SBjoern A. Zeeb {
13326b4cac81SBjoern A. Zeeb 
133311db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
13346b4cac81SBjoern A. Zeeb }
1335b8dfc3ecSBjoern A. Zeeb 
1336b8dfc3ecSBjoern A. Zeeb static void
1337b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1338b8dfc3ecSBjoern A. Zeeb {
1339b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1340b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1341b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1342b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1343b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1344b8dfc3ecSBjoern A. Zeeb 	bool islocked;
1345b8dfc3ecSBjoern A. Zeeb 
1346b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1347b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1348b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1349b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1350b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1351b8dfc3ecSBjoern A. Zeeb 
1352b8dfc3ecSBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
1353b8dfc3ecSBjoern A. Zeeb 
1354b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1355b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1356b8dfc3ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: tid %d vap %p nt %p %slocked "
1357b8dfc3ecSBjoern A. Zeeb 		    "lvif nt_unlocked %d\n", __func__, curthread->td_tid,
1358b8dfc3ecSBjoern A. Zeeb 		    vap, nt, islocked ? "" : "un", lvif->nt_unlocked);
1359b8dfc3ecSBjoern A. Zeeb #endif
1360b8dfc3ecSBjoern A. Zeeb 
1361b8dfc3ecSBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
1362b8dfc3ecSBjoern A. Zeeb 	if (islocked)
1363b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1364b8dfc3ecSBjoern A. Zeeb 
1365b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1366b8dfc3ecSBjoern A. Zeeb 
1367b8dfc3ecSBjoern A. Zeeb 	/*
1368b8dfc3ecSBjoern A. Zeeb 	 * nt_unlocked could be a bool given we are under the lock and there
1369b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1370b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1371b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1372b8dfc3ecSBjoern A. Zeeb 	 */
1373b8dfc3ecSBjoern A. Zeeb 	if (islocked)
1374b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1375b8dfc3ecSBjoern A. Zeeb }
1376b8dfc3ecSBjoern A. Zeeb 
1377b8dfc3ecSBjoern A. Zeeb static void
1378b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1379b8dfc3ecSBjoern A. Zeeb {
1380b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1381b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1382b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1383b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1384b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1385b8dfc3ecSBjoern A. Zeeb 	bool islocked;
1386b8dfc3ecSBjoern A. Zeeb 
1387b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1388b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1389b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1390b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1391b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1392b8dfc3ecSBjoern A. Zeeb 
1393b8dfc3ecSBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
1394b8dfc3ecSBjoern A. Zeeb 	MPASS(!islocked);
1395b8dfc3ecSBjoern A. Zeeb 
1396b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1397b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1398b8dfc3ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: tid %d vap %p nt %p %slocked "
1399b8dfc3ecSBjoern A. Zeeb 		    "lvif nt_unlocked %d\n", __func__, curthread->td_tid,
1400b8dfc3ecSBjoern A. Zeeb 		    vap, nt, islocked ? "" : "un", lvif->nt_unlocked);
1401b8dfc3ecSBjoern A. Zeeb #endif
1402b8dfc3ecSBjoern A. Zeeb 
1403b8dfc3ecSBjoern A. Zeeb 	/*
1404b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1405b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1406b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1407b8dfc3ecSBjoern A. Zeeb 	 */
1408b8dfc3ecSBjoern A. Zeeb 	islocked = refcount_release_if_last(&lvif->nt_unlocked);
1409b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1410b8dfc3ecSBjoern A. Zeeb 
1411b8dfc3ecSBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
1412b8dfc3ecSBjoern A. Zeeb 	if (islocked)
1413b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1414b8dfc3ecSBjoern A. Zeeb }
14156b4cac81SBjoern A. Zeeb #endif
14166b4cac81SBjoern A. Zeeb 
14176b4cac81SBjoern A. Zeeb static u_int
14186b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
14196b4cac81SBjoern A. Zeeb {
14206b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
14216b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
14226b4cac81SBjoern A. Zeeb 
14236b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
14246b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
14256b4cac81SBjoern A. Zeeb 
14266b4cac81SBjoern A. Zeeb 	mc_list = arg;
14276b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
14286b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
14296b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
14306b4cac81SBjoern A. Zeeb 			return (0);
14316b4cac81SBjoern A. Zeeb 	}
14326b4cac81SBjoern A. Zeeb 
14336b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
14346b4cac81SBjoern A. Zeeb 	if (addr == NULL)
14356b4cac81SBjoern A. Zeeb 		return (0);
14366b4cac81SBjoern A. Zeeb 
14376b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
14386b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
14396b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
14406b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
14416b4cac81SBjoern A. Zeeb 	mc_list->count++;
14426b4cac81SBjoern A. Zeeb 
14439d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14449d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
14456b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
14466b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
14479d9ba2b7SBjoern A. Zeeb #endif
14486b4cac81SBjoern A. Zeeb 
14496b4cac81SBjoern A. Zeeb 	return (1);
14506b4cac81SBjoern A. Zeeb }
14516b4cac81SBjoern A. Zeeb 
14526b4cac81SBjoern A. Zeeb static void
14536b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
14546b4cac81SBjoern A. Zeeb {
14556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14576b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
14586b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
14596b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
14606b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
14616b4cac81SBjoern A. Zeeb 	u64 mc;
14626b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
14636b4cac81SBjoern A. Zeeb 
14646b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
14656b4cac81SBjoern A. Zeeb 
14666b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
14676b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
14686b4cac81SBjoern A. Zeeb 		return;
14696b4cac81SBjoern A. Zeeb 
14706b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
14716b4cac81SBjoern A. Zeeb 		return;
14726b4cac81SBjoern A. Zeeb 
14736b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
14746b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
14756b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
14766b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
14776b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
14786b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
14796b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
14806b4cac81SBjoern A. Zeeb 	} else {
14816b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
14826b4cac81SBjoern A. Zeeb 	}
14836b4cac81SBjoern A. Zeeb 
14846b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14856b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
14866b4cac81SBjoern A. Zeeb 	/*
14876b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
14886b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
14896b4cac81SBjoern A. Zeeb 	 */
14906b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
14916b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
14926b4cac81SBjoern A. Zeeb 
14939d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
14956b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
14966b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
14979d9ba2b7SBjoern A. Zeeb #endif
14986b4cac81SBjoern A. Zeeb 
14996b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
15006b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
15016b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
15026b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
15036b4cac81SBjoern A. Zeeb 			mc_list.count--;
15046b4cac81SBjoern A. Zeeb 		}
15056b4cac81SBjoern A. Zeeb 	}
15066b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
15076b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
15086b4cac81SBjoern A. Zeeb }
15096b4cac81SBjoern A. Zeeb 
1510fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1511fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1512fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1513fa8f007dSBjoern A. Zeeb {
1514fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1515fa8f007dSBjoern A. Zeeb 
1516fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1517fa8f007dSBjoern A. Zeeb 
15189d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15199d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1520fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1521fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1522ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1523fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1524616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1525fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1526fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1527fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1528fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1529ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
15309d9ba2b7SBjoern A. Zeeb #endif
1531fa8f007dSBjoern A. Zeeb 
1532fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1533fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1534fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1535fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1536fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1537fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1538fa8f007dSBjoern A. Zeeb 	}
1539fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1540fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1541fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1542fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1543fa8f007dSBjoern A. Zeeb 	}
1544fa8f007dSBjoern A. Zeeb 
1545fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1546fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1547fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1548fa8f007dSBjoern A. Zeeb 
15499d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1551fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1552fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1553ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1554fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1555616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1556fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1557fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1558fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1559fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1560ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
15619d9ba2b7SBjoern A. Zeeb #endif
1562fa8f007dSBjoern A. Zeeb 
1563fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1564fa8f007dSBjoern A. Zeeb }
1565fa8f007dSBjoern A. Zeeb 
15666b4cac81SBjoern A. Zeeb static void
15676b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
15686b4cac81SBjoern A. Zeeb {
15696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15706b4cac81SBjoern A. Zeeb 	int error;
15718ac540d3SBjoern A. Zeeb 	bool cancel;
15726b4cac81SBjoern A. Zeeb 
15738ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15748ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15768ac540d3SBjoern A. Zeeb 	if (!cancel)
15776b4cac81SBjoern A. Zeeb 		return;
15786b4cac81SBjoern A. Zeeb 
15796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15806b4cac81SBjoern A. Zeeb 
1581bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1582bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15836b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
15846b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
15858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15866b4cac81SBjoern A. Zeeb 
15876b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
15888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15898ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
15908ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
15918ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15938ac540d3SBjoern A. Zeeb 
1594bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
15956b4cac81SBjoern A. Zeeb 
15968ac540d3SBjoern A. Zeeb 	if (cancel)
15976b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
15986b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
15996b4cac81SBjoern A. Zeeb }
16006b4cac81SBjoern A. Zeeb 
16016b4cac81SBjoern A. Zeeb static void
1602086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1603086be6a8SBjoern A. Zeeb {
1604086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1605086be6a8SBjoern A. Zeeb 	int error;
1606086be6a8SBjoern A. Zeeb 	bool old;
1607086be6a8SBjoern A. Zeeb 
1608086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1609086be6a8SBjoern A. Zeeb 	if (old == new)
1610086be6a8SBjoern A. Zeeb 		return;
1611086be6a8SBjoern A. Zeeb 
1612086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1613086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1614086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1615086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1616086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1617086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1618086be6a8SBjoern A. Zeeb 	}
1619086be6a8SBjoern A. Zeeb }
1620086be6a8SBjoern A. Zeeb 
16215a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
16226b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
16236b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
16246b4cac81SBjoern A. Zeeb {
16255a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
16265a4d2461SBjoern A. Zeeb 
16275a4d2461SBjoern A. Zeeb 	changed = 0;
16286b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1629616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
16306b4cac81SBjoern A. Zeeb 
16316b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
16326b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
16336b4cac81SBjoern A. Zeeb 
1634616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1635616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
16366b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
16376b4cac81SBjoern A. Zeeb 		IMPROVE();
1638086be6a8SBjoern A. Zeeb 
16395a4d2461SBjoern A. Zeeb 		/*
16405a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
16415a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
16425a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
16435a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
16445a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
16455a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
16465a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
16475a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
16485a4d2461SBjoern A. Zeeb 		 */
16496b4cac81SBjoern A. Zeeb 	}
16505a4d2461SBjoern A. Zeeb 
16515a4d2461SBjoern A. Zeeb 	return (changed);
16526b4cac81SBjoern A. Zeeb }
16536b4cac81SBjoern A. Zeeb 
16546b4cac81SBjoern A. Zeeb static void
16556b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
16566b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
16576b4cac81SBjoern A. Zeeb {
16586b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
16596b4cac81SBjoern A. Zeeb 	int tid;
1660eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
16616b4cac81SBjoern A. Zeeb 
16626b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
16636b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
16646b4cac81SBjoern A. Zeeb 
16656b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
16666b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
16676b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
16686b4cac81SBjoern A. Zeeb 				continue;
16696b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
16706b4cac81SBjoern A. Zeeb 			continue;
16716b4cac81SBjoern A. Zeeb 
16726b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
16736b4cac81SBjoern A. Zeeb 			continue;
16746b4cac81SBjoern A. Zeeb 
16756b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
16766b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
16776b4cac81SBjoern A. Zeeb 			continue;
16786b4cac81SBjoern A. Zeeb 
1679eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1680eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1681eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1682eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
16836b4cac81SBjoern A. Zeeb 			continue;
16846b4cac81SBjoern A. Zeeb 
16856b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
16866b4cac81SBjoern A. Zeeb 	}
16876b4cac81SBjoern A. Zeeb }
16886b4cac81SBjoern A. Zeeb 
168988665349SBjoern A. Zeeb /*
169088665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
169188665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
169288665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
169388665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
169488665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
169588665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
169688665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
169788665349SBjoern A. Zeeb  * re-used.
169888665349SBjoern A. Zeeb  */
169988665349SBjoern A. Zeeb static void
170088665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
170188665349SBjoern A. Zeeb {
170288665349SBjoern A. Zeeb 	struct mbufq mq;
170388665349SBjoern A. Zeeb 	struct mbuf *m;
170488665349SBjoern A. Zeeb 	int len;
170588665349SBjoern A. Zeeb 
170688665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
170788665349SBjoern A. Zeeb 
170888665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
170988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
171088665349SBjoern A. Zeeb 	lsta->txq_ready = false;
171188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
171288665349SBjoern A. Zeeb 
171388665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
171488665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
171588665349SBjoern A. Zeeb 
171688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
171788665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
171888665349SBjoern A. Zeeb 	if (len <= 0) {
171988665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
172088665349SBjoern A. Zeeb 		return;
172188665349SBjoern A. Zeeb 	}
172288665349SBjoern A. Zeeb 
172388665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
172488665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
172588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
172688665349SBjoern A. Zeeb 
172788665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
172888665349SBjoern A. Zeeb 	while (m != NULL) {
172988665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
173088665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
173188665349SBjoern A. Zeeb 	}
173288665349SBjoern A. Zeeb }
173388665349SBjoern A. Zeeb 
173450d826beSBjoern A. Zeeb 
173550d826beSBjoern A. Zeeb static void
173650d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
173750d826beSBjoern A. Zeeb {
173850d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1739*231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
174050d826beSBjoern A. Zeeb 
1741*231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1742*231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1743*231168c7SBjoern A. Zeeb 
1744*231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1745*231168c7SBjoern A. Zeeb 		return;
1746*231168c7SBjoern A. Zeeb 
174750d826beSBjoern A. Zeeb 	/* Remove vif context. */
1748*231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
174950d826beSBjoern A. Zeeb 
175050d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
175150d826beSBjoern A. Zeeb 
175250d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
175350d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1754*231168c7SBjoern A. Zeeb 
1755*231168c7SBjoern A. Zeeb 	/* Cleanup. */
1756*231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
175750d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
175850d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
175950d826beSBjoern A. Zeeb }
176050d826beSBjoern A. Zeeb 
176150d826beSBjoern A. Zeeb 
17626b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
17636b4cac81SBjoern A. Zeeb 
17646b4cac81SBjoern A. Zeeb static int
17656b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17666b4cac81SBjoern A. Zeeb {
17676b4cac81SBjoern A. Zeeb 
17686b4cac81SBjoern A. Zeeb 	return (0);
17696b4cac81SBjoern A. Zeeb }
17706b4cac81SBjoern A. Zeeb 
17716b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
17726b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
17736b4cac81SBjoern A. Zeeb 
17746b4cac81SBjoern A. Zeeb static int
17756b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17766b4cac81SBjoern A. Zeeb {
17776b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1778c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1779d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
17806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17816b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17826b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17836b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17846b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
17856b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
17866b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
17876b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
17886b4cac81SBjoern A. Zeeb 	uint32_t changed;
17896b4cac81SBjoern A. Zeeb 	int error;
17906b4cac81SBjoern A. Zeeb 
17912ac8a218SBjoern A. Zeeb 	/*
17922ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
17932ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
17942ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
17952ac8a218SBjoern A. Zeeb 	 */
17962ac8a218SBjoern A. Zeeb 
17972ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
17982ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
17992ac8a218SBjoern A. Zeeb 		return (EINVAL);
18002ac8a218SBjoern A. Zeeb 	}
18010936c648SBjoern A. Zeeb 	/*
18020936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
18030936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
18040936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
18050936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
18060936c648SBjoern A. Zeeb 	 */
18072ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
18082ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
18092ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
18102ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
18110936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
18122ac8a218SBjoern A. Zeeb 		return (EINVAL);
18136b4cac81SBjoern A. Zeeb 	}
18146b4cac81SBjoern A. Zeeb 
18156b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
18162ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
18172ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
18182ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
18192ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
18200936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
18212ac8a218SBjoern A. Zeeb 		return (ESRCH);
18222ac8a218SBjoern A. Zeeb 	}
18232ac8a218SBjoern A. Zeeb 
18246b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18256b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18266b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
18276b4cac81SBjoern A. Zeeb 
18280936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18290936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
18300936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
18310936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
18320936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
18330936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
18340936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
18350936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
183692690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
183792690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
18380936c648SBjoern A. Zeeb 		return (EBUSY);
18390936c648SBjoern A. Zeeb 	}
18400936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
18410936c648SBjoern A. Zeeb 
18426b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
18438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
18446b4cac81SBjoern A. Zeeb 
18456b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
184611604b2aSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf != NULL) {
184711604b2aSBjoern A. Zeeb 		chanctx_conf = vif->bss_conf.chanctx_conf;
1848d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
18496b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
18506b4cac81SBjoern A. Zeeb 	} else {
18516b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1852c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
18536b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1854d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
18556b4cac81SBjoern A. Zeeb 	}
18566b4cac81SBjoern A. Zeeb 
1857d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1858389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1859d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
18606b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1861d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1862d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
186390d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
186490d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
18659fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
18662ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
18672ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
18689fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
18699fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
187090d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1871d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
187290d8e307SBjoern A. Zeeb 		else
1873d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
18749fb91463SBjoern A. Zeeb 	}
18759fb91463SBjoern A. Zeeb #endif
18769fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
18779fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
18789fb91463SBjoern A. Zeeb #ifdef __notyet__
187990d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1880d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
188190d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1882d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
188390d8e307SBjoern A. Zeeb 		else
188490d8e307SBjoern A. Zeeb #endif
188590d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1886d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
18879fb91463SBjoern A. Zeeb 	}
18889fb91463SBjoern A. Zeeb #endif
1889389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
18906b4cac81SBjoern A. Zeeb 	/* Responder ... */
189190d8e307SBjoern A. Zeeb #if 0
189290d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
1893d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
189490d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
189590d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
189690d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
189790d8e307SBjoern A. Zeeb #endif
189890d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
189990d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
190090d8e307SBjoern A. Zeeb #else
190190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
190290d8e307SBjoern A. Zeeb #endif
19036b4cac81SBjoern A. Zeeb 
19046ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
19056ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
19066ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
19076ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
19086ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
19096ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
19106ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
19116ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
19126ffb7bd4SBjoern A. Zeeb 
19136ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
19146ffb7bd4SBjoern A. Zeeb 
19156ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
19166ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
19176ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
19186ffb7bd4SBjoern A. Zeeb 
19196ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
19206ffb7bd4SBjoern A. Zeeb 
19216b4cac81SBjoern A. Zeeb 	error = 0;
192211604b2aSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf != NULL) {
19236b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
19246b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
19256b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
19266b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1927d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
19286b4cac81SBjoern A. Zeeb 	} else {
1929d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
19306b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
19317b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
19327b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
19337b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1934d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
19357b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1936d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
19376b4cac81SBjoern A. Zeeb 		} else {
1938018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1939018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
19406b4cac81SBjoern A. Zeeb 			goto out;
19416b4cac81SBjoern A. Zeeb 		}
19426ffb7bd4SBjoern A. Zeeb 
1943d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
19446ffb7bd4SBjoern A. Zeeb 
19456b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
19466b4cac81SBjoern A. Zeeb 		if (error == 0)
194768541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1948d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
19496b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
19506b4cac81SBjoern A. Zeeb 			error = 0;
19516b4cac81SBjoern A. Zeeb 		if (error != 0) {
1952018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1953018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1954d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
195550d826beSBjoern A. Zeeb 			vif->bss_conf.chanctx_conf = NULL;
1956d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1957c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
19586b4cac81SBjoern A. Zeeb 			goto out;
19596b4cac81SBjoern A. Zeeb 		}
19606b4cac81SBjoern A. Zeeb 	}
19616b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
19626b4cac81SBjoern A. Zeeb 
19636b4cac81SBjoern A. Zeeb 	/* RATES */
19646b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
19656b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
19666b4cac81SBjoern A. Zeeb 
1967d9f59799SBjoern A. Zeeb 	/*
19680936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
19690936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
19700936c648SBjoern A. Zeeb 	 * under the hoods.
1971d9f59799SBjoern A. Zeeb 	 */
19720936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
19730936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
19746b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1975e24e8103SBjoern A. Zeeb 
197688665349SBjoern A. Zeeb 	/*
197788665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
197888665349SBjoern A. Zeeb 	 * that we can tx again.
197988665349SBjoern A. Zeeb 	 */
198088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
198188665349SBjoern A. Zeeb 	lsta->txq_ready = true;
198288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
198388665349SBjoern A. Zeeb 
1984a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
19852ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1986a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1987a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1988e24e8103SBjoern A. Zeeb 
1989d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
19906b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
19916b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
19926b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1993e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
19946b4cac81SBjoern A. Zeeb 	if (error != 0) {
19956b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1996018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1997018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19986b4cac81SBjoern A. Zeeb 		goto out;
19996b4cac81SBjoern A. Zeeb 	}
20006b4cac81SBjoern A. Zeeb #if 0
20016b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
20026b4cac81SBjoern A. Zeeb #endif
20036b4cac81SBjoern A. Zeeb 
20049d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20059d9ba2b7SBjoern A. Zeeb 
20061665ef97SBjoern A. Zeeb #if 0
20076b4cac81SBjoern A. Zeeb 	/*
20086b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
20096b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
20106b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
20116b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2012d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2013d9f59799SBjoern A. Zeeb 	 * for all queues.
20146b4cac81SBjoern A. Zeeb 	 */
2015e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
20161665ef97SBjoern A. Zeeb #endif
20176b4cac81SBjoern A. Zeeb 
20186b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
20196b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20206b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
20216b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
20226b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
20236b4cac81SBjoern A. Zeeb 
20246b4cac81SBjoern A. Zeeb 	/*
20256b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
20266b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
20276b4cac81SBjoern A. Zeeb 	 * - event_callback
20286b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
20296b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
20306b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
20316b4cac81SBjoern A. Zeeb 	 */
20326b4cac81SBjoern A. Zeeb 
2033105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2034105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2035105b9df2SBjoern A. Zeeb 
2036105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2037105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2038105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2039105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2040105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2041105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2042105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2043105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2044105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2045105b9df2SBjoern A. Zeeb 
2046105b9df2SBjoern A. Zeeb 	/*
2047105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2048105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2049105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2050105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2051105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2052105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2053105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2054105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2055105b9df2SBjoern A. Zeeb 	 */
2056105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2057105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2058105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
2059105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
2060105b9df2SBjoern A. Zeeb 	} else {
2061105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
2062105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
2063105b9df2SBjoern A. Zeeb 		/*
2064105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2065105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2066105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2067105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2068105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2069105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2070105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2071105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
2072105b9df2SBjoern A. Zeeb 		 */
2073105b9df2SBjoern A. Zeeb 	}
2074105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2075105b9df2SBjoern A. Zeeb 	goto out_relocked;
2076105b9df2SBjoern A. Zeeb 
20776b4cac81SBjoern A. Zeeb out:
20788ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20796b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2080105b9df2SBjoern A. Zeeb out_relocked:
20810936c648SBjoern A. Zeeb 	/*
2082105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
20830936c648SBjoern A. Zeeb 	 * during the work of this function.
20840936c648SBjoern A. Zeeb 	 */
20856b4cac81SBjoern A. Zeeb 	if (ni != NULL)
20866b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
20876b4cac81SBjoern A. Zeeb 	return (error);
20886b4cac81SBjoern A. Zeeb }
20896b4cac81SBjoern A. Zeeb 
20906b4cac81SBjoern A. Zeeb static int
20916b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20926b4cac81SBjoern A. Zeeb {
20936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20976b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20986b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20996b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
21006b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21016b4cac81SBjoern A. Zeeb 	int error;
21026b4cac81SBjoern A. Zeeb 
21036b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21056b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21066b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21076b4cac81SBjoern A. Zeeb 
21082ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21092ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21102ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
21112ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
21122ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21132ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21142ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21152ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21162ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21172ac8a218SBjoern A. Zeeb #endif
21182ac8a218SBjoern A. Zeeb 
21192ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21212ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
21222ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
21232ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
21242ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
21256b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
21266b4cac81SBjoern A. Zeeb 
212767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21286b4cac81SBjoern A. Zeeb 
21296b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21308ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21316b4cac81SBjoern A. Zeeb 
2132d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2133d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2134d9f59799SBjoern A. Zeeb 
21356b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
213688665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
21376b4cac81SBjoern A. Zeeb 
21386b4cac81SBjoern A. Zeeb 	/* flush, no drop */
21396b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
21406b4cac81SBjoern A. Zeeb 
21416b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21426b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21436b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21446b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
21456b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21466b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21476b4cac81SBjoern A. Zeeb 	}
21486b4cac81SBjoern A. Zeeb 
21496b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
21506b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
21516b4cac81SBjoern A. Zeeb 
21526b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
21536b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2154d9f59799SBjoern A. Zeeb 
2155d9f59799SBjoern A. Zeeb 	/* Take the station down. */
21566b4cac81SBjoern A. Zeeb 
21576b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
21586b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21596b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2160d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2161e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
21626b4cac81SBjoern A. Zeeb 	if (error != 0) {
21636b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2164018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2165018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21666b4cac81SBjoern A. Zeeb 		goto out;
21676b4cac81SBjoern A. Zeeb 	}
21686b4cac81SBjoern A. Zeeb #if 0
21696b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
21706b4cac81SBjoern A. Zeeb #endif
21716b4cac81SBjoern A. Zeeb 
217267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21736b4cac81SBjoern A. Zeeb 
21742ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21752ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
21762ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
21772ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
21782ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2179d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
21800936c648SBjoern A. Zeeb 	/*
21810936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
21820936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
21830936c648SBjoern A. Zeeb 	 * and potentially freed.
21840936c648SBjoern A. Zeeb 	 */
21850936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2186d9f59799SBjoern A. Zeeb 
2187d9f59799SBjoern A. Zeeb 	/* conf_tx */
2188d9f59799SBjoern A. Zeeb 
2189*231168c7SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
219050d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2191*231168c7SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
21926b4cac81SBjoern A. Zeeb 
21936b4cac81SBjoern A. Zeeb out:
21948ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21956b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21966b4cac81SBjoern A. Zeeb 	return (error);
21976b4cac81SBjoern A. Zeeb }
21986b4cac81SBjoern A. Zeeb 
21996b4cac81SBjoern A. Zeeb static int
22006b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22016b4cac81SBjoern A. Zeeb {
22026b4cac81SBjoern A. Zeeb 	int error;
22036b4cac81SBjoern A. Zeeb 
22046b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
22056b4cac81SBjoern A. Zeeb 	if (error == 0)
22066b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
22076b4cac81SBjoern A. Zeeb 	return (error);
22086b4cac81SBjoern A. Zeeb }
22096b4cac81SBjoern A. Zeeb 
22106b4cac81SBjoern A. Zeeb static int
22116b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22126b4cac81SBjoern A. Zeeb {
22136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22146b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22166b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22176b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22186b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22196b4cac81SBjoern A. Zeeb 	int error;
22206b4cac81SBjoern A. Zeeb 
22216b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22226b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22236b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22246b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22256b4cac81SBjoern A. Zeeb 
22266b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22278ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22282ac8a218SBjoern A. Zeeb 
22292ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22302ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
22312ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
22322ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22332ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22342ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22352ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22362ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22372ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22382ac8a218SBjoern A. Zeeb #endif
22392ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
22402ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
22412ac8a218SBjoern A. Zeeb 		goto out;
22422ac8a218SBjoern A. Zeeb 	}
22432ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22442ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22452ac8a218SBjoern A. Zeeb 
22462ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
22476b4cac81SBjoern A. Zeeb 
22486b4cac81SBjoern A. Zeeb 	/* Finish auth. */
22496b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
22506b4cac81SBjoern A. Zeeb 
22516b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
22526b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
22536b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2254e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2255018d93ecSBjoern A. Zeeb 	if (error != 0) {
2256018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2257018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22586b4cac81SBjoern A. Zeeb 		goto out;
2259018d93ecSBjoern A. Zeeb 	}
22606b4cac81SBjoern A. Zeeb 
22616b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22626b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22636b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22646b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
22656b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22666b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22676b4cac81SBjoern A. Zeeb 	}
22686b4cac81SBjoern A. Zeeb 
22696b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
22706b4cac81SBjoern A. Zeeb 
22716b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
22726b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
22736b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22746b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
22756b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
22766b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
22776b4cac81SBjoern A. Zeeb 	}
22786b4cac81SBjoern A. Zeeb 
22796b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
228088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
22816b4cac81SBjoern A. Zeeb 
22826b4cac81SBjoern A. Zeeb 	/*
22836b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
22846b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
22856b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
22866b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
22876b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
22886b4cac81SBjoern A. Zeeb 	 * - event_callback
22896b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
22906b4cac81SBjoern A. Zeeb 	 */
22916b4cac81SBjoern A. Zeeb 
22926b4cac81SBjoern A. Zeeb out:
22938ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22946b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22956b4cac81SBjoern A. Zeeb 	return (error);
22966b4cac81SBjoern A. Zeeb }
22976b4cac81SBjoern A. Zeeb 
22986b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
22996b4cac81SBjoern A. Zeeb static int
23006b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23016b4cac81SBjoern A. Zeeb {
23026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23066b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23076b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23082ac8a218SBjoern A. Zeeb 	int error;
23096b4cac81SBjoern A. Zeeb 
23106b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23126b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23136b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23146b4cac81SBjoern A. Zeeb 
23156b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
23168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
23172ac8a218SBjoern A. Zeeb 
23182ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23192ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
23202ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
23212ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23222ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23232ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23242ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23252ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23262ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23272ac8a218SBjoern A. Zeeb #endif
23282ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
23292ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
23302ac8a218SBjoern A. Zeeb 		goto out;
23312ac8a218SBjoern A. Zeeb 	}
23322ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23342ac8a218SBjoern A. Zeeb 
23352ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
23362ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
23376b4cac81SBjoern A. Zeeb 
23386b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
23396b4cac81SBjoern A. Zeeb 
23406b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23416b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23426b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23436b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
23446b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23456b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23466b4cac81SBjoern A. Zeeb 	}
23476b4cac81SBjoern A. Zeeb 
23486b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
23496b4cac81SBjoern A. Zeeb 
23506b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
23516b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
23526b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23536b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
23546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
23556b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
23566b4cac81SBjoern A. Zeeb 	}
23576b4cac81SBjoern A. Zeeb 
23582ac8a218SBjoern A. Zeeb 	error = 0;
23592ac8a218SBjoern A. Zeeb out:
23608ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
23616b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
23626b4cac81SBjoern A. Zeeb 
23632ac8a218SBjoern A. Zeeb 	return (error);
23646b4cac81SBjoern A. Zeeb }
23656b4cac81SBjoern A. Zeeb 
23666b4cac81SBjoern A. Zeeb static int
2367d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23686b4cac81SBjoern A. Zeeb {
23696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23736b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23746b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23756b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
23766b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2377d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
23786b4cac81SBjoern A. Zeeb 	int error;
23796b4cac81SBjoern A. Zeeb 
23806b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23816b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23826b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23836b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23846b4cac81SBjoern A. Zeeb 
23852ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
23862ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
23872ac8a218SBjoern A. Zeeb 
23882ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23892ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23902ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23912ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23922ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23932ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23942ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23952ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23962ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23972ac8a218SBjoern A. Zeeb #endif
23982ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23992ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24002ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
24012ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
24022ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
24032ac8a218SBjoern A. Zeeb 
24042ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
24056b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
24066b4cac81SBjoern A. Zeeb 
240767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2408d9f59799SBjoern A. Zeeb 
2409d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2410d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2411d9f59799SBjoern A. Zeeb 
2412d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2413d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2414d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2415d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2416d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2417ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2418d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2419d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2420d9f59799SBjoern A. Zeeb 	}
2421d9f59799SBjoern A. Zeeb 
24228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2423d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2424d9f59799SBjoern A. Zeeb 
242588665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2426d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2427018d93ecSBjoern A. Zeeb 	if (error != 0) {
2428018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2429018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2430d9f59799SBjoern A. Zeeb 		goto outni;
2431018d93ecSBjoern A. Zeeb 	}
2432d9f59799SBjoern A. Zeeb 
2433d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
243488665349SBjoern A. Zeeb 
243588665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
243688665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
243788665349SBjoern A. Zeeb 
24388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2439d9f59799SBjoern A. Zeeb 
244067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2441d9f59799SBjoern A. Zeeb 
2442d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
244388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2444d9f59799SBjoern A. Zeeb 
2445d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2446d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2447d9f59799SBjoern A. Zeeb 
24486b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
24496b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
24506b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24516b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2452ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
24536b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
24546b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24556b4cac81SBjoern A. Zeeb 	}
24566b4cac81SBjoern A. Zeeb 
2457d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2458d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2459d9f59799SBjoern A. Zeeb 
2460d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2461d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2462d9f59799SBjoern A. Zeeb 
2463d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2464d9f59799SBjoern A. Zeeb 
24656b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
24666b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
24676b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
24686b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2469e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2470018d93ecSBjoern A. Zeeb 	if (error != 0) {
2471018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2472018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24736b4cac81SBjoern A. Zeeb 		goto out;
2474018d93ecSBjoern A. Zeeb 	}
24756b4cac81SBjoern A. Zeeb 
24765a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
24775a4d2461SBjoern A. Zeeb 	bss_changed = 0;
24785a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
24796b4cac81SBjoern A. Zeeb 
24805a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
248116e688b2SBjoern A. Zeeb 
2482d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2483d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2484d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2485d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2486e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2487d9f59799SBjoern A. Zeeb 	if (error != 0) {
2488d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2489018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2490018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2491d9f59799SBjoern A. Zeeb 		goto out;
2492d9f59799SBjoern A. Zeeb 	}
2493d9f59799SBjoern A. Zeeb 
249416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2495d9f59799SBjoern A. Zeeb 
2496d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2497d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2498d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2499616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2500616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2501d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2502d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2503d9f59799SBjoern A. Zeeb 
25042ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25052ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
25062ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
25072ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
25082ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2509d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
25100936c648SBjoern A. Zeeb 	/*
25110936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
25120936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
25130936c648SBjoern A. Zeeb 	 * and potentially freed.
25140936c648SBjoern A. Zeeb 	 */
25150936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2516d9f59799SBjoern A. Zeeb 
2517d9f59799SBjoern A. Zeeb 	/* conf_tx */
2518d9f59799SBjoern A. Zeeb 
2519*231168c7SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
252050d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2521*231168c7SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2522d9f59799SBjoern A. Zeeb 
2523d9f59799SBjoern A. Zeeb 	error = EALREADY;
25246b4cac81SBjoern A. Zeeb out:
25258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
25266b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2527d9f59799SBjoern A. Zeeb outni:
25286b4cac81SBjoern A. Zeeb 	return (error);
25296b4cac81SBjoern A. Zeeb }
25306b4cac81SBjoern A. Zeeb 
25316b4cac81SBjoern A. Zeeb static int
2532d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2533d9f59799SBjoern A. Zeeb {
2534d9f59799SBjoern A. Zeeb 	int error;
2535d9f59799SBjoern A. Zeeb 
2536d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2537d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2538d9f59799SBjoern A. Zeeb 		return (error);
2539d9f59799SBjoern A. Zeeb 
2540d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2541d9f59799SBjoern A. Zeeb 
2542d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2543d9f59799SBjoern A. Zeeb 	return (error);
2544d9f59799SBjoern A. Zeeb }
2545d9f59799SBjoern A. Zeeb 
2546d9f59799SBjoern A. Zeeb static int
25476b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25486b4cac81SBjoern A. Zeeb {
25496b4cac81SBjoern A. Zeeb 	int error;
25506b4cac81SBjoern A. Zeeb 
2551d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
25526b4cac81SBjoern A. Zeeb 	return (error);
25536b4cac81SBjoern A. Zeeb }
25546b4cac81SBjoern A. Zeeb 
25556b4cac81SBjoern A. Zeeb static int
25566b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25576b4cac81SBjoern A. Zeeb {
25586b4cac81SBjoern A. Zeeb 	int error;
25596b4cac81SBjoern A. Zeeb 
2560d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
25616b4cac81SBjoern A. Zeeb 	return (error);
25626b4cac81SBjoern A. Zeeb }
25636b4cac81SBjoern A. Zeeb 
25646b4cac81SBjoern A. Zeeb static int
25656b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25666b4cac81SBjoern A. Zeeb {
25676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25686b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25696b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25706b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25716b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25726b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
25746b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
25756b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
25766b4cac81SBjoern A. Zeeb 	int error;
25776b4cac81SBjoern A. Zeeb 
25786b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25806b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25816b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25826b4cac81SBjoern A. Zeeb 
25836b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
25848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
25852ac8a218SBjoern A. Zeeb 
25862ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25872ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
25882ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
25892ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25902ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25912ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25922ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25932ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25942ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25952ac8a218SBjoern A. Zeeb #endif
25962ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
25972ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
25982ac8a218SBjoern A. Zeeb 		goto out;
25992ac8a218SBjoern A. Zeeb 	}
26002ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26012ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26022ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26032ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26042ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
26052ac8a218SBjoern A. Zeeb 
26062ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
26076b4cac81SBjoern A. Zeeb 
26086b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
26096b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
26106b4cac81SBjoern A. Zeeb 
26119597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
26129597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
26139597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
26149597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
26156b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
26169597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
26174a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26184a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
26194a67f1dfSBjoern A. Zeeb 		sta->wme = true;
26204a67f1dfSBjoern A. Zeeb #endif
2621e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2622018d93ecSBjoern A. Zeeb 	if (error != 0) {
2623018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2624018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26259597f7cbSBjoern A. Zeeb 		goto out;
2626018d93ecSBjoern A. Zeeb 	}
26276b4cac81SBjoern A. Zeeb 
26286b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
26296b4cac81SBjoern A. Zeeb 
26306b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
26316b4cac81SBjoern A. Zeeb 	bss_changed = 0;
26324a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26334a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
26344a67f1dfSBjoern A. Zeeb #endif
2635616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2636616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2637616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
26386b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
26396b4cac81SBjoern A. Zeeb 	}
26406b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2641616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2642616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
26436b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
26446b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
26456b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
26466b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
26476b4cac81SBjoern A. Zeeb 	}
26486b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
26496b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
26506b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
26516b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
26526b4cac81SBjoern A. Zeeb 	}
26536b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
26546b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
26556b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
26566b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
26576b4cac81SBjoern A. Zeeb 	}
2658fa8f007dSBjoern A. Zeeb 
2659fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2660fa8f007dSBjoern A. Zeeb 
26616b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
26626b4cac81SBjoern A. Zeeb 
26636b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
26646b4cac81SBjoern A. Zeeb 	 * - event_callback
26656b4cac81SBjoern A. Zeeb 	 */
26666b4cac81SBjoern A. Zeeb 
26676b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
26686b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
26696b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
26706b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
26716b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
26726b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
26736b4cac81SBjoern A. Zeeb 	}
26746b4cac81SBjoern A. Zeeb 
2675086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2676086be6a8SBjoern A. Zeeb 
26776b4cac81SBjoern A. Zeeb 	/*
26786b4cac81SBjoern A. Zeeb 	 * And then:
26796b4cac81SBjoern A. Zeeb 	 * - (more packets)?
26806b4cac81SBjoern A. Zeeb 	 * - set_key
26816b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
26826b4cac81SBjoern A. Zeeb 	 * - set_key (?)
26836b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
26846b4cac81SBjoern A. Zeeb 	 */
26856b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
26866b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
26876b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
26886b4cac81SBjoern A. Zeeb 
26896b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
26906b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
26916b4cac81SBjoern A. Zeeb 	}
26926b4cac81SBjoern A. Zeeb 
2693f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
26949fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
2695f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
26969fb91463SBjoern A. Zeeb 
26976b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
26986b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26996b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
27006b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2701e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
27026b4cac81SBjoern A. Zeeb 	if (error != 0) {
27036b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2704018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2705018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27066b4cac81SBjoern A. Zeeb 		goto out;
27076b4cac81SBjoern A. Zeeb 	}
27086b4cac81SBjoern A. Zeeb 
27096b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
27106b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
27116b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
27126b4cac81SBjoern A. Zeeb 	 *
27136b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
27146b4cac81SBjoern A. Zeeb 	 */
27156b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
27166b4cac81SBjoern A. Zeeb 
2717fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2718fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2719fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2720fa8f007dSBjoern A. Zeeb 
27216b4cac81SBjoern A. Zeeb out:
27228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27236b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
27246b4cac81SBjoern A. Zeeb 	return (error);
27256b4cac81SBjoern A. Zeeb }
27266b4cac81SBjoern A. Zeeb 
27276b4cac81SBjoern A. Zeeb static int
27286b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27296b4cac81SBjoern A. Zeeb {
27306b4cac81SBjoern A. Zeeb 	int error;
27316b4cac81SBjoern A. Zeeb 
27326b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
27336b4cac81SBjoern A. Zeeb 	if (error == 0)
27346b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
27356b4cac81SBjoern A. Zeeb 	return (error);
27366b4cac81SBjoern A. Zeeb }
27376b4cac81SBjoern A. Zeeb 
27386b4cac81SBjoern A. Zeeb static int
27396b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27406b4cac81SBjoern A. Zeeb {
27416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27426b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27436b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27446b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27456b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
27466b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27476b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2748d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2749d9f59799SBjoern A. Zeeb #if 0
2750d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2751d9f59799SBjoern A. Zeeb #endif
27526b4cac81SBjoern A. Zeeb 	int error;
27536b4cac81SBjoern A. Zeeb 
27546b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
27556b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27566b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27576b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27586b4cac81SBjoern A. Zeeb 
27592ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27602ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27612ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
27622ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
27632ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
27642ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27652ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27662ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27672ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27682ac8a218SBjoern A. Zeeb #endif
27692ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27702ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27712ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27722ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27732ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27742ac8a218SBjoern A. Zeeb 
27752ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27766b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27776b4cac81SBjoern A. Zeeb 
277867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2779d9f59799SBjoern A. Zeeb 
2780d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
27818ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2782d9f59799SBjoern A. Zeeb 
2783d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2784d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2785d9f59799SBjoern A. Zeeb 
2786d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2787d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2788d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2789d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2790d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2791ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2792d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2793d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2794d9f59799SBjoern A. Zeeb 	}
2795d9f59799SBjoern A. Zeeb 
27968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2797d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2798d9f59799SBjoern A. Zeeb 
2799d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2800d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2801018d93ecSBjoern A. Zeeb 	if (error != 0) {
2802018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2803018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2804d9f59799SBjoern A. Zeeb 		goto outni;
2805018d93ecSBjoern A. Zeeb 	}
2806d9f59799SBjoern A. Zeeb 
2807d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
280888665349SBjoern A. Zeeb 
280988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
281088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
281188665349SBjoern A. Zeeb 
28128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2813d9f59799SBjoern A. Zeeb 
281467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2815d9f59799SBjoern A. Zeeb 
2816d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
281788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2818d9f59799SBjoern A. Zeeb 
2819d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2820d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2821d9f59799SBjoern A. Zeeb 
2822d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2823d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2824d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2825d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2826ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2827d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2828d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2829d9f59799SBjoern A. Zeeb 	}
2830d9f59799SBjoern A. Zeeb 
2831d9f59799SBjoern A. Zeeb #if 0
2832d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2833d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2834d9f59799SBjoern A. Zeeb 
2835d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2836d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2837d9f59799SBjoern A. Zeeb #endif
2838d9f59799SBjoern A. Zeeb 
2839d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2840d9f59799SBjoern A. Zeeb 
28416b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
28426b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28436b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
28446b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2845e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2846018d93ecSBjoern A. Zeeb 	if (error != 0) {
2847018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2848018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28496b4cac81SBjoern A. Zeeb 		goto out;
2850018d93ecSBjoern A. Zeeb 	}
28516b4cac81SBjoern A. Zeeb 
285267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
28536b4cac81SBjoern A. Zeeb 
285411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
285511db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
285611db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
285711db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
285811db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
285911db70b6SBjoern A. Zeeb 		if (error != 0) {
286011db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
286111db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
286211db70b6SBjoern A. Zeeb 			/*
286311db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
286411db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
286511db70b6SBjoern A. Zeeb 			 * less appropriate time).
286611db70b6SBjoern A. Zeeb 			 */
286711db70b6SBjoern A. Zeeb 			/* goto out; */
286811db70b6SBjoern A. Zeeb 		}
286911db70b6SBjoern A. Zeeb 	}
287011db70b6SBjoern A. Zeeb #endif
287111db70b6SBjoern A. Zeeb 
28726b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
28736b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28746b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28756b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2876e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2877018d93ecSBjoern A. Zeeb 	if (error != 0) {
2878018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2879018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28806b4cac81SBjoern A. Zeeb 		goto out;
2881018d93ecSBjoern A. Zeeb 	}
28826b4cac81SBjoern A. Zeeb 
288367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
28846b4cac81SBjoern A. Zeeb 
2885d9f59799SBjoern A. Zeeb #if 0
2886d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2887d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2888d9f59799SBjoern A. Zeeb #endif
2889d9f59799SBjoern A. Zeeb 
2890d9f59799SBjoern A. Zeeb 	error = EALREADY;
28916b4cac81SBjoern A. Zeeb out:
28928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
28936b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2894d9f59799SBjoern A. Zeeb outni:
28956b4cac81SBjoern A. Zeeb 	return (error);
28966b4cac81SBjoern A. Zeeb }
28976b4cac81SBjoern A. Zeeb 
28986b4cac81SBjoern A. Zeeb static int
2899d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2900d9f59799SBjoern A. Zeeb {
2901d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2902d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2903d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2904d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2905d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2906d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2907d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2908d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2909d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2910d9f59799SBjoern A. Zeeb 	int error;
2911d9f59799SBjoern A. Zeeb 
2912d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2913d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2914d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2915d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2916d9f59799SBjoern A. Zeeb 
29172ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
29182ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
29192ac8a218SBjoern A. Zeeb 
29202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
29212ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
29222ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
29232ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
29242ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
29252ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
29262ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
29272ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
29282ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
29292ac8a218SBjoern A. Zeeb #endif
29302ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
29312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
29322ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
29332ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
29342ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
29352ac8a218SBjoern A. Zeeb 
29362ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2937d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2938d9f59799SBjoern A. Zeeb 
293967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2940d9f59799SBjoern A. Zeeb 
2941d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2942d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2943d9f59799SBjoern A. Zeeb 
2944d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2945d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2946d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2947d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2948d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2949ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2950d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2951d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2952d9f59799SBjoern A. Zeeb 	}
2953d9f59799SBjoern A. Zeeb 
29548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2955d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2956d9f59799SBjoern A. Zeeb 
2957d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2958d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2959018d93ecSBjoern A. Zeeb 	if (error != 0) {
2960018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2961018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2962d9f59799SBjoern A. Zeeb 		goto outni;
2963018d93ecSBjoern A. Zeeb 	}
2964d9f59799SBjoern A. Zeeb 
2965d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
296688665349SBjoern A. Zeeb 
296788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
296888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
296988665349SBjoern A. Zeeb 
29708ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2971d9f59799SBjoern A. Zeeb 
297267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2973d9f59799SBjoern A. Zeeb 
2974d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
297588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2976d9f59799SBjoern A. Zeeb 
2977d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2978d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2979d9f59799SBjoern A. Zeeb 
2980d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2981d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2982d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2983d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2984ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2985d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2986d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2987d9f59799SBjoern A. Zeeb 	}
2988d9f59799SBjoern A. Zeeb 
2989d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2990d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2991d9f59799SBjoern A. Zeeb 
2992d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2993d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2994d9f59799SBjoern A. Zeeb 
2995d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2996d9f59799SBjoern A. Zeeb 
2997d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2998d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2999d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3000d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3001e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3002018d93ecSBjoern A. Zeeb 	if (error != 0) {
3003018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3004018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3005d9f59799SBjoern A. Zeeb 		goto out;
3006018d93ecSBjoern A. Zeeb 	}
3007d9f59799SBjoern A. Zeeb 
300867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3009d9f59799SBjoern A. Zeeb 
301011db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
301111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
301211db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
301311db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
301411db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
301511db70b6SBjoern A. Zeeb 		if (error != 0) {
301611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
301711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
301811db70b6SBjoern A. Zeeb 			/*
301911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
302011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
302111db70b6SBjoern A. Zeeb 			 * less appropriate time).
302211db70b6SBjoern A. Zeeb 			 */
302311db70b6SBjoern A. Zeeb 			/* goto out; */
302411db70b6SBjoern A. Zeeb 		}
302511db70b6SBjoern A. Zeeb 	}
302611db70b6SBjoern A. Zeeb #endif
302711db70b6SBjoern A. Zeeb 
3028d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3029d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3030d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3031d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3032e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3033018d93ecSBjoern A. Zeeb 	if (error != 0) {
3034018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3035018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3036d9f59799SBjoern A. Zeeb 		goto out;
3037018d93ecSBjoern A. Zeeb 	}
3038d9f59799SBjoern A. Zeeb 
303967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3040d9f59799SBjoern A. Zeeb 
3041d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3042d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3043d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3044d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3045e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3046018d93ecSBjoern A. Zeeb 	if (error != 0) {
3047018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3048018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3049d9f59799SBjoern A. Zeeb 		goto out;
3050018d93ecSBjoern A. Zeeb 	}
3051d9f59799SBjoern A. Zeeb 
30525a4d2461SBjoern A. Zeeb 	bss_changed = 0;
305316e688b2SBjoern A. Zeeb 	/*
30545a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
30555a4d2461SBjoern A. Zeeb 	 *
305616e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
30575a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
30585a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
30595a4d2461SBjoern A. Zeeb 	 *
30605a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
30615a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
30625a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
30635a4d2461SBjoern A. Zeeb 	 * a fw assert.
30645a4d2461SBjoern A. Zeeb 	 *
30655a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
30665a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
30675a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
30685a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
30695a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
30705a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
30715a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
30725a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
30735a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
30745a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
307516e688b2SBjoern A. Zeeb 	 */
30765a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
30775a4d2461SBjoern A. Zeeb 
30785a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
307916e688b2SBjoern A. Zeeb 
3080d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3081d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3082d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3083d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3084e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3085d9f59799SBjoern A. Zeeb 	if (error != 0) {
3086d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3087018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3088018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3089d9f59799SBjoern A. Zeeb 		goto out;
3090d9f59799SBjoern A. Zeeb 	}
3091d9f59799SBjoern A. Zeeb 
30925a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
30935a4d2461SBjoern A. Zeeb 
309416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3095d9f59799SBjoern A. Zeeb 
3096d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3097d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3098d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3099616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3100616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3101d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
31025a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
31035a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
31045a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3105d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3106d9f59799SBjoern A. Zeeb 
31072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
31082ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
31092ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
31102ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
31112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
31120936c648SBjoern A. Zeeb 	/*
31130936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
31140936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
31150936c648SBjoern A. Zeeb 	 * and potentially freed.
31160936c648SBjoern A. Zeeb 	 */
31170936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3118d9f59799SBjoern A. Zeeb 
3119d9f59799SBjoern A. Zeeb 	/* conf_tx */
3120d9f59799SBjoern A. Zeeb 
3121*231168c7SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
312250d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3123*231168c7SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3124d9f59799SBjoern A. Zeeb 
3125d9f59799SBjoern A. Zeeb 	error = EALREADY;
3126d9f59799SBjoern A. Zeeb out:
31278ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3128d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3129d9f59799SBjoern A. Zeeb outni:
3130d9f59799SBjoern A. Zeeb 	return (error);
3131d9f59799SBjoern A. Zeeb }
3132d9f59799SBjoern A. Zeeb 
3133d9f59799SBjoern A. Zeeb static int
3134d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3135d9f59799SBjoern A. Zeeb {
3136d9f59799SBjoern A. Zeeb 
3137d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3138d9f59799SBjoern A. Zeeb }
3139d9f59799SBjoern A. Zeeb 
3140d9f59799SBjoern A. Zeeb static int
31416b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
31426b4cac81SBjoern A. Zeeb {
31436b4cac81SBjoern A. Zeeb 	int error;
31446b4cac81SBjoern A. Zeeb 
3145d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3146d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3147d9f59799SBjoern A. Zeeb 		return (error);
3148d9f59799SBjoern A. Zeeb 
3149d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3150d9f59799SBjoern A. Zeeb 
3151d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
31526b4cac81SBjoern A. Zeeb 	return (error);
31536b4cac81SBjoern A. Zeeb }
31546b4cac81SBjoern A. Zeeb 
3155d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
31566b4cac81SBjoern A. Zeeb 
31576b4cac81SBjoern A. Zeeb /*
31586b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
31596b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
31606b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
31616b4cac81SBjoern A. Zeeb  */
31626b4cac81SBjoern A. Zeeb struct fsm_state {
31636b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
31646b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31656b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
31666b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
31676b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
31686b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
31696b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
31706b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3171d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3172d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
31736b4cac81SBjoern A. Zeeb 
31746b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31756b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31766b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
31776b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3178d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
31796b4cac81SBjoern A. Zeeb 
3180d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3181d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3182d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3183d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3184d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
31856b4cac81SBjoern A. Zeeb 
3186d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3187d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3188d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
31896b4cac81SBjoern A. Zeeb 
31906b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
31916b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
31926b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
31936b4cac81SBjoern A. Zeeb 
31946b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
31956b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
31966b4cac81SBjoern A. Zeeb };
31976b4cac81SBjoern A. Zeeb 
31986b4cac81SBjoern A. Zeeb static int
31996b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
32006b4cac81SBjoern A. Zeeb {
32016b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
32026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32046b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32056b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
32066b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
32076b4cac81SBjoern A. Zeeb 	int error;
32086b4cac81SBjoern A. Zeeb 
32096b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
32106b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
32116b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
32126b4cac81SBjoern A. Zeeb 
32139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
32156b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
32166b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
32179d9ba2b7SBjoern A. Zeeb #endif
32186b4cac81SBjoern A. Zeeb 
32196b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
32206b4cac81SBjoern A. Zeeb 
32216b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
32226b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32236b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
32246b4cac81SBjoern A. Zeeb 
32256b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
32266b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
32276b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
32286b4cac81SBjoern A. Zeeb 
32296b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
32306b4cac81SBjoern A. Zeeb 
32316b4cac81SBjoern A. Zeeb 	} else {
32326b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
32336b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
32346b4cac81SBjoern A. Zeeb 		return (ENOSYS);
32356b4cac81SBjoern A. Zeeb 	}
32366b4cac81SBjoern A. Zeeb 
32376b4cac81SBjoern A. Zeeb 	error = 0;
32386b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
32396b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
32409d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32419d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3242d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3243d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3244d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3245d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
32469d9ba2b7SBjoern A. Zeeb #endif
32476b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
32486b4cac81SBjoern A. Zeeb 			break;
32496b4cac81SBjoern A. Zeeb 		}
32506b4cac81SBjoern A. Zeeb 	}
32516b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
32526b4cac81SBjoern A. Zeeb 
32536b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3254d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
32556b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
32566b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
32576b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
32586b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
32596b4cac81SBjoern A. Zeeb 		return (ENOSYS);
32606b4cac81SBjoern A. Zeeb 	}
32616b4cac81SBjoern A. Zeeb 
32626b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
32639d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32649d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3265d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3266d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3267d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3268d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
32699d9ba2b7SBjoern A. Zeeb #endif
32706b4cac81SBjoern A. Zeeb 		return (0);
32716b4cac81SBjoern A. Zeeb 	}
32726b4cac81SBjoern A. Zeeb 
32736b4cac81SBjoern A. Zeeb 	if (error != 0) {
32746b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
32756b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
32766b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
32776b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
327845c27ad5SBjoern A. Zeeb 		return (error);
32796b4cac81SBjoern A. Zeeb 	}
32806b4cac81SBjoern A. Zeeb 
32819d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32829d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3283d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3284d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
32856b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
32869d9ba2b7SBjoern A. Zeeb #endif
32876b4cac81SBjoern A. Zeeb 
32886b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
32896b4cac81SBjoern A. Zeeb }
32906b4cac81SBjoern A. Zeeb 
32916b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32926b4cac81SBjoern A. Zeeb 
3293d9f59799SBjoern A. Zeeb /*
3294d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3295d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3296d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3297d9f59799SBjoern A. Zeeb  */
3298d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3299d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3300d9f59799SBjoern A. Zeeb {
3301d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33022ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3303d9f59799SBjoern A. Zeeb 
33042ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3305d9f59799SBjoern A. Zeeb 
3306ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
33072ac8a218SBjoern A. Zeeb 
33082ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
33092ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33102ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
33112ac8a218SBjoern A. Zeeb 
33122ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
33132ac8a218SBjoern A. Zeeb 	return (rni);
3314d9f59799SBjoern A. Zeeb }
3315d9f59799SBjoern A. Zeeb 
33164a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
33176b4cac81SBjoern A. Zeeb static int
33184a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
33196b4cac81SBjoern A. Zeeb {
33204a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
33216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33246b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
33256b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
33266b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
33276b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
33286b4cac81SBjoern A. Zeeb 	int error;
33296b4cac81SBjoern A. Zeeb 	uint16_t ac;
33306b4cac81SBjoern A. Zeeb 
33316b4cac81SBjoern A. Zeeb 	IMPROVE();
33326b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
33336b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
33346b4cac81SBjoern A. Zeeb 
33356b4cac81SBjoern A. Zeeb 	if (vap == NULL)
33366b4cac81SBjoern A. Zeeb 		return (0);
33376b4cac81SBjoern A. Zeeb 
33384a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
33394a67f1dfSBjoern A. Zeeb 		return (0);
33404a67f1dfSBjoern A. Zeeb 
33416b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
33426b4cac81SBjoern A. Zeeb 		return (0);
33436b4cac81SBjoern A. Zeeb 
33444a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
33454a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
33464a67f1dfSBjoern A. Zeeb 		return (0);
33474a67f1dfSBjoern A. Zeeb 	}
33484a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
33496b4cac81SBjoern A. Zeeb 
33504a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
33516b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
33526b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
33536b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
33546b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
33556b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
33566b4cac81SBjoern A. Zeeb 
33574a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33584a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
33594a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33604a67f1dfSBjoern A. Zeeb 
33616b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
33626b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
33636b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
33646b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
33656b4cac81SBjoern A. Zeeb 
33666b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
33676b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
33686b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
33696b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
33706b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
33716b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
337268541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
33736b4cac81SBjoern A. Zeeb 		if (error != 0)
33743f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
33756b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
33766b4cac81SBjoern A. Zeeb 	}
33776b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
33786b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
33794a67f1dfSBjoern A. Zeeb 	if (!planned)
33806b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
33814a67f1dfSBjoern A. Zeeb 
33824a67f1dfSBjoern A. Zeeb 	return (changed);
33834a67f1dfSBjoern A. Zeeb }
33846b4cac81SBjoern A. Zeeb #endif
33856b4cac81SBjoern A. Zeeb 
33864a67f1dfSBjoern A. Zeeb static int
33874a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
33884a67f1dfSBjoern A. Zeeb {
33894a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
33904a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
33914a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33924a67f1dfSBjoern A. Zeeb 
33934a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
33944a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
33954a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
33966b4cac81SBjoern A. Zeeb 		return (0);
33974a67f1dfSBjoern A. Zeeb 
33984a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
33994a67f1dfSBjoern A. Zeeb 
34004a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
34014a67f1dfSBjoern A. Zeeb #endif
34024a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
34036b4cac81SBjoern A. Zeeb }
34046b4cac81SBjoern A. Zeeb 
34054aff4048SBjoern A. Zeeb /*
34064aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
34074aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
34084aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
34094aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
34104aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
34114aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3412edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3413edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3414edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3415edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
34164aff4048SBjoern A. Zeeb  */
34174aff4048SBjoern A. Zeeb static void
34184aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
34194aff4048SBjoern A. Zeeb {
34204aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
34214aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34224aff4048SBjoern A. Zeeb 
34234aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3424edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3425edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3426edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
34274aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
34284aff4048SBjoern A. Zeeb 		return;
34294aff4048SBjoern A. Zeeb 	}
34304aff4048SBjoern A. Zeeb 
34314aff4048SBjoern A. Zeeb 	vif = arg;
343257609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
34334aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
34344aff4048SBjoern A. Zeeb }
34354aff4048SBjoern A. Zeeb 
34366b4cac81SBjoern A. Zeeb static struct ieee80211vap *
34376b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
34386b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
34396b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
34406b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
34416b4cac81SBjoern A. Zeeb {
34426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34436b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34446b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34456b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
34466b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3447a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
34486b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
344940839418SBjoern A. Zeeb 	struct sysctl_oid *node;
34506b4cac81SBjoern A. Zeeb 	size_t len;
3451e3a0b120SBjoern A. Zeeb 	int error, i;
3452a6042e17SBjoern A. Zeeb 	uint16_t ac;
34536b4cac81SBjoern A. Zeeb 
34546b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
34556b4cac81SBjoern A. Zeeb 		return (NULL);
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34586b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34596b4cac81SBjoern A. Zeeb 
34606b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
34616b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
34626b4cac81SBjoern A. Zeeb 
34636b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
34646b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3465a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
34662ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3467b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
34682ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34696b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
34706b4cac81SBjoern A. Zeeb 
34716b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
34726b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
34736b4cac81SBjoern A. Zeeb 	vif->p2p = false;
34746b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
34756b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
34766b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
34776b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
34786b4cac81SBjoern A. Zeeb 	IMPROVE();
34796b4cac81SBjoern A. Zeeb 
34806b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
34816b4cac81SBjoern A. Zeeb #if 1
348211604b2aSBjoern A. Zeeb 	vif->bss_conf.chanctx_conf = NULL;
3483adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
34846ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
34856ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
34864aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
34874aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
34886ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
34897b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
34906b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
34916b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
34926b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
34936b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
34946b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3495616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3496616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3497616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3498616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
34996ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3500caaa79c3SBjoern A. Zeeb 	/*
3501caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3502caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3503caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3504caaa79c3SBjoern A. Zeeb 	 */
35056ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
35066b4cac81SBjoern A. Zeeb #endif
35076b4cac81SBjoern A. Zeeb #if 0
35086b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
35096b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
35106b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
35116b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
35126b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
35136b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
35146b4cac81SBjoern A. Zeeb #endif
3515e3a0b120SBjoern A. Zeeb 
35166ffb7bd4SBjoern A. Zeeb 	/* Link Config */
35176ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
35186ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
35196ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
35206ffb7bd4SBjoern A. Zeeb 	}
35216ffb7bd4SBjoern A. Zeeb 
3522e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3523e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3524e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3525e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3526e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3527e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3528e3a0b120SBjoern A. Zeeb 		else
3529e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
35300cbcfa19SBjoern A. Zeeb 
35310cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
35320cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3533e3a0b120SBjoern A. Zeeb 	}
3534e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3535e3a0b120SBjoern A. Zeeb 
35366b4cac81SBjoern A. Zeeb 	IMPROVE();
35376b4cac81SBjoern A. Zeeb 
35386b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
35396b4cac81SBjoern A. Zeeb 	if (error != 0) {
35403f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
35416b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
35426b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
35436b4cac81SBjoern A. Zeeb 		return (NULL);
35446b4cac81SBjoern A. Zeeb 	}
35456b4cac81SBjoern A. Zeeb 
35466b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
35476b4cac81SBjoern A. Zeeb 	if (error != 0) {
35486b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
35493f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
35506b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
35516b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
35526b4cac81SBjoern A. Zeeb 		return (NULL);
35536b4cac81SBjoern A. Zeeb 	}
35546b4cac81SBjoern A. Zeeb 
35558891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
35566b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
35578891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
35586b4cac81SBjoern A. Zeeb 
35596b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
35606b4cac81SBjoern A. Zeeb 	changed = 0;
35616b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35626b4cac81SBjoern A. Zeeb 
3563a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3564a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3565a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3566a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3567a6042e17SBjoern A. Zeeb 
3568a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3569a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3570a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3571a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3572a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3573a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3574a6042e17SBjoern A. Zeeb 		if (error != 0)
3575a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3576a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3577a6042e17SBjoern A. Zeeb 	}
3578a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3579a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3580a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35816b4cac81SBjoern A. Zeeb 
35826b4cac81SBjoern A. Zeeb 	/* Force MC init. */
35836b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
35846b4cac81SBjoern A. Zeeb 
35856b4cac81SBjoern A. Zeeb 	IMPROVE();
35866b4cac81SBjoern A. Zeeb 
35876b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
35886b4cac81SBjoern A. Zeeb 
35896b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
35906b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
35916b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3592d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3593d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
35946b4cac81SBjoern A. Zeeb 
3595b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
359611db70b6SBjoern A. Zeeb 	/* Key management. */
359711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
35986b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
35996b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3600b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3601b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
36026b4cac81SBjoern A. Zeeb 	}
360311db70b6SBjoern A. Zeeb #endif
36046b4cac81SBjoern A. Zeeb 
36059fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
36069fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
36079fb91463SBjoern A. Zeeb #endif
36089fb91463SBjoern A. Zeeb 
36096b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
36106b4cac81SBjoern A. Zeeb 
36116b4cac81SBjoern A. Zeeb 	/* Complete setup. */
36126b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
36136b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
36146b4cac81SBjoern A. Zeeb 
361511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
361611db70b6SBjoern A. Zeeb 	/*
361711db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
361811db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
361911db70b6SBjoern A. Zeeb 	 */
362011db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
362111db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
362211db70b6SBjoern A. Zeeb #endif
3623ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3624ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3625ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3626ac2c7271SBjoern A. Zeeb #endif
362711db70b6SBjoern A. Zeeb 
36286b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
36296b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
36306b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
36316b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
36326b4cac81SBjoern A. Zeeb 
36336b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
36346b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
36356b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
36366b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
36376b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
36386b4cac81SBjoern A. Zeeb 	/* any others? */
363940839418SBjoern A. Zeeb 
364040839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
364140839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
364240839418SBjoern A. Zeeb 
364340839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
364440839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
364540839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
364640839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
364740839418SBjoern A. Zeeb 
364840839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
364940839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
365040839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
365140839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
365240839418SBjoern A. Zeeb 
36536b4cac81SBjoern A. Zeeb 	IMPROVE();
36546b4cac81SBjoern A. Zeeb 
36556b4cac81SBjoern A. Zeeb 	return (vap);
36566b4cac81SBjoern A. Zeeb }
36576b4cac81SBjoern A. Zeeb 
36584b0af114SBjoern A. Zeeb void
36594b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
36604b0af114SBjoern A. Zeeb {
36614b0af114SBjoern A. Zeeb 
36624b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
36634b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
36644b0af114SBjoern A. Zeeb 
36654b0af114SBjoern A. Zeeb 	IMPROVE();
36664b0af114SBjoern A. Zeeb }
36674b0af114SBjoern A. Zeeb 
36684b0af114SBjoern A. Zeeb void
36694b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
36704b0af114SBjoern A. Zeeb {
36714b0af114SBjoern A. Zeeb 
36724b0af114SBjoern A. Zeeb 	TODO();
36734b0af114SBjoern A. Zeeb }
36744b0af114SBjoern A. Zeeb 
36756b4cac81SBjoern A. Zeeb static void
36766b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
36776b4cac81SBjoern A. Zeeb {
36786b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36836b4cac81SBjoern A. Zeeb 
36846b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
36856b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36866b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
36876b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36896b4cac81SBjoern A. Zeeb 
36904aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
36914aff4048SBjoern A. Zeeb 
369240839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
369340839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
369440839418SBjoern A. Zeeb 
36958891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36966b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
36978891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36986b4cac81SBjoern A. Zeeb 
36996b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
37006b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3701dbf76919SBjoern A. Zeeb 
3702dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3703dbf76919SBjoern A. Zeeb 
3704dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3705dbf76919SBjoern A. Zeeb 
37066c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
37077b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
37086c38c6b1SBjoern A. Zeeb 
37096b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
37106b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
37116b4cac81SBjoern A. Zeeb }
37126b4cac81SBjoern A. Zeeb 
37136b4cac81SBjoern A. Zeeb static void
37146b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
37156b4cac81SBjoern A. Zeeb {
37166b4cac81SBjoern A. Zeeb 
37176b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
37186b4cac81SBjoern A. Zeeb 	TRACEOK();
37196b4cac81SBjoern A. Zeeb }
37206b4cac81SBjoern A. Zeeb 
37216b4cac81SBjoern A. Zeeb static void
37226b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
37236b4cac81SBjoern A. Zeeb {
37246b4cac81SBjoern A. Zeeb 
37256b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
37266b4cac81SBjoern A. Zeeb }
37276b4cac81SBjoern A. Zeeb 
37286b4cac81SBjoern A. Zeeb static void
37296b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
37306b4cac81SBjoern A. Zeeb {
37316b4cac81SBjoern A. Zeeb 
37326b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
37336b4cac81SBjoern A. Zeeb }
37346b4cac81SBjoern A. Zeeb 
37356b4cac81SBjoern A. Zeeb /* Start / stop device. */
37366b4cac81SBjoern A. Zeeb static void
37376b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
37386b4cac81SBjoern A. Zeeb {
37396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37408d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37426b4cac81SBjoern A. Zeeb 	int error;
37438d58a057SBjoern A. Zeeb #endif
37446b4cac81SBjoern A. Zeeb 	bool start_all;
37456b4cac81SBjoern A. Zeeb 
37466b4cac81SBjoern A. Zeeb 	IMPROVE();
37476b4cac81SBjoern A. Zeeb 
37486b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37498d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37506b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37518d58a057SBjoern A. Zeeb #endif
37526b4cac81SBjoern A. Zeeb 	start_all = false;
37536b4cac81SBjoern A. Zeeb 
37548ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
37558ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
37566b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
37578d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37586b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
37596b4cac81SBjoern A. Zeeb 		if (error == 0)
37608d58a057SBjoern A. Zeeb #endif
37616b4cac81SBjoern A. Zeeb 			start_all = true;
37626b4cac81SBjoern A. Zeeb 	} else {
37638d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37647b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
37658d58a057SBjoern A. Zeeb #endif
37666b4cac81SBjoern A. Zeeb 	}
37678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
37688ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
37696b4cac81SBjoern A. Zeeb 
37706b4cac81SBjoern A. Zeeb 	if (start_all)
37716b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
37726b4cac81SBjoern A. Zeeb }
37736b4cac81SBjoern A. Zeeb 
37746b4cac81SBjoern A. Zeeb bool
37756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
37766b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
37776b4cac81SBjoern A. Zeeb {
37786b4cac81SBjoern A. Zeeb 	int i;
37796b4cac81SBjoern A. Zeeb 
37806b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
37816b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
37826b4cac81SBjoern A. Zeeb 			return (true);
37836b4cac81SBjoern A. Zeeb 	}
37846b4cac81SBjoern A. Zeeb 
37856b4cac81SBjoern A. Zeeb 	return (false);
37866b4cac81SBjoern A. Zeeb }
37876b4cac81SBjoern A. Zeeb 
37886b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
37896b4cac81SBjoern A. Zeeb bool
37906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
37916b4cac81SBjoern A. Zeeb {
37926b4cac81SBjoern A. Zeeb 	size_t x;
37936b4cac81SBjoern A. Zeeb 	uint8_t l;
37946b4cac81SBjoern A. Zeeb 
37956b4cac81SBjoern A. Zeeb 	x = *xp;
37966b4cac81SBjoern A. Zeeb 
37976b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
37986b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
37996b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
38006b4cac81SBjoern A. Zeeb 	x += 2 + l;
38016b4cac81SBjoern A. Zeeb 
38026b4cac81SBjoern A. Zeeb 	if (x > ies_len)
38036b4cac81SBjoern A. Zeeb 		return (false);
38046b4cac81SBjoern A. Zeeb 
38056b4cac81SBjoern A. Zeeb 	*xp = x;
38066b4cac81SBjoern A. Zeeb 	return (true);
38076b4cac81SBjoern A. Zeeb }
38086b4cac81SBjoern A. Zeeb 
3809d9945d78SBjoern A. Zeeb static uint8_t *
3810d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3811d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
38126b4cac81SBjoern A. Zeeb {
3813d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3814d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
38153dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3816d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3817d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3818d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3819d9945d78SBjoern A. Zeeb 	int band, i;
38206b4cac81SBjoern A. Zeeb 
38213dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3822d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3823d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3824d9945d78SBjoern A. Zeeb 			continue;
3825d9945d78SBjoern A. Zeeb 
3826d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3827d9945d78SBjoern A. Zeeb 		/*
3828d9945d78SBjoern A. Zeeb 		 * This should not happen;
3829d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3830d9945d78SBjoern A. Zeeb 		 */
3831d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3832d9945d78SBjoern A. Zeeb 			continue;
3833d9945d78SBjoern A. Zeeb 
3834d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3835d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3836d9945d78SBjoern A. Zeeb 		chan = NULL;
3837d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3838d9945d78SBjoern A. Zeeb 
3839d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3840d9945d78SBjoern A. Zeeb 				continue;
3841d9945d78SBjoern A. Zeeb 
38423dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3843d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3844d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3845d9945d78SBjoern A. Zeeb 				break;
3846d9945d78SBjoern A. Zeeb 		}
3847d9945d78SBjoern A. Zeeb 
3848d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3849d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3850d9945d78SBjoern A. Zeeb 			continue;
3851d9945d78SBjoern A. Zeeb 
3852d9945d78SBjoern A. Zeeb 		pb = p;
38533dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3854d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3855d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3856d9945d78SBjoern A. Zeeb 
38573dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
38583dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
38593dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
38603dd98026SBjoern A. Zeeb 
38613dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
38623dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
38633dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
38643dd98026SBjoern A. Zeeb 		}
38653dd98026SBjoern A. Zeeb #endif
38663dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
38673dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
38683dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
38693dd98026SBjoern A. Zeeb 
38703dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
38713dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
38723dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
38733dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
38743dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
38753dd98026SBjoern A. Zeeb 		}
38763dd98026SBjoern A. Zeeb #endif
38773dd98026SBjoern A. Zeeb 
3878d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3879d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3880d9945d78SBjoern A. Zeeb 	}
3881d9945d78SBjoern A. Zeeb 
3882d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3883d9945d78SBjoern A. Zeeb 	pb = p;
3884d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3885d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3886d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3887d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3888d9945d78SBjoern A. Zeeb 	}
3889d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3890d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3891d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3892d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3893d9945d78SBjoern A. Zeeb 	}
3894d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3895d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3896d9945d78SBjoern A. Zeeb 
3897d9945d78SBjoern A. Zeeb 	return (p);
38986b4cac81SBjoern A. Zeeb }
38996b4cac81SBjoern A. Zeeb 
39006b4cac81SBjoern A. Zeeb static void
39016b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
39026b4cac81SBjoern A. Zeeb {
39036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39046b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39056b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39066b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39076b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
39086b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
39096b4cac81SBjoern A. Zeeb 	int error;
39108ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
39116b4cac81SBjoern A. Zeeb 
39126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3914a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
39156b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
39168ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
39176b4cac81SBjoern A. Zeeb 		return;
39186b4cac81SBjoern A. Zeeb 	}
39198ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
39208ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
39216b4cac81SBjoern A. Zeeb 
39226b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
39236b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
39246b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3925d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
39266b4cac81SBjoern A. Zeeb 		return;
39276b4cac81SBjoern A. Zeeb 	}
39286b4cac81SBjoern A. Zeeb 
39296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39308ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3931a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3932a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3933d3ef7fb4SBjoern A. Zeeb sw_scan:
39346b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
39356b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3936086be6a8SBjoern A. Zeeb 
3937086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3938086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3939086be6a8SBjoern A. Zeeb 
39406b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
39416b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
39426b4cac81SBjoern A. Zeeb 		IMPROVE();
39436b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
39446b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
39456b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
39466b4cac81SBjoern A. Zeeb 
39476b4cac81SBjoern A. Zeeb 	} else {
39486b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
39496b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
39506b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
39516b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
39526b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3953d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3954d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3955d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
39563206587aSBjoern A. Zeeb 		bool running;
3957d9945d78SBjoern A. Zeeb 
3958d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3959d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
39606b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
39616b4cac81SBjoern A. Zeeb 
3962d9945d78SBjoern A. Zeeb 		band_mask = 0;
39636b4cac81SBjoern A. Zeeb 		nchan = 0;
3964c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3965c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3966d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
39676b4cac81SBjoern A. Zeeb 				nchan++;
3968d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3969d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3970d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3971d9945d78SBjoern A. Zeeb 			}
3972c272abc5SBjoern A. Zeeb #else
3973c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3974c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3975c272abc5SBjoern A. Zeeb 				switch (band) {
3976c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3977c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3978c272abc5SBjoern A. Zeeb 					break;
3979c272abc5SBjoern A. Zeeb 				default:
3980c272abc5SBjoern A. Zeeb 					continue;
3981c272abc5SBjoern A. Zeeb 				}
3982c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3983c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3984c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3985c272abc5SBjoern A. Zeeb 				}
3986c272abc5SBjoern A. Zeeb 			}
3987c272abc5SBjoern A. Zeeb #endif
3988c272abc5SBjoern A. Zeeb 		} else {
39893206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
39903206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
39913206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
39923206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
39933206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
39943206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
39953206587aSBjoern A. Zeeb 			 */
39963206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
39973206587aSBjoern A. Zeeb 		}
39983206587aSBjoern A. Zeeb 
39996b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
40006b4cac81SBjoern A. Zeeb 
4001d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4002d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4003d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4004d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4005d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4006d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4007d9945d78SBjoern A. Zeeb 
4008d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4009d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4010d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4011d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4012d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4013d9945d78SBjoern A. Zeeb 		}
4014d9945d78SBjoern A. Zeeb 
40153206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4016d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4017d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
40186b4cac81SBjoern A. Zeeb 
40196b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
40206b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
40216b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
40226b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
40236b4cac81SBjoern A. Zeeb #if 0
40246b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
40256b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
40266b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
40276b4cac81SBjoern A. Zeeb #endif
40286b4cac81SBjoern A. Zeeb #ifdef __notyet__
40296b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
40306b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
40316b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
40326b4cac81SBjoern A. Zeeb #endif
4033e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
40346b4cac81SBjoern A. Zeeb 
40356b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
40366b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
40376b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
40386b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
40396b4cac81SBjoern A. Zeeb 			*(cpp + i) =
40406b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
40416b4cac81SBjoern A. Zeeb 		}
4042c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
40436b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4044c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
40456b4cac81SBjoern A. Zeeb 
4046c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
40476b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
40483206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
40496b4cac81SBjoern A. Zeeb 			/* lc->flags */
40506b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
40516b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
40526b4cac81SBjoern A. Zeeb 			/* lc-> ... */
40536b4cac81SBjoern A. Zeeb 			lc++;
40546b4cac81SBjoern A. Zeeb 		}
4055c272abc5SBjoern A. Zeeb #else
4056c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4057c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4058c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4059c272abc5SBjoern A. Zeeb 
4060c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4061c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4062c272abc5SBjoern A. Zeeb 				continue;
4063c272abc5SBjoern A. Zeeb 
4064c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4065c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4066c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4067c272abc5SBjoern A. Zeeb 				continue;
4068c272abc5SBjoern A. Zeeb 
4069c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4070c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4071c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4072c272abc5SBjoern A. Zeeb 				lc++;
4073c272abc5SBjoern A. Zeeb 			}
4074c272abc5SBjoern A. Zeeb 		}
4075c272abc5SBjoern A. Zeeb #endif
40766b4cac81SBjoern A. Zeeb 
4077d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
40786b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
40796b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
40806b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4081d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
40826b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
40836b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
40846b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
40856b4cac81SBjoern A. Zeeb 				ssids++;
40866b4cac81SBjoern A. Zeeb 			}
40876b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
40886b4cac81SBjoern A. Zeeb 		} else {
40896b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
40906b4cac81SBjoern A. Zeeb 		}
40916b4cac81SBjoern A. Zeeb 
40926b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
40936b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
40946b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
40956b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
40966b4cac81SBjoern A. Zeeb 		/* s6gp->... */
40976b4cac81SBjoern A. Zeeb 
4098d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4099d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4100d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4101d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4102d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4103d9945d78SBjoern A. Zeeb 
41046b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41056b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
41063206587aSBjoern A. Zeeb 
41073206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
41083206587aSBjoern A. Zeeb 		/* Re-check under lock. */
41093206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
41103206587aSBjoern A. Zeeb 		if (!running) {
41113206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
41123206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
41133206587aSBjoern A. Zeeb 
41143206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
41153206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
41163206587aSBjoern A. Zeeb 		}
41173206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41183206587aSBjoern A. Zeeb 		if (running) {
41193206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
41203206587aSBjoern A. Zeeb 			return;
41213206587aSBjoern A. Zeeb 		}
41223206587aSBjoern A. Zeeb 
41236b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
41246b4cac81SBjoern A. Zeeb 		if (error != 0) {
4125d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4126d9945d78SBjoern A. Zeeb 
41273206587aSBjoern A. Zeeb 			/*
41283206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
41293206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
41303206587aSBjoern A. Zeeb 			 * and only there.
41313206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
41323206587aSBjoern A. Zeeb 			 * behave differently:
41333206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
41343206587aSBjoern A. Zeeb 			 *        and can then still return an error.
41353206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
41363206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
41373206587aSBjoern A. Zeeb 			 * ...
41383206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
41393206587aSBjoern A. Zeeb 			 * and balance between both code paths.
41403206587aSBjoern A. Zeeb 			 */
41413206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
41423206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
41433206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
41446b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
41453206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
41463206587aSBjoern A. Zeeb 			}
41473206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4148d3ef7fb4SBjoern A. Zeeb 
4149d3ef7fb4SBjoern A. Zeeb 			/*
4150d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4151d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4152d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4153d3ef7fb4SBjoern A. Zeeb 			 */
4154196cfd0bSBjoern A. Zeeb 			if (error == 1) {
41558ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4156a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
41578ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41583206587aSBjoern A. Zeeb 				/*
41593206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
41603206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
41613206587aSBjoern A. Zeeb 				 * currently not re-enable it?
41623206587aSBjoern A. Zeeb 				 */
41633206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4164196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4165196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4166196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4167196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4168196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4169196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4170196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4171196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4172d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4173196cfd0bSBjoern A. Zeeb 			}
4174d3ef7fb4SBjoern A. Zeeb 
4175d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4176d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
41776b4cac81SBjoern A. Zeeb 		}
41786b4cac81SBjoern A. Zeeb 	}
41796b4cac81SBjoern A. Zeeb }
41806b4cac81SBjoern A. Zeeb 
41816b4cac81SBjoern A. Zeeb static void
41826b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
41836b4cac81SBjoern A. Zeeb {
41846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41858ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
41866b4cac81SBjoern A. Zeeb 
41876b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4189a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
41908ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41916b4cac81SBjoern A. Zeeb 		return;
41926b4cac81SBjoern A. Zeeb 	}
41938ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41948ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41956b4cac81SBjoern A. Zeeb 
41968ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4197a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4198a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
41996b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
42006b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
42016b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
42026b4cac81SBjoern A. Zeeb 
4203a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4204a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
42056b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
42066b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
42076b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4208a486fbbdSBjoern A. Zeeb 
42096b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
42106b4cac81SBjoern A. Zeeb 
42116b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4212086be6a8SBjoern A. Zeeb 
4213086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4214086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
42156b4cac81SBjoern A. Zeeb 	}
42166b4cac81SBjoern A. Zeeb }
42176b4cac81SBjoern A. Zeeb 
42186b4cac81SBjoern A. Zeeb static void
4219a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4220a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4221a486fbbdSBjoern A. Zeeb {
4222a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
42238ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4224a486fbbdSBjoern A. Zeeb 
4225a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
42268ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
42278ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
42288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42298ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4230a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4231a486fbbdSBjoern A. Zeeb }
4232a486fbbdSBjoern A. Zeeb 
4233a486fbbdSBjoern A. Zeeb static void
4234a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4235a486fbbdSBjoern A. Zeeb {
4236a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
42378ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4238a486fbbdSBjoern A. Zeeb 
4239a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
42408ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
42418ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
42428ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42438ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4244a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4245a486fbbdSBjoern A. Zeeb }
4246a486fbbdSBjoern A. Zeeb 
4247a486fbbdSBjoern A. Zeeb static void
42486b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
42496b4cac81SBjoern A. Zeeb {
42506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42516b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4252b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4253b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
42546b4cac81SBjoern A. Zeeb 	int error;
42558ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
42566b4cac81SBjoern A. Zeeb 
42576b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4258b2cf3c21SBjoern A. Zeeb 
4259b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4260b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
42616b4cac81SBjoern A. Zeeb 		return;
42626b4cac81SBjoern A. Zeeb 
4263a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
42648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
42658ac540d3SBjoern A. Zeeb 	hw_scan_running =
42668ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
42678ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
42688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42698ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4270a486fbbdSBjoern A. Zeeb 		return;
4271a486fbbdSBjoern A. Zeeb 
4272b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4273b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
42746b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
42756b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
42766b4cac81SBjoern A. Zeeb 		return;
42776b4cac81SBjoern A. Zeeb 	}
42786b4cac81SBjoern A. Zeeb 
42796b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
42806b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
42816b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
42826b4cac81SBjoern A. Zeeb 		    c, chan);
42836b4cac81SBjoern A. Zeeb 		return;
42846b4cac81SBjoern A. Zeeb 	}
42856b4cac81SBjoern A. Zeeb 
42866b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
42876b4cac81SBjoern A. Zeeb 	IMPROVE();
42886b4cac81SBjoern A. Zeeb 
42896b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42904a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
42919fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
429211450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
42939fb91463SBjoern A. Zeeb #endif
42944a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
42956b4cac81SBjoern A. Zeeb 
42966b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
42976b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
42986b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
42996b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
43006b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
43016b4cac81SBjoern A. Zeeb 		IMPROVE();
43026b4cac81SBjoern A. Zeeb 	} else {
43036b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
43046b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
43056b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
43066b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
43076b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
43086b4cac81SBjoern A. Zeeb 	}
43096b4cac81SBjoern A. Zeeb 
43106b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
43116b4cac81SBjoern A. Zeeb 	IMPROVE();
43126b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
43136b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
43146b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
43156b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
43166b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
43176b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
43186b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
43196b4cac81SBjoern A. Zeeb 			    error);
43206b4cac81SBjoern A. Zeeb 	}
43216b4cac81SBjoern A. Zeeb }
43226b4cac81SBjoern A. Zeeb 
43236b4cac81SBjoern A. Zeeb static struct ieee80211_node *
43246b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
43256b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
43266b4cac81SBjoern A. Zeeb {
43276b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43294f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
43306b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43316b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43326b4cac81SBjoern A. Zeeb 
43336b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
43346b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43356b4cac81SBjoern A. Zeeb 
43366b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
43374f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
43384f61ef8bSBjoern A. Zeeb 		return (NULL);
43394f61ef8bSBjoern A. Zeeb 
43406b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
43416b4cac81SBjoern A. Zeeb 	if (ni == NULL)
43426b4cac81SBjoern A. Zeeb 		return (NULL);
43436b4cac81SBjoern A. Zeeb 
43446b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
43454f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
43466b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
43476b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
43486b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
43496b4cac81SBjoern A. Zeeb 		return (NULL);
43506b4cac81SBjoern A. Zeeb 	}
43516b4cac81SBjoern A. Zeeb 
43526b4cac81SBjoern A. Zeeb 	return (ni);
43536b4cac81SBjoern A. Zeeb }
43546b4cac81SBjoern A. Zeeb 
43556b4cac81SBjoern A. Zeeb static int
43566b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
43576b4cac81SBjoern A. Zeeb {
43586b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43596b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43606b4cac81SBjoern A. Zeeb 	int error;
43616b4cac81SBjoern A. Zeeb 
43626b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43636b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43646b4cac81SBjoern A. Zeeb 
43656b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
43666b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
43676b4cac81SBjoern A. Zeeb 		if (error != 0)
43686b4cac81SBjoern A. Zeeb 			return (error);
43696b4cac81SBjoern A. Zeeb 	}
43706b4cac81SBjoern A. Zeeb 
43716b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
43726b4cac81SBjoern A. Zeeb 	IMPROVE();
43736b4cac81SBjoern A. Zeeb 
43746b4cac81SBjoern A. Zeeb 	return (0);
43756b4cac81SBjoern A. Zeeb }
43766b4cac81SBjoern A. Zeeb 
43776b4cac81SBjoern A. Zeeb static void
43786b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
43796b4cac81SBjoern A. Zeeb {
43806b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43826b4cac81SBjoern A. Zeeb 
43836b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43846b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43856b4cac81SBjoern A. Zeeb 
43866b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
43876b4cac81SBjoern A. Zeeb 	IMPROVE();
43886b4cac81SBjoern A. Zeeb 
43896b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
43906b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
43916b4cac81SBjoern A. Zeeb }
43926b4cac81SBjoern A. Zeeb 
43936b4cac81SBjoern A. Zeeb static void
43946b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
43956b4cac81SBjoern A. Zeeb {
43966b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43986b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43996b4cac81SBjoern A. Zeeb 
44006b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44016b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44026b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
44036b4cac81SBjoern A. Zeeb 
44040936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
44056b4cac81SBjoern A. Zeeb 
44060936c648SBjoern A. Zeeb 	/*
44070936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
44080936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
44090936c648SBjoern A. Zeeb 	 */
44100936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
44116b4cac81SBjoern A. Zeeb 
44126b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
44136b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
44146b4cac81SBjoern A. Zeeb }
44156b4cac81SBjoern A. Zeeb 
44166b4cac81SBjoern A. Zeeb static int
44176b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
44186b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
44196b4cac81SBjoern A. Zeeb {
44206b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
44216b4cac81SBjoern A. Zeeb 
44226b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4423fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
44242372f8ccSBjoern A. Zeeb #if 0
442588665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
44262372f8ccSBjoern A. Zeeb #else
44272372f8ccSBjoern A. Zeeb 	/*
44282372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
44292372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
44302372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
44312372f8ccSBjoern A. Zeeb 	 */
44322372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
44332372f8ccSBjoern A. Zeeb #endif
4434fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4435fa4e4257SBjoern A. Zeeb 		/*
4436fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4437fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4438fa4e4257SBjoern A. Zeeb 		 */
44390936c648SBjoern A. Zeeb 		m_free(m);
44400936c648SBjoern A. Zeeb 		return (ENETDOWN);
44410936c648SBjoern A. Zeeb 	}
44426b4cac81SBjoern A. Zeeb 
44436b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
44446b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4445fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4446fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
44476b4cac81SBjoern A. Zeeb 
44489d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
44499d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
44506b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
44516b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
44526b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
44539d9ba2b7SBjoern A. Zeeb #endif
44546b4cac81SBjoern A. Zeeb 
44556b4cac81SBjoern A. Zeeb 	return (0);
44566b4cac81SBjoern A. Zeeb }
44576b4cac81SBjoern A. Zeeb 
445811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
445911db70b6SBjoern A. Zeeb static int
446011db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
446111db70b6SBjoern A. Zeeb     struct sk_buff *skb)
446211db70b6SBjoern A. Zeeb {
446311db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
446411db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
446511db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
446611db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
446711db70b6SBjoern A. Zeeb 	uint8_t *p;
446811db70b6SBjoern A. Zeeb 
446911db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
447011db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
447111db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
447211db70b6SBjoern A. Zeeb 
447311db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
447411db70b6SBjoern A. Zeeb 
447511db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
447611db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
447711db70b6SBjoern A. Zeeb 
447811db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
447911db70b6SBjoern A. Zeeb 	if (kc == NULL) {
448011db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
448111db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
448211db70b6SBjoern A. Zeeb 		return (ENXIO);
448311db70b6SBjoern A. Zeeb 	}
448411db70b6SBjoern A. Zeeb 
448511db70b6SBjoern A. Zeeb 
448611db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
448711db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
448811db70b6SBjoern A. Zeeb 
448911db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
449011db70b6SBjoern A. Zeeb 
449111db70b6SBjoern A. Zeeb 	/*
449211db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
449311db70b6SBjoern A. Zeeb 	 * or if we are done?
449411db70b6SBjoern A. Zeeb 	 */
449511db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
449611db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
449711db70b6SBjoern A. Zeeb 	    /* MFP */
449811db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
449911db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
450011db70b6SBjoern A. Zeeb 			return (0);
450111db70b6SBjoern A. Zeeb 
450211db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
450311db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
450411db70b6SBjoern A. Zeeb 		return (ENOSPC);
450511db70b6SBjoern A. Zeeb 
450611db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
450711db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
450811db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
450911db70b6SBjoern A. Zeeb 
451011db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
451111db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
451211db70b6SBjoern A. Zeeb 		return (0);
451311db70b6SBjoern A. Zeeb 
451411db70b6SBjoern A. Zeeb 	p += hdrlen;
451511db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
451611db70b6SBjoern A. Zeeb 
451711db70b6SBjoern A. Zeeb 	return (0);
451811db70b6SBjoern A. Zeeb }
451911db70b6SBjoern A. Zeeb #endif
452011db70b6SBjoern A. Zeeb 
45216b4cac81SBjoern A. Zeeb static void
45226b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
45236b4cac81SBjoern A. Zeeb {
45246b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
45256b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
45266b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
45276b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
45286b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45306b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
45316b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45326b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
45336b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
45346b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
45356b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
45366b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4537e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4538ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
45396b4cac81SBjoern A. Zeeb 	void *buf;
454011db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4541e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
45426b4cac81SBjoern A. Zeeb 
45436b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
45446b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45459d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
45466b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
45476b4cac81SBjoern A. Zeeb #endif
45486b4cac81SBjoern A. Zeeb 
45496b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4550b35f6cd0SBjoern A. Zeeb 	k = NULL;
455111db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
45526b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
45536b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
455411db70b6SBjoern A. Zeeb 
455511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
455611db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
455711db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
455811db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
455911db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
456011db70b6SBjoern A. Zeeb 		}
456111db70b6SBjoern A. Zeeb #endif
456211db70b6SBjoern A. Zeeb 
456311db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
456411db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
45656b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
45666b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
45676b4cac81SBjoern A. Zeeb 			if (k == NULL) {
45686b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
45696b4cac81SBjoern A. Zeeb 				m_freem(m);
45706b4cac81SBjoern A. Zeeb 				return;
45716b4cac81SBjoern A. Zeeb 			}
45726b4cac81SBjoern A. Zeeb 		}
457311db70b6SBjoern A. Zeeb 	}
45746b4cac81SBjoern A. Zeeb 
45756b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45766b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45776b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45786b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
45796b4cac81SBjoern A. Zeeb 
45806b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
45816b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
45826b4cac81SBjoern A. Zeeb 
45836b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
45846b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
45856b4cac81SBjoern A. Zeeb 		if (k != NULL)
45866b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
45876b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
45886b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
45896b4cac81SBjoern A. Zeeb 		IMPROVE();
45906b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
45916b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
45926b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
45936b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
45946b4cac81SBjoern A. Zeeb 		}
45956b4cac81SBjoern A. Zeeb 
45966b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
45976b4cac81SBjoern A. Zeeb 	}
45986b4cac81SBjoern A. Zeeb 
45996b4cac81SBjoern A. Zeeb 	/*
46006b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
46016b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
46023d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
46033d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
46046b4cac81SBjoern A. Zeeb 	 */
46056b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
46066b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4607bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4608bcf1d8eeSBjoern A. Zeeb 
4609bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4610bcf1d8eeSBjoern A. Zeeb 			int tid;
4611bcf1d8eeSBjoern A. Zeeb 
4612bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4613bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4614bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4615bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4616bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4617bcf1d8eeSBjoern A. Zeeb 					continue;
4618bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4619bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4620bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4621bcf1d8eeSBjoern A. Zeeb 			}
4622bcf1d8eeSBjoern A. Zeeb 		}
46236b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
46246b4cac81SBjoern A. Zeeb 		m_freem(m);
46256b4cac81SBjoern A. Zeeb 		return;
46266b4cac81SBjoern A. Zeeb 	}
46276b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
46286b4cac81SBjoern A. Zeeb 
46296b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
46306b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
46316b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
46326b4cac81SBjoern A. Zeeb 	skb->m = m;
46336b4cac81SBjoern A. Zeeb #if 0
46346b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
46356b4cac81SBjoern A. Zeeb #else
46366b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
46376b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
46386b4cac81SBjoern A. Zeeb #endif
46396b4cac81SBjoern A. Zeeb 	/* Save the ni. */
46406b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
46416b4cac81SBjoern A. Zeeb 
46426b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
46436b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
46446b4cac81SBjoern A. Zeeb 
4645e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4646e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4647e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
46481665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
46491665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
46501665ef97SBjoern A. Zeeb 			skb->priority = 7;
46511665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
46521665ef97SBjoern A. Zeeb 		} else {
46531665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
46541665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4655e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4656e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
46571665ef97SBjoern A. Zeeb 		}
4658e3a0b120SBjoern A. Zeeb 	} else {
4659e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4660fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4661e3a0b120SBjoern A. Zeeb 	}
46626b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
46636b4cac81SBjoern A. Zeeb 
46646b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
46656b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
46666b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
46676b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
46686b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
46696b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4670e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
46716b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
46726b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
46736b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
46746b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
46759fb91463SBjoern A. Zeeb #ifdef __notyet__
46769fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46779fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
46789fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
46799fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
46809fb91463SBjoern A. Zeeb #endif
46819fb91463SBjoern A. Zeeb #endif
46826b4cac81SBjoern A. Zeeb 
46836b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4684b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
468511db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
468611db70b6SBjoern A. Zeeb 		int error;
468711db70b6SBjoern A. Zeeb 
468811db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
468911db70b6SBjoern A. Zeeb 		if (error != 0) {
469011db70b6SBjoern A. Zeeb 			/*
469111db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
469211db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
469311db70b6SBjoern A. Zeeb 			 */
469411db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
469511db70b6SBjoern A. Zeeb 			return;
469611db70b6SBjoern A. Zeeb 		}
469711db70b6SBjoern A. Zeeb 	}
46986b4cac81SBjoern A. Zeeb #endif
46996b4cac81SBjoern A. Zeeb 
47006b4cac81SBjoern A. Zeeb 	IMPROVE();
47016b4cac81SBjoern A. Zeeb 
4702e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4703e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4704e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4705e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4706e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4707d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4708e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4709e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4710e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4711e3a0b120SBjoern A. Zeeb 	}
4712e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4713d0d29110SBjoern A. Zeeb 		goto ops_tx;
4714e3a0b120SBjoern A. Zeeb 
4715d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4716d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4717d0d29110SBjoern A. Zeeb 
4718eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
47196b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
47209d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47219d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4722d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4723d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4724d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4725fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4726d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4727d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4728d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4729d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
47309d9ba2b7SBjoern A. Zeeb #endif
4731eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
473245bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4733d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
473445bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
47356b4cac81SBjoern A. Zeeb 	return;
47366b4cac81SBjoern A. Zeeb 
47376b4cac81SBjoern A. Zeeb ops_tx:
47389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47399d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4740d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4741d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
47426b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
47436b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
47449d9ba2b7SBjoern A. Zeeb #endif
47456b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
47466b4cac81SBjoern A. Zeeb 	control.sta = sta;
474745bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
47486b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
474945bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
47506b4cac81SBjoern A. Zeeb }
47516b4cac81SBjoern A. Zeeb 
47526b4cac81SBjoern A. Zeeb static void
47536b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
47546b4cac81SBjoern A. Zeeb {
47556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
47566b4cac81SBjoern A. Zeeb 	struct mbufq mq;
47576b4cac81SBjoern A. Zeeb 	struct mbuf *m;
475888665349SBjoern A. Zeeb 	bool shall_tx;
47596b4cac81SBjoern A. Zeeb 
47606b4cac81SBjoern A. Zeeb 	lsta = ctx;
47616b4cac81SBjoern A. Zeeb 
47629d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47639d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
47646b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
47659d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
47666b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
47679d9ba2b7SBjoern A. Zeeb #endif
47686b4cac81SBjoern A. Zeeb 
47696b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
47706b4cac81SBjoern A. Zeeb 
4771fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4772fa4e4257SBjoern A. Zeeb 	/*
4773fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
477488665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
477588665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
477688665349SBjoern A. Zeeb 	 * point to try to TX.
477788665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
477888665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4779fa4e4257SBjoern A. Zeeb 	 */
47802372f8ccSBjoern A. Zeeb #if 0
478188665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
47822372f8ccSBjoern A. Zeeb #else
47832372f8ccSBjoern A. Zeeb 	/*
47842372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
47852372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
47862372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
47872372f8ccSBjoern A. Zeeb 	 */
47882372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
47892372f8ccSBjoern A. Zeeb #endif
479088665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
47916b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
479288665349SBjoern A. Zeeb 	/*
479388665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
479488665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
479588665349SBjoern A. Zeeb 	 */
4796fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47976b4cac81SBjoern A. Zeeb 
47986b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
47996b4cac81SBjoern A. Zeeb 	while (m != NULL) {
48006b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
48016b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
48026b4cac81SBjoern A. Zeeb 	}
48036b4cac81SBjoern A. Zeeb }
48046b4cac81SBjoern A. Zeeb 
48056b4cac81SBjoern A. Zeeb static int
48066b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
48076b4cac81SBjoern A. Zeeb {
48086b4cac81SBjoern A. Zeeb 
48096b4cac81SBjoern A. Zeeb 	/* XXX TODO */
48106b4cac81SBjoern A. Zeeb 	IMPROVE();
48116b4cac81SBjoern A. Zeeb 
48126b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
48136b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
48146b4cac81SBjoern A. Zeeb 
48156b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
48166b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
48176b4cac81SBjoern A. Zeeb }
48186b4cac81SBjoern A. Zeeb 
48199fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
48209fb91463SBjoern A. Zeeb static int
48219fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
48229fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
48239fb91463SBjoern A. Zeeb {
48249fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48259fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48269fb91463SBjoern A. Zeeb 
48279fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48289fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
48299fb91463SBjoern A. Zeeb 
4830310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
48319fb91463SBjoern A. Zeeb 
48329fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
48339fb91463SBjoern A. Zeeb }
48349fb91463SBjoern A. Zeeb 
48359fb91463SBjoern A. Zeeb static int
48369fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
48379fb91463SBjoern A. Zeeb {
48389fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48399fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48409fb91463SBjoern A. Zeeb 
48419fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48429fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
48439fb91463SBjoern A. Zeeb 
4844310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
48459fb91463SBjoern A. Zeeb 
48469fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
48479fb91463SBjoern A. Zeeb }
48489fb91463SBjoern A. Zeeb 
48499fb91463SBjoern A. Zeeb 
48509fb91463SBjoern A. Zeeb static int
48519fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
48529fb91463SBjoern A. Zeeb {
48539fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48549fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48559fb91463SBjoern A. Zeeb 
48569fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48579fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
48589fb91463SBjoern A. Zeeb 
4859310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
48609fb91463SBjoern A. Zeeb 
48619fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
48629fb91463SBjoern A. Zeeb }
48639fb91463SBjoern A. Zeeb 
4864310743c4SBjoern A. Zeeb /*
4865310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4866310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4867310743c4SBjoern A. Zeeb  *
4868310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4869310743c4SBjoern A. Zeeb  */
48709fb91463SBjoern A. Zeeb static int
48719fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48729fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
48739fb91463SBjoern A. Zeeb {
48749fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48759fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4876310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4877310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4878310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4879310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4880310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4881310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4882310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4883310743c4SBjoern A. Zeeb 	int error;
48849fb91463SBjoern A. Zeeb 
48859fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48869fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4887310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4888310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4889310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4890310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4891310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4892310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48939fb91463SBjoern A. Zeeb 
4894310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4895310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4896310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4897310743c4SBjoern A. Zeeb 		return (0);
4898310743c4SBjoern A. Zeeb 	}
4899310743c4SBjoern A. Zeeb 
4900310743c4SBjoern A. Zeeb 	params.sta = sta;
4901310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4902310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4903310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4904310743c4SBjoern A. Zeeb 	params.timeout = 0;
4905310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4906310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4907310743c4SBjoern A. Zeeb 	params.amsdu = false;
4908310743c4SBjoern A. Zeeb 
4909310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4910310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4911310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4912310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4913310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4914310743c4SBjoern A. Zeeb 	if (error != 0) {
4915310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4916310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4917310743c4SBjoern A. Zeeb 		return (0);
4918310743c4SBjoern A. Zeeb 	}
49199fb91463SBjoern A. Zeeb 
49209fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
49219fb91463SBjoern A. Zeeb }
49229fb91463SBjoern A. Zeeb 
4923310743c4SBjoern A. Zeeb /*
4924310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4925310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4926310743c4SBjoern A. Zeeb  *
4927310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4928310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4929310743c4SBjoern A. Zeeb  */
49309fb91463SBjoern A. Zeeb static int
49319fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
49329fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
49339fb91463SBjoern A. Zeeb {
49349fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49359fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4936310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4937310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4938310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4939310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4940310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4941310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4942310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4943310743c4SBjoern A. Zeeb 	int error;
49449fb91463SBjoern A. Zeeb 
49459fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49469fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4947310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4948310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4949310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4950310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4951310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4952310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49539fb91463SBjoern A. Zeeb 
4954310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4955310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4956310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4957310743c4SBjoern A. Zeeb 		return (0);
4958310743c4SBjoern A. Zeeb 	}
4959310743c4SBjoern A. Zeeb 
4960310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4961310743c4SBjoern A. Zeeb 		params.sta = sta;
4962310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4963310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4964310743c4SBjoern A. Zeeb 		params.timeout = 0;
4965310743c4SBjoern A. Zeeb 		params.ssn = 0;
4966310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4967310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4968310743c4SBjoern A. Zeeb 			params.amsdu = true;
4969310743c4SBjoern A. Zeeb 		else
4970310743c4SBjoern A. Zeeb 			params.amsdu = false;
4971310743c4SBjoern A. Zeeb 	} else {
4972310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4973310743c4SBjoern A. Zeeb 		params.sta = sta;
4974310743c4SBjoern A. Zeeb 		switch (status) {
4975310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4976310743c4SBjoern A. Zeeb 		default:
4977310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4978310743c4SBjoern A. Zeeb 			break;
4979310743c4SBjoern A. Zeeb 		}
4980310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4981310743c4SBjoern A. Zeeb 		params.timeout = 0;
4982310743c4SBjoern A. Zeeb 		params.ssn = 0;
4983310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4984310743c4SBjoern A. Zeeb 		params.amsdu = false;
4985310743c4SBjoern A. Zeeb 	}
4986310743c4SBjoern A. Zeeb 
4987310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4988310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4989310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4990310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4991310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4992310743c4SBjoern A. Zeeb 	if (error != 0) {
4993310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4994310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4995310743c4SBjoern A. Zeeb 		return (0);
4996310743c4SBjoern A. Zeeb 	}
4997310743c4SBjoern A. Zeeb 
4998310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
49999fb91463SBjoern A. Zeeb 
50009fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
50019fb91463SBjoern A. Zeeb }
50029fb91463SBjoern A. Zeeb 
5003310743c4SBjoern A. Zeeb /*
5004310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5005310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5006310743c4SBjoern A. Zeeb  */
50079fb91463SBjoern A. Zeeb static void
50089fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
50099fb91463SBjoern A. Zeeb {
50109fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50119fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5012310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5013310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5014310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5015310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5016310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5017310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5018310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5019310743c4SBjoern A. Zeeb 	int error;
50209fb91463SBjoern A. Zeeb 
50219fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50229fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5023310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5024310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5025310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5026310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5027310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5028310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50299fb91463SBjoern A. Zeeb 
5030310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5031310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5032310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5033310743c4SBjoern A. Zeeb 		goto n80211;
5034310743c4SBjoern A. Zeeb 	}
50359fb91463SBjoern A. Zeeb 
5036310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5037310743c4SBjoern A. Zeeb 	params.sta = sta;
5038310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5039310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5040310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5041310743c4SBjoern A. Zeeb 	params.timeout = 0;
5042310743c4SBjoern A. Zeeb 	params.ssn = 0;
5043310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5044310743c4SBjoern A. Zeeb 	params.amsdu = false;
5045310743c4SBjoern A. Zeeb 
5046310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5047310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
5048310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5049310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
5050310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5051310743c4SBjoern A. Zeeb 	if (error != 0) {
5052310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5053310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5054310743c4SBjoern A. Zeeb 		goto n80211;
5055310743c4SBjoern A. Zeeb 	}
5056310743c4SBjoern A. Zeeb 
5057310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5058310743c4SBjoern A. Zeeb 
5059310743c4SBjoern A. Zeeb n80211:
50609fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
50619fb91463SBjoern A. Zeeb }
50629fb91463SBjoern A. Zeeb 
50639fb91463SBjoern A. Zeeb static void
50649fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
50659fb91463SBjoern A. Zeeb {
50669fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50679fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50689fb91463SBjoern A. Zeeb 
50699fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50709fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50719fb91463SBjoern A. Zeeb 
50729fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50739fb91463SBjoern A. Zeeb 
50749fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
50759fb91463SBjoern A. Zeeb }
50769fb91463SBjoern A. Zeeb 
50779fb91463SBjoern A. Zeeb static void
50789fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
50799fb91463SBjoern A. Zeeb     int status)
50809fb91463SBjoern A. Zeeb {
50819fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50829fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50839fb91463SBjoern A. Zeeb 
50849fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50859fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50869fb91463SBjoern A. Zeeb 
50879fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50889fb91463SBjoern A. Zeeb 
50899fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
50909fb91463SBjoern A. Zeeb }
50919fb91463SBjoern A. Zeeb 
50929fb91463SBjoern A. Zeeb static int
50939fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
50949fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
50959fb91463SBjoern A. Zeeb {
50969fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50979fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50989fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50999fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
51009fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51019fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
51029fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51039fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5104310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
51059fb91463SBjoern A. Zeeb 	int error;
51069fb91463SBjoern A. Zeeb 
51079fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51089fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51099fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51109fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
51119fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
51129fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
51139fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
51149fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51159fb91463SBjoern A. Zeeb 
5116310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5117310743c4SBjoern A. Zeeb 
5118310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5119310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5120310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5121310743c4SBjoern A. Zeeb 		return (-ENXIO);
5122310743c4SBjoern A. Zeeb 	}
5123310743c4SBjoern A. Zeeb 
51249fb91463SBjoern A. Zeeb 	params.sta = sta;
51259fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
51269fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
51279fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
51289fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
51299fb91463SBjoern A. Zeeb 	else
51309fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5131310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5132310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
51339fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
51349fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
51359fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
51369fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5137310743c4SBjoern A. Zeeb 
5138310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5139310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5140310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5141310743c4SBjoern A. Zeeb 		params.amsdu = true;
5142310743c4SBjoern A. Zeeb 	else
51439fb91463SBjoern A. Zeeb 		params.amsdu = false;
51449fb91463SBjoern A. Zeeb 
5145310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
51469fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5147310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
51489fb91463SBjoern A. Zeeb 	if (error != 0) {
51499fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
51509fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
51519fb91463SBjoern A. Zeeb 		return (error);
51529fb91463SBjoern A. Zeeb 	}
5153310743c4SBjoern A. Zeeb 
5154310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5155310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5156310743c4SBjoern A. Zeeb 	}
5157310743c4SBjoern A. Zeeb 
51589fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
51599fb91463SBjoern A. Zeeb 
51609fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
51619fb91463SBjoern A. Zeeb 	return (error);
51629fb91463SBjoern A. Zeeb }
51639fb91463SBjoern A. Zeeb 
51649fb91463SBjoern A. Zeeb static void
51659fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
51669fb91463SBjoern A. Zeeb {
51679fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51689fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51699fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
51709fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
51719fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51729fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
51739fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51749fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5175310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
51769fb91463SBjoern A. Zeeb 	int error;
51779fb91463SBjoern A. Zeeb 	uint8_t tid;
517865c573e4SBjoern A. Zeeb 	bool ic_locked;
51799fb91463SBjoern A. Zeeb 
51809fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51819fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51829fb91463SBjoern A. Zeeb 
51839fb91463SBjoern A. Zeeb 	/*
51849fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
51859fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
51869fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
51879fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
51889fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
51899fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
51909fb91463SBjoern A. Zeeb 	 * track some state itself.
51919fb91463SBjoern A. Zeeb 	 */
51929fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
51939fb91463SBjoern A. Zeeb 		goto net80211_only;
51949fb91463SBjoern A. Zeeb 
51959fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51969fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
51979fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
51989fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
51999fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
52009fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52019fb91463SBjoern A. Zeeb 
52029fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
52039fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
52049fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
52059fb91463SBjoern A. Zeeb 			break;
52069fb91463SBjoern A. Zeeb 	}
52079fb91463SBjoern A. Zeeb 
52089fb91463SBjoern A. Zeeb 	params.sta = sta;
52099fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
52109fb91463SBjoern A. Zeeb 	params.buf_size = 0;
52119fb91463SBjoern A. Zeeb 	params.timeout = 0;
52129fb91463SBjoern A. Zeeb 	params.ssn = 0;
52139fb91463SBjoern A. Zeeb 	params.tid = tid;
52149fb91463SBjoern A. Zeeb 	params.amsdu = false;
52159fb91463SBjoern A. Zeeb 
521665c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
521765c573e4SBjoern A. Zeeb 	if (ic_locked)
521865c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5219310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
52209fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5221310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
522265c573e4SBjoern A. Zeeb 	if (ic_locked)
522365c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
52249fb91463SBjoern A. Zeeb 	if (error != 0)
52259fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
52269fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
52279fb91463SBjoern A. Zeeb 
52289fb91463SBjoern A. Zeeb net80211_only:
52299fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
52309fb91463SBjoern A. Zeeb }
52319fb91463SBjoern A. Zeeb #endif
52329fb91463SBjoern A. Zeeb 
52339fb91463SBjoern A. Zeeb static void
52349fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
52359fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
52369fb91463SBjoern A. Zeeb {
52379fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
52389fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
52399fb91463SBjoern A. Zeeb 
52409fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
52419fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
52429fb91463SBjoern A. Zeeb 		return;
52439fb91463SBjoern A. Zeeb 
52449fb91463SBjoern A. Zeeb 	switch (band) {
52459fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
52469fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
52479fb91463SBjoern A. Zeeb 		break;
52489fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
52499fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
52509fb91463SBjoern A. Zeeb 		break;
52519fb91463SBjoern A. Zeeb 	default:
52529fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
52539fb91463SBjoern A. Zeeb 		return;
52549fb91463SBjoern A. Zeeb 	}
52559fb91463SBjoern A. Zeeb 
52569fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
52579fb91463SBjoern A. Zeeb 
52589fb91463SBjoern A. Zeeb 	/*
52599fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
52609fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
52619fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
52629fb91463SBjoern A. Zeeb 	 */
52639fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
52649fb91463SBjoern A. Zeeb 
52659fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
52669fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
52679fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
52689fb91463SBjoern A. Zeeb #ifdef __notyet__
52699fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
52709fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
52719fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
52729fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
52739fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
52749fb91463SBjoern A. Zeeb #endif
52759fb91463SBjoern A. Zeeb 
52769fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
52779fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
52789fb91463SBjoern A. Zeeb 
52799fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
52809fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
52819fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
52829fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
52839fb91463SBjoern A. Zeeb #endif
52849fb91463SBjoern A. Zeeb }
52859fb91463SBjoern A. Zeeb 
52866b4cac81SBjoern A. Zeeb static void
52876b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
52886b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
52896b4cac81SBjoern A. Zeeb {
52906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52916b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52926b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
52936b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
52946b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
52956b4cac81SBjoern A. Zeeb 
52966b4cac81SBjoern A. Zeeb 	/* Channels */
52976b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
52986b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
52996b4cac81SBjoern A. Zeeb 
53006b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
53016b4cac81SBjoern A. Zeeb 	nchans = 0;
53026b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
53036b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
53046b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
53056b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
53066b4cac81SBjoern A. Zeeb 		chan_flags = 0;
53076b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
53086b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
53099fb91463SBjoern A. Zeeb 
53109fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
53116b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
53129fb91463SBjoern A. Zeeb 
53139fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
53149fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
53156b4cac81SBjoern A. Zeeb 
53166b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5317cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
53186b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
53196b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
53206b4cac81SBjoern A. Zeeb 
53216b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
53223f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
53233f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
53246b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
53256b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
53266b4cac81SBjoern A. Zeeb 				continue;
53276b4cac81SBjoern A. Zeeb 			}
53286b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
53296b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
53306b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
53316b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
53326b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
53336b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
53346b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
53356b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
53366b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
53376b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
53386b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
53396b4cac81SBjoern A. Zeeb 
53406b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
53416b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
53426b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
53435856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5344cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5345cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
53463f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
53473f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
53486b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
53496b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
53506b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5351cee56e77SBjoern A. Zeeb 			if (error != 0)
53526b4cac81SBjoern A. Zeeb 				break;
53536b4cac81SBjoern A. Zeeb 		}
53546b4cac81SBjoern A. Zeeb 	}
53556b4cac81SBjoern A. Zeeb 
53566b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
53576b4cac81SBjoern A. Zeeb 	nchans = 0;
53586b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
53596b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
53606b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
53616b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
53626b4cac81SBjoern A. Zeeb 		chan_flags = 0;
53636b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
53649fb91463SBjoern A. Zeeb 
53659fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
53669fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
53679fb91463SBjoern A. Zeeb 
53689fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
53696b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
53706b4cac81SBjoern A. Zeeb 
53716b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5372562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
53736b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5374ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5375ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
53766b4cac81SBjoern A. Zeeb 
53776b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
53786b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
53796b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5380562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53816b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
53826b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5383562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53846b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
53856b4cac81SBjoern A. Zeeb 		}
53866b4cac81SBjoern A. Zeeb #endif
53876b4cac81SBjoern A. Zeeb 
53886b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5389cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
53906b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
53916b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
53926b4cac81SBjoern A. Zeeb 
53936b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
53943f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
53953f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
53966b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
53976b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
53986b4cac81SBjoern A. Zeeb 				continue;
53996b4cac81SBjoern A. Zeeb 			}
54006b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
54016b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
54026b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
54036b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
54046b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
54056b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
54066b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
54076b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
54086b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
54096b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
54106b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
54116b4cac81SBjoern A. Zeeb 
54126b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
54136b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
54146b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
54155856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5416cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5417cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
54183f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
54193f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
54206b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
54216b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
54226b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5423cee56e77SBjoern A. Zeeb 			if (error != 0)
54246b4cac81SBjoern A. Zeeb 				break;
54256b4cac81SBjoern A. Zeeb 		}
54266b4cac81SBjoern A. Zeeb 	}
54276b4cac81SBjoern A. Zeeb }
54286b4cac81SBjoern A. Zeeb 
54296b4cac81SBjoern A. Zeeb static void *
54306b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
54316b4cac81SBjoern A. Zeeb {
54326b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54336b4cac81SBjoern A. Zeeb 
54346b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
54356b4cac81SBjoern A. Zeeb 
54366b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
54376b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
54386b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
54396b4cac81SBjoern A. Zeeb 
54406b4cac81SBjoern A. Zeeb 	return (ic);
54416b4cac81SBjoern A. Zeeb }
54426b4cac81SBjoern A. Zeeb 
54436b4cac81SBjoern A. Zeeb struct ieee80211_hw *
54446b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
54456b4cac81SBjoern A. Zeeb {
54466b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54476b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54486b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5449a5ae63edSBjoern A. Zeeb 	int ac;
54506b4cac81SBjoern A. Zeeb 
54516b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
54526b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
54536b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
54546b4cac81SBjoern A. Zeeb 		return (NULL);
54556b4cac81SBjoern A. Zeeb 
54566b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54576b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5458652e22d3SBjoern A. Zeeb 
54598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
54608ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5461eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
54628891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
54636b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5464a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5465a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5466a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5467a5ae63edSBjoern A. Zeeb 	}
54686b4cac81SBjoern A. Zeeb 
5469759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5470759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5471759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5472759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5473759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5474759a996dSBjoern A. Zeeb 
54756b4cac81SBjoern A. Zeeb 	/*
54766b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
54776b4cac81SBjoern A. Zeeb 	 * not initialized.
54786b4cac81SBjoern A. Zeeb 	 */
54796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54806b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5481086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
54826b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
54836b4cac81SBjoern A. Zeeb 
54846b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
54856b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
54866b4cac81SBjoern A. Zeeb 
54876b4cac81SBjoern A. Zeeb 	IMPROVE();
54886b4cac81SBjoern A. Zeeb 
54896b4cac81SBjoern A. Zeeb 	return (hw);
54906b4cac81SBjoern A. Zeeb }
54916b4cac81SBjoern A. Zeeb 
54926b4cac81SBjoern A. Zeeb void
54936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
54946b4cac81SBjoern A. Zeeb {
54956b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5496759a996dSBjoern A. Zeeb 	struct mbuf *m;
54976b4cac81SBjoern A. Zeeb 
54986b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54996b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
55006b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
55016b4cac81SBjoern A. Zeeb 
5502759a996dSBjoern A. Zeeb 	/*
5503759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5504759a996dSBjoern A. Zeeb 	 */
5505759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5506759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5507759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5508759a996dSBjoern A. Zeeb 
5509759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5510759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5511759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5512759a996dSBjoern A. Zeeb 
5513759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5514759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5515759a996dSBjoern A. Zeeb 	while (m != NULL) {
5516759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5517759a996dSBjoern A. Zeeb 
5518759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5519759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5520759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5521759a996dSBjoern A. Zeeb 
5522759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5523759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5524759a996dSBjoern A. Zeeb 		}
5525759a996dSBjoern A. Zeeb 		m_freem(m);
5526759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5527759a996dSBjoern A. Zeeb 	}
5528759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5529759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5530759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5531759a996dSBjoern A. Zeeb 
55326b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5533eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
55348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5535eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5536eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
55376b4cac81SBjoern A. Zeeb 	IMPROVE();
55386b4cac81SBjoern A. Zeeb }
55396b4cac81SBjoern A. Zeeb 
55406b4cac81SBjoern A. Zeeb void
55416b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
55426b4cac81SBjoern A. Zeeb {
55436b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55446b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55456b4cac81SBjoern A. Zeeb 
55466b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55476b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55486b4cac81SBjoern A. Zeeb 
55496b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
55506b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
55516b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
55526b4cac81SBjoern A. Zeeb 
55536b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
55546b4cac81SBjoern A. Zeeb }
55556b4cac81SBjoern A. Zeeb 
55566b4cac81SBjoern A. Zeeb struct ieee80211_hw *
55576b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
55586b4cac81SBjoern A. Zeeb {
55596b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55606b4cac81SBjoern A. Zeeb 
55616b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
55626b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
55636b4cac81SBjoern A. Zeeb }
55646b4cac81SBjoern A. Zeeb 
55656b4cac81SBjoern A. Zeeb static void
55666b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
55676b4cac81SBjoern A. Zeeb {
55686b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55696b4cac81SBjoern A. Zeeb 
55706b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55716b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
55726b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
55736b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
55746b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
55756b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
55766b4cac81SBjoern A. Zeeb }
55776b4cac81SBjoern A. Zeeb 
55782e183d99SBjoern A. Zeeb int
55796b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
55806b4cac81SBjoern A. Zeeb {
55816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5583c5b96b3eSBjoern A. Zeeb 	int band, i;
55846b4cac81SBjoern A. Zeeb 
55856b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55866b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55876b4cac81SBjoern A. Zeeb 
5588652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5589652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5590652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5591652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5592652e22d3SBjoern A. Zeeb 
55936b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
55946b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
55956b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55966b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
55976b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
55986b4cac81SBjoern A. Zeeb 		/* We take the first one. */
55996b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
56006b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
56016b4cac81SBjoern A. Zeeb 	} else {
56026b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
56036b4cac81SBjoern A. Zeeb 	}
56046b4cac81SBjoern A. Zeeb 
56053d09d310SBjoern A. Zeeb #ifdef __not_yet__
56063d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
56076b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
56083d09d310SBjoern A. Zeeb #endif
56096b4cac81SBjoern A. Zeeb 
56106b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
56116b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
56126b4cac81SBjoern A. Zeeb 
56136b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
56146b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
56156b4cac81SBjoern A. Zeeb 	ic->ic_caps =
56166b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
56176b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
56186b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
56194a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
56206b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
56214a67f1dfSBjoern A. Zeeb #endif
56226b4cac81SBjoern A. Zeeb #if 0
56236b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
56246b4cac81SBjoern A. Zeeb #endif
56256b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
56266b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
56276b4cac81SBjoern A. Zeeb 	    ;
56286b4cac81SBjoern A. Zeeb #if 0
56296b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
56306b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
56316b4cac81SBjoern A. Zeeb #endif
5632cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5633cc4e78d5SBjoern A. Zeeb 		/*
5634cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5635cc4e78d5SBjoern A. Zeeb 		 *
5636cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5637cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5638cc4e78d5SBjoern A. Zeeb 		 * the flag.
5639cc4e78d5SBjoern A. Zeeb 		 */
56406b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5641a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
56426b4cac81SBjoern A. Zeeb 	}
56436b4cac81SBjoern A. Zeeb 
5644527687a9SBjoern A. Zeeb 	/*
5645527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5646527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5647527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5648527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5649527687a9SBjoern A. Zeeb 	 */
5650527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5651527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5652527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5653527687a9SBjoern A. Zeeb 
5654527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5655527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5656527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5657527687a9SBjoern A. Zeeb 		}
5658527687a9SBjoern A. Zeeb 	}
5659527687a9SBjoern A. Zeeb 
56606b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5661b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
566211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
56636b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
56646b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
56656b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
56666b4cac81SBjoern A. Zeeb 	}
56676b4cac81SBjoern A. Zeeb #endif
56686b4cac81SBjoern A. Zeeb 
56696b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
56706b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
56716b4cac81SBjoern A. Zeeb 
56726b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
56736b4cac81SBjoern A. Zeeb 
56746b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
56756b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
56766b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
56776b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
56786b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
56796b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
56806b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
56816b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
56826b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
56836b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
56846b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
56856b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
56866b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
56876b4cac81SBjoern A. Zeeb 
5688a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5689a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5690a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5691a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5692a486fbbdSBjoern A. Zeeb 
56936b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
56946b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
56956b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
56966b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
56976b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
56986b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
56996b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
57006b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
57016b4cac81SBjoern A. Zeeb 
57029fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
570386bc7259SBjoern A. Zeeb 	/*
570486bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
570586bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
570686bc7259SBjoern A. Zeeb 	 */
570786bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
57089fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
57099fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
57109fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
57119fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
57129fb91463SBjoern A. Zeeb 
57139fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
57149fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
57159fb91463SBjoern A. Zeeb 
57169fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
57179fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
57189fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
57199fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
57209fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
57219fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
57229fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
57239fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
57249fb91463SBjoern A. Zeeb 
57259fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
57269fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
57279fb91463SBjoern A. Zeeb 
57289fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
57299fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
57309fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
57319fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
573286bc7259SBjoern A. Zeeb 	}
57339fb91463SBjoern A. Zeeb #endif
57349fb91463SBjoern A. Zeeb 
57356b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
57366b4cac81SBjoern A. Zeeb 
5737c5b96b3eSBjoern A. Zeeb 	/*
5738c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5739c5b96b3eSBjoern A. Zeeb 	 * expect one.
5740d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5741d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5742c5b96b3eSBjoern A. Zeeb 	 */
5743d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5744f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5745c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5746c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5747c5b96b3eSBjoern A. Zeeb 
5748c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5749c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5750c5b96b3eSBjoern A. Zeeb 			continue;
5751c5b96b3eSBjoern A. Zeeb 
5752d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5753d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5754d9945d78SBjoern A. Zeeb 
5755f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5756f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5757f454a4a1SBjoern A. Zeeb 			continue;
5758f454a4a1SBjoern A. Zeeb 
5759c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5760c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5761c5b96b3eSBjoern A. Zeeb 
5762c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5763c5b96b3eSBjoern A. Zeeb 				continue;
5764c5b96b3eSBjoern A. Zeeb 
57654a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
57669fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
576711450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
57689fb91463SBjoern A. Zeeb #endif
5769c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5770c5b96b3eSBjoern A. Zeeb 			break;
5771c5b96b3eSBjoern A. Zeeb 		}
5772c5b96b3eSBjoern A. Zeeb 	}
5773c5b96b3eSBjoern A. Zeeb 
5774d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5775d9945d78SBjoern A. Zeeb 
5776d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5777d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5778d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5779d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5780d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5781d9945d78SBjoern A. Zeeb 	}
5782d9945d78SBjoern A. Zeeb 
5783d9945d78SBjoern A. Zeeb 	/*
5784d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5785d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5786d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5787d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5788d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5789d9945d78SBjoern A. Zeeb 	 */
5790d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5791d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5792d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
579378ca45dfSBjoern A. Zeeb 
579478ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5795d9945d78SBjoern A. Zeeb 		/*
579678ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
579778ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
579878ca45dfSBjoern A. Zeeb 		 * space in.
5799d9945d78SBjoern A. Zeeb 		 */
5800d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
580178ca45dfSBjoern A. Zeeb 	}
5802d9945d78SBjoern A. Zeeb 
58039fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
58049fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
58059fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
58069fb91463SBjoern A. Zeeb #endif
58079fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
58081f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
58099fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
58109fb91463SBjoern A. Zeeb #endif
58119fb91463SBjoern A. Zeeb 
5812d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
581378ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
581478ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
581578ca45dfSBjoern A. Zeeb 			goto err;
5816d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
581778ca45dfSBjoern A. Zeeb 	}
5818d9945d78SBjoern A. Zeeb 
58196b4cac81SBjoern A. Zeeb 	if (bootverbose)
58206b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
58212e183d99SBjoern A. Zeeb 
58222e183d99SBjoern A. Zeeb 	return (0);
582378ca45dfSBjoern A. Zeeb err:
582478ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
582578ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
58266b4cac81SBjoern A. Zeeb }
58276b4cac81SBjoern A. Zeeb 
58286b4cac81SBjoern A. Zeeb void
58296b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
58306b4cac81SBjoern A. Zeeb {
58316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58326b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58336b4cac81SBjoern A. Zeeb 
58346b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58356b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58366b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
58376b4cac81SBjoern A. Zeeb }
58386b4cac81SBjoern A. Zeeb 
58396b4cac81SBjoern A. Zeeb void
58406b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
58416b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
58426b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
58436b4cac81SBjoern A. Zeeb     void *arg)
58446b4cac81SBjoern A. Zeeb {
58456b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58466b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58476b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
584861a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
58496b4cac81SBjoern A. Zeeb 
58506b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58516b4cac81SBjoern A. Zeeb 
58526b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
58536b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
585461a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5855adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
58566b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
58576b4cac81SBjoern A. Zeeb 		    __func__, flags);
58586b4cac81SBjoern A. Zeeb 	}
58596b4cac81SBjoern A. Zeeb 
5860adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
58616b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
586261a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
58636b4cac81SBjoern A. Zeeb 
58646b4cac81SBjoern A. Zeeb 	if (atomic)
58658891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
58666b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58676b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
58686b4cac81SBjoern A. Zeeb 
58696b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
58706b4cac81SBjoern A. Zeeb 
58716b4cac81SBjoern A. Zeeb 		/*
58726b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
58736b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
58746b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
58756b4cac81SBjoern A. Zeeb 		 * driver does not know about.
58766b4cac81SBjoern A. Zeeb 		 */
58776b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
58786b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
58796b4cac81SBjoern A. Zeeb 			continue;
58806b4cac81SBjoern A. Zeeb 
58816b4cac81SBjoern A. Zeeb 		/*
588261a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
588361a68e50SBjoern A. Zeeb 		 * if we haven't yet.
588461a68e50SBjoern A. Zeeb 		 */
588561a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
588661a68e50SBjoern A. Zeeb 			continue;
588761a68e50SBjoern A. Zeeb 
588861a68e50SBjoern A. Zeeb 		/*
58896b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
58906b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
58916b4cac81SBjoern A. Zeeb 		 */
58926b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
58936b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
58946b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
58956b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
58966b4cac81SBjoern A. Zeeb 	}
58976b4cac81SBjoern A. Zeeb 	if (atomic)
58988891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58996b4cac81SBjoern A. Zeeb }
59006b4cac81SBjoern A. Zeeb 
590111db70b6SBjoern A. Zeeb static void
590211db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
590311db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
590411db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
590511db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
590611db70b6SBjoern A. Zeeb     void *arg)
590711db70b6SBjoern A. Zeeb {
590811db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
590911db70b6SBjoern A. Zeeb 		return;
591011db70b6SBjoern A. Zeeb 
591111db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
591211db70b6SBjoern A. Zeeb 		return;
591311db70b6SBjoern A. Zeeb 
591411db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
591511db70b6SBjoern A. Zeeb }
591611db70b6SBjoern A. Zeeb 
59176b4cac81SBjoern A. Zeeb void
59186b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
59196b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
59206b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
59216b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
592211db70b6SBjoern A. Zeeb     void *arg, bool rcu)
59236b4cac81SBjoern A. Zeeb {
592411db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
592511db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59266b4cac81SBjoern A. Zeeb 
592711db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
592811db70b6SBjoern A. Zeeb 
592911db70b6SBjoern A. Zeeb 	if (rcu) {
5930a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5931a8f735a6SBjoern A. Zeeb 
593211db70b6SBjoern A. Zeeb 		if (vif == NULL) {
593311db70b6SBjoern A. Zeeb 			TODO();
593411db70b6SBjoern A. Zeeb 		} else {
5935a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
593611db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
593711db70b6SBjoern A. Zeeb 				    keyix++)
593811db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
593911db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
594011db70b6SBjoern A. Zeeb 			}
594111db70b6SBjoern A. Zeeb 		}
594211db70b6SBjoern A. Zeeb 	} else {
594311db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
594411db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
594511db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
594611db70b6SBjoern A. Zeeb 
594711db70b6SBjoern A. Zeeb 		if (vif == NULL) {
594811db70b6SBjoern A. Zeeb 			TODO();
594911db70b6SBjoern A. Zeeb 		} else {
5950a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
595111db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
595211db70b6SBjoern A. Zeeb 				    keyix++)
595311db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
595411db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
595511db70b6SBjoern A. Zeeb 			}
595611db70b6SBjoern A. Zeeb 		}
595711db70b6SBjoern A. Zeeb 	}
59586b4cac81SBjoern A. Zeeb }
59596b4cac81SBjoern A. Zeeb 
59606b4cac81SBjoern A. Zeeb void
59616b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
59626b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
59636b4cac81SBjoern A. Zeeb 	void *),
59646b4cac81SBjoern A. Zeeb     void *arg)
59656b4cac81SBjoern A. Zeeb {
5966c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5967c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5968c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5969c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
59706b4cac81SBjoern A. Zeeb 
5971c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5972c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5973c5e25798SBjoern A. Zeeb 
5974c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5975c5e25798SBjoern A. Zeeb 
5976c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5977c5e25798SBjoern A. Zeeb 
5978c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5979c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5980c5e25798SBjoern A. Zeeb 
5981c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
598211604b2aSBjoern A. Zeeb 		if (vif->bss_conf.chanctx_conf == NULL)
5983c5e25798SBjoern A. Zeeb 			continue;
5984c5e25798SBjoern A. Zeeb 
598511604b2aSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->bss_conf.chanctx_conf);
5986c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5987c5e25798SBjoern A. Zeeb 			continue;
5988c5e25798SBjoern A. Zeeb 
5989d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5990c5e25798SBjoern A. Zeeb 	}
5991c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59926b4cac81SBjoern A. Zeeb }
59936b4cac81SBjoern A. Zeeb 
59946b4cac81SBjoern A. Zeeb void
59956b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
59966b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
59976b4cac81SBjoern A. Zeeb {
59986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
60006b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
60016b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
60026b4cac81SBjoern A. Zeeb 
60036b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
60046b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
60056b4cac81SBjoern A. Zeeb 
60066b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
60076b4cac81SBjoern A. Zeeb 
60088891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
60096b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
60106b4cac81SBjoern A. Zeeb 
6011a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6012a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
60136b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
60146b4cac81SBjoern A. Zeeb 				continue;
60156b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
60166b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
60176b4cac81SBjoern A. Zeeb 		}
6018a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
60196b4cac81SBjoern A. Zeeb 	}
60208891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
60216b4cac81SBjoern A. Zeeb }
60226b4cac81SBjoern A. Zeeb 
6023673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6024673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6025673d62fcSBjoern A. Zeeb {
6026673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6027673d62fcSBjoern A. Zeeb 
6028673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6029673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6030673d62fcSBjoern A. Zeeb 	return (regd);
6031673d62fcSBjoern A. Zeeb }
6032673d62fcSBjoern A. Zeeb 
60336b4cac81SBjoern A. Zeeb int
60346b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
60356b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
60366b4cac81SBjoern A. Zeeb {
60376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
60386b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
60396b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
60406b4cac81SBjoern A. Zeeb 
60416b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
60426b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
60436b4cac81SBjoern A. Zeeb 
60446b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
60456b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
60466b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
60476b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
60486b4cac81SBjoern A. Zeeb 	}
60496b4cac81SBjoern A. Zeeb 
60506b4cac81SBjoern A. Zeeb 	TODO();
60516b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
60526b4cac81SBjoern A. Zeeb 
60536b4cac81SBjoern A. Zeeb 	return (0);
60546b4cac81SBjoern A. Zeeb }
60556b4cac81SBjoern A. Zeeb 
60566b4cac81SBjoern A. Zeeb void
60576b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
60586b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
60596b4cac81SBjoern A. Zeeb {
60606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
60616b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
60626b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
60636b4cac81SBjoern A. Zeeb 
60646b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
60656b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
60666b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
60676b4cac81SBjoern A. Zeeb 
60686b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
60696b4cac81SBjoern A. Zeeb 
60708ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
60716b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
60726b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6073a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
60746b4cac81SBjoern A. Zeeb 	wakeup(lhw);
60758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
60766b4cac81SBjoern A. Zeeb 
60776b4cac81SBjoern A. Zeeb 	return;
60786b4cac81SBjoern A. Zeeb }
60796b4cac81SBjoern A. Zeeb 
6080759a996dSBjoern A. Zeeb static void
6081759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6082759a996dSBjoern A. Zeeb {
6083759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6084759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6085759a996dSBjoern A. Zeeb 	int ok;
6086759a996dSBjoern A. Zeeb 
6087759a996dSBjoern A. Zeeb 	ni = NULL;
6088759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6089759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6090759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6091759a996dSBjoern A. Zeeb 
6092759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6093759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6094759a996dSBjoern A. Zeeb 	}
6095759a996dSBjoern A. Zeeb 
6096759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6097759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6098759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6099759a996dSBjoern A. Zeeb 		if (ok < 0)
6100759a996dSBjoern A. Zeeb 			m_freem(m);
6101759a996dSBjoern A. Zeeb 	} else {
6102759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6103759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6104759a996dSBjoern A. Zeeb 	}
6105759a996dSBjoern A. Zeeb 
6106759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6107759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6108bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6109759a996dSBjoern A. Zeeb #endif
6110759a996dSBjoern A. Zeeb }
6111759a996dSBjoern A. Zeeb 
6112759a996dSBjoern A. Zeeb static void
6113759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6114759a996dSBjoern A. Zeeb {
6115759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6116759a996dSBjoern A. Zeeb 	struct mbufq mq;
6117759a996dSBjoern A. Zeeb 	struct mbuf *m;
6118759a996dSBjoern A. Zeeb 
6119759a996dSBjoern A. Zeeb 	lhw = ctx;
6120759a996dSBjoern A. Zeeb 
6121759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6122759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6123bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6124bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6125759a996dSBjoern A. Zeeb #endif
6126759a996dSBjoern A. Zeeb 
6127759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6128759a996dSBjoern A. Zeeb 
6129759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6130759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6131759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6132759a996dSBjoern A. Zeeb 
6133759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6134759a996dSBjoern A. Zeeb 	while (m != NULL) {
6135759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6136759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6137759a996dSBjoern A. Zeeb 	}
6138759a996dSBjoern A. Zeeb }
6139759a996dSBjoern A. Zeeb 
614049010ba7SBjoern A. Zeeb static void
614149010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
614249010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
614349010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
614449010ba7SBjoern A. Zeeb     uint8_t *rssip)
614549010ba7SBjoern A. Zeeb {
614649010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
614749010ba7SBjoern A. Zeeb 	int i;
614849010ba7SBjoern A. Zeeb 	uint8_t rssi;
614949010ba7SBjoern A. Zeeb 
615049010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
615149010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
615249010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
615349010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
615449010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
615549010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
615649010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
615749010ba7SBjoern A. Zeeb 	else
615849010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
615949010ba7SBjoern A. Zeeb 	/*
616049010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
616149010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
616249010ba7SBjoern A. Zeeb 	 */
616349010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
616449010ba7SBjoern A. Zeeb 	if (rssip != NULL)
616549010ba7SBjoern A. Zeeb 		*rssip = rssi;
616649010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
616749010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
616849010ba7SBjoern A. Zeeb 	rx_stats->c_band =
616949010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
617049010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
617149010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
617249010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
617349010ba7SBjoern A. Zeeb 
617449010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
617549010ba7SBjoern A. Zeeb 
617649010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
617749010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
617849010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
617949010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
618049010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
618149010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
618249010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
618349010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
618449010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
618549010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
618649010ba7SBjoern A. Zeeb 	}
618749010ba7SBjoern A. Zeeb 
618849010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
618949010ba7SBjoern A. Zeeb 		int cc;
619049010ba7SBjoern A. Zeeb 		int8_t crssi;
619149010ba7SBjoern A. Zeeb 
619249010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
619349010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
619449010ba7SBjoern A. Zeeb 
619549010ba7SBjoern A. Zeeb 		cc = 0;
619649010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
619749010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
619849010ba7SBjoern A. Zeeb 				continue;
619949010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
620049010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
620149010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
620249010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
620349010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
620449010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
620549010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
620649010ba7SBjoern A. Zeeb 			cc++;
620749010ba7SBjoern A. Zeeb 		}
620849010ba7SBjoern A. Zeeb 		if (cc > 0)
620949010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
621049010ba7SBjoern A. Zeeb 	}
621149010ba7SBjoern A. Zeeb 
621249010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
621349010ba7SBjoern A. Zeeb 
621449010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
621549010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
621649010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
621749010ba7SBjoern A. Zeeb 		if (supband != NULL)
621849010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
621949010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
622049010ba7SBjoern A. Zeeb 		break;
622149010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
622249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
622349010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
622449010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
622549010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
622649010ba7SBjoern A. Zeeb 		break;
622749010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
622849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
622949010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
623049010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
623149010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
623249010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
623349010ba7SBjoern A. Zeeb 		break;
623449010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
623549010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
623649010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
623749010ba7SBjoern A. Zeeb 		break;
623849010ba7SBjoern A. Zeeb 	}
623949010ba7SBjoern A. Zeeb 
624049010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
624149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
624249010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
624349010ba7SBjoern A. Zeeb 		break;
624449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
624549010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
624649010ba7SBjoern A. Zeeb 		break;
624749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
624849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
624949010ba7SBjoern A. Zeeb 		break;
625049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
625149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
625249010ba7SBjoern A. Zeeb 		break;
625349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
625449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
625549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
625649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
625749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
625849010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
625949010ba7SBjoern A. Zeeb 		break;
626049010ba7SBjoern A. Zeeb 	}
626149010ba7SBjoern A. Zeeb 
626249010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
626349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
626449010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
626549010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
626649010ba7SBjoern A. Zeeb 
626749010ba7SBjoern A. Zeeb 	/*
626849010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
626949010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
627049010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
627149010ba7SBjoern A. Zeeb 	 */
627249010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
627349010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
627449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
627549010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
627649010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
627749010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
627849010ba7SBjoern A. Zeeb 	}
627949010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
628049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6281394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6282394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
6283394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6284394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
628549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
628649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
628749010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
628849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
628949010ba7SBjoern A. Zeeb #endif
629049010ba7SBjoern A. Zeeb }
629149010ba7SBjoern A. Zeeb 
6292e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
62936b4cac81SBjoern A. Zeeb void
62946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6295e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6296e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
62976b4cac81SBjoern A. Zeeb {
62986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62996b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
63006b4cac81SBjoern A. Zeeb 	struct mbuf *m;
63016b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
63026b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
63036b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
63046b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
63056b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
63066b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
63076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
63089d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
630949010ba7SBjoern A. Zeeb 	uint8_t rssi;
6310c0cadd99SBjoern A. Zeeb 	bool is_beacon;
63116b4cac81SBjoern A. Zeeb 
63126b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
63136b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
63146b4cac81SBjoern A. Zeeb 		IMPROVE();
63156b4cac81SBjoern A. Zeeb 		goto err;
63166b4cac81SBjoern A. Zeeb 	}
63176b4cac81SBjoern A. Zeeb 
63186b4cac81SBjoern A. Zeeb 	/*
63196b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
63206b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
63216b4cac81SBjoern A. Zeeb 	 */
63226b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
63236b4cac81SBjoern A. Zeeb 	if (m == NULL)
63246b4cac81SBjoern A. Zeeb 		goto err;
63256b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
63266b4cac81SBjoern A. Zeeb 
63276b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
63286b4cac81SBjoern A. Zeeb 	offset = m->m_len;
63296b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
63306b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
63316b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
63326b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
63336b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
63346b4cac81SBjoern A. Zeeb 	}
63356b4cac81SBjoern A. Zeeb 
63366b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
63376b4cac81SBjoern A. Zeeb 
63386b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6339c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6340c0cadd99SBjoern A. Zeeb 
6341c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63429d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
63436b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
63446b4cac81SBjoern A. Zeeb 
63459d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
63466b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6347c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
63486b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
63496b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
63506b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6351c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
63526b4cac81SBjoern A. Zeeb 
63539d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
63546b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
63556b4cac81SBjoern A. Zeeb 
63566b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
63579d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6358f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
635960970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
63606b4cac81SBjoern A. Zeeb 			__func__,
6361c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6362c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
63636b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6364f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
63656b4cac81SBjoern A. Zeeb 			rx_status->freq,
63666b4cac81SBjoern A. Zeeb 			rx_status->bw,
63676b4cac81SBjoern A. Zeeb 			rx_status->encoding,
63686b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
63696b4cac81SBjoern A. Zeeb 			rx_status->band,
63706b4cac81SBjoern A. Zeeb 			rx_status->chains,
63716b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
63726b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
63736b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
637460970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
63756b4cac81SBjoern A. Zeeb 			rx_status->signal,
63766b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
63776b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
63786b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
63796b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
63806b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
63816b4cac81SBjoern A. Zeeb 			rx_status->nss,
63826b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
63836b4cac81SBjoern A. Zeeb no_trace_beacons:
63846b4cac81SBjoern A. Zeeb #endif
63856b4cac81SBjoern A. Zeeb 
638649010ba7SBjoern A. Zeeb 	rssi = 0;
638749010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
63886b4cac81SBjoern A. Zeeb 
63896b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63906b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63916b4cac81SBjoern A. Zeeb 
63926b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
63936b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6394dbc06dd9SBjoern A. Zeeb 		m_freem(m);
63956b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
63966b4cac81SBjoern A. Zeeb 		goto err;
63976b4cac81SBjoern A. Zeeb 	}
63986b4cac81SBjoern A. Zeeb 
639958246901SBjoern A. Zeeb 	lsta = NULL;
64006b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
64016b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
64026b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
64036b4cac81SBjoern A. Zeeb 	} else {
6404c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6405c8dafefaSBjoern A. Zeeb 
64066b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
64076b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
64086b4cac81SBjoern A. Zeeb 		if (ni != NULL)
64096b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
64106b4cac81SBjoern A. Zeeb 	}
64116b4cac81SBjoern A. Zeeb 
64126b4cac81SBjoern A. Zeeb 	if (ni != NULL)
64136b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
64146b4cac81SBjoern A. Zeeb 	else
64156b4cac81SBjoern A. Zeeb 		/*
64166b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
64176b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
64186b4cac81SBjoern A. Zeeb 		 */
64196b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
64206b4cac81SBjoern A. Zeeb 
64219d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
64229d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6423bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6424c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6425c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
64269d9ba2b7SBjoern A. Zeeb #endif
64276b4cac81SBjoern A. Zeeb 
6428c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6429c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6430c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6431c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6432c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6433c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6434c8dafefaSBjoern A. Zeeb 
6435c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6436c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6437c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6438c8dafefaSBjoern A. Zeeb 
6439c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6440c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6441c8dafefaSBjoern A. Zeeb 
6442c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6443c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6444c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6445c8dafefaSBjoern A. Zeeb 		/*
6446c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6447c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6448c8dafefaSBjoern A. Zeeb 		 */
6449c8dafefaSBjoern A. Zeeb skip_device_ts:
64509fb91463SBjoern A. Zeeb 		;
64519fb91463SBjoern A. Zeeb 	}
6452c8dafefaSBjoern A. Zeeb 
64536b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
64546b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
64556b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
64566b4cac81SBjoern A. Zeeb 
64576b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
64586b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
64596b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
64606b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
64616b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
64626b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
64636b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
64646b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
64656b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64666b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
64676b4cac81SBjoern A. Zeeb #endif
64686b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
64696b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
64706b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
64716b4cac81SBjoern A. Zeeb 		IMPROVE();
64726b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
64736b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
64746b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
64756b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6476170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
64776b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
64786b4cac81SBjoern A. Zeeb 	}
64796b4cac81SBjoern A. Zeeb 
64806b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64816b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
64826b4cac81SBjoern A. Zeeb 
6483e30e05d3SBjoern A. Zeeb #if 0
6484e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6485e30e05d3SBjoern A. Zeeb 		/*
6486e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6487e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6488e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6489e30e05d3SBjoern A. Zeeb 		*/
6490e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6491e30e05d3SBjoern A. Zeeb 	}
6492e30e05d3SBjoern A. Zeeb #endif
6493e30e05d3SBjoern A. Zeeb 
6494759a996dSBjoern A. Zeeb 	/*
6495759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6496759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6497759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6498759a996dSBjoern A. Zeeb 	 */
64996b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6500759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6501759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6502759a996dSBjoern A. Zeeb 
6503759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6504759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6505759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6506759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6507759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6508759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6509759a996dSBjoern A. Zeeb 		}
65106b4cac81SBjoern A. Zeeb 	}
65116b4cac81SBjoern A. Zeeb 
6512759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6513759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6514759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6515759a996dSBjoern A. Zeeb 		m_freem(m);
6516759a996dSBjoern A. Zeeb 		goto err;
6517759a996dSBjoern A. Zeeb 	}
6518759a996dSBjoern A. Zeeb 
6519759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6520759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6521759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
65226b4cac81SBjoern A. Zeeb 
65236b4cac81SBjoern A. Zeeb 	IMPROVE();
65246b4cac81SBjoern A. Zeeb 
65256b4cac81SBjoern A. Zeeb err:
65266b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
65276b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
65286b4cac81SBjoern A. Zeeb }
65296b4cac81SBjoern A. Zeeb 
65306b4cac81SBjoern A. Zeeb uint8_t
6531ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
65326b4cac81SBjoern A. Zeeb {
65336b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6534ec190d91SBjoern A. Zeeb 	uint8_t tid;
6535ec190d91SBjoern A. Zeeb 
6536ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6537ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6538ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6539ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
65406b4cac81SBjoern A. Zeeb 
65416b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6542ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6543ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6544ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6545ec190d91SBjoern A. Zeeb 
6546ec190d91SBjoern A. Zeeb 	return (tid);
65476b4cac81SBjoern A. Zeeb }
65486b4cac81SBjoern A. Zeeb 
6549ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6550ac1d519cSBjoern A. Zeeb 
6551ac1d519cSBjoern A. Zeeb static void
6552ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6553ac1d519cSBjoern A. Zeeb {
6554ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6555ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6556ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6557ac1d519cSBjoern A. Zeeb 
6558ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6559ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6560ac1d519cSBjoern A. Zeeb 
6561ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6562ac1d519cSBjoern A. Zeeb 
6563ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6564ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6565ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6566ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6567ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6568ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6569ac1d519cSBjoern A. Zeeb 		return;
6570ac1d519cSBjoern A. Zeeb 	}
6571ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6572ac1d519cSBjoern A. Zeeb 
6573ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6574ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6575ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6576ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6577ac1d519cSBjoern A. Zeeb 
6578ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6579ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6580ac1d519cSBjoern A. Zeeb 
6581ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6582ac1d519cSBjoern A. Zeeb }
6583ac1d519cSBjoern A. Zeeb 
6584ac1d519cSBjoern A. Zeeb void
6585ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6586ac1d519cSBjoern A. Zeeb {
6587ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6588ac1d519cSBjoern A. Zeeb 
6589ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6590ac1d519cSBjoern A. Zeeb 
6591ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6592ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6593ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6594ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6595ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6596ac1d519cSBjoern A. Zeeb 
6597ac1d519cSBjoern A. Zeeb 	/*
6598ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6599ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6600ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6601ac1d519cSBjoern A. Zeeb 	 */
6602ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6603ac1d519cSBjoern A. Zeeb }
6604ac1d519cSBjoern A. Zeeb 
6605ac1d519cSBjoern A. Zeeb void
6606ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6607ac1d519cSBjoern A. Zeeb {
6608ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6609ac1d519cSBjoern A. Zeeb 
6610ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6611ac1d519cSBjoern A. Zeeb 
6612ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6613ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6614ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6615ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6616ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6617ac1d519cSBjoern A. Zeeb }
6618ac1d519cSBjoern A. Zeeb 
6619ac1d519cSBjoern A. Zeeb void
6620ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6621ac1d519cSBjoern A. Zeeb {
6622ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6623ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6624ac1d519cSBjoern A. Zeeb 
6625ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6626ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6627ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6628ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6629ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6630ac1d519cSBjoern A. Zeeb 		return;
6631ac1d519cSBjoern A. Zeeb 	}
6632ac1d519cSBjoern A. Zeeb 
6633ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6634ac1d519cSBjoern A. Zeeb 
6635ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6636ac1d519cSBjoern A. Zeeb 		    entry);
6637ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6638ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6639ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6640ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6641ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6642ac1d519cSBjoern A. Zeeb 			break;
6643ac1d519cSBjoern A. Zeeb 	}
6644ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6645ac1d519cSBjoern A. Zeeb }
6646ac1d519cSBjoern A. Zeeb 
6647ac1d519cSBjoern A. Zeeb void
6648ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6649ac1d519cSBjoern A. Zeeb {
6650ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6651ac1d519cSBjoern A. Zeeb 
6652ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6653ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6654ac1d519cSBjoern A. Zeeb }
6655ac1d519cSBjoern A. Zeeb 
6656ac1d519cSBjoern A. Zeeb void
6657ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6658ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6659ac1d519cSBjoern A. Zeeb {
6660ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6661ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6662ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6663ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6664ac1d519cSBjoern A. Zeeb 	} else {
6665ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6666ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6667ac1d519cSBjoern A. Zeeb 	}
6668ac1d519cSBjoern A. Zeeb }
6669ac1d519cSBjoern A. Zeeb 
6670ac1d519cSBjoern A. Zeeb void
6671ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6672ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6673ac1d519cSBjoern A. Zeeb {
6674ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6675ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6676ac1d519cSBjoern A. Zeeb }
6677ac1d519cSBjoern A. Zeeb 
6678ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6679ac1d519cSBjoern A. Zeeb 
66806b4cac81SBjoern A. Zeeb struct wiphy *
66816b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
66826b4cac81SBjoern A. Zeeb {
66836b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6684ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
66856b4cac81SBjoern A. Zeeb 
66866b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
66876b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
66886b4cac81SBjoern A. Zeeb 		return (NULL);
66896b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
66906b4cac81SBjoern A. Zeeb 
6691ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6692ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6693ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6694ac1d519cSBjoern A. Zeeb 
6695ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6696ac1d519cSBjoern A. Zeeb 
6697ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6698ac1d519cSBjoern A. Zeeb 	TODO();
6699ac1d519cSBjoern A. Zeeb 
6700ac1d519cSBjoern A. Zeeb 	return (wiphy);
67016b4cac81SBjoern A. Zeeb }
67026b4cac81SBjoern A. Zeeb 
67036b4cac81SBjoern A. Zeeb void
67046b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
67056b4cac81SBjoern A. Zeeb {
67066b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
67076b4cac81SBjoern A. Zeeb 
67086b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
67096b4cac81SBjoern A. Zeeb 		return;
67106b4cac81SBjoern A. Zeeb 
6711ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6712ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6713ac1d519cSBjoern A. Zeeb 
67146b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6715ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6716ac1d519cSBjoern A. Zeeb 
67176b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
67186b4cac81SBjoern A. Zeeb }
67196b4cac81SBjoern A. Zeeb 
6720a7c19b8aSBjoern A. Zeeb static uint32_t
6721a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6722a7c19b8aSBjoern A. Zeeb {
6723a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6724a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6725a7c19b8aSBjoern A. Zeeb }
6726a7c19b8aSBjoern A. Zeeb 
6727a7c19b8aSBjoern A. Zeeb static uint32_t
6728a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6729a7c19b8aSBjoern A. Zeeb {
6730a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6731a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6732a7c19b8aSBjoern A. Zeeb }
6733a7c19b8aSBjoern A. Zeeb 
6734a7c19b8aSBjoern A. Zeeb uint32_t
6735a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6736a7c19b8aSBjoern A. Zeeb {
6737a7c19b8aSBjoern A. Zeeb 
6738a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6739a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6740a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6741a7c19b8aSBjoern A. Zeeb 
6742a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6743a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6744a7c19b8aSBjoern A. Zeeb 
6745a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6746a7c19b8aSBjoern A. Zeeb 
6747a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6748a7c19b8aSBjoern A. Zeeb }
6749a7c19b8aSBjoern A. Zeeb 
67506b4cac81SBjoern A. Zeeb uint32_t
67516b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
67526b4cac81SBjoern A. Zeeb     enum nl80211_band band)
67536b4cac81SBjoern A. Zeeb {
67546b4cac81SBjoern A. Zeeb 
67556b4cac81SBjoern A. Zeeb 	switch (band) {
67566b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
67576b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
67586b4cac81SBjoern A. Zeeb 		break;
67596b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
67606b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
67616b4cac81SBjoern A. Zeeb 		break;
67626b4cac81SBjoern A. Zeeb 	default:
67636b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
67646b4cac81SBjoern A. Zeeb 		break;
67656b4cac81SBjoern A. Zeeb 	}
67666b4cac81SBjoern A. Zeeb 
67676b4cac81SBjoern A. Zeeb 	return (0);
67686b4cac81SBjoern A. Zeeb }
67696b4cac81SBjoern A. Zeeb 
67706b4cac81SBjoern A. Zeeb uint32_t
67716b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
67726b4cac81SBjoern A. Zeeb {
67736b4cac81SBjoern A. Zeeb 
67746b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
67756b4cac81SBjoern A. Zeeb }
67766b4cac81SBjoern A. Zeeb 
6777ac867c20SBjoern A. Zeeb #if 0
67786b4cac81SBjoern A. Zeeb static struct lkpi_sta *
67796b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
67806b4cac81SBjoern A. Zeeb {
67816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
67826b4cac81SBjoern A. Zeeb 
6783a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6784a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67856b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6786a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67876b4cac81SBjoern A. Zeeb 			return (lsta);
67886b4cac81SBjoern A. Zeeb 		}
67896b4cac81SBjoern A. Zeeb 	}
6790a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67916b4cac81SBjoern A. Zeeb 
67926b4cac81SBjoern A. Zeeb 	return (NULL);
67936b4cac81SBjoern A. Zeeb }
6794ac867c20SBjoern A. Zeeb #endif
67956b4cac81SBjoern A. Zeeb 
67966b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67976b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
67986b4cac81SBjoern A. Zeeb {
67996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6800a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
68016b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
68026b4cac81SBjoern A. Zeeb 
68036b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
68046b4cac81SBjoern A. Zeeb 
6805a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6806a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
68076b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
68086b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6809a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
68106b4cac81SBjoern A. Zeeb 			return (sta);
68116b4cac81SBjoern A. Zeeb 		}
68126b4cac81SBjoern A. Zeeb 	}
6813a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
68146b4cac81SBjoern A. Zeeb 	return (NULL);
68156b4cac81SBjoern A. Zeeb }
68166b4cac81SBjoern A. Zeeb 
68176b4cac81SBjoern A. Zeeb struct ieee80211_sta *
68182e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
68192e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
68206b4cac81SBjoern A. Zeeb {
68216b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
68226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
68236b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
68246b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
68256b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
68266b4cac81SBjoern A. Zeeb 
68276b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
68286b4cac81SBjoern A. Zeeb 	sta = NULL;
68296b4cac81SBjoern A. Zeeb 
68308891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
68316b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
68326b4cac81SBjoern A. Zeeb 
68336b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
68346b4cac81SBjoern A. Zeeb 
68356b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
68366b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
68376b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
68386b4cac81SBjoern A. Zeeb 			continue;
68396b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
68406b4cac81SBjoern A. Zeeb 		if (sta != NULL)
68416b4cac81SBjoern A. Zeeb 			break;
68426b4cac81SBjoern A. Zeeb 	}
68438891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
68446b4cac81SBjoern A. Zeeb 
68456b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
68466b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
68476b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
68486b4cac81SBjoern A. Zeeb 			return (NULL);
68496b4cac81SBjoern A. Zeeb 	}
68506b4cac81SBjoern A. Zeeb 
68516b4cac81SBjoern A. Zeeb 	return (sta);
68526b4cac81SBjoern A. Zeeb }
68536b4cac81SBjoern A. Zeeb 
68546b4cac81SBjoern A. Zeeb struct sk_buff *
68556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
68566b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
68576b4cac81SBjoern A. Zeeb {
68586b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68590cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
68606b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
68616b4cac81SBjoern A. Zeeb 
68620cbcfa19SBjoern A. Zeeb 	skb = NULL;
68636b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68646b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
68656b4cac81SBjoern A. Zeeb 
68660cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
68670cbcfa19SBjoern A. Zeeb 		goto stopped;
68680cbcfa19SBjoern A. Zeeb 
68690cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
68700cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
68710cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
68720cbcfa19SBjoern A. Zeeb 		goto stopped;
68730cbcfa19SBjoern A. Zeeb 	}
68740cbcfa19SBjoern A. Zeeb 
6875eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6876eac3646fSBjoern A. Zeeb 
6877eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68786b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6879eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68806b4cac81SBjoern A. Zeeb 
68810cbcfa19SBjoern A. Zeeb stopped:
68826b4cac81SBjoern A. Zeeb 	return (skb);
68836b4cac81SBjoern A. Zeeb }
68846b4cac81SBjoern A. Zeeb 
68856b4cac81SBjoern A. Zeeb void
68866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
688786220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
68886b4cac81SBjoern A. Zeeb {
68896b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68906b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
689186220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
68926b4cac81SBjoern A. Zeeb 
68936b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68946b4cac81SBjoern A. Zeeb 
68956b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6896eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68976b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
68986b4cac81SBjoern A. Zeeb 		fc++;
68996b4cac81SBjoern A. Zeeb 		bc += skb->len;
69006b4cac81SBjoern A. Zeeb 	}
6901eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
69026b4cac81SBjoern A. Zeeb 	if (frame_cnt)
69036b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
69046b4cac81SBjoern A. Zeeb 	if (byte_cnt)
69056b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
69066b4cac81SBjoern A. Zeeb 
69076b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
69086b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
69096b4cac81SBjoern A. Zeeb 	IMPROVE();
69106b4cac81SBjoern A. Zeeb }
69116b4cac81SBjoern A. Zeeb 
69126b4cac81SBjoern A. Zeeb /*
69136b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
69146b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
69156b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
69166b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
69176b4cac81SBjoern A. Zeeb  */
6918a8397571SBjoern A. Zeeb static void
6919a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
69206b4cac81SBjoern A. Zeeb     int status)
69216b4cac81SBjoern A. Zeeb {
69226b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
69236b4cac81SBjoern A. Zeeb 	struct mbuf *m;
69246b4cac81SBjoern A. Zeeb 
69256b4cac81SBjoern A. Zeeb 	m = skb->m;
69266b4cac81SBjoern A. Zeeb 	skb->m = NULL;
69276b4cac81SBjoern A. Zeeb 
69286b4cac81SBjoern A. Zeeb 	if (m != NULL) {
69296b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
69306b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
69316b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
69326b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
69336b4cac81SBjoern A. Zeeb 	}
6934a8397571SBjoern A. Zeeb }
69356b4cac81SBjoern A. Zeeb 
6936a8397571SBjoern A. Zeeb void
6937a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6938a8397571SBjoern A. Zeeb     int status)
6939a8397571SBjoern A. Zeeb {
6940a8397571SBjoern A. Zeeb 
6941a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
69426b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
69436b4cac81SBjoern A. Zeeb }
69446b4cac81SBjoern A. Zeeb 
6945383b3e8fSBjoern A. Zeeb void
6946a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6947a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6948383b3e8fSBjoern A. Zeeb {
6949a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6950383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6951383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6952383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6953383b3e8fSBjoern A. Zeeb 	int status;
6954383b3e8fSBjoern A. Zeeb 
6955a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6956383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6957383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6958383b3e8fSBjoern A. Zeeb 
6959383b3e8fSBjoern A. Zeeb 		m = skb->m;
6960383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6961383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6962383b3e8fSBjoern A. Zeeb 	} else {
6963383b3e8fSBjoern A. Zeeb 		ni = NULL;
6964383b3e8fSBjoern A. Zeeb 	}
6965383b3e8fSBjoern A. Zeeb 
6966a8397571SBjoern A. Zeeb 	info = txstat->info;
6967383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6968383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6969383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6970383b3e8fSBjoern A. Zeeb 	} else {
6971383b3e8fSBjoern A. Zeeb 		status = 1;
6972383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6973383b3e8fSBjoern A. Zeeb 	}
6974383b3e8fSBjoern A. Zeeb 
6975383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6976383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6977383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6978383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6979383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6980383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6981383b3e8fSBjoern A. Zeeb 		}
6982383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6983a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6984383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6985383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6986383b3e8fSBjoern A. Zeeb #endif
6987adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6988383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6989383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6990383b3e8fSBjoern A. Zeeb 		}
6991383b3e8fSBjoern A. Zeeb 
6992d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
6993383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
699446de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6995383b3e8fSBjoern A. Zeeb 
6996383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6997383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6998d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
699946de4d9fSAdrian Chadd 			    txs.long_retries);
7000383b3e8fSBjoern A. Zeeb 		}
7001383b3e8fSBjoern A. Zeeb #endif
7002383b3e8fSBjoern A. Zeeb 	}
7003383b3e8fSBjoern A. Zeeb 
7004383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7005383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7006383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7007383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7008383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7009383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7010adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7011383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7012383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7013383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7014383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7015383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7016383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7017383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7018383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7019383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7020383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7021383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7022383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7023383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7024adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7025383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7026383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7027383b3e8fSBjoern A. Zeeb #endif
7028383b3e8fSBjoern A. Zeeb 
7029a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7030a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7031a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7032a8397571SBjoern A. Zeeb 	} else {
7033383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7034383b3e8fSBjoern A. Zeeb 	}
7035a8397571SBjoern A. Zeeb }
7036a8397571SBjoern A. Zeeb 
7037a8397571SBjoern A. Zeeb void
7038a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7039a8397571SBjoern A. Zeeb {
7040a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7041a8397571SBjoern A. Zeeb 
7042a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7043a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7044a8397571SBjoern A. Zeeb 	status.skb = skb;
7045a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7046a8397571SBjoern A. Zeeb 
7047a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7048a8397571SBjoern A. Zeeb }
7049383b3e8fSBjoern A. Zeeb 
70506b4cac81SBjoern A. Zeeb /*
70516b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
70526b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
70536b4cac81SBjoern A. Zeeb  * mbufs this should go away.
70546b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
70556b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
70566b4cac81SBjoern A. Zeeb  */
70576b4cac81SBjoern A. Zeeb static void
70586b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
70596b4cac81SBjoern A. Zeeb {
70606b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
70616b4cac81SBjoern A. Zeeb 	struct mbuf *m;
70626b4cac81SBjoern A. Zeeb 
70636b4cac81SBjoern A. Zeeb 	if (p == NULL)
70646b4cac81SBjoern A. Zeeb 		return;
70656b4cac81SBjoern A. Zeeb 
70666b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
70676b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
70686b4cac81SBjoern A. Zeeb 
70696b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
70706b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
70716b4cac81SBjoern A. Zeeb 	if (ni != NULL)
70726b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
70736b4cac81SBjoern A. Zeeb 	m_freem(m);
70746b4cac81SBjoern A. Zeeb }
70756b4cac81SBjoern A. Zeeb 
70766b4cac81SBjoern A. Zeeb void
70776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
70786b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
70796b4cac81SBjoern A. Zeeb {
70806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70816b4cac81SBjoern A. Zeeb 
70826b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70836b4cac81SBjoern A. Zeeb 	IMPROVE();
70846b4cac81SBjoern A. Zeeb 
70856b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70866b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
70876b4cac81SBjoern A. Zeeb }
70886b4cac81SBjoern A. Zeeb 
70896b4cac81SBjoern A. Zeeb void
70906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
70916b4cac81SBjoern A. Zeeb     struct work_struct *w)
70926b4cac81SBjoern A. Zeeb {
70936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70946b4cac81SBjoern A. Zeeb 
70956b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70966b4cac81SBjoern A. Zeeb 	IMPROVE();
70976b4cac81SBjoern A. Zeeb 
70986b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70996b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
71006b4cac81SBjoern A. Zeeb }
71016b4cac81SBjoern A. Zeeb 
71026b4cac81SBjoern A. Zeeb struct sk_buff *
7103ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7104ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7105ade774b1SBjoern A. Zeeb {
7106ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7107ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7108ade774b1SBjoern A. Zeeb 	uint8_t *p;
7109ade774b1SBjoern A. Zeeb 	size_t len;
7110ade774b1SBjoern A. Zeeb 
7111ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7112ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7113ade774b1SBjoern A. Zeeb 
7114ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7115ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7116ade774b1SBjoern A. Zeeb 		return (NULL);
7117ade774b1SBjoern A. Zeeb 
7118ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7119ade774b1SBjoern A. Zeeb 
7120ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7121ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7122ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7123ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7124ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7125ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7126ade774b1SBjoern A. Zeeb 
7127ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7128ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7129ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7130ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7131ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7132ade774b1SBjoern A. Zeeb 
7133ade774b1SBjoern A. Zeeb 	return (skb);
7134ade774b1SBjoern A. Zeeb }
7135ade774b1SBjoern A. Zeeb 
7136ade774b1SBjoern A. Zeeb struct sk_buff *
71376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
71386b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
71396b4cac81SBjoern A. Zeeb {
71406b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71416b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71426b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
71436b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
71446b4cac81SBjoern A. Zeeb 	uint16_t v;
71456b4cac81SBjoern A. Zeeb 
71466b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
71476b4cac81SBjoern A. Zeeb 	if (skb == NULL)
71486b4cac81SBjoern A. Zeeb 		return (NULL);
71496b4cac81SBjoern A. Zeeb 
71506b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71516b4cac81SBjoern A. Zeeb 
71526b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71536b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71546b4cac81SBjoern A. Zeeb 
71556b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
71566b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
71576b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7158616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
71596b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
71606b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
71616b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
71626b4cac81SBjoern A. Zeeb 
71636b4cac81SBjoern A. Zeeb 	return (skb);
71646b4cac81SBjoern A. Zeeb }
71656b4cac81SBjoern A. Zeeb 
71666b4cac81SBjoern A. Zeeb struct sk_buff *
71676b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7168adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
71696b4cac81SBjoern A. Zeeb {
71706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71716b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71726b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
71736b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
71746b4cac81SBjoern A. Zeeb 
7175adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7176adff403fSBjoern A. Zeeb 
71776b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
71786b4cac81SBjoern A. Zeeb 	if (skb == NULL)
71796b4cac81SBjoern A. Zeeb 		return (NULL);
71806b4cac81SBjoern A. Zeeb 
71816b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71826b4cac81SBjoern A. Zeeb 
71836b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71846b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71856b4cac81SBjoern A. Zeeb 
71866b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
71876b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
71886b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
71896b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
71906b4cac81SBjoern A. Zeeb 
71916b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
71926b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
71936b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
71946b4cac81SBjoern A. Zeeb 
71956b4cac81SBjoern A. Zeeb 	return (skb);
71966b4cac81SBjoern A. Zeeb }
71976b4cac81SBjoern A. Zeeb 
71986b4cac81SBjoern A. Zeeb struct wireless_dev *
71996b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
72006b4cac81SBjoern A. Zeeb {
72016b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72026b4cac81SBjoern A. Zeeb 
72036b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
72046b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
72056b4cac81SBjoern A. Zeeb }
72066b4cac81SBjoern A. Zeeb 
72076b4cac81SBjoern A. Zeeb void
72086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
72096b4cac81SBjoern A. Zeeb {
72106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72116b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
72126b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
72136b4cac81SBjoern A. Zeeb 	int arg;
72146b4cac81SBjoern A. Zeeb 
72156b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
72166b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
72176b4cac81SBjoern A. Zeeb 
72186b4cac81SBjoern A. Zeeb 	/*
7219f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
72206b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
72216b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
72226b4cac81SBjoern A. Zeeb 	 */
7223f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7224bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
72256b4cac81SBjoern A. Zeeb 
7226018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7227018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
72286b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
72296b4cac81SBjoern A. Zeeb }
72306b4cac81SBjoern A. Zeeb 
7231bb81db90SBjoern A. Zeeb void
7232bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7233bb81db90SBjoern A. Zeeb {
7234bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7235bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7236bb81db90SBjoern A. Zeeb 
7237bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7238bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7239bb81db90SBjoern A. Zeeb 
7240bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7241bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
72423540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7243bb81db90SBjoern A. Zeeb }
7244bb81db90SBjoern A. Zeeb 
72455edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
72465edde07cSBjoern A. Zeeb 
72475a9a0d78SBjoern A. Zeeb void
72485a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
72495a9a0d78SBjoern A. Zeeb {
72505a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72515a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72525a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
72535a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
72545a9a0d78SBjoern A. Zeeb 
72555a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
72565a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
72575a9a0d78SBjoern A. Zeeb 
72585a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72595a9a0d78SBjoern A. Zeeb 
72605a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72615a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72625a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72635a9a0d78SBjoern A. Zeeb 	else
72645a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72655a9a0d78SBjoern A. Zeeb 
72665a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72675a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72685a9a0d78SBjoern A. Zeeb 
72695a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72705a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72715a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
72725a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
72730d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72745a9a0d78SBjoern A. Zeeb 				/*
72755a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72765a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72775a9a0d78SBjoern A. Zeeb 				 */
72780d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
72790d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72805a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72815a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
72825a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
72835a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
72840d2cb6a6SBjoern A. Zeeb #endif
72855a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
72865a9a0d78SBjoern A. Zeeb 			}
72875a9a0d78SBjoern A. Zeeb 		}
72885a9a0d78SBjoern A. Zeeb 	}
72895a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72905a9a0d78SBjoern A. Zeeb }
72915a9a0d78SBjoern A. Zeeb 
72925a9a0d78SBjoern A. Zeeb void
72935a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
72945a9a0d78SBjoern A. Zeeb {
72955a9a0d78SBjoern A. Zeeb 	int i;
72965a9a0d78SBjoern A. Zeeb 
72975a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
72985a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72995a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
73005a9a0d78SBjoern A. Zeeb }
73015a9a0d78SBjoern A. Zeeb 
73025a9a0d78SBjoern A. Zeeb 
73035a9a0d78SBjoern A. Zeeb static void
73045a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
73055a9a0d78SBjoern A. Zeeb {
73065a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73075a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73085a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
73095a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
73105a9a0d78SBjoern A. Zeeb 
73115a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
73125a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
73135a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
73145a9a0d78SBjoern A. Zeeb 	else
73155a9a0d78SBjoern A. Zeeb 		ac_count = 1;
73165a9a0d78SBjoern A. Zeeb 
73175a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
73185a9a0d78SBjoern A. Zeeb 
73195a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
73205a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
73215a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
73225a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
73235a9a0d78SBjoern A. Zeeb 
73245a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
73255a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
73265a9a0d78SBjoern A. Zeeb 
73275a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
73285a9a0d78SBjoern A. Zeeb 
73295a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
73305a9a0d78SBjoern A. Zeeb 
73310d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
73325a9a0d78SBjoern A. Zeeb 				/*
73335a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
73345a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
73355a9a0d78SBjoern A. Zeeb 				 */
73360d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
73370d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
73385a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
73395a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
73405a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
73415a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
73420d2cb6a6SBjoern A. Zeeb #endif
73435a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
73445a9a0d78SBjoern A. Zeeb 
7345a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7346a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
73475a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
73485a9a0d78SBjoern A. Zeeb 
73495a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
73505a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
73515a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
73525a9a0d78SBjoern A. Zeeb 
73535a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
73545a9a0d78SBjoern A. Zeeb 							continue;
73555a9a0d78SBjoern A. Zeeb 
73565a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
73575a9a0d78SBjoern A. Zeeb 							continue;
73585a9a0d78SBjoern A. Zeeb 
73595a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
73605a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
73615a9a0d78SBjoern A. Zeeb 							continue;
73625a9a0d78SBjoern A. Zeeb 
73635a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
73645a9a0d78SBjoern A. Zeeb 
73655a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
73665a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
73675a9a0d78SBjoern A. Zeeb 					}
73685a9a0d78SBjoern A. Zeeb 				}
7369a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
73705a9a0d78SBjoern A. Zeeb 			}
73715a9a0d78SBjoern A. Zeeb 		}
73725a9a0d78SBjoern A. Zeeb 	}
73735a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
73745a9a0d78SBjoern A. Zeeb }
73755a9a0d78SBjoern A. Zeeb 
73765a9a0d78SBjoern A. Zeeb void
73775a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
73785a9a0d78SBjoern A. Zeeb {
73795a9a0d78SBjoern A. Zeeb 	int i;
73805a9a0d78SBjoern A. Zeeb 
73815a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
73825a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
73835a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
73845a9a0d78SBjoern A. Zeeb }
73855a9a0d78SBjoern A. Zeeb 
73865a9a0d78SBjoern A. Zeeb void
73875a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
73885a9a0d78SBjoern A. Zeeb {
73895a9a0d78SBjoern A. Zeeb 
73905a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
73915a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
73925a9a0d78SBjoern A. Zeeb 
73935a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
73945a9a0d78SBjoern A. Zeeb }
73955a9a0d78SBjoern A. Zeeb 
73965a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
73975a9a0d78SBjoern A. Zeeb void
73985a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
73995a9a0d78SBjoern A. Zeeb {
74005a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74015a9a0d78SBjoern A. Zeeb 
74025a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74035a9a0d78SBjoern A. Zeeb 
74045a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
74055a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
74065a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
74075a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
74085a9a0d78SBjoern A. Zeeb }
74095a9a0d78SBjoern A. Zeeb 
74105a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
74115a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
74125a9a0d78SBjoern A. Zeeb {
74135a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74145a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
74155a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
74165a9a0d78SBjoern A. Zeeb 
74175a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74185a9a0d78SBjoern A. Zeeb 	txq = NULL;
74195a9a0d78SBjoern A. Zeeb 
74205a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
74215a9a0d78SBjoern A. Zeeb 
74225a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
74235a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
74245a9a0d78SBjoern A. Zeeb 		goto out;
74255a9a0d78SBjoern A. Zeeb 
74265a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
74275a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
74285a9a0d78SBjoern A. Zeeb 		goto out;
74295a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
74305a9a0d78SBjoern A. Zeeb 		goto out;
74315a9a0d78SBjoern A. Zeeb 
74325a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
74335a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
74345a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
74355a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
74365a9a0d78SBjoern A. Zeeb 
74375a9a0d78SBjoern A. Zeeb out:
74385a9a0d78SBjoern A. Zeeb 	return (txq);
74395a9a0d78SBjoern A. Zeeb }
74405a9a0d78SBjoern A. Zeeb 
74415a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
74425a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
74435a9a0d78SBjoern A. Zeeb {
74445a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74455a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7446eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
74475a9a0d78SBjoern A. Zeeb 
74485a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
74495a9a0d78SBjoern A. Zeeb 
74505a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
74515a9a0d78SBjoern A. Zeeb 
74525a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7453eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7454eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7455eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7456eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
74575a9a0d78SBjoern A. Zeeb 		goto out;
74585a9a0d78SBjoern A. Zeeb 
745941b746e0SAustin Shafer 	/*
746041b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
746141b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
746241b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
746341b746e0SAustin Shafer 	 */
746441b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
74655a9a0d78SBjoern A. Zeeb 		goto out;
74665a9a0d78SBjoern A. Zeeb 
74675a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74685a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
74695a9a0d78SBjoern A. Zeeb out:
74705a9a0d78SBjoern A. Zeeb 	return;
74715a9a0d78SBjoern A. Zeeb }
74725a9a0d78SBjoern A. Zeeb 
7473eac3646fSBjoern A. Zeeb void
7474eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7475eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7476eac3646fSBjoern A. Zeeb {
7477eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7478eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7479eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7480eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7481eac3646fSBjoern A. Zeeb 
7482eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7483eac3646fSBjoern A. Zeeb 
7484eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7485eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7486eac3646fSBjoern A. Zeeb 	do {
7487eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7488eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7489eac3646fSBjoern A. Zeeb 			break;
7490eac3646fSBjoern A. Zeeb 
7491eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7492eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7493eac3646fSBjoern A. Zeeb 		do {
7494eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7495eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7496eac3646fSBjoern A. Zeeb 				break;
7497eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7498eac3646fSBjoern A. Zeeb 		} while(1);
7499eac3646fSBjoern A. Zeeb 
7500eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7501eac3646fSBjoern A. Zeeb 	} while (1);
7502eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7503eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7504eac3646fSBjoern A. Zeeb }
7505eac3646fSBjoern A. Zeeb 
75065a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
75075a9a0d78SBjoern A. Zeeb 
75085edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
75095edde07cSBjoern A. Zeeb 	u_int refcnt;
75105edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
75115edde07cSBjoern A. Zeeb };
75125edde07cSBjoern A. Zeeb 
75135edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
75145edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
75155edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
75165edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
75175edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
75185edde07cSBjoern A. Zeeb 	size_t ssid_len;
75195edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
75205edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
75215edde07cSBjoern A. Zeeb 
75225edde07cSBjoern A. Zeeb 	/*
75235edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
75245edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
75255edde07cSBjoern A. Zeeb 	 */
75265edde07cSBjoern A. Zeeb 	bool match;
75275edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
75285edde07cSBjoern A. Zeeb };
75295edde07cSBjoern A. Zeeb 
75305edde07cSBjoern A. Zeeb static void
75315edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
75325edde07cSBjoern A. Zeeb {
75335edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
75345edde07cSBjoern A. Zeeb 	size_t ielen;
75355edde07cSBjoern A. Zeeb 
75365edde07cSBjoern A. Zeeb 	lookup = arg;
75375edde07cSBjoern A. Zeeb 
75385edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
75395edde07cSBjoern A. Zeeb 	if (lookup->match)
75405edde07cSBjoern A. Zeeb 		return;
75415edde07cSBjoern A. Zeeb 
75425edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
75435edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
75445edde07cSBjoern A. Zeeb 		return;
75455edde07cSBjoern A. Zeeb 
75465edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
75475edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
75485edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
75495edde07cSBjoern A. Zeeb 		return;
75505edde07cSBjoern A. Zeeb 	}
75515edde07cSBjoern A. Zeeb 
75525edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
75535edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
75545edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
75555edde07cSBjoern A. Zeeb 		return;
75565edde07cSBjoern A. Zeeb 	}
75575edde07cSBjoern A. Zeeb 
75585edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
75595edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
75605edde07cSBjoern A. Zeeb 
75615edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
75625edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
75635edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
75645edde07cSBjoern A. Zeeb 			return;
75655edde07cSBjoern A. Zeeb 	}
75665edde07cSBjoern A. Zeeb 
75675edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
75685edde07cSBjoern A. Zeeb 		return;
75695edde07cSBjoern A. Zeeb 
75705edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
75715edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
75725edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
75735edde07cSBjoern A. Zeeb 			return;
75745edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
75755edde07cSBjoern A. Zeeb 			return;
75765edde07cSBjoern A. Zeeb 	}
75775edde07cSBjoern A. Zeeb 
75785edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
75795edde07cSBjoern A. Zeeb 
75805edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
75815edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
75825edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
75835edde07cSBjoern A. Zeeb 		return;
75845edde07cSBjoern A. Zeeb 
75855edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
75865edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
75875edde07cSBjoern A. Zeeb 	if (ielen)
75885edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
75895edde07cSBjoern A. Zeeb 
75905edde07cSBjoern A. Zeeb 	lookup->match = true;
75915edde07cSBjoern A. Zeeb }
75925edde07cSBjoern A. Zeeb 
75935edde07cSBjoern A. Zeeb struct cfg80211_bss *
75945edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
75955edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
75965edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
75975edde07cSBjoern A. Zeeb {
75985edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75995edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
76005edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
76015edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
76025edde07cSBjoern A. Zeeb 
76035edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
76045edde07cSBjoern A. Zeeb 
76055edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
76065edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
76075edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
76085edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
76095edde07cSBjoern A. Zeeb 		return (NULL);
76105edde07cSBjoern A. Zeeb 	}
76115edde07cSBjoern A. Zeeb 
76125edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
76135edde07cSBjoern A. Zeeb 	lookup.chan = chan;
76145edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
76155edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
76165edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
76175edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
76185edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
76195edde07cSBjoern A. Zeeb 	lookup.match = false;
76205edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
76215edde07cSBjoern A. Zeeb 
76225edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
76235edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
76245edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
76255edde07cSBjoern A. Zeeb 	if (!lookup.match) {
76265edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
76275edde07cSBjoern A. Zeeb 		return (NULL);
76285edde07cSBjoern A. Zeeb 	}
76295edde07cSBjoern A. Zeeb 
76305edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
76315edde07cSBjoern A. Zeeb 	return (&lbss->bss);
76325edde07cSBjoern A. Zeeb }
76335edde07cSBjoern A. Zeeb 
76345edde07cSBjoern A. Zeeb void
76355edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
76365edde07cSBjoern A. Zeeb {
76375edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
76385edde07cSBjoern A. Zeeb 
76395edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
76405edde07cSBjoern A. Zeeb 
76415edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
76425edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
76435edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
76445edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
76455edde07cSBjoern A. Zeeb 	}
76465edde07cSBjoern A. Zeeb }
76475edde07cSBjoern A. Zeeb 
76485edde07cSBjoern A. Zeeb void
76495edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
76505edde07cSBjoern A. Zeeb {
76515edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
76525edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
76535edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
76545edde07cSBjoern A. Zeeb 
76555edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
76565edde07cSBjoern A. Zeeb 	ic = lhw->ic;
76575edde07cSBjoern A. Zeeb 
76585edde07cSBjoern A. Zeeb 	/*
76595edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
76605edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
76615edde07cSBjoern A. Zeeb 	 */
76625edde07cSBjoern A. Zeeb 	if (ic == NULL ||
76635edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
76645edde07cSBjoern A. Zeeb 		return;
76655edde07cSBjoern A. Zeeb 
76665edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
76675edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
76685edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
76695edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
76705edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
76715edde07cSBjoern A. Zeeb }
76725edde07cSBjoern A. Zeeb 
76735edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
76745edde07cSBjoern A. Zeeb 
7675ac1d519cSBjoern A. Zeeb /*
7676ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7677ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7678ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7679ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7680ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7681ac1d519cSBjoern A. Zeeb  */
7682ac1d519cSBjoern A. Zeeb 
7683ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7684ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7685ac1d519cSBjoern A. Zeeb {
7686ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7687ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7688ac1d519cSBjoern A. Zeeb 	int error;
7689ac1d519cSBjoern A. Zeeb 
7690ac1d519cSBjoern A. Zeeb 	changed = 0;
7691ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7692ac1d519cSBjoern A. Zeeb 		cd = NULL;
7693ac1d519cSBjoern A. Zeeb 	else
7694ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7695ac1d519cSBjoern A. Zeeb 
7696ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7697ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7698ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7699ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7700ac1d519cSBjoern A. Zeeb 	}
7701ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7702ac1d519cSBjoern A. Zeeb 
7703ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7704ac1d519cSBjoern A. Zeeb 		return (0);
7705ac1d519cSBjoern A. Zeeb 
7706ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7707ac1d519cSBjoern A. Zeeb 	return (error);
7708ac1d519cSBjoern A. Zeeb }
7709ac1d519cSBjoern A. Zeeb 
7710ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7711ac1d519cSBjoern A. Zeeb 
77126b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
77136b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
77146b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7715