xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 2c44f1ff6935e93f7774e526f2f1ab0a2812eab5)
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
83*2c44f1ffSBjoern 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;
113811db70b6SBjoern A. Zeeb 	struct ieee80211_node_table *nt;
11396b4cac81SBjoern A. Zeeb 	int error;
114011db70b6SBjoern A. Zeeb 	bool islocked;
11416b4cac81SBjoern A. Zeeb 
11426b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
11436b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
11446b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11456b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11466b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11476b4cac81SBjoern A. Zeeb 
114811db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
114911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
115011db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
115111db70b6SBjoern A. Zeeb 		return (0);
115211db70b6SBjoern A. Zeeb 	}
115311db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
115411db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
115511db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
115611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
115711db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
115811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
115911db70b6SBjoern A. Zeeb 		return (0);
116011db70b6SBjoern A. Zeeb 	}
116111db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
116211db70b6SBjoern A. Zeeb 
116311db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
116411db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
116511db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
116611db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
116711db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
116811db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
116911db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
117011db70b6SBjoern A. Zeeb #endif
117111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
117211db70b6SBjoern A. Zeeb 		return (1);
117311db70b6SBjoern A. Zeeb 	}
117411db70b6SBjoern A. Zeeb 
117511db70b6SBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
117611db70b6SBjoern A. Zeeb 	nt = &ic->ic_sta;
117711db70b6SBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
117811db70b6SBjoern A. Zeeb 	if (islocked)
117911db70b6SBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
118011db70b6SBjoern A. Zeeb 
118111db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
118211db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
118311db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
118411db70b6SBjoern A. Zeeb 	if (kc == NULL) {
118511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
118611db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
118711db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
118811db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
118911db70b6SBjoern A. Zeeb #endif
119011db70b6SBjoern A. Zeeb 		error = 1;
119111db70b6SBjoern A. Zeeb 		goto out;
119211db70b6SBjoern A. Zeeb 	}
119311db70b6SBjoern A. Zeeb 
119411db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
119511db70b6SBjoern A. Zeeb 	if (error != 0) {
119611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
119711db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
119811db70b6SBjoern A. Zeeb 		error = 0;
119911db70b6SBjoern A. Zeeb 		goto out;
120011db70b6SBjoern A. Zeeb 	}
120111db70b6SBjoern A. Zeeb 
120211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
120311db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
120411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
120511db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
120611db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
120711db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
120811db70b6SBjoern A. Zeeb #endif
120911db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
121011db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
121111db70b6SBjoern A. Zeeb 	error = 1;
121211db70b6SBjoern A. Zeeb out:
121311db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
121411db70b6SBjoern A. Zeeb 	if (islocked)
121511db70b6SBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
121611db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
121711db70b6SBjoern A. Zeeb 	return (error);
121811db70b6SBjoern A. Zeeb }
121911db70b6SBjoern A. Zeeb 
122011db70b6SBjoern A. Zeeb static int
122111db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
122211db70b6SBjoern A. Zeeb {
122311db70b6SBjoern A. Zeeb 
122411db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
122511db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
122611db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
122711db70b6SBjoern A. Zeeb }
122811db70b6SBjoern A. Zeeb 
122911db70b6SBjoern A. Zeeb static int
123011db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
123111db70b6SBjoern A. Zeeb {
123211db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
123311db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
123411db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
123511db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
123611db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
123711db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
123811db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
123911db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
124011db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
124111db70b6SBjoern A. Zeeb 	uint32_t lcipher;
124211db70b6SBjoern A. Zeeb 	int error;
124311db70b6SBjoern A. Zeeb 
124411db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
124511db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
124611db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
124711db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
124811db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
124911db70b6SBjoern A. Zeeb 
125011db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
125111db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
125211db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
125311db70b6SBjoern A. Zeeb 		return (0);
125411db70b6SBjoern A. Zeeb 	}
125511db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
125611db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
125711db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
125811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
125911db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
126011db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
126111db70b6SBjoern A. Zeeb 		return (0);
126211db70b6SBjoern A. Zeeb 	}
126311db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
126411db70b6SBjoern A. Zeeb 
126511db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
126611db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
126711db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
126811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
126911db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
127011db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
127111db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
127211db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
127311db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
127411db70b6SBjoern A. Zeeb 	}
127511db70b6SBjoern A. Zeeb 
127611db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
12776b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
127811db70b6SBjoern A. Zeeb 	switch (lcipher) {
127911db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
128011db70b6SBjoern A. Zeeb 		break;
128111db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
128211db70b6SBjoern A. Zeeb 	default:
128312a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
128412a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
128511db70b6SBjoern A. Zeeb 		IMPROVE();
128611db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
128711db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
128811db70b6SBjoern A. Zeeb 		return (0);
128911db70b6SBjoern A. Zeeb 	}
129011db70b6SBjoern A. Zeeb 
129111db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
129211db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
129311db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
129411db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
12956b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
12966b4cac81SBjoern A. Zeeb #if 0
12976b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
12986b4cac81SBjoern A. Zeeb #endif
12996b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
13006b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
13016b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
13026b4cac81SBjoern A. Zeeb 
130311db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
130411db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
130511db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
130611db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
130711db70b6SBjoern A. Zeeb 
13086b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
13096b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
13106b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
13116b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
13126b4cac81SBjoern A. Zeeb 		break;
13136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
13146b4cac81SBjoern A. Zeeb 	default:
131511db70b6SBjoern A. Zeeb 		/* currently UNREACH */
13166b4cac81SBjoern A. Zeeb 		IMPROVE();
131711db70b6SBjoern A. Zeeb 		break;
13186b4cac81SBjoern A. Zeeb 	};
131911db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
13206b4cac81SBjoern A. Zeeb 
132111db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
132211db70b6SBjoern A. Zeeb 	if (error != 0) {
132311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
132411db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
132511db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
132611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
132711db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
132811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
132911db70b6SBjoern A. Zeeb 		return (0);
13306b4cac81SBjoern A. Zeeb 	}
13316b4cac81SBjoern A. Zeeb 
133211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
133311db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
133411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
133511db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
133611db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
133711db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
133811db70b6SBjoern A. Zeeb #endif
133911db70b6SBjoern A. Zeeb 
134011db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
134111db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
13426b4cac81SBjoern A. Zeeb 	return (1);
13436b4cac81SBjoern A. Zeeb }
13446b4cac81SBjoern A. Zeeb 
13456b4cac81SBjoern A. Zeeb static  int
13466b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
13476b4cac81SBjoern A. Zeeb {
13486b4cac81SBjoern A. Zeeb 
134911db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
13506b4cac81SBjoern A. Zeeb }
13516b4cac81SBjoern A. Zeeb #endif
13526b4cac81SBjoern A. Zeeb 
13536b4cac81SBjoern A. Zeeb static u_int
13546b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
13556b4cac81SBjoern A. Zeeb {
13566b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
13576b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13586b4cac81SBjoern A. Zeeb 
13596b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
13606b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
13616b4cac81SBjoern A. Zeeb 
13626b4cac81SBjoern A. Zeeb 	mc_list = arg;
13636b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
13646b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
13656b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
13666b4cac81SBjoern A. Zeeb 			return (0);
13676b4cac81SBjoern A. Zeeb 	}
13686b4cac81SBjoern A. Zeeb 
13696b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
13706b4cac81SBjoern A. Zeeb 	if (addr == NULL)
13716b4cac81SBjoern A. Zeeb 		return (0);
13726b4cac81SBjoern A. Zeeb 
13736b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
13746b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
13756b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
13766b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
13776b4cac81SBjoern A. Zeeb 	mc_list->count++;
13786b4cac81SBjoern A. Zeeb 
13799d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13816b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
13826b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
13839d9ba2b7SBjoern A. Zeeb #endif
13846b4cac81SBjoern A. Zeeb 
13856b4cac81SBjoern A. Zeeb 	return (1);
13866b4cac81SBjoern A. Zeeb }
13876b4cac81SBjoern A. Zeeb 
13886b4cac81SBjoern A. Zeeb static void
13896b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
13906b4cac81SBjoern A. Zeeb {
13916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13936b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
13946b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
13956b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13966b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
13976b4cac81SBjoern A. Zeeb 	u64 mc;
13986b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
13996b4cac81SBjoern A. Zeeb 
14006b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
14016b4cac81SBjoern A. Zeeb 
14026b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
14036b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
14046b4cac81SBjoern A. Zeeb 		return;
14056b4cac81SBjoern A. Zeeb 
14066b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
14076b4cac81SBjoern A. Zeeb 		return;
14086b4cac81SBjoern A. Zeeb 
14096b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
14106b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
14116b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
14126b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
14136b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
14146b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
14156b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
14166b4cac81SBjoern A. Zeeb 	} else {
14176b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
14186b4cac81SBjoern A. Zeeb 	}
14196b4cac81SBjoern A. Zeeb 
14206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14216b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
14226b4cac81SBjoern A. Zeeb 	/*
14236b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
14246b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
14256b4cac81SBjoern A. Zeeb 	 */
14266b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
14276b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
14286b4cac81SBjoern A. Zeeb 
14299d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14309d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
14316b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
14326b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
14339d9ba2b7SBjoern A. Zeeb #endif
14346b4cac81SBjoern A. Zeeb 
14356b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
14366b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
14376b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
14386b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
14396b4cac81SBjoern A. Zeeb 			mc_list.count--;
14406b4cac81SBjoern A. Zeeb 		}
14416b4cac81SBjoern A. Zeeb 	}
14426b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
14436b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
14446b4cac81SBjoern A. Zeeb }
14456b4cac81SBjoern A. Zeeb 
1446fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1447fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1448fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1449fa8f007dSBjoern A. Zeeb {
1450fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1451fa8f007dSBjoern A. Zeeb 
1452fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1453fa8f007dSBjoern A. Zeeb 
14549d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14559d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1456fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1457fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1458ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1459fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1460616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1461fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1462fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1463fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1464fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1465ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14669d9ba2b7SBjoern A. Zeeb #endif
1467fa8f007dSBjoern A. Zeeb 
1468fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1469fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1470fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1471fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1472fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1473fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1474fa8f007dSBjoern A. Zeeb 	}
1475fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1476fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1477fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1478fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1479fa8f007dSBjoern A. Zeeb 	}
1480fa8f007dSBjoern A. Zeeb 
1481fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1482fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1483fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1484fa8f007dSBjoern A. Zeeb 
14859d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14869d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1487fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1488fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1489ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1490fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1491616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1492fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1493fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1494fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1495fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1496ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14979d9ba2b7SBjoern A. Zeeb #endif
1498fa8f007dSBjoern A. Zeeb 
1499fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1500fa8f007dSBjoern A. Zeeb }
1501fa8f007dSBjoern A. Zeeb 
15026b4cac81SBjoern A. Zeeb static void
15036b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
15046b4cac81SBjoern A. Zeeb {
15056b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15066b4cac81SBjoern A. Zeeb 	int error;
15078ac540d3SBjoern A. Zeeb 	bool cancel;
15086b4cac81SBjoern A. Zeeb 
15098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15108ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15118ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15128ac540d3SBjoern A. Zeeb 	if (!cancel)
15136b4cac81SBjoern A. Zeeb 		return;
15146b4cac81SBjoern A. Zeeb 
15156b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15166b4cac81SBjoern A. Zeeb 
1517bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1518bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15196b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
15206b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
15218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15226b4cac81SBjoern A. Zeeb 
15236b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
15248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15258ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
15268ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
15278ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15298ac540d3SBjoern A. Zeeb 
1530bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
15316b4cac81SBjoern A. Zeeb 
15328ac540d3SBjoern A. Zeeb 	if (cancel)
15336b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
15346b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
15356b4cac81SBjoern A. Zeeb }
15366b4cac81SBjoern A. Zeeb 
15376b4cac81SBjoern A. Zeeb static void
1538086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1539086be6a8SBjoern A. Zeeb {
1540086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1541086be6a8SBjoern A. Zeeb 	int error;
1542086be6a8SBjoern A. Zeeb 	bool old;
1543086be6a8SBjoern A. Zeeb 
1544086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1545086be6a8SBjoern A. Zeeb 	if (old == new)
1546086be6a8SBjoern A. Zeeb 		return;
1547086be6a8SBjoern A. Zeeb 
1548086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1549086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1550086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1551086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1552086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1553086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1554086be6a8SBjoern A. Zeeb 	}
1555086be6a8SBjoern A. Zeeb }
1556086be6a8SBjoern A. Zeeb 
15575a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
15586b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
15596b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
15606b4cac81SBjoern A. Zeeb {
15615a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
15625a4d2461SBjoern A. Zeeb 
15635a4d2461SBjoern A. Zeeb 	changed = 0;
15646b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1565616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
15666b4cac81SBjoern A. Zeeb 
15676b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
15686b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
15696b4cac81SBjoern A. Zeeb 
1570616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1571616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
15726b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
15736b4cac81SBjoern A. Zeeb 		IMPROVE();
1574086be6a8SBjoern A. Zeeb 
15755a4d2461SBjoern A. Zeeb 		/*
15765a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
15775a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
15785a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
15795a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
15805a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
15815a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
15825a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
15835a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
15845a4d2461SBjoern A. Zeeb 		 */
15856b4cac81SBjoern A. Zeeb 	}
15865a4d2461SBjoern A. Zeeb 
15875a4d2461SBjoern A. Zeeb 	return (changed);
15886b4cac81SBjoern A. Zeeb }
15896b4cac81SBjoern A. Zeeb 
15906b4cac81SBjoern A. Zeeb static void
15916b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
15926b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
15936b4cac81SBjoern A. Zeeb {
15946b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
15956b4cac81SBjoern A. Zeeb 	int tid;
1596eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
15976b4cac81SBjoern A. Zeeb 
15986b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
15996b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
16006b4cac81SBjoern A. Zeeb 
16016b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
16026b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
16036b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
16046b4cac81SBjoern A. Zeeb 				continue;
16056b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
16066b4cac81SBjoern A. Zeeb 			continue;
16076b4cac81SBjoern A. Zeeb 
16086b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
16096b4cac81SBjoern A. Zeeb 			continue;
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
16126b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
16136b4cac81SBjoern A. Zeeb 			continue;
16146b4cac81SBjoern A. Zeeb 
1615eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1616eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1617eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1618eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
16196b4cac81SBjoern A. Zeeb 			continue;
16206b4cac81SBjoern A. Zeeb 
16216b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
16226b4cac81SBjoern A. Zeeb 	}
16236b4cac81SBjoern A. Zeeb }
16246b4cac81SBjoern A. Zeeb 
162588665349SBjoern A. Zeeb /*
162688665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
162788665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
162888665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
162988665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
163088665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
163188665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
163288665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
163388665349SBjoern A. Zeeb  * re-used.
163488665349SBjoern A. Zeeb  */
163588665349SBjoern A. Zeeb static void
163688665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
163788665349SBjoern A. Zeeb {
163888665349SBjoern A. Zeeb 	struct mbufq mq;
163988665349SBjoern A. Zeeb 	struct mbuf *m;
164088665349SBjoern A. Zeeb 	int len;
164188665349SBjoern A. Zeeb 
164288665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
164388665349SBjoern A. Zeeb 
164488665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
164588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
164688665349SBjoern A. Zeeb 	lsta->txq_ready = false;
164788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
164888665349SBjoern A. Zeeb 
164988665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
165088665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
165188665349SBjoern A. Zeeb 
165288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
165388665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
165488665349SBjoern A. Zeeb 	if (len <= 0) {
165588665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
165688665349SBjoern A. Zeeb 		return;
165788665349SBjoern A. Zeeb 	}
165888665349SBjoern A. Zeeb 
165988665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
166088665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
166188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
166288665349SBjoern A. Zeeb 
166388665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
166488665349SBjoern A. Zeeb 	while (m != NULL) {
166588665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
166688665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
166788665349SBjoern A. Zeeb 	}
166888665349SBjoern A. Zeeb }
166988665349SBjoern A. Zeeb 
16706b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
16716b4cac81SBjoern A. Zeeb 
16726b4cac81SBjoern A. Zeeb static int
16736b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16746b4cac81SBjoern A. Zeeb {
16756b4cac81SBjoern A. Zeeb 
16766b4cac81SBjoern A. Zeeb 	return (0);
16776b4cac81SBjoern A. Zeeb }
16786b4cac81SBjoern A. Zeeb 
16796b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
16806b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
16816b4cac81SBjoern A. Zeeb 
16826b4cac81SBjoern A. Zeeb static int
16836b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16846b4cac81SBjoern A. Zeeb {
16856b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1686c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1687d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
16886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16896b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16906b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16916b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16926b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16936b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16946b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
16956b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16966b4cac81SBjoern A. Zeeb 	uint32_t changed;
16976b4cac81SBjoern A. Zeeb 	int error;
16986b4cac81SBjoern A. Zeeb 
16992ac8a218SBjoern A. Zeeb 	/*
17002ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
17012ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
17022ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
17032ac8a218SBjoern A. Zeeb 	 */
17042ac8a218SBjoern A. Zeeb 
17052ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
17062ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
17072ac8a218SBjoern A. Zeeb 		return (EINVAL);
17082ac8a218SBjoern A. Zeeb 	}
17090936c648SBjoern A. Zeeb 	/*
17100936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
17110936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
17120936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
17130936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
17140936c648SBjoern A. Zeeb 	 */
17152ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
17162ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
17172ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
17182ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
17190936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17202ac8a218SBjoern A. Zeeb 		return (EINVAL);
17216b4cac81SBjoern A. Zeeb 	}
17226b4cac81SBjoern A. Zeeb 
17236b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17242ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
17252ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
17262ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
17272ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
17280936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17292ac8a218SBjoern A. Zeeb 		return (ESRCH);
17302ac8a218SBjoern A. Zeeb 	}
17312ac8a218SBjoern A. Zeeb 
17326b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17336b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17346b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17356b4cac81SBjoern A. Zeeb 
17360936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17370936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
17380936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
17390936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17400936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17410936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17420936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17430936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
174492690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
174592690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17460936c648SBjoern A. Zeeb 		return (EBUSY);
17470936c648SBjoern A. Zeeb 	}
17480936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17490936c648SBjoern A. Zeeb 
17506b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17526b4cac81SBjoern A. Zeeb 
17536b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
17546b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1755d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1756d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
17576b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
17586b4cac81SBjoern A. Zeeb 	} else {
17596b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1760c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
17616b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1762d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
17636b4cac81SBjoern A. Zeeb 	}
17646b4cac81SBjoern A. Zeeb 
1765d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1766389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1767d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
17686b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1769d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1770d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
177190d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
177290d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
17739fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
17742ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
17752ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
17769fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
17779fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
177890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1779d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
178090d8e307SBjoern A. Zeeb 		else
1781d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
17829fb91463SBjoern A. Zeeb 	}
17839fb91463SBjoern A. Zeeb #endif
17849fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
17859fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
17869fb91463SBjoern A. Zeeb #ifdef __notyet__
178790d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1788d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
178990d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1790d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
179190d8e307SBjoern A. Zeeb 		else
179290d8e307SBjoern A. Zeeb #endif
179390d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1794d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
17959fb91463SBjoern A. Zeeb 	}
17969fb91463SBjoern A. Zeeb #endif
1797389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
17986b4cac81SBjoern A. Zeeb 	/* Responder ... */
179990d8e307SBjoern A. Zeeb #if 0
180090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
1801d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
180290d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
180390d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
180490d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
180590d8e307SBjoern A. Zeeb #endif
180690d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
180790d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
180890d8e307SBjoern A. Zeeb #else
180990d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
181090d8e307SBjoern A. Zeeb #endif
18116b4cac81SBjoern A. Zeeb 
18126ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
18136ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
18146ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
18156ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
18166ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
18176ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
18186ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
18196ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
18206ffb7bd4SBjoern A. Zeeb 
18216ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
18226ffb7bd4SBjoern A. Zeeb 
18236ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
18246ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
18256ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
18266ffb7bd4SBjoern A. Zeeb 
18276ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
18286ffb7bd4SBjoern A. Zeeb 
18296b4cac81SBjoern A. Zeeb 	error = 0;
18306b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
18316b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
18326b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
18336b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
18346b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1835d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
18366b4cac81SBjoern A. Zeeb 	} else {
1837d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
18386b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
18397b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
18407b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
18417b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1842d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
18437b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1844d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
18456b4cac81SBjoern A. Zeeb 		} else {
1846018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1847018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
18486b4cac81SBjoern A. Zeeb 			goto out;
18496b4cac81SBjoern A. Zeeb 		}
18506ffb7bd4SBjoern A. Zeeb 
1851d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
18526ffb7bd4SBjoern A. Zeeb 
18536b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
18546b4cac81SBjoern A. Zeeb 		if (error == 0)
185568541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1856d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
18576b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
18586b4cac81SBjoern A. Zeeb 			error = 0;
18596b4cac81SBjoern A. Zeeb 		if (error != 0) {
1860018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1861018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1862d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1863d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1864c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
18656b4cac81SBjoern A. Zeeb 			goto out;
18666b4cac81SBjoern A. Zeeb 		}
18676b4cac81SBjoern A. Zeeb 	}
18686b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
18696b4cac81SBjoern A. Zeeb 
18706b4cac81SBjoern A. Zeeb 	/* RATES */
18716b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
18726b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
18736b4cac81SBjoern A. Zeeb 
1874d9f59799SBjoern A. Zeeb 	/*
18750936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
18760936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
18770936c648SBjoern A. Zeeb 	 * under the hoods.
1878d9f59799SBjoern A. Zeeb 	 */
18790936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
18800936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
18816b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1882e24e8103SBjoern A. Zeeb 
188388665349SBjoern A. Zeeb 	/*
188488665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
188588665349SBjoern A. Zeeb 	 * that we can tx again.
188688665349SBjoern A. Zeeb 	 */
188788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
188888665349SBjoern A. Zeeb 	lsta->txq_ready = true;
188988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
189088665349SBjoern A. Zeeb 
1891a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
18922ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1893a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1894a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1895e24e8103SBjoern A. Zeeb 
1896d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
18976b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18986b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
18996b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1900e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
19016b4cac81SBjoern A. Zeeb 	if (error != 0) {
19026b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1903018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1904018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19056b4cac81SBjoern A. Zeeb 		goto out;
19066b4cac81SBjoern A. Zeeb 	}
19076b4cac81SBjoern A. Zeeb #if 0
19086b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
19096b4cac81SBjoern A. Zeeb #endif
19106b4cac81SBjoern A. Zeeb 
19119d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19129d9ba2b7SBjoern A. Zeeb 
19131665ef97SBjoern A. Zeeb #if 0
19146b4cac81SBjoern A. Zeeb 	/*
19156b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
19166b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
19176b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
19186b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1919d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1920d9f59799SBjoern A. Zeeb 	 * for all queues.
19216b4cac81SBjoern A. Zeeb 	 */
1922e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
19231665ef97SBjoern A. Zeeb #endif
19246b4cac81SBjoern A. Zeeb 
19256b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
19266b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19276b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
19286b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
19296b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
19306b4cac81SBjoern A. Zeeb 
19316b4cac81SBjoern A. Zeeb 	/*
19326b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
19336b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
19346b4cac81SBjoern A. Zeeb 	 * - event_callback
19356b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
19366b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
19376b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
19386b4cac81SBjoern A. Zeeb 	 */
19396b4cac81SBjoern A. Zeeb 
1940105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1941105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1942105b9df2SBjoern A. Zeeb 
1943105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1944105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1945105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1946105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1947105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1948105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1949105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1950105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1951105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1952105b9df2SBjoern A. Zeeb 
1953105b9df2SBjoern A. Zeeb 	/*
1954105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1955105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1956105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1957105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1958105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1959105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1960105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1961105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1962105b9df2SBjoern A. Zeeb 	 */
1963105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1964105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1965105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1966105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1967105b9df2SBjoern A. Zeeb 	} else {
1968105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1969105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1970105b9df2SBjoern A. Zeeb 		/*
1971105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1972105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1973105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1974105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1975105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1976105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1977105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1978105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1979105b9df2SBjoern A. Zeeb 		 */
1980105b9df2SBjoern A. Zeeb 	}
1981105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1982105b9df2SBjoern A. Zeeb 	goto out_relocked;
1983105b9df2SBjoern A. Zeeb 
19846b4cac81SBjoern A. Zeeb out:
19858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19866b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1987105b9df2SBjoern A. Zeeb out_relocked:
19880936c648SBjoern A. Zeeb 	/*
1989105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
19900936c648SBjoern A. Zeeb 	 * during the work of this function.
19910936c648SBjoern A. Zeeb 	 */
19926b4cac81SBjoern A. Zeeb 	if (ni != NULL)
19936b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
19946b4cac81SBjoern A. Zeeb 	return (error);
19956b4cac81SBjoern A. Zeeb }
19966b4cac81SBjoern A. Zeeb 
19976b4cac81SBjoern A. Zeeb static int
19986b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19996b4cac81SBjoern A. Zeeb {
20006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20026b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20036b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20046b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
20076b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
20086b4cac81SBjoern A. Zeeb 	int error;
20096b4cac81SBjoern A. Zeeb 
20106b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20126b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20136b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20146b4cac81SBjoern A. Zeeb 
20152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20162ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20172ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
20182ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
20192ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20202ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20212ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20222ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20232ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20242ac8a218SBjoern A. Zeeb #endif
20252ac8a218SBjoern A. Zeeb 
20262ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20282ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
20292ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
20302ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
20312ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
20326b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
20336b4cac81SBjoern A. Zeeb 
203467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20356b4cac81SBjoern A. Zeeb 
20366b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
20386b4cac81SBjoern A. Zeeb 
2039d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2040d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2041d9f59799SBjoern A. Zeeb 
20426b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
204388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
20446b4cac81SBjoern A. Zeeb 
20456b4cac81SBjoern A. Zeeb 	/* flush, no drop */
20466b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
20476b4cac81SBjoern A. Zeeb 
20486b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20496b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20506b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20516b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
20526b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20536b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20546b4cac81SBjoern A. Zeeb 	}
20556b4cac81SBjoern A. Zeeb 
20566b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
20576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
20586b4cac81SBjoern A. Zeeb 
20596b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
20606b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2061d9f59799SBjoern A. Zeeb 
2062d9f59799SBjoern A. Zeeb 	/* Take the station down. */
20636b4cac81SBjoern A. Zeeb 
20646b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
20656b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20666b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2067d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2068e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
20696b4cac81SBjoern A. Zeeb 	if (error != 0) {
20706b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2071018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2072018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20736b4cac81SBjoern A. Zeeb 		goto out;
20746b4cac81SBjoern A. Zeeb 	}
20756b4cac81SBjoern A. Zeeb #if 0
20766b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
20776b4cac81SBjoern A. Zeeb #endif
20786b4cac81SBjoern A. Zeeb 
207967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20806b4cac81SBjoern A. Zeeb 
20812ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20822ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
20832ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
20842ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
20852ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2086d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
20870936c648SBjoern A. Zeeb 	/*
20880936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
20890936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
20900936c648SBjoern A. Zeeb 	 * and potentially freed.
20910936c648SBjoern A. Zeeb 	 */
20920936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2093d9f59799SBjoern A. Zeeb 
2094d9f59799SBjoern A. Zeeb 	/* conf_tx */
2095d9f59799SBjoern A. Zeeb 
2096d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
20976b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2098c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2099d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
21006b4cac81SBjoern A. Zeeb 
2101d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
21026b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
210368541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
21046b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
21056b4cac81SBjoern A. Zeeb 
21065a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
21075a4d2461SBjoern A. Zeeb 
21086b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
2109d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2110d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2111c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
21126b4cac81SBjoern A. Zeeb 	}
21136b4cac81SBjoern A. Zeeb 
21146b4cac81SBjoern A. Zeeb out:
21158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21166b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21176b4cac81SBjoern A. Zeeb 	return (error);
21186b4cac81SBjoern A. Zeeb }
21196b4cac81SBjoern A. Zeeb 
21206b4cac81SBjoern A. Zeeb static int
21216b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21226b4cac81SBjoern A. Zeeb {
21236b4cac81SBjoern A. Zeeb 	int error;
21246b4cac81SBjoern A. Zeeb 
21256b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
21266b4cac81SBjoern A. Zeeb 	if (error == 0)
21276b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
21286b4cac81SBjoern A. Zeeb 	return (error);
21296b4cac81SBjoern A. Zeeb }
21306b4cac81SBjoern A. Zeeb 
21316b4cac81SBjoern A. Zeeb static int
21326b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21336b4cac81SBjoern A. Zeeb {
21346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21356b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21376b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21386b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21396b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21406b4cac81SBjoern A. Zeeb 	int error;
21416b4cac81SBjoern A. Zeeb 
21426b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21436b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21446b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21456b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21466b4cac81SBjoern A. Zeeb 
21476b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21492ac8a218SBjoern A. Zeeb 
21502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21512ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21522ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21532ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21542ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21552ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21562ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21572ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21582ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21592ac8a218SBjoern A. Zeeb #endif
21602ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21612ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21622ac8a218SBjoern A. Zeeb 		goto out;
21632ac8a218SBjoern A. Zeeb 	}
21642ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21652ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21662ac8a218SBjoern A. Zeeb 
21672ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
21686b4cac81SBjoern A. Zeeb 
21696b4cac81SBjoern A. Zeeb 	/* Finish auth. */
21706b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
21716b4cac81SBjoern A. Zeeb 
21726b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
21736b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
21746b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2175e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2176018d93ecSBjoern A. Zeeb 	if (error != 0) {
2177018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2178018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21796b4cac81SBjoern A. Zeeb 		goto out;
2180018d93ecSBjoern A. Zeeb 	}
21816b4cac81SBjoern A. Zeeb 
21826b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21836b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21846b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21856b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
21866b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21876b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21886b4cac81SBjoern A. Zeeb 	}
21896b4cac81SBjoern A. Zeeb 
21906b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
21916b4cac81SBjoern A. Zeeb 
21926b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21936b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21946b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21956b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21966b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21976b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21986b4cac81SBjoern A. Zeeb 	}
21996b4cac81SBjoern A. Zeeb 
22006b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
220188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
22026b4cac81SBjoern A. Zeeb 
22036b4cac81SBjoern A. Zeeb 	/*
22046b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
22056b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
22066b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
22076b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
22086b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
22096b4cac81SBjoern A. Zeeb 	 * - event_callback
22106b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
22116b4cac81SBjoern A. Zeeb 	 */
22126b4cac81SBjoern A. Zeeb 
22136b4cac81SBjoern A. Zeeb out:
22148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22156b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22166b4cac81SBjoern A. Zeeb 	return (error);
22176b4cac81SBjoern A. Zeeb }
22186b4cac81SBjoern A. Zeeb 
22196b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
22206b4cac81SBjoern A. Zeeb static int
22216b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22226b4cac81SBjoern A. Zeeb {
22236b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22246b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22256b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22266b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22276b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22286b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22292ac8a218SBjoern A. Zeeb 	int error;
22306b4cac81SBjoern A. Zeeb 
22316b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22326b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22336b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22346b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22356b4cac81SBjoern A. Zeeb 
22366b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22382ac8a218SBjoern A. Zeeb 
22392ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22402ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
22412ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
22422ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22432ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22442ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22452ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22462ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22472ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22482ac8a218SBjoern A. Zeeb #endif
22492ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
22502ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
22512ac8a218SBjoern A. Zeeb 		goto out;
22522ac8a218SBjoern A. Zeeb 	}
22532ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22542ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22552ac8a218SBjoern A. Zeeb 
22562ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
22572ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
22586b4cac81SBjoern A. Zeeb 
22596b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
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 = false;
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 
22792ac8a218SBjoern A. Zeeb 	error = 0;
22802ac8a218SBjoern A. Zeeb out:
22818ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22826b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22836b4cac81SBjoern A. Zeeb 
22842ac8a218SBjoern A. Zeeb 	return (error);
22856b4cac81SBjoern A. Zeeb }
22866b4cac81SBjoern A. Zeeb 
22876b4cac81SBjoern A. Zeeb static int
2288d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22896b4cac81SBjoern A. Zeeb {
22906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22916b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22926b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22936b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22946b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22956b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22966b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22976b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2298d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
22996b4cac81SBjoern A. Zeeb 	int error;
23006b4cac81SBjoern A. Zeeb 
23016b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23026b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23036b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23046b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23056b4cac81SBjoern A. Zeeb 
23062ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
23072ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
23082ac8a218SBjoern A. Zeeb 
23092ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23102ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23112ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23122ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23132ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23142ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23152ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23162ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23172ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23182ac8a218SBjoern A. Zeeb #endif
23192ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23212ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
23222ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
23232ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
23242ac8a218SBjoern A. Zeeb 
23252ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
23266b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
23276b4cac81SBjoern A. Zeeb 
232867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2329d9f59799SBjoern A. Zeeb 
2330d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2331d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2332d9f59799SBjoern A. Zeeb 
2333d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2334d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2335d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2336d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2337d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2338ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2339d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2340d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2341d9f59799SBjoern A. Zeeb 	}
2342d9f59799SBjoern A. Zeeb 
23438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2344d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2345d9f59799SBjoern A. Zeeb 
234688665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2347d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2348018d93ecSBjoern A. Zeeb 	if (error != 0) {
2349018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2350018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2351d9f59799SBjoern A. Zeeb 		goto outni;
2352018d93ecSBjoern A. Zeeb 	}
2353d9f59799SBjoern A. Zeeb 
2354d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
235588665349SBjoern A. Zeeb 
235688665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
235788665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
235888665349SBjoern A. Zeeb 
23598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2360d9f59799SBjoern A. Zeeb 
236167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2362d9f59799SBjoern A. Zeeb 
2363d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
236488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2365d9f59799SBjoern A. Zeeb 
2366d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2367d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2368d9f59799SBjoern A. Zeeb 
23696b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23706b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23716b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23726b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2373ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
23746b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23756b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23766b4cac81SBjoern A. Zeeb 	}
23776b4cac81SBjoern A. Zeeb 
2378d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2379d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2380d9f59799SBjoern A. Zeeb 
2381d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2382d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2383d9f59799SBjoern A. Zeeb 
2384d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2385d9f59799SBjoern A. Zeeb 
23866b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
23876b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
23886b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
23896b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2390e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2391018d93ecSBjoern A. Zeeb 	if (error != 0) {
2392018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2393018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23946b4cac81SBjoern A. Zeeb 		goto out;
2395018d93ecSBjoern A. Zeeb 	}
23966b4cac81SBjoern A. Zeeb 
23975a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
23985a4d2461SBjoern A. Zeeb 	bss_changed = 0;
23995a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
24006b4cac81SBjoern A. Zeeb 
24015a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
240216e688b2SBjoern A. Zeeb 
2403d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2404d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2405d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2406d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2407e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2408d9f59799SBjoern A. Zeeb 	if (error != 0) {
2409d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2410018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2411018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2412d9f59799SBjoern A. Zeeb 		goto out;
2413d9f59799SBjoern A. Zeeb 	}
2414d9f59799SBjoern A. Zeeb 
241516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2416d9f59799SBjoern A. Zeeb 
2417d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2418d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2419d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2420616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2421616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2422d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2423d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2424d9f59799SBjoern A. Zeeb 
24252ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24262ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24272ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24282ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24292ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2430d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24310936c648SBjoern A. Zeeb 	/*
24320936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24330936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24340936c648SBjoern A. Zeeb 	 * and potentially freed.
24350936c648SBjoern A. Zeeb 	 */
24360936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2437d9f59799SBjoern A. Zeeb 
2438d9f59799SBjoern A. Zeeb 	/* conf_tx */
2439d9f59799SBjoern A. Zeeb 
2440d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2441d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2442c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2443d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2444d9f59799SBjoern A. Zeeb 
2445d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2446d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
244768541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2448d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2449d9f59799SBjoern A. Zeeb 
24505a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24515a4d2461SBjoern A. Zeeb 
2452d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2453d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2454d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2455c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2456d9f59799SBjoern A. Zeeb 	}
2457d9f59799SBjoern A. Zeeb 
2458d9f59799SBjoern A. Zeeb 	error = EALREADY;
24596b4cac81SBjoern A. Zeeb out:
24608ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
24616b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2462d9f59799SBjoern A. Zeeb outni:
24636b4cac81SBjoern A. Zeeb 	return (error);
24646b4cac81SBjoern A. Zeeb }
24656b4cac81SBjoern A. Zeeb 
24666b4cac81SBjoern A. Zeeb static int
2467d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2468d9f59799SBjoern A. Zeeb {
2469d9f59799SBjoern A. Zeeb 	int error;
2470d9f59799SBjoern A. Zeeb 
2471d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2472d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2473d9f59799SBjoern A. Zeeb 		return (error);
2474d9f59799SBjoern A. Zeeb 
2475d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2476d9f59799SBjoern A. Zeeb 
2477d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2478d9f59799SBjoern A. Zeeb 	return (error);
2479d9f59799SBjoern A. Zeeb }
2480d9f59799SBjoern A. Zeeb 
2481d9f59799SBjoern A. Zeeb static int
24826b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24836b4cac81SBjoern A. Zeeb {
24846b4cac81SBjoern A. Zeeb 	int error;
24856b4cac81SBjoern A. Zeeb 
2486d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24876b4cac81SBjoern A. Zeeb 	return (error);
24886b4cac81SBjoern A. Zeeb }
24896b4cac81SBjoern A. Zeeb 
24906b4cac81SBjoern A. Zeeb static int
24916b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24926b4cac81SBjoern A. Zeeb {
24936b4cac81SBjoern A. Zeeb 	int error;
24946b4cac81SBjoern A. Zeeb 
2495d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24966b4cac81SBjoern A. Zeeb 	return (error);
24976b4cac81SBjoern A. Zeeb }
24986b4cac81SBjoern A. Zeeb 
24996b4cac81SBjoern A. Zeeb static int
25006b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25016b4cac81SBjoern A. Zeeb {
25026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25086b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
25096b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
25106b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
25116b4cac81SBjoern A. Zeeb 	int error;
25126b4cac81SBjoern A. Zeeb 
25136b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25156b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25166b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25176b4cac81SBjoern A. Zeeb 
25186b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
25198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
25202ac8a218SBjoern A. Zeeb 
25212ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25222ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
25232ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
25242ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25252ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25262ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25272ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25282ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25292ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25302ac8a218SBjoern A. Zeeb #endif
25312ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
25322ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
25332ac8a218SBjoern A. Zeeb 		goto out;
25342ac8a218SBjoern A. Zeeb 	}
25352ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25362ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25372ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25382ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25392ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25402ac8a218SBjoern A. Zeeb 
25412ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25426b4cac81SBjoern A. Zeeb 
25436b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
25446b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
25456b4cac81SBjoern A. Zeeb 
25469597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
25479597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
25489597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
25499597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
25506b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25519597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
25524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25534a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
25544a67f1dfSBjoern A. Zeeb 		sta->wme = true;
25554a67f1dfSBjoern A. Zeeb #endif
2556e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2557018d93ecSBjoern A. Zeeb 	if (error != 0) {
2558018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2559018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25609597f7cbSBjoern A. Zeeb 		goto out;
2561018d93ecSBjoern A. Zeeb 	}
25626b4cac81SBjoern A. Zeeb 
25636b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
25646b4cac81SBjoern A. Zeeb 
25656b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
25666b4cac81SBjoern A. Zeeb 	bss_changed = 0;
25674a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25684a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
25694a67f1dfSBjoern A. Zeeb #endif
2570616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2571616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2572616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
25736b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
25746b4cac81SBjoern A. Zeeb 	}
25756b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2576616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2577616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
25786b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
25796b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
25806b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
25816b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25826b4cac81SBjoern A. Zeeb 	}
25836b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
25846b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
25856b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
25866b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25876b4cac81SBjoern A. Zeeb 	}
25886b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
25896b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
25906b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
25916b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
25926b4cac81SBjoern A. Zeeb 	}
2593fa8f007dSBjoern A. Zeeb 
2594fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2595fa8f007dSBjoern A. Zeeb 
25966b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
25976b4cac81SBjoern A. Zeeb 
25986b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
25996b4cac81SBjoern A. Zeeb 	 * - event_callback
26006b4cac81SBjoern A. Zeeb 	 */
26016b4cac81SBjoern A. Zeeb 
26026b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
26036b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
26046b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
26056b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
26066b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
26076b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
26086b4cac81SBjoern A. Zeeb 	}
26096b4cac81SBjoern A. Zeeb 
2610086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2611086be6a8SBjoern A. Zeeb 
26126b4cac81SBjoern A. Zeeb 	/*
26136b4cac81SBjoern A. Zeeb 	 * And then:
26146b4cac81SBjoern A. Zeeb 	 * - (more packets)?
26156b4cac81SBjoern A. Zeeb 	 * - set_key
26166b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
26176b4cac81SBjoern A. Zeeb 	 * - set_key (?)
26186b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
26196b4cac81SBjoern A. Zeeb 	 */
26206b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
26216b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
26226b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
26236b4cac81SBjoern A. Zeeb 
26246b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
26256b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
26266b4cac81SBjoern A. Zeeb 	}
26276b4cac81SBjoern A. Zeeb 
2628f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
26299fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
2630f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
26319fb91463SBjoern A. Zeeb 
26326b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
26336b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26346b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
26356b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2636e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
26376b4cac81SBjoern A. Zeeb 	if (error != 0) {
26386b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2639018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2640018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26416b4cac81SBjoern A. Zeeb 		goto out;
26426b4cac81SBjoern A. Zeeb 	}
26436b4cac81SBjoern A. Zeeb 
26446b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
26456b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
26466b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
26476b4cac81SBjoern A. Zeeb 	 *
26486b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
26496b4cac81SBjoern A. Zeeb 	 */
26506b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
26516b4cac81SBjoern A. Zeeb 
2652fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2653fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2654fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2655fa8f007dSBjoern A. Zeeb 
26566b4cac81SBjoern A. Zeeb out:
26578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
26586b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
26596b4cac81SBjoern A. Zeeb 	return (error);
26606b4cac81SBjoern A. Zeeb }
26616b4cac81SBjoern A. Zeeb 
26626b4cac81SBjoern A. Zeeb static int
26636b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26646b4cac81SBjoern A. Zeeb {
26656b4cac81SBjoern A. Zeeb 	int error;
26666b4cac81SBjoern A. Zeeb 
26676b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
26686b4cac81SBjoern A. Zeeb 	if (error == 0)
26696b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
26706b4cac81SBjoern A. Zeeb 	return (error);
26716b4cac81SBjoern A. Zeeb }
26726b4cac81SBjoern A. Zeeb 
26736b4cac81SBjoern A. Zeeb static int
26746b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26756b4cac81SBjoern A. Zeeb {
26766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26806b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26826b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2683d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2684d9f59799SBjoern A. Zeeb #if 0
2685d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2686d9f59799SBjoern A. Zeeb #endif
26876b4cac81SBjoern A. Zeeb 	int error;
26886b4cac81SBjoern A. Zeeb 
26896b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26916b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26926b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26936b4cac81SBjoern A. Zeeb 
26942ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26952ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26962ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26972ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26982ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26992ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27002ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27012ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27022ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27032ac8a218SBjoern A. Zeeb #endif
27042ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27052ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27062ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27072ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27082ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27092ac8a218SBjoern A. Zeeb 
27102ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27116b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27126b4cac81SBjoern A. Zeeb 
271367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2714d9f59799SBjoern A. Zeeb 
2715d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
27168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2717d9f59799SBjoern A. Zeeb 
2718d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2719d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2720d9f59799SBjoern A. Zeeb 
2721d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2722d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2723d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2724d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2725d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2726ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2727d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2728d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2729d9f59799SBjoern A. Zeeb 	}
2730d9f59799SBjoern A. Zeeb 
27318ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2732d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2733d9f59799SBjoern A. Zeeb 
2734d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2735d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2736018d93ecSBjoern A. Zeeb 	if (error != 0) {
2737018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2738018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2739d9f59799SBjoern A. Zeeb 		goto outni;
2740018d93ecSBjoern A. Zeeb 	}
2741d9f59799SBjoern A. Zeeb 
2742d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
274388665349SBjoern A. Zeeb 
274488665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
274588665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
274688665349SBjoern A. Zeeb 
27478ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2748d9f59799SBjoern A. Zeeb 
274967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2750d9f59799SBjoern A. Zeeb 
2751d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
275288665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2753d9f59799SBjoern A. Zeeb 
2754d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2755d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2756d9f59799SBjoern A. Zeeb 
2757d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2758d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2759d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2760d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2761ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2762d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2763d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2764d9f59799SBjoern A. Zeeb 	}
2765d9f59799SBjoern A. Zeeb 
2766d9f59799SBjoern A. Zeeb #if 0
2767d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2768d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2769d9f59799SBjoern A. Zeeb 
2770d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2771d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2772d9f59799SBjoern A. Zeeb #endif
2773d9f59799SBjoern A. Zeeb 
2774d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2775d9f59799SBjoern A. Zeeb 
27766b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
27776b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27786b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
27796b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2780e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2781018d93ecSBjoern A. Zeeb 	if (error != 0) {
2782018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2783018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27846b4cac81SBjoern A. Zeeb 		goto out;
2785018d93ecSBjoern A. Zeeb 	}
27866b4cac81SBjoern A. Zeeb 
278767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27886b4cac81SBjoern A. Zeeb 
278911db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
279011db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
279111db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
279211db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
279311db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
279411db70b6SBjoern A. Zeeb 		if (error != 0) {
279511db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
279611db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
279711db70b6SBjoern A. Zeeb 			/*
279811db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
279911db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
280011db70b6SBjoern A. Zeeb 			 * less appropriate time).
280111db70b6SBjoern A. Zeeb 			 */
280211db70b6SBjoern A. Zeeb 			/* goto out; */
280311db70b6SBjoern A. Zeeb 		}
280411db70b6SBjoern A. Zeeb 	}
280511db70b6SBjoern A. Zeeb #endif
280611db70b6SBjoern A. Zeeb 
28076b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
28086b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28096b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28106b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2811e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2812018d93ecSBjoern A. Zeeb 	if (error != 0) {
2813018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2814018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28156b4cac81SBjoern A. Zeeb 		goto out;
2816018d93ecSBjoern A. Zeeb 	}
28176b4cac81SBjoern A. Zeeb 
281867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
28196b4cac81SBjoern A. Zeeb 
2820d9f59799SBjoern A. Zeeb #if 0
2821d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2822d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2823d9f59799SBjoern A. Zeeb #endif
2824d9f59799SBjoern A. Zeeb 
2825d9f59799SBjoern A. Zeeb 	error = EALREADY;
28266b4cac81SBjoern A. Zeeb out:
28278ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
28286b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2829d9f59799SBjoern A. Zeeb outni:
28306b4cac81SBjoern A. Zeeb 	return (error);
28316b4cac81SBjoern A. Zeeb }
28326b4cac81SBjoern A. Zeeb 
28336b4cac81SBjoern A. Zeeb static int
2834d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2835d9f59799SBjoern A. Zeeb {
2836d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2837d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2838d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2839d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2840d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2841d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2842d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2843d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2844d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2845d9f59799SBjoern A. Zeeb 	int error;
2846d9f59799SBjoern A. Zeeb 
2847d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2848d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2849d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2850d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2851d9f59799SBjoern A. Zeeb 
28522ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
28532ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
28542ac8a218SBjoern A. Zeeb 
28552ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28562ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28572ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
28582ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
28592ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28602ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28612ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28622ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28632ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28642ac8a218SBjoern A. Zeeb #endif
28652ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28672ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28682ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28692ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28702ac8a218SBjoern A. Zeeb 
28712ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2872d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2873d9f59799SBjoern A. Zeeb 
287467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2875d9f59799SBjoern A. Zeeb 
2876d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2877d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2878d9f59799SBjoern A. Zeeb 
2879d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2880d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2881d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2882d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2883d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2884ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2885d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2886d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2887d9f59799SBjoern A. Zeeb 	}
2888d9f59799SBjoern A. Zeeb 
28898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2890d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2891d9f59799SBjoern A. Zeeb 
2892d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2893d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2894018d93ecSBjoern A. Zeeb 	if (error != 0) {
2895018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2896018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2897d9f59799SBjoern A. Zeeb 		goto outni;
2898018d93ecSBjoern A. Zeeb 	}
2899d9f59799SBjoern A. Zeeb 
2900d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
290188665349SBjoern A. Zeeb 
290288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
290388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
290488665349SBjoern A. Zeeb 
29058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2906d9f59799SBjoern A. Zeeb 
290767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2908d9f59799SBjoern A. Zeeb 
2909d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
291088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2911d9f59799SBjoern A. Zeeb 
2912d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2913d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2914d9f59799SBjoern A. Zeeb 
2915d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2916d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2917d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2918d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2919ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2920d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2921d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2922d9f59799SBjoern A. Zeeb 	}
2923d9f59799SBjoern A. Zeeb 
2924d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2925d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2926d9f59799SBjoern A. Zeeb 
2927d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2928d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2929d9f59799SBjoern A. Zeeb 
2930d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2931d9f59799SBjoern A. Zeeb 
2932d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2933d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2934d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2935d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2936e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2937018d93ecSBjoern A. Zeeb 	if (error != 0) {
2938018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2939018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2940d9f59799SBjoern A. Zeeb 		goto out;
2941018d93ecSBjoern A. Zeeb 	}
2942d9f59799SBjoern A. Zeeb 
294367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2944d9f59799SBjoern A. Zeeb 
294511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
294611db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
294711db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
294811db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
294911db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
295011db70b6SBjoern A. Zeeb 		if (error != 0) {
295111db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
295211db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
295311db70b6SBjoern A. Zeeb 			/*
295411db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
295511db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
295611db70b6SBjoern A. Zeeb 			 * less appropriate time).
295711db70b6SBjoern A. Zeeb 			 */
295811db70b6SBjoern A. Zeeb 			/* goto out; */
295911db70b6SBjoern A. Zeeb 		}
296011db70b6SBjoern A. Zeeb 	}
296111db70b6SBjoern A. Zeeb #endif
296211db70b6SBjoern A. Zeeb 
2963d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2964d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2965d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2966d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2967e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2968018d93ecSBjoern A. Zeeb 	if (error != 0) {
2969018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2970018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2971d9f59799SBjoern A. Zeeb 		goto out;
2972018d93ecSBjoern A. Zeeb 	}
2973d9f59799SBjoern A. Zeeb 
297467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2975d9f59799SBjoern A. Zeeb 
2976d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2977d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2978d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2979d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2980e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2981018d93ecSBjoern A. Zeeb 	if (error != 0) {
2982018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2983018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2984d9f59799SBjoern A. Zeeb 		goto out;
2985018d93ecSBjoern A. Zeeb 	}
2986d9f59799SBjoern A. Zeeb 
29875a4d2461SBjoern A. Zeeb 	bss_changed = 0;
298816e688b2SBjoern A. Zeeb 	/*
29895a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
29905a4d2461SBjoern A. Zeeb 	 *
299116e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
29925a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
29935a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
29945a4d2461SBjoern A. Zeeb 	 *
29955a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
29965a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
29975a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
29985a4d2461SBjoern A. Zeeb 	 * a fw assert.
29995a4d2461SBjoern A. Zeeb 	 *
30005a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
30015a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
30025a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
30035a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
30045a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
30055a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
30065a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
30075a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
30085a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
30095a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
301016e688b2SBjoern A. Zeeb 	 */
30115a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
30125a4d2461SBjoern A. Zeeb 
30135a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
301416e688b2SBjoern A. Zeeb 
3015d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3016d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3017d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3018d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3019e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3020d9f59799SBjoern A. Zeeb 	if (error != 0) {
3021d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3022018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3023018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3024d9f59799SBjoern A. Zeeb 		goto out;
3025d9f59799SBjoern A. Zeeb 	}
3026d9f59799SBjoern A. Zeeb 
30275a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
30285a4d2461SBjoern A. Zeeb 
302916e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3030d9f59799SBjoern A. Zeeb 
3031d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3032d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3033d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3034616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3035616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3036d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
30375a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
30385a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
30395a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3040d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3041d9f59799SBjoern A. Zeeb 
30422ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30432ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
30442ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
30452ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
30462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30470936c648SBjoern A. Zeeb 	/*
30480936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
30490936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
30500936c648SBjoern A. Zeeb 	 * and potentially freed.
30510936c648SBjoern A. Zeeb 	 */
30520936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3053d9f59799SBjoern A. Zeeb 
3054d9f59799SBjoern A. Zeeb 	/* conf_tx */
3055d9f59799SBjoern A. Zeeb 
3056d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
3057d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
3058c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
3059d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
3060d9f59799SBjoern A. Zeeb 
3061d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
3062d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
306368541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
3064d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
3065d9f59799SBjoern A. Zeeb 
30665a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
30675a4d2461SBjoern A. Zeeb 
3068d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
3069d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
3070d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
3071c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
3072d9f59799SBjoern A. Zeeb 	}
3073d9f59799SBjoern A. Zeeb 
3074d9f59799SBjoern A. Zeeb 	error = EALREADY;
3075d9f59799SBjoern A. Zeeb out:
30768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3077d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3078d9f59799SBjoern A. Zeeb outni:
3079d9f59799SBjoern A. Zeeb 	return (error);
3080d9f59799SBjoern A. Zeeb }
3081d9f59799SBjoern A. Zeeb 
3082d9f59799SBjoern A. Zeeb static int
3083d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3084d9f59799SBjoern A. Zeeb {
3085d9f59799SBjoern A. Zeeb 
3086d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3087d9f59799SBjoern A. Zeeb }
3088d9f59799SBjoern A. Zeeb 
3089d9f59799SBjoern A. Zeeb static int
30906b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
30916b4cac81SBjoern A. Zeeb {
30926b4cac81SBjoern A. Zeeb 	int error;
30936b4cac81SBjoern A. Zeeb 
3094d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3095d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3096d9f59799SBjoern A. Zeeb 		return (error);
3097d9f59799SBjoern A. Zeeb 
3098d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3099d9f59799SBjoern A. Zeeb 
3100d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
31016b4cac81SBjoern A. Zeeb 	return (error);
31026b4cac81SBjoern A. Zeeb }
31036b4cac81SBjoern A. Zeeb 
3104d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
31056b4cac81SBjoern A. Zeeb 
31066b4cac81SBjoern A. Zeeb /*
31076b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
31086b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
31096b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
31106b4cac81SBjoern A. Zeeb  */
31116b4cac81SBjoern A. Zeeb struct fsm_state {
31126b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
31136b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31146b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
31156b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
31166b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
31176b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
31186b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
31196b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3120d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3121d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
31226b4cac81SBjoern A. Zeeb 
31236b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31246b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
31266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3127d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
31286b4cac81SBjoern A. Zeeb 
3129d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3130d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3131d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3132d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3133d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
31346b4cac81SBjoern A. Zeeb 
3135d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3136d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3137d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
31386b4cac81SBjoern A. Zeeb 
31396b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
31406b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
31416b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
31426b4cac81SBjoern A. Zeeb 
31436b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
31446b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
31456b4cac81SBjoern A. Zeeb };
31466b4cac81SBjoern A. Zeeb 
31476b4cac81SBjoern A. Zeeb static int
31486b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
31496b4cac81SBjoern A. Zeeb {
31506b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
31516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
31526b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31536b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31546b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
31556b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31566b4cac81SBjoern A. Zeeb 	int error;
31576b4cac81SBjoern A. Zeeb 
31586b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
31596b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
31606b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
31616b4cac81SBjoern A. Zeeb 
31629d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31639d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
31646b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
31656b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31669d9ba2b7SBjoern A. Zeeb #endif
31676b4cac81SBjoern A. Zeeb 
31686b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
31696b4cac81SBjoern A. Zeeb 
31706b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
31716b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
31726b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
31736b4cac81SBjoern A. Zeeb 
31746b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
31756b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
31766b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
31776b4cac81SBjoern A. Zeeb 
31786b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
31796b4cac81SBjoern A. Zeeb 
31806b4cac81SBjoern A. Zeeb 	} else {
31816b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
31826b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
31836b4cac81SBjoern A. Zeeb 		return (ENOSYS);
31846b4cac81SBjoern A. Zeeb 	}
31856b4cac81SBjoern A. Zeeb 
31866b4cac81SBjoern A. Zeeb 	error = 0;
31876b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
31886b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
31899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31909d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3191d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3192d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3193d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3194d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
31959d9ba2b7SBjoern A. Zeeb #endif
31966b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
31976b4cac81SBjoern A. Zeeb 			break;
31986b4cac81SBjoern A. Zeeb 		}
31996b4cac81SBjoern A. Zeeb 	}
32006b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
32016b4cac81SBjoern A. Zeeb 
32026b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3203d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
32046b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
32056b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
32066b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
32076b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
32086b4cac81SBjoern A. Zeeb 		return (ENOSYS);
32096b4cac81SBjoern A. Zeeb 	}
32106b4cac81SBjoern A. Zeeb 
32116b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
32129d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32139d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3214d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3215d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3216d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3217d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
32189d9ba2b7SBjoern A. Zeeb #endif
32196b4cac81SBjoern A. Zeeb 		return (0);
32206b4cac81SBjoern A. Zeeb 	}
32216b4cac81SBjoern A. Zeeb 
32226b4cac81SBjoern A. Zeeb 	if (error != 0) {
32236b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
32246b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
32256b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
32266b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
322745c27ad5SBjoern A. Zeeb 		return (error);
32286b4cac81SBjoern A. Zeeb 	}
32296b4cac81SBjoern A. Zeeb 
32309d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32319d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3232d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3233d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
32346b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
32359d9ba2b7SBjoern A. Zeeb #endif
32366b4cac81SBjoern A. Zeeb 
32376b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
32386b4cac81SBjoern A. Zeeb }
32396b4cac81SBjoern A. Zeeb 
32406b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32416b4cac81SBjoern A. Zeeb 
3242d9f59799SBjoern A. Zeeb /*
3243d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3244d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3245d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3246d9f59799SBjoern A. Zeeb  */
3247d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3248d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3249d9f59799SBjoern A. Zeeb {
3250d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32512ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3252d9f59799SBjoern A. Zeeb 
32532ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3254d9f59799SBjoern A. Zeeb 
3255ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32562ac8a218SBjoern A. Zeeb 
32572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32582ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32592ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32602ac8a218SBjoern A. Zeeb 
32612ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
32622ac8a218SBjoern A. Zeeb 	return (rni);
3263d9f59799SBjoern A. Zeeb }
3264d9f59799SBjoern A. Zeeb 
32654a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
32666b4cac81SBjoern A. Zeeb static int
32674a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
32686b4cac81SBjoern A. Zeeb {
32694a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
32706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32736b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
32746b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
32756b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
32766b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
32776b4cac81SBjoern A. Zeeb 	int error;
32786b4cac81SBjoern A. Zeeb 	uint16_t ac;
32796b4cac81SBjoern A. Zeeb 
32806b4cac81SBjoern A. Zeeb 	IMPROVE();
32816b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
32826b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
32836b4cac81SBjoern A. Zeeb 
32846b4cac81SBjoern A. Zeeb 	if (vap == NULL)
32856b4cac81SBjoern A. Zeeb 		return (0);
32866b4cac81SBjoern A. Zeeb 
32874a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
32884a67f1dfSBjoern A. Zeeb 		return (0);
32894a67f1dfSBjoern A. Zeeb 
32906b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
32916b4cac81SBjoern A. Zeeb 		return (0);
32926b4cac81SBjoern A. Zeeb 
32934a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
32944a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
32954a67f1dfSBjoern A. Zeeb 		return (0);
32964a67f1dfSBjoern A. Zeeb 	}
32974a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
32986b4cac81SBjoern A. Zeeb 
32994a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
33006b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
33016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
33026b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
33036b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
33046b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
33056b4cac81SBjoern A. Zeeb 
33064a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33074a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
33084a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33094a67f1dfSBjoern A. Zeeb 
33106b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
33116b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
33126b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
33136b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
33146b4cac81SBjoern A. Zeeb 
33156b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
33166b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
33176b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
33186b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
33196b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
33206b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
332168541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
33226b4cac81SBjoern A. Zeeb 		if (error != 0)
33233f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
33246b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
33256b4cac81SBjoern A. Zeeb 	}
33266b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
33276b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
33284a67f1dfSBjoern A. Zeeb 	if (!planned)
33296b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
33304a67f1dfSBjoern A. Zeeb 
33314a67f1dfSBjoern A. Zeeb 	return (changed);
33324a67f1dfSBjoern A. Zeeb }
33336b4cac81SBjoern A. Zeeb #endif
33346b4cac81SBjoern A. Zeeb 
33354a67f1dfSBjoern A. Zeeb static int
33364a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
33374a67f1dfSBjoern A. Zeeb {
33384a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
33394a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
33404a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33414a67f1dfSBjoern A. Zeeb 
33424a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
33434a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
33444a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
33456b4cac81SBjoern A. Zeeb 		return (0);
33464a67f1dfSBjoern A. Zeeb 
33474a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
33484a67f1dfSBjoern A. Zeeb 
33494a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
33504a67f1dfSBjoern A. Zeeb #endif
33514a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
33526b4cac81SBjoern A. Zeeb }
33536b4cac81SBjoern A. Zeeb 
33544aff4048SBjoern A. Zeeb /*
33554aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
33564aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
33574aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
33584aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
33594aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
33604aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3361edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3362edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3363edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3364edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
33654aff4048SBjoern A. Zeeb  */
33664aff4048SBjoern A. Zeeb static void
33674aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
33684aff4048SBjoern A. Zeeb {
33694aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
33704aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33714aff4048SBjoern A. Zeeb 
33724aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3373edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3374edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3375edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
33764aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
33774aff4048SBjoern A. Zeeb 		return;
33784aff4048SBjoern A. Zeeb 	}
33794aff4048SBjoern A. Zeeb 
33804aff4048SBjoern A. Zeeb 	vif = arg;
338157609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
33824aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
33834aff4048SBjoern A. Zeeb }
33844aff4048SBjoern A. Zeeb 
33856b4cac81SBjoern A. Zeeb static struct ieee80211vap *
33866b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
33876b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
33886b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
33896b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
33906b4cac81SBjoern A. Zeeb {
33916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33936b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33946b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
33956b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3396a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
33976b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
339840839418SBjoern A. Zeeb 	struct sysctl_oid *node;
33996b4cac81SBjoern A. Zeeb 	size_t len;
3400e3a0b120SBjoern A. Zeeb 	int error, i;
3401a6042e17SBjoern A. Zeeb 	uint16_t ac;
34026b4cac81SBjoern A. Zeeb 
34036b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
34046b4cac81SBjoern A. Zeeb 		return (NULL);
34056b4cac81SBjoern A. Zeeb 
34066b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34086b4cac81SBjoern A. Zeeb 
34096b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
34106b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
34116b4cac81SBjoern A. Zeeb 
34126b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
34136b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3414a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
34152ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
34162ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34176b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
34186b4cac81SBjoern A. Zeeb 
34196b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
34206b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
34216b4cac81SBjoern A. Zeeb 	vif->p2p = false;
34226b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
34236b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
34246b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
34256b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
34266b4cac81SBjoern A. Zeeb 	IMPROVE();
34276b4cac81SBjoern A. Zeeb 
34286b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
34296b4cac81SBjoern A. Zeeb #if 1
34306b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3431adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
34326ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
34336ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
34344aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
34354aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
34366ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
34377b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
34386b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
34396b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
34406b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
34416b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
34426b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3443616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3444616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3445616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3446616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
34476ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3448caaa79c3SBjoern A. Zeeb 	/*
3449caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3450caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3451caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3452caaa79c3SBjoern A. Zeeb 	 */
34536ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
34546b4cac81SBjoern A. Zeeb #endif
34556b4cac81SBjoern A. Zeeb #if 0
34566b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
34576b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
34586b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
34596b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
34606b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
34616b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
34626b4cac81SBjoern A. Zeeb #endif
3463e3a0b120SBjoern A. Zeeb 
34646ffb7bd4SBjoern A. Zeeb 	/* Link Config */
34656ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
34666ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
34676ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
34686ffb7bd4SBjoern A. Zeeb 	}
34696ffb7bd4SBjoern A. Zeeb 
3470e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3471e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3472e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3473e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3474e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3475e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3476e3a0b120SBjoern A. Zeeb 		else
3477e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
34780cbcfa19SBjoern A. Zeeb 
34790cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
34800cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3481e3a0b120SBjoern A. Zeeb 	}
3482e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3483e3a0b120SBjoern A. Zeeb 
34846b4cac81SBjoern A. Zeeb 	IMPROVE();
34856b4cac81SBjoern A. Zeeb 
34866b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
34876b4cac81SBjoern A. Zeeb 	if (error != 0) {
34883f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
34896b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34906b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
34916b4cac81SBjoern A. Zeeb 		return (NULL);
34926b4cac81SBjoern A. Zeeb 	}
34936b4cac81SBjoern A. Zeeb 
34946b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
34956b4cac81SBjoern A. Zeeb 	if (error != 0) {
34966b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
34973f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
34986b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34996b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
35006b4cac81SBjoern A. Zeeb 		return (NULL);
35016b4cac81SBjoern A. Zeeb 	}
35026b4cac81SBjoern A. Zeeb 
35038891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
35046b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
35058891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
35066b4cac81SBjoern A. Zeeb 
35076b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
35086b4cac81SBjoern A. Zeeb 	changed = 0;
35096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35106b4cac81SBjoern A. Zeeb 
3511a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3512a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3513a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3514a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3515a6042e17SBjoern A. Zeeb 
3516a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3517a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3518a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3519a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3520a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3521a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3522a6042e17SBjoern A. Zeeb 		if (error != 0)
3523a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3524a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3525a6042e17SBjoern A. Zeeb 	}
3526a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3527a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3528a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35296b4cac81SBjoern A. Zeeb 
35306b4cac81SBjoern A. Zeeb 	/* Force MC init. */
35316b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
35326b4cac81SBjoern A. Zeeb 
35336b4cac81SBjoern A. Zeeb 	IMPROVE();
35346b4cac81SBjoern A. Zeeb 
35356b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
35366b4cac81SBjoern A. Zeeb 
35376b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
35386b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
35396b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3540d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3541d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
35426b4cac81SBjoern A. Zeeb 
3543b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
354411db70b6SBjoern A. Zeeb 	/* Key management. */
354511db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
35466b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
35476b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
35486b4cac81SBjoern A. Zeeb 	}
354911db70b6SBjoern A. Zeeb #endif
35506b4cac81SBjoern A. Zeeb 
35519fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
35529fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
35539fb91463SBjoern A. Zeeb #endif
35549fb91463SBjoern A. Zeeb 
35556b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
35566b4cac81SBjoern A. Zeeb 
35576b4cac81SBjoern A. Zeeb 	/* Complete setup. */
35586b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
35596b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
35606b4cac81SBjoern A. Zeeb 
356111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
356211db70b6SBjoern A. Zeeb 	/*
356311db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
356411db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
356511db70b6SBjoern A. Zeeb 	 */
356611db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
356711db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
356811db70b6SBjoern A. Zeeb #endif
3569ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3570ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3571ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3572ac2c7271SBjoern A. Zeeb #endif
357311db70b6SBjoern A. Zeeb 
35746b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
35756b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
35766b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
35776b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
35786b4cac81SBjoern A. Zeeb 
35796b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
35806b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
35816b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
35826b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
35836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
35846b4cac81SBjoern A. Zeeb 	/* any others? */
358540839418SBjoern A. Zeeb 
358640839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
358740839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
358840839418SBjoern A. Zeeb 
358940839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
359040839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
359140839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
359240839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
359340839418SBjoern A. Zeeb 
359440839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
359540839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
359640839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
359740839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
359840839418SBjoern A. Zeeb 
35996b4cac81SBjoern A. Zeeb 	IMPROVE();
36006b4cac81SBjoern A. Zeeb 
36016b4cac81SBjoern A. Zeeb 	return (vap);
36026b4cac81SBjoern A. Zeeb }
36036b4cac81SBjoern A. Zeeb 
36044b0af114SBjoern A. Zeeb void
36054b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
36064b0af114SBjoern A. Zeeb {
36074b0af114SBjoern A. Zeeb 
36084b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
36094b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
36104b0af114SBjoern A. Zeeb 
36114b0af114SBjoern A. Zeeb 	IMPROVE();
36124b0af114SBjoern A. Zeeb }
36134b0af114SBjoern A. Zeeb 
36144b0af114SBjoern A. Zeeb void
36154b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
36164b0af114SBjoern A. Zeeb {
36174b0af114SBjoern A. Zeeb 
36184b0af114SBjoern A. Zeeb 	TODO();
36194b0af114SBjoern A. Zeeb }
36204b0af114SBjoern A. Zeeb 
36216b4cac81SBjoern A. Zeeb static void
36226b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
36236b4cac81SBjoern A. Zeeb {
36246b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36296b4cac81SBjoern A. Zeeb 
36306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
36316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36326b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
36336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36356b4cac81SBjoern A. Zeeb 
36364aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
36374aff4048SBjoern A. Zeeb 
363840839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
363940839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
364040839418SBjoern A. Zeeb 
36418891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36426b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
36438891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36446b4cac81SBjoern A. Zeeb 
36456b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
36466b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3647dbf76919SBjoern A. Zeeb 
3648dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3649dbf76919SBjoern A. Zeeb 
3650dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3651dbf76919SBjoern A. Zeeb 
36526c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
36537b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
36546c38c6b1SBjoern A. Zeeb 
36556b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
36566b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
36576b4cac81SBjoern A. Zeeb }
36586b4cac81SBjoern A. Zeeb 
36596b4cac81SBjoern A. Zeeb static void
36606b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
36616b4cac81SBjoern A. Zeeb {
36626b4cac81SBjoern A. Zeeb 
36636b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
36646b4cac81SBjoern A. Zeeb 	TRACEOK();
36656b4cac81SBjoern A. Zeeb }
36666b4cac81SBjoern A. Zeeb 
36676b4cac81SBjoern A. Zeeb static void
36686b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
36696b4cac81SBjoern A. Zeeb {
36706b4cac81SBjoern A. Zeeb 
36716b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36726b4cac81SBjoern A. Zeeb }
36736b4cac81SBjoern A. Zeeb 
36746b4cac81SBjoern A. Zeeb static void
36756b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
36766b4cac81SBjoern A. Zeeb {
36776b4cac81SBjoern A. Zeeb 
36786b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36796b4cac81SBjoern A. Zeeb }
36806b4cac81SBjoern A. Zeeb 
36816b4cac81SBjoern A. Zeeb /* Start / stop device. */
36826b4cac81SBjoern A. Zeeb static void
36836b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
36846b4cac81SBjoern A. Zeeb {
36856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36868d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36876b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36886b4cac81SBjoern A. Zeeb 	int error;
36898d58a057SBjoern A. Zeeb #endif
36906b4cac81SBjoern A. Zeeb 	bool start_all;
36916b4cac81SBjoern A. Zeeb 
36926b4cac81SBjoern A. Zeeb 	IMPROVE();
36936b4cac81SBjoern A. Zeeb 
36946b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36958d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36966b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36978d58a057SBjoern A. Zeeb #endif
36986b4cac81SBjoern A. Zeeb 	start_all = false;
36996b4cac81SBjoern A. Zeeb 
37008ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
37018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
37026b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
37038d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37046b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
37056b4cac81SBjoern A. Zeeb 		if (error == 0)
37068d58a057SBjoern A. Zeeb #endif
37076b4cac81SBjoern A. Zeeb 			start_all = true;
37086b4cac81SBjoern A. Zeeb 	} else {
37098d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37107b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
37118d58a057SBjoern A. Zeeb #endif
37126b4cac81SBjoern A. Zeeb 	}
37138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
37148ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
37156b4cac81SBjoern A. Zeeb 
37166b4cac81SBjoern A. Zeeb 	if (start_all)
37176b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
37186b4cac81SBjoern A. Zeeb }
37196b4cac81SBjoern A. Zeeb 
37206b4cac81SBjoern A. Zeeb bool
37216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
37226b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
37236b4cac81SBjoern A. Zeeb {
37246b4cac81SBjoern A. Zeeb 	int i;
37256b4cac81SBjoern A. Zeeb 
37266b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
37276b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
37286b4cac81SBjoern A. Zeeb 			return (true);
37296b4cac81SBjoern A. Zeeb 	}
37306b4cac81SBjoern A. Zeeb 
37316b4cac81SBjoern A. Zeeb 	return (false);
37326b4cac81SBjoern A. Zeeb }
37336b4cac81SBjoern A. Zeeb 
37346b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
37356b4cac81SBjoern A. Zeeb bool
37366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
37376b4cac81SBjoern A. Zeeb {
37386b4cac81SBjoern A. Zeeb 	size_t x;
37396b4cac81SBjoern A. Zeeb 	uint8_t l;
37406b4cac81SBjoern A. Zeeb 
37416b4cac81SBjoern A. Zeeb 	x = *xp;
37426b4cac81SBjoern A. Zeeb 
37436b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
37446b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
37456b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
37466b4cac81SBjoern A. Zeeb 	x += 2 + l;
37476b4cac81SBjoern A. Zeeb 
37486b4cac81SBjoern A. Zeeb 	if (x > ies_len)
37496b4cac81SBjoern A. Zeeb 		return (false);
37506b4cac81SBjoern A. Zeeb 
37516b4cac81SBjoern A. Zeeb 	*xp = x;
37526b4cac81SBjoern A. Zeeb 	return (true);
37536b4cac81SBjoern A. Zeeb }
37546b4cac81SBjoern A. Zeeb 
3755d9945d78SBjoern A. Zeeb static uint8_t *
3756d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3757d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
37586b4cac81SBjoern A. Zeeb {
3759d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3760d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
37613dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3762d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3763d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3764d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3765d9945d78SBjoern A. Zeeb 	int band, i;
37666b4cac81SBjoern A. Zeeb 
37673dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3768d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3769d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3770d9945d78SBjoern A. Zeeb 			continue;
3771d9945d78SBjoern A. Zeeb 
3772d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3773d9945d78SBjoern A. Zeeb 		/*
3774d9945d78SBjoern A. Zeeb 		 * This should not happen;
3775d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3776d9945d78SBjoern A. Zeeb 		 */
3777d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3778d9945d78SBjoern A. Zeeb 			continue;
3779d9945d78SBjoern A. Zeeb 
3780d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3781d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3782d9945d78SBjoern A. Zeeb 		chan = NULL;
3783d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3784d9945d78SBjoern A. Zeeb 
3785d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3786d9945d78SBjoern A. Zeeb 				continue;
3787d9945d78SBjoern A. Zeeb 
37883dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3789d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3790d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3791d9945d78SBjoern A. Zeeb 				break;
3792d9945d78SBjoern A. Zeeb 		}
3793d9945d78SBjoern A. Zeeb 
3794d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3795d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3796d9945d78SBjoern A. Zeeb 			continue;
3797d9945d78SBjoern A. Zeeb 
3798d9945d78SBjoern A. Zeeb 		pb = p;
37993dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3800d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3801d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3802d9945d78SBjoern A. Zeeb 
38033dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
38043dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
38053dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
38063dd98026SBjoern A. Zeeb 
38073dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
38083dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
38093dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
38103dd98026SBjoern A. Zeeb 		}
38113dd98026SBjoern A. Zeeb #endif
38123dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
38133dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
38143dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
38153dd98026SBjoern A. Zeeb 
38163dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
38173dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
38183dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
38193dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
38203dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
38213dd98026SBjoern A. Zeeb 		}
38223dd98026SBjoern A. Zeeb #endif
38233dd98026SBjoern A. Zeeb 
3824d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3825d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3826d9945d78SBjoern A. Zeeb 	}
3827d9945d78SBjoern A. Zeeb 
3828d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3829d9945d78SBjoern A. Zeeb 	pb = p;
3830d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3831d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3832d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3833d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3834d9945d78SBjoern A. Zeeb 	}
3835d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3836d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3837d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3838d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3839d9945d78SBjoern A. Zeeb 	}
3840d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3841d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3842d9945d78SBjoern A. Zeeb 
3843d9945d78SBjoern A. Zeeb 	return (p);
38446b4cac81SBjoern A. Zeeb }
38456b4cac81SBjoern A. Zeeb 
38466b4cac81SBjoern A. Zeeb static void
38476b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
38486b4cac81SBjoern A. Zeeb {
38496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38506b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38516b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38526b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38536b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
38546b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
38556b4cac81SBjoern A. Zeeb 	int error;
38568ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
38576b4cac81SBjoern A. Zeeb 
38586b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3860a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
38616b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
38628ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38636b4cac81SBjoern A. Zeeb 		return;
38646b4cac81SBjoern A. Zeeb 	}
38658ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
38668ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38676b4cac81SBjoern A. Zeeb 
38686b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
38696b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
38706b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3871d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
38726b4cac81SBjoern A. Zeeb 		return;
38736b4cac81SBjoern A. Zeeb 	}
38746b4cac81SBjoern A. Zeeb 
38756b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38768ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3877a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3878a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3879d3ef7fb4SBjoern A. Zeeb sw_scan:
38806b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
38816b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3882086be6a8SBjoern A. Zeeb 
3883086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3884086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3885086be6a8SBjoern A. Zeeb 
38866b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
38876b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
38886b4cac81SBjoern A. Zeeb 		IMPROVE();
38896b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
38906b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
38916b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
38926b4cac81SBjoern A. Zeeb 
38936b4cac81SBjoern A. Zeeb 	} else {
38946b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
38956b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
38966b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
38976b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
38986b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3899d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3900d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3901d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
39023206587aSBjoern A. Zeeb 		bool running;
3903d9945d78SBjoern A. Zeeb 
3904d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3905d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
39066b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
39076b4cac81SBjoern A. Zeeb 
3908d9945d78SBjoern A. Zeeb 		band_mask = 0;
39096b4cac81SBjoern A. Zeeb 		nchan = 0;
3910c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3911c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3912d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
39136b4cac81SBjoern A. Zeeb 				nchan++;
3914d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3915d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3916d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3917d9945d78SBjoern A. Zeeb 			}
3918c272abc5SBjoern A. Zeeb #else
3919c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3920c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3921c272abc5SBjoern A. Zeeb 				switch (band) {
3922c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3923c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3924c272abc5SBjoern A. Zeeb 					break;
3925c272abc5SBjoern A. Zeeb 				default:
3926c272abc5SBjoern A. Zeeb 					continue;
3927c272abc5SBjoern A. Zeeb 				}
3928c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3929c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3930c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3931c272abc5SBjoern A. Zeeb 				}
3932c272abc5SBjoern A. Zeeb 			}
3933c272abc5SBjoern A. Zeeb #endif
3934c272abc5SBjoern A. Zeeb 		} else {
39353206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
39363206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
39373206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
39383206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
39393206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
39403206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
39413206587aSBjoern A. Zeeb 			 */
39423206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
39433206587aSBjoern A. Zeeb 		}
39443206587aSBjoern A. Zeeb 
39456b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
39466b4cac81SBjoern A. Zeeb 
3947d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3948d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3949d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3950d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3951d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3952d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3953d9945d78SBjoern A. Zeeb 
3954d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3955d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3956d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3957d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3958d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3959d9945d78SBjoern A. Zeeb 		}
3960d9945d78SBjoern A. Zeeb 
39613206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3962d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3963d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
39646b4cac81SBjoern A. Zeeb 
39656b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
39666b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
39676b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
39686b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
39696b4cac81SBjoern A. Zeeb #if 0
39706b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
39716b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
39726b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
39736b4cac81SBjoern A. Zeeb #endif
39746b4cac81SBjoern A. Zeeb #ifdef __notyet__
39756b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
39766b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
39776b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
39786b4cac81SBjoern A. Zeeb #endif
3979e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
39806b4cac81SBjoern A. Zeeb 
39816b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
39826b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
39836b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
39846b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
39856b4cac81SBjoern A. Zeeb 			*(cpp + i) =
39866b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
39876b4cac81SBjoern A. Zeeb 		}
3988c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
39896b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3990c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
39916b4cac81SBjoern A. Zeeb 
3992c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
39936b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
39943206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
39956b4cac81SBjoern A. Zeeb 			/* lc->flags */
39966b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
39976b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
39986b4cac81SBjoern A. Zeeb 			/* lc-> ... */
39996b4cac81SBjoern A. Zeeb 			lc++;
40006b4cac81SBjoern A. Zeeb 		}
4001c272abc5SBjoern A. Zeeb #else
4002c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4003c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4004c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4005c272abc5SBjoern A. Zeeb 
4006c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4007c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4008c272abc5SBjoern A. Zeeb 				continue;
4009c272abc5SBjoern A. Zeeb 
4010c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4011c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4012c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4013c272abc5SBjoern A. Zeeb 				continue;
4014c272abc5SBjoern A. Zeeb 
4015c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4016c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4017c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4018c272abc5SBjoern A. Zeeb 				lc++;
4019c272abc5SBjoern A. Zeeb 			}
4020c272abc5SBjoern A. Zeeb 		}
4021c272abc5SBjoern A. Zeeb #endif
40226b4cac81SBjoern A. Zeeb 
4023d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
40246b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
40256b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
40266b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4027d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
40286b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
40296b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
40306b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
40316b4cac81SBjoern A. Zeeb 				ssids++;
40326b4cac81SBjoern A. Zeeb 			}
40336b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
40346b4cac81SBjoern A. Zeeb 		} else {
40356b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
40366b4cac81SBjoern A. Zeeb 		}
40376b4cac81SBjoern A. Zeeb 
40386b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
40396b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
40406b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
40416b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
40426b4cac81SBjoern A. Zeeb 		/* s6gp->... */
40436b4cac81SBjoern A. Zeeb 
4044d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4045d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4046d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4047d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4048d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4049d9945d78SBjoern A. Zeeb 
40506b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40516b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
40523206587aSBjoern A. Zeeb 
40533206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
40543206587aSBjoern A. Zeeb 		/* Re-check under lock. */
40553206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
40563206587aSBjoern A. Zeeb 		if (!running) {
40573206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
40583206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
40593206587aSBjoern A. Zeeb 
40603206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
40613206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
40623206587aSBjoern A. Zeeb 		}
40633206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40643206587aSBjoern A. Zeeb 		if (running) {
40653206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
40663206587aSBjoern A. Zeeb 			return;
40673206587aSBjoern A. Zeeb 		}
40683206587aSBjoern A. Zeeb 
40696b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
40706b4cac81SBjoern A. Zeeb 		if (error != 0) {
4071d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4072d9945d78SBjoern A. Zeeb 
40733206587aSBjoern A. Zeeb 			/*
40743206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
40753206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
40763206587aSBjoern A. Zeeb 			 * and only there.
40773206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
40783206587aSBjoern A. Zeeb 			 * behave differently:
40793206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
40803206587aSBjoern A. Zeeb 			 *        and can then still return an error.
40813206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
40823206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
40833206587aSBjoern A. Zeeb 			 * ...
40843206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
40853206587aSBjoern A. Zeeb 			 * and balance between both code paths.
40863206587aSBjoern A. Zeeb 			 */
40873206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
40883206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40893206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
40906b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
40913206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
40923206587aSBjoern A. Zeeb 			}
40933206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4094d3ef7fb4SBjoern A. Zeeb 
4095d3ef7fb4SBjoern A. Zeeb 			/*
4096d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4097d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4098d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4099d3ef7fb4SBjoern A. Zeeb 			 */
4100196cfd0bSBjoern A. Zeeb 			if (error == 1) {
41018ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4102a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
41038ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41043206587aSBjoern A. Zeeb 				/*
41053206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
41063206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
41073206587aSBjoern A. Zeeb 				 * currently not re-enable it?
41083206587aSBjoern A. Zeeb 				 */
41093206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4110196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4111196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4112196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4113196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4114196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4115196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4116196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4117196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4118d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4119196cfd0bSBjoern A. Zeeb 			}
4120d3ef7fb4SBjoern A. Zeeb 
4121d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4122d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
41236b4cac81SBjoern A. Zeeb 		}
41246b4cac81SBjoern A. Zeeb 	}
41256b4cac81SBjoern A. Zeeb }
41266b4cac81SBjoern A. Zeeb 
41276b4cac81SBjoern A. Zeeb static void
41286b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
41296b4cac81SBjoern A. Zeeb {
41306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41318ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
41326b4cac81SBjoern A. Zeeb 
41336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4135a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
41368ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41376b4cac81SBjoern A. Zeeb 		return;
41386b4cac81SBjoern A. Zeeb 	}
41398ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41408ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41416b4cac81SBjoern A. Zeeb 
41428ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4143a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4144a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
41456b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
41466b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
41476b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
41486b4cac81SBjoern A. Zeeb 
4149a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4150a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
41516b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
41526b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41536b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4154a486fbbdSBjoern A. Zeeb 
41556b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
41566b4cac81SBjoern A. Zeeb 
41576b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4158086be6a8SBjoern A. Zeeb 
4159086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4160086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
41616b4cac81SBjoern A. Zeeb 	}
41626b4cac81SBjoern A. Zeeb }
41636b4cac81SBjoern A. Zeeb 
41646b4cac81SBjoern A. Zeeb static void
4165a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4166a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4167a486fbbdSBjoern A. Zeeb {
4168a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41698ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4170a486fbbdSBjoern A. Zeeb 
4171a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41728ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41738ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41748ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41758ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4176a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4177a486fbbdSBjoern A. Zeeb }
4178a486fbbdSBjoern A. Zeeb 
4179a486fbbdSBjoern A. Zeeb static void
4180a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4181a486fbbdSBjoern A. Zeeb {
4182a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41838ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4184a486fbbdSBjoern A. Zeeb 
4185a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41878ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41898ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4190a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4191a486fbbdSBjoern A. Zeeb }
4192a486fbbdSBjoern A. Zeeb 
4193a486fbbdSBjoern A. Zeeb static void
41946b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
41956b4cac81SBjoern A. Zeeb {
41966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41976b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4198b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4199b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
42006b4cac81SBjoern A. Zeeb 	int error;
42018ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
42026b4cac81SBjoern A. Zeeb 
42036b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4204b2cf3c21SBjoern A. Zeeb 
4205b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4206b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
42076b4cac81SBjoern A. Zeeb 		return;
42086b4cac81SBjoern A. Zeeb 
4209a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
42108ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
42118ac540d3SBjoern A. Zeeb 	hw_scan_running =
42128ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
42138ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
42148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42158ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4216a486fbbdSBjoern A. Zeeb 		return;
4217a486fbbdSBjoern A. Zeeb 
4218b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4219b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
42206b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
42216b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
42226b4cac81SBjoern A. Zeeb 		return;
42236b4cac81SBjoern A. Zeeb 	}
42246b4cac81SBjoern A. Zeeb 
42256b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
42266b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
42276b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
42286b4cac81SBjoern A. Zeeb 		    c, chan);
42296b4cac81SBjoern A. Zeeb 		return;
42306b4cac81SBjoern A. Zeeb 	}
42316b4cac81SBjoern A. Zeeb 
42326b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
42336b4cac81SBjoern A. Zeeb 	IMPROVE();
42346b4cac81SBjoern A. Zeeb 
42356b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42364a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
42379fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
423811450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
42399fb91463SBjoern A. Zeeb #endif
42404a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
42416b4cac81SBjoern A. Zeeb 
42426b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
42436b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
42446b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
42456b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
42466b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
42476b4cac81SBjoern A. Zeeb 		IMPROVE();
42486b4cac81SBjoern A. Zeeb 	} else {
42496b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
42506b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
42516b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
42526b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
42536b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
42546b4cac81SBjoern A. Zeeb 	}
42556b4cac81SBjoern A. Zeeb 
42566b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
42576b4cac81SBjoern A. Zeeb 	IMPROVE();
42586b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
42596b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
42606b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
42616b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
42626b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
42636b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
42646b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
42656b4cac81SBjoern A. Zeeb 			    error);
42666b4cac81SBjoern A. Zeeb 	}
42676b4cac81SBjoern A. Zeeb }
42686b4cac81SBjoern A. Zeeb 
42696b4cac81SBjoern A. Zeeb static struct ieee80211_node *
42706b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
42716b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
42726b4cac81SBjoern A. Zeeb {
42736b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42754f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
42766b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
42776b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42786b4cac81SBjoern A. Zeeb 
42796b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
42806b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42816b4cac81SBjoern A. Zeeb 
42826b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
42834f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
42844f61ef8bSBjoern A. Zeeb 		return (NULL);
42854f61ef8bSBjoern A. Zeeb 
42866b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
42876b4cac81SBjoern A. Zeeb 	if (ni == NULL)
42886b4cac81SBjoern A. Zeeb 		return (NULL);
42896b4cac81SBjoern A. Zeeb 
42906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42914f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
42926b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
42936b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
42946b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
42956b4cac81SBjoern A. Zeeb 		return (NULL);
42966b4cac81SBjoern A. Zeeb 	}
42976b4cac81SBjoern A. Zeeb 
42986b4cac81SBjoern A. Zeeb 	return (ni);
42996b4cac81SBjoern A. Zeeb }
43006b4cac81SBjoern A. Zeeb 
43016b4cac81SBjoern A. Zeeb static int
43026b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
43036b4cac81SBjoern A. Zeeb {
43046b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43056b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43066b4cac81SBjoern A. Zeeb 	int error;
43076b4cac81SBjoern A. Zeeb 
43086b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43096b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43106b4cac81SBjoern A. Zeeb 
43116b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
43126b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
43136b4cac81SBjoern A. Zeeb 		if (error != 0)
43146b4cac81SBjoern A. Zeeb 			return (error);
43156b4cac81SBjoern A. Zeeb 	}
43166b4cac81SBjoern A. Zeeb 
43176b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
43186b4cac81SBjoern A. Zeeb 	IMPROVE();
43196b4cac81SBjoern A. Zeeb 
43206b4cac81SBjoern A. Zeeb 	return (0);
43216b4cac81SBjoern A. Zeeb }
43226b4cac81SBjoern A. Zeeb 
43236b4cac81SBjoern A. Zeeb static void
43246b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
43256b4cac81SBjoern A. Zeeb {
43266b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43286b4cac81SBjoern A. Zeeb 
43296b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43306b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43316b4cac81SBjoern A. Zeeb 
43326b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
43336b4cac81SBjoern A. Zeeb 	IMPROVE();
43346b4cac81SBjoern A. Zeeb 
43356b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
43366b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
43376b4cac81SBjoern A. Zeeb }
43386b4cac81SBjoern A. Zeeb 
43396b4cac81SBjoern A. Zeeb static void
43406b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
43416b4cac81SBjoern A. Zeeb {
43426b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43436b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43456b4cac81SBjoern A. Zeeb 
43466b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43476b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43486b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
43496b4cac81SBjoern A. Zeeb 
43500936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
43516b4cac81SBjoern A. Zeeb 
43520936c648SBjoern A. Zeeb 	/*
43530936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
43540936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
43550936c648SBjoern A. Zeeb 	 */
43560936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
43576b4cac81SBjoern A. Zeeb 
43586b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
43596b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
43606b4cac81SBjoern A. Zeeb }
43616b4cac81SBjoern A. Zeeb 
43626b4cac81SBjoern A. Zeeb static int
43636b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
43646b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
43656b4cac81SBjoern A. Zeeb {
43666b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43676b4cac81SBjoern A. Zeeb 
43686b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4369fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
43702372f8ccSBjoern A. Zeeb #if 0
437188665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
43722372f8ccSBjoern A. Zeeb #else
43732372f8ccSBjoern A. Zeeb 	/*
43742372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
43752372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
43762372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
43772372f8ccSBjoern A. Zeeb 	 */
43782372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
43792372f8ccSBjoern A. Zeeb #endif
4380fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4381fa4e4257SBjoern A. Zeeb 		/*
4382fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4383fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4384fa4e4257SBjoern A. Zeeb 		 */
43850936c648SBjoern A. Zeeb 		m_free(m);
43860936c648SBjoern A. Zeeb 		return (ENETDOWN);
43870936c648SBjoern A. Zeeb 	}
43886b4cac81SBjoern A. Zeeb 
43896b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
43906b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4391fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4392fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
43936b4cac81SBjoern A. Zeeb 
43949d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
43959d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
43966b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
43976b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
43986b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
43999d9ba2b7SBjoern A. Zeeb #endif
44006b4cac81SBjoern A. Zeeb 
44016b4cac81SBjoern A. Zeeb 	return (0);
44026b4cac81SBjoern A. Zeeb }
44036b4cac81SBjoern A. Zeeb 
440411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
440511db70b6SBjoern A. Zeeb static int
440611db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
440711db70b6SBjoern A. Zeeb     struct sk_buff *skb)
440811db70b6SBjoern A. Zeeb {
440911db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
441011db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
441111db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
441211db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
441311db70b6SBjoern A. Zeeb 	uint8_t *p;
441411db70b6SBjoern A. Zeeb 
441511db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
441611db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
441711db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
441811db70b6SBjoern A. Zeeb 
441911db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
442011db70b6SBjoern A. Zeeb 
442111db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
442211db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
442311db70b6SBjoern A. Zeeb 
442411db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
442511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
442611db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
442711db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
442811db70b6SBjoern A. Zeeb 		return (ENXIO);
442911db70b6SBjoern A. Zeeb 	}
443011db70b6SBjoern A. Zeeb 
443111db70b6SBjoern A. Zeeb 
443211db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
443311db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
443411db70b6SBjoern A. Zeeb 
443511db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
443611db70b6SBjoern A. Zeeb 
443711db70b6SBjoern A. Zeeb 	/*
443811db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
443911db70b6SBjoern A. Zeeb 	 * or if we are done?
444011db70b6SBjoern A. Zeeb 	 */
444111db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
444211db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
444311db70b6SBjoern A. Zeeb 	    /* MFP */
444411db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
444511db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
444611db70b6SBjoern A. Zeeb 			return (0);
444711db70b6SBjoern A. Zeeb 
444811db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
444911db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
445011db70b6SBjoern A. Zeeb 		return (ENOSPC);
445111db70b6SBjoern A. Zeeb 
445211db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
445311db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
445411db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
445511db70b6SBjoern A. Zeeb 
445611db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
445711db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
445811db70b6SBjoern A. Zeeb 		return (0);
445911db70b6SBjoern A. Zeeb 
446011db70b6SBjoern A. Zeeb 	p += hdrlen;
446111db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
446211db70b6SBjoern A. Zeeb 
446311db70b6SBjoern A. Zeeb 	return (0);
446411db70b6SBjoern A. Zeeb }
446511db70b6SBjoern A. Zeeb #endif
446611db70b6SBjoern A. Zeeb 
44676b4cac81SBjoern A. Zeeb static void
44686b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
44696b4cac81SBjoern A. Zeeb {
44706b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
44716b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
44726b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
44736b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
44746b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44756b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44766b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44776b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
44786b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
44796b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
44806b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
44816b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
44826b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4483e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4484ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
44856b4cac81SBjoern A. Zeeb 	void *buf;
448611db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4487e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
44886b4cac81SBjoern A. Zeeb 
44896b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
44906b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
44919d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
44926b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
44936b4cac81SBjoern A. Zeeb #endif
44946b4cac81SBjoern A. Zeeb 
44956b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4496b35f6cd0SBjoern A. Zeeb 	k = NULL;
449711db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
44986b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
44996b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
450011db70b6SBjoern A. Zeeb 
450111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
450211db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
450311db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
450411db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
450511db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
450611db70b6SBjoern A. Zeeb 		}
450711db70b6SBjoern A. Zeeb #endif
450811db70b6SBjoern A. Zeeb 
450911db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
451011db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
45116b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
45126b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
45136b4cac81SBjoern A. Zeeb 			if (k == NULL) {
45146b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
45156b4cac81SBjoern A. Zeeb 				m_freem(m);
45166b4cac81SBjoern A. Zeeb 				return;
45176b4cac81SBjoern A. Zeeb 			}
45186b4cac81SBjoern A. Zeeb 		}
451911db70b6SBjoern A. Zeeb 	}
45206b4cac81SBjoern A. Zeeb 
45216b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45226b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45246b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
45256b4cac81SBjoern A. Zeeb 
45266b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
45276b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
45286b4cac81SBjoern A. Zeeb 
45296b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
45306b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
45316b4cac81SBjoern A. Zeeb 		if (k != NULL)
45326b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
45336b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
45346b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
45356b4cac81SBjoern A. Zeeb 		IMPROVE();
45366b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
45376b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
45386b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
45396b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
45406b4cac81SBjoern A. Zeeb 		}
45416b4cac81SBjoern A. Zeeb 
45426b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
45436b4cac81SBjoern A. Zeeb 	}
45446b4cac81SBjoern A. Zeeb 
45456b4cac81SBjoern A. Zeeb 	/*
45466b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
45476b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
45483d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
45493d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
45506b4cac81SBjoern A. Zeeb 	 */
45516b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
45526b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4553bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4554bcf1d8eeSBjoern A. Zeeb 
4555bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4556bcf1d8eeSBjoern A. Zeeb 			int tid;
4557bcf1d8eeSBjoern A. Zeeb 
4558bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4559bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4560bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4561bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4562bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4563bcf1d8eeSBjoern A. Zeeb 					continue;
4564bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4565bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4566bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4567bcf1d8eeSBjoern A. Zeeb 			}
4568bcf1d8eeSBjoern A. Zeeb 		}
45696b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
45706b4cac81SBjoern A. Zeeb 		m_freem(m);
45716b4cac81SBjoern A. Zeeb 		return;
45726b4cac81SBjoern A. Zeeb 	}
45736b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
45746b4cac81SBjoern A. Zeeb 
45756b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
45766b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
45776b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
45786b4cac81SBjoern A. Zeeb 	skb->m = m;
45796b4cac81SBjoern A. Zeeb #if 0
45806b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
45816b4cac81SBjoern A. Zeeb #else
45826b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
45836b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
45846b4cac81SBjoern A. Zeeb #endif
45856b4cac81SBjoern A. Zeeb 	/* Save the ni. */
45866b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
45876b4cac81SBjoern A. Zeeb 
45886b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
45896b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
45906b4cac81SBjoern A. Zeeb 
4591e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4592e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4593e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
45941665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
45951665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
45961665ef97SBjoern A. Zeeb 			skb->priority = 7;
45971665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
45981665ef97SBjoern A. Zeeb 		} else {
45991665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
46001665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4601e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4602e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
46031665ef97SBjoern A. Zeeb 		}
4604e3a0b120SBjoern A. Zeeb 	} else {
4605e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4606fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4607e3a0b120SBjoern A. Zeeb 	}
46086b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
46096b4cac81SBjoern A. Zeeb 
46106b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
46116b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
46126b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
46136b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
46146b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
46156b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4616e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
46176b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
46186b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
46196b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
46206b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
46219fb91463SBjoern A. Zeeb #ifdef __notyet__
46229fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46239fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
46249fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
46259fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
46269fb91463SBjoern A. Zeeb #endif
46279fb91463SBjoern A. Zeeb #endif
46286b4cac81SBjoern A. Zeeb 
46296b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4630b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
463111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
463211db70b6SBjoern A. Zeeb 		int error;
463311db70b6SBjoern A. Zeeb 
463411db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
463511db70b6SBjoern A. Zeeb 		if (error != 0) {
463611db70b6SBjoern A. Zeeb 			/*
463711db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
463811db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
463911db70b6SBjoern A. Zeeb 			 */
464011db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
464111db70b6SBjoern A. Zeeb 			return;
464211db70b6SBjoern A. Zeeb 		}
464311db70b6SBjoern A. Zeeb 	}
46446b4cac81SBjoern A. Zeeb #endif
46456b4cac81SBjoern A. Zeeb 
46466b4cac81SBjoern A. Zeeb 	IMPROVE();
46476b4cac81SBjoern A. Zeeb 
4648e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4649e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4650e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4651e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4652e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4653d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4654e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4655e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4656e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4657e3a0b120SBjoern A. Zeeb 	}
4658e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4659d0d29110SBjoern A. Zeeb 		goto ops_tx;
4660e3a0b120SBjoern A. Zeeb 
4661d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4662d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4663d0d29110SBjoern A. Zeeb 
4664eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
46656b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
46669d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46679d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4668d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4669d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4670d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4671fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4672d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4673d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4674d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4675d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
46769d9ba2b7SBjoern A. Zeeb #endif
4677eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
467845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4679d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
468045bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46816b4cac81SBjoern A. Zeeb 	return;
46826b4cac81SBjoern A. Zeeb 
46836b4cac81SBjoern A. Zeeb ops_tx:
46849d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46859d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4686d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4687d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
46886b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
46896b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
46909d9ba2b7SBjoern A. Zeeb #endif
46916b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
46926b4cac81SBjoern A. Zeeb 	control.sta = sta;
469345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
46946b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
469545bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46966b4cac81SBjoern A. Zeeb }
46976b4cac81SBjoern A. Zeeb 
46986b4cac81SBjoern A. Zeeb static void
46996b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
47006b4cac81SBjoern A. Zeeb {
47016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
47026b4cac81SBjoern A. Zeeb 	struct mbufq mq;
47036b4cac81SBjoern A. Zeeb 	struct mbuf *m;
470488665349SBjoern A. Zeeb 	bool shall_tx;
47056b4cac81SBjoern A. Zeeb 
47066b4cac81SBjoern A. Zeeb 	lsta = ctx;
47076b4cac81SBjoern A. Zeeb 
47089d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47099d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
47106b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
47119d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
47126b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
47139d9ba2b7SBjoern A. Zeeb #endif
47146b4cac81SBjoern A. Zeeb 
47156b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
47166b4cac81SBjoern A. Zeeb 
4717fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4718fa4e4257SBjoern A. Zeeb 	/*
4719fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
472088665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
472188665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
472288665349SBjoern A. Zeeb 	 * point to try to TX.
472388665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
472488665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4725fa4e4257SBjoern A. Zeeb 	 */
47262372f8ccSBjoern A. Zeeb #if 0
472788665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
47282372f8ccSBjoern A. Zeeb #else
47292372f8ccSBjoern A. Zeeb 	/*
47302372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
47312372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
47322372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
47332372f8ccSBjoern A. Zeeb 	 */
47342372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
47352372f8ccSBjoern A. Zeeb #endif
473688665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
47376b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
473888665349SBjoern A. Zeeb 	/*
473988665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
474088665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
474188665349SBjoern A. Zeeb 	 */
4742fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47436b4cac81SBjoern A. Zeeb 
47446b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
47456b4cac81SBjoern A. Zeeb 	while (m != NULL) {
47466b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
47476b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
47486b4cac81SBjoern A. Zeeb 	}
47496b4cac81SBjoern A. Zeeb }
47506b4cac81SBjoern A. Zeeb 
47516b4cac81SBjoern A. Zeeb static int
47526b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
47536b4cac81SBjoern A. Zeeb {
47546b4cac81SBjoern A. Zeeb 
47556b4cac81SBjoern A. Zeeb 	/* XXX TODO */
47566b4cac81SBjoern A. Zeeb 	IMPROVE();
47576b4cac81SBjoern A. Zeeb 
47586b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
47596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47606b4cac81SBjoern A. Zeeb 
47616b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
47626b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
47636b4cac81SBjoern A. Zeeb }
47646b4cac81SBjoern A. Zeeb 
47659fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
47669fb91463SBjoern A. Zeeb static int
47679fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
47689fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
47699fb91463SBjoern A. Zeeb {
47709fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47719fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47729fb91463SBjoern A. Zeeb 
47739fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47749fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47759fb91463SBjoern A. Zeeb 
4776310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
47779fb91463SBjoern A. Zeeb 
47789fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
47799fb91463SBjoern A. Zeeb }
47809fb91463SBjoern A. Zeeb 
47819fb91463SBjoern A. Zeeb static int
47829fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
47839fb91463SBjoern A. Zeeb {
47849fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47859fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47869fb91463SBjoern A. Zeeb 
47879fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47889fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47899fb91463SBjoern A. Zeeb 
4790310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
47919fb91463SBjoern A. Zeeb 
47929fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
47939fb91463SBjoern A. Zeeb }
47949fb91463SBjoern A. Zeeb 
47959fb91463SBjoern A. Zeeb 
47969fb91463SBjoern A. Zeeb static int
47979fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
47989fb91463SBjoern A. Zeeb {
47999fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48009fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48019fb91463SBjoern A. Zeeb 
48029fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48039fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
48049fb91463SBjoern A. Zeeb 
4805310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
48069fb91463SBjoern A. Zeeb 
48079fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
48089fb91463SBjoern A. Zeeb }
48099fb91463SBjoern A. Zeeb 
4810310743c4SBjoern A. Zeeb /*
4811310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4812310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4813310743c4SBjoern A. Zeeb  *
4814310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4815310743c4SBjoern A. Zeeb  */
48169fb91463SBjoern A. Zeeb static int
48179fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48189fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
48199fb91463SBjoern A. Zeeb {
48209fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48219fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4822310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4823310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4824310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4825310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4826310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4827310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4828310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4829310743c4SBjoern A. Zeeb 	int error;
48309fb91463SBjoern A. Zeeb 
48319fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48329fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4833310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4834310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4835310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4836310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4837310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4838310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48399fb91463SBjoern A. Zeeb 
4840310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4841310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4842310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4843310743c4SBjoern A. Zeeb 		return (0);
4844310743c4SBjoern A. Zeeb 	}
4845310743c4SBjoern A. Zeeb 
4846310743c4SBjoern A. Zeeb 	params.sta = sta;
4847310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4848310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4849310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4850310743c4SBjoern A. Zeeb 	params.timeout = 0;
4851310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4852310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4853310743c4SBjoern A. Zeeb 	params.amsdu = false;
4854310743c4SBjoern A. Zeeb 
4855310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4856310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4857310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4858310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4859310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4860310743c4SBjoern A. Zeeb 	if (error != 0) {
4861310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4862310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4863310743c4SBjoern A. Zeeb 		return (0);
4864310743c4SBjoern A. Zeeb 	}
48659fb91463SBjoern A. Zeeb 
48669fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
48679fb91463SBjoern A. Zeeb }
48689fb91463SBjoern A. Zeeb 
4869310743c4SBjoern A. Zeeb /*
4870310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4871310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4872310743c4SBjoern A. Zeeb  *
4873310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4874310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4875310743c4SBjoern A. Zeeb  */
48769fb91463SBjoern A. Zeeb static int
48779fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48789fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
48799fb91463SBjoern A. Zeeb {
48809fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48819fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4882310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4883310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4884310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4885310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4886310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4887310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4888310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4889310743c4SBjoern A. Zeeb 	int error;
48909fb91463SBjoern A. Zeeb 
48919fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48929fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4893310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4894310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4895310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4896310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4897310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4898310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48999fb91463SBjoern A. Zeeb 
4900310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4901310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4902310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4903310743c4SBjoern A. Zeeb 		return (0);
4904310743c4SBjoern A. Zeeb 	}
4905310743c4SBjoern A. Zeeb 
4906310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4907310743c4SBjoern A. Zeeb 		params.sta = sta;
4908310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4909310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4910310743c4SBjoern A. Zeeb 		params.timeout = 0;
4911310743c4SBjoern A. Zeeb 		params.ssn = 0;
4912310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4913310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4914310743c4SBjoern A. Zeeb 			params.amsdu = true;
4915310743c4SBjoern A. Zeeb 		else
4916310743c4SBjoern A. Zeeb 			params.amsdu = false;
4917310743c4SBjoern A. Zeeb 	} else {
4918310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4919310743c4SBjoern A. Zeeb 		params.sta = sta;
4920310743c4SBjoern A. Zeeb 		switch (status) {
4921310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4922310743c4SBjoern A. Zeeb 		default:
4923310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4924310743c4SBjoern A. Zeeb 			break;
4925310743c4SBjoern A. Zeeb 		}
4926310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4927310743c4SBjoern A. Zeeb 		params.timeout = 0;
4928310743c4SBjoern A. Zeeb 		params.ssn = 0;
4929310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4930310743c4SBjoern A. Zeeb 		params.amsdu = false;
4931310743c4SBjoern A. Zeeb 	}
4932310743c4SBjoern A. Zeeb 
4933310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4934310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4935310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4936310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4937310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4938310743c4SBjoern A. Zeeb 	if (error != 0) {
4939310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4940310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4941310743c4SBjoern A. Zeeb 		return (0);
4942310743c4SBjoern A. Zeeb 	}
4943310743c4SBjoern A. Zeeb 
4944310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
49459fb91463SBjoern A. Zeeb 
49469fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
49479fb91463SBjoern A. Zeeb }
49489fb91463SBjoern A. Zeeb 
4949310743c4SBjoern A. Zeeb /*
4950310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4951310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4952310743c4SBjoern A. Zeeb  */
49539fb91463SBjoern A. Zeeb static void
49549fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49559fb91463SBjoern A. Zeeb {
49569fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49579fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4958310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4959310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4960310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4961310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4962310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4963310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4964310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4965310743c4SBjoern A. Zeeb 	int error;
49669fb91463SBjoern A. Zeeb 
49679fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49689fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4969310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4970310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4971310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4972310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4973310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4974310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49759fb91463SBjoern A. Zeeb 
4976310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4977310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4978310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4979310743c4SBjoern A. Zeeb 		goto n80211;
4980310743c4SBjoern A. Zeeb 	}
49819fb91463SBjoern A. Zeeb 
4982310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4983310743c4SBjoern A. Zeeb 	params.sta = sta;
4984310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4985310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4986310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4987310743c4SBjoern A. Zeeb 	params.timeout = 0;
4988310743c4SBjoern A. Zeeb 	params.ssn = 0;
4989310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4990310743c4SBjoern A. Zeeb 	params.amsdu = false;
4991310743c4SBjoern A. Zeeb 
4992310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4993310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4994310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4995310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4996310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4997310743c4SBjoern A. Zeeb 	if (error != 0) {
4998310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4999310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5000310743c4SBjoern A. Zeeb 		goto n80211;
5001310743c4SBjoern A. Zeeb 	}
5002310743c4SBjoern A. Zeeb 
5003310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5004310743c4SBjoern A. Zeeb 
5005310743c4SBjoern A. Zeeb n80211:
50069fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
50079fb91463SBjoern A. Zeeb }
50089fb91463SBjoern A. Zeeb 
50099fb91463SBjoern A. Zeeb static void
50109fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
50119fb91463SBjoern A. Zeeb {
50129fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50139fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50149fb91463SBjoern A. Zeeb 
50159fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50169fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50179fb91463SBjoern A. Zeeb 
50189fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50199fb91463SBjoern A. Zeeb 
50209fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
50219fb91463SBjoern A. Zeeb }
50229fb91463SBjoern A. Zeeb 
50239fb91463SBjoern A. Zeeb static void
50249fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
50259fb91463SBjoern A. Zeeb     int status)
50269fb91463SBjoern A. Zeeb {
50279fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50289fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50299fb91463SBjoern A. Zeeb 
50309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50329fb91463SBjoern A. Zeeb 
50339fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50349fb91463SBjoern A. Zeeb 
50359fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
50369fb91463SBjoern A. Zeeb }
50379fb91463SBjoern A. Zeeb 
50389fb91463SBjoern A. Zeeb static int
50399fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
50409fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
50419fb91463SBjoern A. Zeeb {
50429fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50439fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50449fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50459fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50469fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50479fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50489fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50499fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5050310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50519fb91463SBjoern A. Zeeb 	int error;
50529fb91463SBjoern A. Zeeb 
50539fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50549fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50559fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
50569fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
50579fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
50589fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50599fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
50609fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50619fb91463SBjoern A. Zeeb 
5062310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5063310743c4SBjoern A. Zeeb 
5064310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5065310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5066310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5067310743c4SBjoern A. Zeeb 		return (-ENXIO);
5068310743c4SBjoern A. Zeeb 	}
5069310743c4SBjoern A. Zeeb 
50709fb91463SBjoern A. Zeeb 	params.sta = sta;
50719fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
50729fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
50739fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
50749fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
50759fb91463SBjoern A. Zeeb 	else
50769fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5077310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5078310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
50799fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
50809fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
50819fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
50829fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5083310743c4SBjoern A. Zeeb 
5084310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5085310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5086310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5087310743c4SBjoern A. Zeeb 		params.amsdu = true;
5088310743c4SBjoern A. Zeeb 	else
50899fb91463SBjoern A. Zeeb 		params.amsdu = false;
50909fb91463SBjoern A. Zeeb 
5091310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
50929fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5093310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
50949fb91463SBjoern A. Zeeb 	if (error != 0) {
50959fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
50969fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50979fb91463SBjoern A. Zeeb 		return (error);
50989fb91463SBjoern A. Zeeb 	}
5099310743c4SBjoern A. Zeeb 
5100310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5101310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5102310743c4SBjoern A. Zeeb 	}
5103310743c4SBjoern A. Zeeb 
51049fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
51059fb91463SBjoern A. Zeeb 
51069fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
51079fb91463SBjoern A. Zeeb 	return (error);
51089fb91463SBjoern A. Zeeb }
51099fb91463SBjoern A. Zeeb 
51109fb91463SBjoern A. Zeeb static void
51119fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
51129fb91463SBjoern A. Zeeb {
51139fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51149fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51159fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
51169fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
51179fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51189fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
51199fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51209fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5121310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
51229fb91463SBjoern A. Zeeb 	int error;
51239fb91463SBjoern A. Zeeb 	uint8_t tid;
512465c573e4SBjoern A. Zeeb 	bool ic_locked;
51259fb91463SBjoern A. Zeeb 
51269fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51279fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51289fb91463SBjoern A. Zeeb 
51299fb91463SBjoern A. Zeeb 	/*
51309fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
51319fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
51329fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
51339fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
51349fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
51359fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
51369fb91463SBjoern A. Zeeb 	 * track some state itself.
51379fb91463SBjoern A. Zeeb 	 */
51389fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
51399fb91463SBjoern A. Zeeb 		goto net80211_only;
51409fb91463SBjoern A. Zeeb 
51419fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51429fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
51439fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
51449fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
51459fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
51469fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51479fb91463SBjoern A. Zeeb 
51489fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
51499fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
51509fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
51519fb91463SBjoern A. Zeeb 			break;
51529fb91463SBjoern A. Zeeb 	}
51539fb91463SBjoern A. Zeeb 
51549fb91463SBjoern A. Zeeb 	params.sta = sta;
51559fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
51569fb91463SBjoern A. Zeeb 	params.buf_size = 0;
51579fb91463SBjoern A. Zeeb 	params.timeout = 0;
51589fb91463SBjoern A. Zeeb 	params.ssn = 0;
51599fb91463SBjoern A. Zeeb 	params.tid = tid;
51609fb91463SBjoern A. Zeeb 	params.amsdu = false;
51619fb91463SBjoern A. Zeeb 
516265c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
516365c573e4SBjoern A. Zeeb 	if (ic_locked)
516465c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5165310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
51669fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5167310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
516865c573e4SBjoern A. Zeeb 	if (ic_locked)
516965c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
51709fb91463SBjoern A. Zeeb 	if (error != 0)
51719fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
51729fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
51739fb91463SBjoern A. Zeeb 
51749fb91463SBjoern A. Zeeb net80211_only:
51759fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
51769fb91463SBjoern A. Zeeb }
51779fb91463SBjoern A. Zeeb #endif
51789fb91463SBjoern A. Zeeb 
51799fb91463SBjoern A. Zeeb static void
51809fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
51819fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
51829fb91463SBjoern A. Zeeb {
51839fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
51849fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
51859fb91463SBjoern A. Zeeb 
51869fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
51879fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
51889fb91463SBjoern A. Zeeb 		return;
51899fb91463SBjoern A. Zeeb 
51909fb91463SBjoern A. Zeeb 	switch (band) {
51919fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
51929fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
51939fb91463SBjoern A. Zeeb 		break;
51949fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
51959fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
51969fb91463SBjoern A. Zeeb 		break;
51979fb91463SBjoern A. Zeeb 	default:
51989fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
51999fb91463SBjoern A. Zeeb 		return;
52009fb91463SBjoern A. Zeeb 	}
52019fb91463SBjoern A. Zeeb 
52029fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
52039fb91463SBjoern A. Zeeb 
52049fb91463SBjoern A. Zeeb 	/*
52059fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
52069fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
52079fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
52089fb91463SBjoern A. Zeeb 	 */
52099fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
52109fb91463SBjoern A. Zeeb 
52119fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
52129fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
52139fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
52149fb91463SBjoern A. Zeeb #ifdef __notyet__
52159fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
52169fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
52179fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
52189fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
52199fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
52209fb91463SBjoern A. Zeeb #endif
52219fb91463SBjoern A. Zeeb 
52229fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
52239fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
52249fb91463SBjoern A. Zeeb 
52259fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
52269fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
52279fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
52289fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
52299fb91463SBjoern A. Zeeb #endif
52309fb91463SBjoern A. Zeeb }
52319fb91463SBjoern A. Zeeb 
52326b4cac81SBjoern A. Zeeb static void
52336b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
52346b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
52356b4cac81SBjoern A. Zeeb {
52366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52376b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52386b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
52396b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
52406b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
52416b4cac81SBjoern A. Zeeb 
52426b4cac81SBjoern A. Zeeb 	/* Channels */
52436b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
52446b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
52456b4cac81SBjoern A. Zeeb 
52466b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
52476b4cac81SBjoern A. Zeeb 	nchans = 0;
52486b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
52496b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
52506b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52516b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52526b4cac81SBjoern A. Zeeb 		chan_flags = 0;
52536b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
52546b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
52559fb91463SBjoern A. Zeeb 
52569fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
52576b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
52589fb91463SBjoern A. Zeeb 
52599fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
52609fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
52616b4cac81SBjoern A. Zeeb 
52626b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5263cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52646b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
52656b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
52666b4cac81SBjoern A. Zeeb 
52676b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
52683f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
52693f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
52706b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
52716b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
52726b4cac81SBjoern A. Zeeb 				continue;
52736b4cac81SBjoern A. Zeeb 			}
52746b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
52756b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
52766b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
52776b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
52786b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
52796b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
52806b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
52816b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
52826b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
52836b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
52846b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
52856b4cac81SBjoern A. Zeeb 
52866b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
52876b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
52886b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
52895856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5290cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5291cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
52923f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
52933f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
52946b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
52956b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
52966b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5297cee56e77SBjoern A. Zeeb 			if (error != 0)
52986b4cac81SBjoern A. Zeeb 				break;
52996b4cac81SBjoern A. Zeeb 		}
53006b4cac81SBjoern A. Zeeb 	}
53016b4cac81SBjoern A. Zeeb 
53026b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
53036b4cac81SBjoern A. Zeeb 	nchans = 0;
53046b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
53056b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
53066b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
53076b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
53086b4cac81SBjoern A. Zeeb 		chan_flags = 0;
53096b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
53109fb91463SBjoern A. Zeeb 
53119fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
53129fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
53139fb91463SBjoern A. Zeeb 
53149fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
53156b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
53166b4cac81SBjoern A. Zeeb 
53176b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5318562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
53196b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5320ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5321ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
53226b4cac81SBjoern A. Zeeb 
53236b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
53246b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
53256b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5326562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53276b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
53286b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5329562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53306b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
53316b4cac81SBjoern A. Zeeb 		}
53326b4cac81SBjoern A. Zeeb #endif
53336b4cac81SBjoern A. Zeeb 
53346b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5335cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
53366b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
53376b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
53386b4cac81SBjoern A. Zeeb 
53396b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
53403f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
53413f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
53426b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
53436b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
53446b4cac81SBjoern A. Zeeb 				continue;
53456b4cac81SBjoern A. Zeeb 			}
53466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
53476b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
53486b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
53496b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
53506b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
53516b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
53526b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
53536b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
53546b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
53556b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
53566b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
53576b4cac81SBjoern A. Zeeb 
53586b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
53596b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
53606b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
53615856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5362cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5363cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
53643f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
53653f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
53666b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
53676b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
53686b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5369cee56e77SBjoern A. Zeeb 			if (error != 0)
53706b4cac81SBjoern A. Zeeb 				break;
53716b4cac81SBjoern A. Zeeb 		}
53726b4cac81SBjoern A. Zeeb 	}
53736b4cac81SBjoern A. Zeeb }
53746b4cac81SBjoern A. Zeeb 
53756b4cac81SBjoern A. Zeeb static void *
53766b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
53776b4cac81SBjoern A. Zeeb {
53786b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53796b4cac81SBjoern A. Zeeb 
53806b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
53816b4cac81SBjoern A. Zeeb 
53826b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
53836b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
53846b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
53856b4cac81SBjoern A. Zeeb 
53866b4cac81SBjoern A. Zeeb 	return (ic);
53876b4cac81SBjoern A. Zeeb }
53886b4cac81SBjoern A. Zeeb 
53896b4cac81SBjoern A. Zeeb struct ieee80211_hw *
53906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
53916b4cac81SBjoern A. Zeeb {
53926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
53936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53946b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5395a5ae63edSBjoern A. Zeeb 	int ac;
53966b4cac81SBjoern A. Zeeb 
53976b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
53986b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
53996b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
54006b4cac81SBjoern A. Zeeb 		return (NULL);
54016b4cac81SBjoern A. Zeeb 
54026b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54036b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5404652e22d3SBjoern A. Zeeb 
54058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
54068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5407eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
54088891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
54096b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5410a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5411a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5412a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5413a5ae63edSBjoern A. Zeeb 	}
54146b4cac81SBjoern A. Zeeb 
5415759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5416759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5417759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5418759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5419759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5420759a996dSBjoern A. Zeeb 
54216b4cac81SBjoern A. Zeeb 	/*
54226b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
54236b4cac81SBjoern A. Zeeb 	 * not initialized.
54246b4cac81SBjoern A. Zeeb 	 */
54256b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54266b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5427086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
54286b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
54296b4cac81SBjoern A. Zeeb 
54306b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
54316b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
54326b4cac81SBjoern A. Zeeb 
54336b4cac81SBjoern A. Zeeb 	IMPROVE();
54346b4cac81SBjoern A. Zeeb 
54356b4cac81SBjoern A. Zeeb 	return (hw);
54366b4cac81SBjoern A. Zeeb }
54376b4cac81SBjoern A. Zeeb 
54386b4cac81SBjoern A. Zeeb void
54396b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
54406b4cac81SBjoern A. Zeeb {
54416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5442759a996dSBjoern A. Zeeb 	struct mbuf *m;
54436b4cac81SBjoern A. Zeeb 
54446b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54456b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
54466b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
54476b4cac81SBjoern A. Zeeb 
5448759a996dSBjoern A. Zeeb 	/*
5449759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5450759a996dSBjoern A. Zeeb 	 */
5451759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5452759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5453759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5454759a996dSBjoern A. Zeeb 
5455759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5456759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5457759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5458759a996dSBjoern A. Zeeb 
5459759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5460759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5461759a996dSBjoern A. Zeeb 	while (m != NULL) {
5462759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5463759a996dSBjoern A. Zeeb 
5464759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5465759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5466759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5467759a996dSBjoern A. Zeeb 
5468759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5469759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5470759a996dSBjoern A. Zeeb 		}
5471759a996dSBjoern A. Zeeb 		m_freem(m);
5472759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5473759a996dSBjoern A. Zeeb 	}
5474759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5475759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5476759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5477759a996dSBjoern A. Zeeb 
54786b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5479eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
54808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5481eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5482eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
54836b4cac81SBjoern A. Zeeb 	IMPROVE();
54846b4cac81SBjoern A. Zeeb }
54856b4cac81SBjoern A. Zeeb 
54866b4cac81SBjoern A. Zeeb void
54876b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
54886b4cac81SBjoern A. Zeeb {
54896b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54906b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54916b4cac81SBjoern A. Zeeb 
54926b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54936b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54946b4cac81SBjoern A. Zeeb 
54956b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
54966b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
54976b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
54986b4cac81SBjoern A. Zeeb 
54996b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
55006b4cac81SBjoern A. Zeeb }
55016b4cac81SBjoern A. Zeeb 
55026b4cac81SBjoern A. Zeeb struct ieee80211_hw *
55036b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
55046b4cac81SBjoern A. Zeeb {
55056b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55066b4cac81SBjoern A. Zeeb 
55076b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
55086b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
55096b4cac81SBjoern A. Zeeb }
55106b4cac81SBjoern A. Zeeb 
55116b4cac81SBjoern A. Zeeb static void
55126b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
55136b4cac81SBjoern A. Zeeb {
55146b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55156b4cac81SBjoern A. Zeeb 
55166b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55176b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
55186b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
55196b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
55206b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
55216b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
55226b4cac81SBjoern A. Zeeb }
55236b4cac81SBjoern A. Zeeb 
55242e183d99SBjoern A. Zeeb int
55256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
55266b4cac81SBjoern A. Zeeb {
55276b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5529c5b96b3eSBjoern A. Zeeb 	int band, i;
55306b4cac81SBjoern A. Zeeb 
55316b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55326b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55336b4cac81SBjoern A. Zeeb 
5534652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5535652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5536652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5537652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5538652e22d3SBjoern A. Zeeb 
55396b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
55406b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
55416b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55426b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
55436b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
55446b4cac81SBjoern A. Zeeb 		/* We take the first one. */
55456b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55466b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
55476b4cac81SBjoern A. Zeeb 	} else {
55486b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
55496b4cac81SBjoern A. Zeeb 	}
55506b4cac81SBjoern A. Zeeb 
55513d09d310SBjoern A. Zeeb #ifdef __not_yet__
55523d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
55536b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
55543d09d310SBjoern A. Zeeb #endif
55556b4cac81SBjoern A. Zeeb 
55566b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
55576b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
55586b4cac81SBjoern A. Zeeb 
55596b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
55606b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
55616b4cac81SBjoern A. Zeeb 	ic->ic_caps =
55626b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
55636b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
55646b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
55654a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
55666b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
55674a67f1dfSBjoern A. Zeeb #endif
55686b4cac81SBjoern A. Zeeb #if 0
55696b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
55706b4cac81SBjoern A. Zeeb #endif
55716b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
55726b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
55736b4cac81SBjoern A. Zeeb 	    ;
55746b4cac81SBjoern A. Zeeb #if 0
55756b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
55766b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
55776b4cac81SBjoern A. Zeeb #endif
5578cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5579cc4e78d5SBjoern A. Zeeb 		/*
5580cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5581cc4e78d5SBjoern A. Zeeb 		 *
5582cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5583cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5584cc4e78d5SBjoern A. Zeeb 		 * the flag.
5585cc4e78d5SBjoern A. Zeeb 		 */
55866b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5587a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
55886b4cac81SBjoern A. Zeeb 	}
55896b4cac81SBjoern A. Zeeb 
5590527687a9SBjoern A. Zeeb 	/*
5591527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5592527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5593527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5594527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5595527687a9SBjoern A. Zeeb 	 */
5596527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5597527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5598527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5599527687a9SBjoern A. Zeeb 
5600527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5601527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5602527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5603527687a9SBjoern A. Zeeb 		}
5604527687a9SBjoern A. Zeeb 	}
5605527687a9SBjoern A. Zeeb 
56066b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5607b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
560811db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
56096b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
56106b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
56116b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
56126b4cac81SBjoern A. Zeeb 	}
56136b4cac81SBjoern A. Zeeb #endif
56146b4cac81SBjoern A. Zeeb 
56156b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
56166b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
56176b4cac81SBjoern A. Zeeb 
56186b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
56196b4cac81SBjoern A. Zeeb 
56206b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
56216b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
56226b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
56236b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
56246b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
56256b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
56266b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
56276b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
56286b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
56296b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
56306b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
56316b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
56326b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
56336b4cac81SBjoern A. Zeeb 
5634a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5635a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5636a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5637a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5638a486fbbdSBjoern A. Zeeb 
56396b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
56406b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
56416b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
56426b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
56436b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
56446b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
56456b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
56466b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
56476b4cac81SBjoern A. Zeeb 
56489fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
564986bc7259SBjoern A. Zeeb 	/*
565086bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
565186bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
565286bc7259SBjoern A. Zeeb 	 */
565386bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
56549fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
56559fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
56569fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
56579fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
56589fb91463SBjoern A. Zeeb 
56599fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
56609fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
56619fb91463SBjoern A. Zeeb 
56629fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
56639fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
56649fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
56659fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
56669fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
56679fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
56689fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
56699fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
56709fb91463SBjoern A. Zeeb 
56719fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
56729fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
56739fb91463SBjoern A. Zeeb 
56749fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
56759fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
56769fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
56779fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
567886bc7259SBjoern A. Zeeb 	}
56799fb91463SBjoern A. Zeeb #endif
56809fb91463SBjoern A. Zeeb 
56816b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
56826b4cac81SBjoern A. Zeeb 
5683c5b96b3eSBjoern A. Zeeb 	/*
5684c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5685c5b96b3eSBjoern A. Zeeb 	 * expect one.
5686d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5687d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5688c5b96b3eSBjoern A. Zeeb 	 */
5689d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5690f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5691c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5692c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5693c5b96b3eSBjoern A. Zeeb 
5694c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5695c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5696c5b96b3eSBjoern A. Zeeb 			continue;
5697c5b96b3eSBjoern A. Zeeb 
5698d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5699d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5700d9945d78SBjoern A. Zeeb 
5701f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5702f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5703f454a4a1SBjoern A. Zeeb 			continue;
5704f454a4a1SBjoern A. Zeeb 
5705c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5706c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5707c5b96b3eSBjoern A. Zeeb 
5708c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5709c5b96b3eSBjoern A. Zeeb 				continue;
5710c5b96b3eSBjoern A. Zeeb 
57114a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
57129fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
571311450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
57149fb91463SBjoern A. Zeeb #endif
5715c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5716c5b96b3eSBjoern A. Zeeb 			break;
5717c5b96b3eSBjoern A. Zeeb 		}
5718c5b96b3eSBjoern A. Zeeb 	}
5719c5b96b3eSBjoern A. Zeeb 
5720d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5721d9945d78SBjoern A. Zeeb 
5722d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5723d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5724d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5725d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5726d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5727d9945d78SBjoern A. Zeeb 	}
5728d9945d78SBjoern A. Zeeb 
5729d9945d78SBjoern A. Zeeb 	/*
5730d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5731d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5732d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5733d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5734d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5735d9945d78SBjoern A. Zeeb 	 */
5736d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5737d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5738d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
573978ca45dfSBjoern A. Zeeb 
574078ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5741d9945d78SBjoern A. Zeeb 		/*
574278ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
574378ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
574478ca45dfSBjoern A. Zeeb 		 * space in.
5745d9945d78SBjoern A. Zeeb 		 */
5746d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
574778ca45dfSBjoern A. Zeeb 	}
5748d9945d78SBjoern A. Zeeb 
57499fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
57509fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
57519fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
57529fb91463SBjoern A. Zeeb #endif
57539fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
57541f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
57559fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
57569fb91463SBjoern A. Zeeb #endif
57579fb91463SBjoern A. Zeeb 
5758d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
575978ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
576078ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
576178ca45dfSBjoern A. Zeeb 			goto err;
5762d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
576378ca45dfSBjoern A. Zeeb 	}
5764d9945d78SBjoern A. Zeeb 
57656b4cac81SBjoern A. Zeeb 	if (bootverbose)
57666b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
57672e183d99SBjoern A. Zeeb 
57682e183d99SBjoern A. Zeeb 	return (0);
576978ca45dfSBjoern A. Zeeb err:
577078ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
577178ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
57726b4cac81SBjoern A. Zeeb }
57736b4cac81SBjoern A. Zeeb 
57746b4cac81SBjoern A. Zeeb void
57756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
57766b4cac81SBjoern A. Zeeb {
57776b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57786b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57796b4cac81SBjoern A. Zeeb 
57806b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57816b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57826b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
57836b4cac81SBjoern A. Zeeb }
57846b4cac81SBjoern A. Zeeb 
57856b4cac81SBjoern A. Zeeb void
57866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
57876b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
57886b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
57896b4cac81SBjoern A. Zeeb     void *arg)
57906b4cac81SBjoern A. Zeeb {
57916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57926b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57936b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
579461a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
57956b4cac81SBjoern A. Zeeb 
57966b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57976b4cac81SBjoern A. Zeeb 
57986b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
57996b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
580061a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5801adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
58026b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
58036b4cac81SBjoern A. Zeeb 		    __func__, flags);
58046b4cac81SBjoern A. Zeeb 	}
58056b4cac81SBjoern A. Zeeb 
5806adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
58076b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
580861a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
58096b4cac81SBjoern A. Zeeb 
58106b4cac81SBjoern A. Zeeb 	if (atomic)
58118891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
58126b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58136b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
58146b4cac81SBjoern A. Zeeb 
58156b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
58166b4cac81SBjoern A. Zeeb 
58176b4cac81SBjoern A. Zeeb 		/*
58186b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
58196b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
58206b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
58216b4cac81SBjoern A. Zeeb 		 * driver does not know about.
58226b4cac81SBjoern A. Zeeb 		 */
58236b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
58246b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
58256b4cac81SBjoern A. Zeeb 			continue;
58266b4cac81SBjoern A. Zeeb 
58276b4cac81SBjoern A. Zeeb 		/*
582861a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
582961a68e50SBjoern A. Zeeb 		 * if we haven't yet.
583061a68e50SBjoern A. Zeeb 		 */
583161a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
583261a68e50SBjoern A. Zeeb 			continue;
583361a68e50SBjoern A. Zeeb 
583461a68e50SBjoern A. Zeeb 		/*
58356b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
58366b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
58376b4cac81SBjoern A. Zeeb 		 */
58386b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
58396b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
58406b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
58416b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
58426b4cac81SBjoern A. Zeeb 	}
58436b4cac81SBjoern A. Zeeb 	if (atomic)
58448891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58456b4cac81SBjoern A. Zeeb }
58466b4cac81SBjoern A. Zeeb 
584711db70b6SBjoern A. Zeeb static void
584811db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
584911db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
585011db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
585111db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
585211db70b6SBjoern A. Zeeb     void *arg)
585311db70b6SBjoern A. Zeeb {
585411db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
585511db70b6SBjoern A. Zeeb 		return;
585611db70b6SBjoern A. Zeeb 
585711db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
585811db70b6SBjoern A. Zeeb 		return;
585911db70b6SBjoern A. Zeeb 
586011db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
586111db70b6SBjoern A. Zeeb }
586211db70b6SBjoern A. Zeeb 
58636b4cac81SBjoern A. Zeeb void
58646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
58656b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
58666b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
58676b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
586811db70b6SBjoern A. Zeeb     void *arg, bool rcu)
58696b4cac81SBjoern A. Zeeb {
587011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
587111db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58726b4cac81SBjoern A. Zeeb 
587311db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
587411db70b6SBjoern A. Zeeb 
587511db70b6SBjoern A. Zeeb 	if (rcu) {
5876a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5877a8f735a6SBjoern A. Zeeb 
587811db70b6SBjoern A. Zeeb 		if (vif == NULL) {
587911db70b6SBjoern A. Zeeb 			TODO();
588011db70b6SBjoern A. Zeeb 		} else {
5881a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
588211db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
588311db70b6SBjoern A. Zeeb 				    keyix++)
588411db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
588511db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
588611db70b6SBjoern A. Zeeb 			}
588711db70b6SBjoern A. Zeeb 		}
588811db70b6SBjoern A. Zeeb 	} else {
588911db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
589011db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
589111db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
589211db70b6SBjoern A. Zeeb 
589311db70b6SBjoern A. Zeeb 		if (vif == NULL) {
589411db70b6SBjoern A. Zeeb 			TODO();
589511db70b6SBjoern A. Zeeb 		} else {
5896a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
589711db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
589811db70b6SBjoern A. Zeeb 				    keyix++)
589911db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
590011db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
590111db70b6SBjoern A. Zeeb 			}
590211db70b6SBjoern A. Zeeb 		}
590311db70b6SBjoern A. Zeeb 	}
59046b4cac81SBjoern A. Zeeb }
59056b4cac81SBjoern A. Zeeb 
59066b4cac81SBjoern A. Zeeb void
59076b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
59086b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
59096b4cac81SBjoern A. Zeeb 	void *),
59106b4cac81SBjoern A. Zeeb     void *arg)
59116b4cac81SBjoern A. Zeeb {
5912c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5913c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5914c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5915c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
59166b4cac81SBjoern A. Zeeb 
5917c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5918c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5919c5e25798SBjoern A. Zeeb 
5920c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5921c5e25798SBjoern A. Zeeb 
5922c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5923c5e25798SBjoern A. Zeeb 
5924c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5925c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5926c5e25798SBjoern A. Zeeb 
5927c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5928c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5929c5e25798SBjoern A. Zeeb 			continue;
5930c5e25798SBjoern A. Zeeb 
5931c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5932c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5933c5e25798SBjoern A. Zeeb 			continue;
5934c5e25798SBjoern A. Zeeb 
5935d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5936c5e25798SBjoern A. Zeeb 	}
5937c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59386b4cac81SBjoern A. Zeeb }
59396b4cac81SBjoern A. Zeeb 
59406b4cac81SBjoern A. Zeeb void
59416b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
59426b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
59436b4cac81SBjoern A. Zeeb {
59446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59466b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
59476b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
59486b4cac81SBjoern A. Zeeb 
59496b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
59506b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
59516b4cac81SBjoern A. Zeeb 
59526b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59536b4cac81SBjoern A. Zeeb 
59548891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
59556b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
59566b4cac81SBjoern A. Zeeb 
5957a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
5958a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
59596b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
59606b4cac81SBjoern A. Zeeb 				continue;
59616b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
59626b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
59636b4cac81SBjoern A. Zeeb 		}
5964a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
59656b4cac81SBjoern A. Zeeb 	}
59668891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59676b4cac81SBjoern A. Zeeb }
59686b4cac81SBjoern A. Zeeb 
5969673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5970673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5971673d62fcSBjoern A. Zeeb {
5972673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5973673d62fcSBjoern A. Zeeb 
5974673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5975673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5976673d62fcSBjoern A. Zeeb 	return (regd);
5977673d62fcSBjoern A. Zeeb }
5978673d62fcSBjoern A. Zeeb 
59796b4cac81SBjoern A. Zeeb int
59806b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
59816b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
59826b4cac81SBjoern A. Zeeb {
59836b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59846b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59856b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
59866b4cac81SBjoern A. Zeeb 
59876b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59886b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59896b4cac81SBjoern A. Zeeb 
59906b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
59916b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
59926b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
59936b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
59946b4cac81SBjoern A. Zeeb 	}
59956b4cac81SBjoern A. Zeeb 
59966b4cac81SBjoern A. Zeeb 	TODO();
59976b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
59986b4cac81SBjoern A. Zeeb 
59996b4cac81SBjoern A. Zeeb 	return (0);
60006b4cac81SBjoern A. Zeeb }
60016b4cac81SBjoern A. Zeeb 
60026b4cac81SBjoern A. Zeeb void
60036b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
60046b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
60056b4cac81SBjoern A. Zeeb {
60066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
60076b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
60086b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
60096b4cac81SBjoern A. Zeeb 
60106b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
60116b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
60126b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
60136b4cac81SBjoern A. Zeeb 
60146b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
60156b4cac81SBjoern A. Zeeb 
60168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
60176b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
60186b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6019a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
60206b4cac81SBjoern A. Zeeb 	wakeup(lhw);
60218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
60226b4cac81SBjoern A. Zeeb 
60236b4cac81SBjoern A. Zeeb 	return;
60246b4cac81SBjoern A. Zeeb }
60256b4cac81SBjoern A. Zeeb 
6026759a996dSBjoern A. Zeeb static void
6027759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6028759a996dSBjoern A. Zeeb {
6029759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6030759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6031759a996dSBjoern A. Zeeb 	int ok;
6032759a996dSBjoern A. Zeeb 
6033759a996dSBjoern A. Zeeb 	ni = NULL;
6034759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6035759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6036759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6037759a996dSBjoern A. Zeeb 
6038759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6039759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6040759a996dSBjoern A. Zeeb 	}
6041759a996dSBjoern A. Zeeb 
6042759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6043759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6044759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6045759a996dSBjoern A. Zeeb 		if (ok < 0)
6046759a996dSBjoern A. Zeeb 			m_freem(m);
6047759a996dSBjoern A. Zeeb 	} else {
6048759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6049759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6050759a996dSBjoern A. Zeeb 	}
6051759a996dSBjoern A. Zeeb 
6052759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6053759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6054bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6055759a996dSBjoern A. Zeeb #endif
6056759a996dSBjoern A. Zeeb }
6057759a996dSBjoern A. Zeeb 
6058759a996dSBjoern A. Zeeb static void
6059759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6060759a996dSBjoern A. Zeeb {
6061759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6062759a996dSBjoern A. Zeeb 	struct mbufq mq;
6063759a996dSBjoern A. Zeeb 	struct mbuf *m;
6064759a996dSBjoern A. Zeeb 
6065759a996dSBjoern A. Zeeb 	lhw = ctx;
6066759a996dSBjoern A. Zeeb 
6067759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6068759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6069bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6070bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6071759a996dSBjoern A. Zeeb #endif
6072759a996dSBjoern A. Zeeb 
6073759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6074759a996dSBjoern A. Zeeb 
6075759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6076759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6077759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6078759a996dSBjoern A. Zeeb 
6079759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6080759a996dSBjoern A. Zeeb 	while (m != NULL) {
6081759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6082759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6083759a996dSBjoern A. Zeeb 	}
6084759a996dSBjoern A. Zeeb }
6085759a996dSBjoern A. Zeeb 
608649010ba7SBjoern A. Zeeb static void
608749010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
608849010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
608949010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
609049010ba7SBjoern A. Zeeb     uint8_t *rssip)
609149010ba7SBjoern A. Zeeb {
609249010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
609349010ba7SBjoern A. Zeeb 	int i;
609449010ba7SBjoern A. Zeeb 	uint8_t rssi;
609549010ba7SBjoern A. Zeeb 
609649010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
609749010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
609849010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
609949010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
610049010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
610149010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
610249010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
610349010ba7SBjoern A. Zeeb 	else
610449010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
610549010ba7SBjoern A. Zeeb 	/*
610649010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
610749010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
610849010ba7SBjoern A. Zeeb 	 */
610949010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
611049010ba7SBjoern A. Zeeb 	if (rssip != NULL)
611149010ba7SBjoern A. Zeeb 		*rssip = rssi;
611249010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
611349010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
611449010ba7SBjoern A. Zeeb 	rx_stats->c_band =
611549010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
611649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
611749010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
611849010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
611949010ba7SBjoern A. Zeeb 
612049010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
612149010ba7SBjoern A. Zeeb 
612249010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
612349010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
612449010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
612549010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
612649010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
612749010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
612849010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
612949010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
613049010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
613149010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
613249010ba7SBjoern A. Zeeb 	}
613349010ba7SBjoern A. Zeeb 
613449010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
613549010ba7SBjoern A. Zeeb 		int cc;
613649010ba7SBjoern A. Zeeb 		int8_t crssi;
613749010ba7SBjoern A. Zeeb 
613849010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
613949010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
614049010ba7SBjoern A. Zeeb 
614149010ba7SBjoern A. Zeeb 		cc = 0;
614249010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
614349010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
614449010ba7SBjoern A. Zeeb 				continue;
614549010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
614649010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
614749010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
614849010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
614949010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
615049010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
615149010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
615249010ba7SBjoern A. Zeeb 			cc++;
615349010ba7SBjoern A. Zeeb 		}
615449010ba7SBjoern A. Zeeb 		if (cc > 0)
615549010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
615649010ba7SBjoern A. Zeeb 	}
615749010ba7SBjoern A. Zeeb 
615849010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
615949010ba7SBjoern A. Zeeb 
616049010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
616149010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
616249010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
616349010ba7SBjoern A. Zeeb 		if (supband != NULL)
616449010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
616549010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
616649010ba7SBjoern A. Zeeb 		break;
616749010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
616849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
616949010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
617049010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
617149010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
617249010ba7SBjoern A. Zeeb 		break;
617349010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
617449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
617549010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
617649010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
617749010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
617849010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
617949010ba7SBjoern A. Zeeb 		break;
618049010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
618149010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
618249010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
618349010ba7SBjoern A. Zeeb 		break;
618449010ba7SBjoern A. Zeeb 	}
618549010ba7SBjoern A. Zeeb 
618649010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
618749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
618849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
618949010ba7SBjoern A. Zeeb 		break;
619049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
619149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
619249010ba7SBjoern A. Zeeb 		break;
619349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
619449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
619549010ba7SBjoern A. Zeeb 		break;
619649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
619749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
619849010ba7SBjoern A. Zeeb 		break;
619949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
620049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
620149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
620249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
620349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
620449010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
620549010ba7SBjoern A. Zeeb 		break;
620649010ba7SBjoern A. Zeeb 	}
620749010ba7SBjoern A. Zeeb 
620849010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
620949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
621049010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
621149010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
621249010ba7SBjoern A. Zeeb 
621349010ba7SBjoern A. Zeeb 	/*
621449010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
621549010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
621649010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
621749010ba7SBjoern A. Zeeb 	 */
621849010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
621949010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
622049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
622149010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
622249010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
622349010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
622449010ba7SBjoern A. Zeeb 	}
622549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
622649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
622749010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
622849010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
622949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
623049010ba7SBjoern A. Zeeb 	}
623149010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
623249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
623349010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
623449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
623549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
623649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
623749010ba7SBjoern A. Zeeb #endif
623849010ba7SBjoern A. Zeeb }
623949010ba7SBjoern A. Zeeb 
6240e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
62416b4cac81SBjoern A. Zeeb void
62426b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6243e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6244e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
62456b4cac81SBjoern A. Zeeb {
62466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62476b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
62486b4cac81SBjoern A. Zeeb 	struct mbuf *m;
62496b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
62506b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
62516b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
62526b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
62536b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
62546b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
62556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
62569d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
625749010ba7SBjoern A. Zeeb 	uint8_t rssi;
6258c0cadd99SBjoern A. Zeeb 	bool is_beacon;
62596b4cac81SBjoern A. Zeeb 
62606b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
62616b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
62626b4cac81SBjoern A. Zeeb 		IMPROVE();
62636b4cac81SBjoern A. Zeeb 		goto err;
62646b4cac81SBjoern A. Zeeb 	}
62656b4cac81SBjoern A. Zeeb 
62666b4cac81SBjoern A. Zeeb 	/*
62676b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
62686b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
62696b4cac81SBjoern A. Zeeb 	 */
62706b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
62716b4cac81SBjoern A. Zeeb 	if (m == NULL)
62726b4cac81SBjoern A. Zeeb 		goto err;
62736b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
62746b4cac81SBjoern A. Zeeb 
62756b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
62766b4cac81SBjoern A. Zeeb 	offset = m->m_len;
62776b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
62786b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
62796b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
62806b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
62816b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
62826b4cac81SBjoern A. Zeeb 	}
62836b4cac81SBjoern A. Zeeb 
62846b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
62856b4cac81SBjoern A. Zeeb 
62866b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6287c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6288c0cadd99SBjoern A. Zeeb 
6289c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
62909d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
62916b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
62926b4cac81SBjoern A. Zeeb 
62939d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
62946b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6295c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
62966b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
62976b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
62986b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6299c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
63006b4cac81SBjoern A. Zeeb 
63019d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
63026b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
63036b4cac81SBjoern A. Zeeb 
63046b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
63059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6306f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
630760970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
63086b4cac81SBjoern A. Zeeb 			__func__,
6309c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6310c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
63116b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6312f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
63136b4cac81SBjoern A. Zeeb 			rx_status->freq,
63146b4cac81SBjoern A. Zeeb 			rx_status->bw,
63156b4cac81SBjoern A. Zeeb 			rx_status->encoding,
63166b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
63176b4cac81SBjoern A. Zeeb 			rx_status->band,
63186b4cac81SBjoern A. Zeeb 			rx_status->chains,
63196b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
63206b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
63216b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
632260970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
63236b4cac81SBjoern A. Zeeb 			rx_status->signal,
63246b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
63256b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
63266b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
63276b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
63286b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
63296b4cac81SBjoern A. Zeeb 			rx_status->nss,
63306b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
63316b4cac81SBjoern A. Zeeb no_trace_beacons:
63326b4cac81SBjoern A. Zeeb #endif
63336b4cac81SBjoern A. Zeeb 
633449010ba7SBjoern A. Zeeb 	rssi = 0;
633549010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
63366b4cac81SBjoern A. Zeeb 
63376b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63386b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63396b4cac81SBjoern A. Zeeb 
63406b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
63416b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6342dbc06dd9SBjoern A. Zeeb 		m_freem(m);
63436b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
63446b4cac81SBjoern A. Zeeb 		goto err;
63456b4cac81SBjoern A. Zeeb 	}
63466b4cac81SBjoern A. Zeeb 
634758246901SBjoern A. Zeeb 	lsta = NULL;
63486b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
63496b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
63506b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
63516b4cac81SBjoern A. Zeeb 	} else {
6352c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6353c8dafefaSBjoern A. Zeeb 
63546b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
63556b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
63566b4cac81SBjoern A. Zeeb 		if (ni != NULL)
63576b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
63586b4cac81SBjoern A. Zeeb 	}
63596b4cac81SBjoern A. Zeeb 
63606b4cac81SBjoern A. Zeeb 	if (ni != NULL)
63616b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
63626b4cac81SBjoern A. Zeeb 	else
63636b4cac81SBjoern A. Zeeb 		/*
63646b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
63656b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
63666b4cac81SBjoern A. Zeeb 		 */
63676b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
63686b4cac81SBjoern A. Zeeb 
63699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6371bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6372c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6373c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
63749d9ba2b7SBjoern A. Zeeb #endif
63756b4cac81SBjoern A. Zeeb 
6376c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6377c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6378c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6379c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6380c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6381c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6382c8dafefaSBjoern A. Zeeb 
6383c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6384c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6385c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6386c8dafefaSBjoern A. Zeeb 
6387c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6388c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6389c8dafefaSBjoern A. Zeeb 
6390c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6391c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6392c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6393c8dafefaSBjoern A. Zeeb 		/*
6394c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6395c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6396c8dafefaSBjoern A. Zeeb 		 */
6397c8dafefaSBjoern A. Zeeb skip_device_ts:
63989fb91463SBjoern A. Zeeb 		;
63999fb91463SBjoern A. Zeeb 	}
6400c8dafefaSBjoern A. Zeeb 
64016b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
64026b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
64036b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
64046b4cac81SBjoern A. Zeeb 
64056b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
64066b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
64076b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
64086b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
64096b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
64106b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
64116b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
64126b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
64136b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64146b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
64156b4cac81SBjoern A. Zeeb #endif
64166b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
64176b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
64186b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
64196b4cac81SBjoern A. Zeeb 		IMPROVE();
64206b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
64216b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
64226b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
64236b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6424170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
64256b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
64266b4cac81SBjoern A. Zeeb 	}
64276b4cac81SBjoern A. Zeeb 
64286b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64296b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
64306b4cac81SBjoern A. Zeeb 
6431e30e05d3SBjoern A. Zeeb #if 0
6432e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6433e30e05d3SBjoern A. Zeeb 		/*
6434e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6435e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6436e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6437e30e05d3SBjoern A. Zeeb 		*/
6438e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6439e30e05d3SBjoern A. Zeeb 	}
6440e30e05d3SBjoern A. Zeeb #endif
6441e30e05d3SBjoern A. Zeeb 
6442759a996dSBjoern A. Zeeb 	/*
6443759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6444759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6445759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6446759a996dSBjoern A. Zeeb 	 */
64476b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6448759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6449759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6450759a996dSBjoern A. Zeeb 
6451759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6452759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6453759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6454759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6455759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6456759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6457759a996dSBjoern A. Zeeb 		}
64586b4cac81SBjoern A. Zeeb 	}
64596b4cac81SBjoern A. Zeeb 
6460759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6461759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6462759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6463759a996dSBjoern A. Zeeb 		m_freem(m);
6464759a996dSBjoern A. Zeeb 		goto err;
6465759a996dSBjoern A. Zeeb 	}
6466759a996dSBjoern A. Zeeb 
6467759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6468759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6469759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
64706b4cac81SBjoern A. Zeeb 
64716b4cac81SBjoern A. Zeeb 	IMPROVE();
64726b4cac81SBjoern A. Zeeb 
64736b4cac81SBjoern A. Zeeb err:
64746b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
64756b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
64766b4cac81SBjoern A. Zeeb }
64776b4cac81SBjoern A. Zeeb 
64786b4cac81SBjoern A. Zeeb uint8_t
6479ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
64806b4cac81SBjoern A. Zeeb {
64816b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6482ec190d91SBjoern A. Zeeb 	uint8_t tid;
6483ec190d91SBjoern A. Zeeb 
6484ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6485ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6486ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6487ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
64886b4cac81SBjoern A. Zeeb 
64896b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6490ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6491ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6492ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6493ec190d91SBjoern A. Zeeb 
6494ec190d91SBjoern A. Zeeb 	return (tid);
64956b4cac81SBjoern A. Zeeb }
64966b4cac81SBjoern A. Zeeb 
6497ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6498ac1d519cSBjoern A. Zeeb 
6499ac1d519cSBjoern A. Zeeb static void
6500ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6501ac1d519cSBjoern A. Zeeb {
6502ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6503ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6504ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6505ac1d519cSBjoern A. Zeeb 
6506ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6507ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6508ac1d519cSBjoern A. Zeeb 
6509ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6510ac1d519cSBjoern A. Zeeb 
6511ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6512ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6513ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6514ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6515ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6516ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6517ac1d519cSBjoern A. Zeeb 		return;
6518ac1d519cSBjoern A. Zeeb 	}
6519ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6520ac1d519cSBjoern A. Zeeb 
6521ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6522ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6523ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6524ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6525ac1d519cSBjoern A. Zeeb 
6526ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6527ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6528ac1d519cSBjoern A. Zeeb 
6529ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6530ac1d519cSBjoern A. Zeeb }
6531ac1d519cSBjoern A. Zeeb 
6532ac1d519cSBjoern A. Zeeb void
6533ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6534ac1d519cSBjoern A. Zeeb {
6535ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6536ac1d519cSBjoern A. Zeeb 
6537ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6538ac1d519cSBjoern A. Zeeb 
6539ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6540ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6541ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6542ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6543ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6544ac1d519cSBjoern A. Zeeb 
6545ac1d519cSBjoern A. Zeeb 	/*
6546ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6547ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6548ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6549ac1d519cSBjoern A. Zeeb 	 */
6550ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6551ac1d519cSBjoern A. Zeeb }
6552ac1d519cSBjoern A. Zeeb 
6553ac1d519cSBjoern A. Zeeb void
6554ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6555ac1d519cSBjoern A. Zeeb {
6556ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6557ac1d519cSBjoern A. Zeeb 
6558ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6559ac1d519cSBjoern A. Zeeb 
6560ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6561ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6562ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6563ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6564ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6565ac1d519cSBjoern A. Zeeb }
6566ac1d519cSBjoern A. Zeeb 
6567ac1d519cSBjoern A. Zeeb void
6568ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6569ac1d519cSBjoern A. Zeeb {
6570ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6571ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6572ac1d519cSBjoern A. Zeeb 
6573ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6574ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6575ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6576ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6577ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6578ac1d519cSBjoern A. Zeeb 		return;
6579ac1d519cSBjoern A. Zeeb 	}
6580ac1d519cSBjoern A. Zeeb 
6581ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6582ac1d519cSBjoern A. Zeeb 
6583ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6584ac1d519cSBjoern A. Zeeb 		    entry);
6585ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6586ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6587ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6588ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6589ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6590ac1d519cSBjoern A. Zeeb 			break;
6591ac1d519cSBjoern A. Zeeb 	}
6592ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6593ac1d519cSBjoern A. Zeeb }
6594ac1d519cSBjoern A. Zeeb 
6595ac1d519cSBjoern A. Zeeb void
6596ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6597ac1d519cSBjoern A. Zeeb {
6598ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6599ac1d519cSBjoern A. Zeeb 
6600ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6601ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6602ac1d519cSBjoern A. Zeeb }
6603ac1d519cSBjoern A. Zeeb 
6604ac1d519cSBjoern A. Zeeb void
6605ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6606ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6607ac1d519cSBjoern A. Zeeb {
6608ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6609ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6610ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6611ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6612ac1d519cSBjoern A. Zeeb 	} else {
6613ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6614ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6615ac1d519cSBjoern A. Zeeb 	}
6616ac1d519cSBjoern A. Zeeb }
6617ac1d519cSBjoern A. Zeeb 
6618ac1d519cSBjoern A. Zeeb void
6619ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6620ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6621ac1d519cSBjoern A. Zeeb {
6622ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6623ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6624ac1d519cSBjoern A. Zeeb }
6625ac1d519cSBjoern A. Zeeb 
6626ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6627ac1d519cSBjoern A. Zeeb 
66286b4cac81SBjoern A. Zeeb struct wiphy *
66296b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
66306b4cac81SBjoern A. Zeeb {
66316b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6632ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
66336b4cac81SBjoern A. Zeeb 
66346b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
66356b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
66366b4cac81SBjoern A. Zeeb 		return (NULL);
66376b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
66386b4cac81SBjoern A. Zeeb 
6639ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6640ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6641ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6642ac1d519cSBjoern A. Zeeb 
6643ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6644ac1d519cSBjoern A. Zeeb 
6645ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6646ac1d519cSBjoern A. Zeeb 	TODO();
6647ac1d519cSBjoern A. Zeeb 
6648ac1d519cSBjoern A. Zeeb 	return (wiphy);
66496b4cac81SBjoern A. Zeeb }
66506b4cac81SBjoern A. Zeeb 
66516b4cac81SBjoern A. Zeeb void
66526b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
66536b4cac81SBjoern A. Zeeb {
66546b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
66556b4cac81SBjoern A. Zeeb 
66566b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
66576b4cac81SBjoern A. Zeeb 		return;
66586b4cac81SBjoern A. Zeeb 
6659ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6660ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6661ac1d519cSBjoern A. Zeeb 
66626b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6663ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6664ac1d519cSBjoern A. Zeeb 
66656b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
66666b4cac81SBjoern A. Zeeb }
66676b4cac81SBjoern A. Zeeb 
6668a7c19b8aSBjoern A. Zeeb static uint32_t
6669a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6670a7c19b8aSBjoern A. Zeeb {
6671a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6672a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6673a7c19b8aSBjoern A. Zeeb }
6674a7c19b8aSBjoern A. Zeeb 
6675a7c19b8aSBjoern A. Zeeb static uint32_t
6676a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6677a7c19b8aSBjoern A. Zeeb {
6678a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6679a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6680a7c19b8aSBjoern A. Zeeb }
6681a7c19b8aSBjoern A. Zeeb 
6682a7c19b8aSBjoern A. Zeeb uint32_t
6683a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6684a7c19b8aSBjoern A. Zeeb {
6685a7c19b8aSBjoern A. Zeeb 
6686a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6687a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6688a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6689a7c19b8aSBjoern A. Zeeb 
6690a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6691a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6692a7c19b8aSBjoern A. Zeeb 
6693a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6694a7c19b8aSBjoern A. Zeeb 
6695a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6696a7c19b8aSBjoern A. Zeeb }
6697a7c19b8aSBjoern A. Zeeb 
66986b4cac81SBjoern A. Zeeb uint32_t
66996b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
67006b4cac81SBjoern A. Zeeb     enum nl80211_band band)
67016b4cac81SBjoern A. Zeeb {
67026b4cac81SBjoern A. Zeeb 
67036b4cac81SBjoern A. Zeeb 	switch (band) {
67046b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
67056b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
67066b4cac81SBjoern A. Zeeb 		break;
67076b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
67086b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
67096b4cac81SBjoern A. Zeeb 		break;
67106b4cac81SBjoern A. Zeeb 	default:
67116b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
67126b4cac81SBjoern A. Zeeb 		break;
67136b4cac81SBjoern A. Zeeb 	}
67146b4cac81SBjoern A. Zeeb 
67156b4cac81SBjoern A. Zeeb 	return (0);
67166b4cac81SBjoern A. Zeeb }
67176b4cac81SBjoern A. Zeeb 
67186b4cac81SBjoern A. Zeeb uint32_t
67196b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
67206b4cac81SBjoern A. Zeeb {
67216b4cac81SBjoern A. Zeeb 
67226b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
67236b4cac81SBjoern A. Zeeb }
67246b4cac81SBjoern A. Zeeb 
6725ac867c20SBjoern A. Zeeb #if 0
67266b4cac81SBjoern A. Zeeb static struct lkpi_sta *
67276b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
67286b4cac81SBjoern A. Zeeb {
67296b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
67306b4cac81SBjoern A. Zeeb 
6731a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6732a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67336b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6734a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67356b4cac81SBjoern A. Zeeb 			return (lsta);
67366b4cac81SBjoern A. Zeeb 		}
67376b4cac81SBjoern A. Zeeb 	}
6738a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67396b4cac81SBjoern A. Zeeb 
67406b4cac81SBjoern A. Zeeb 	return (NULL);
67416b4cac81SBjoern A. Zeeb }
6742ac867c20SBjoern A. Zeeb #endif
67436b4cac81SBjoern A. Zeeb 
67446b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
67466b4cac81SBjoern A. Zeeb {
67476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6748a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67496b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67506b4cac81SBjoern A. Zeeb 
67516b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
67526b4cac81SBjoern A. Zeeb 
6753a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6754a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67556b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
67566b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6757a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67586b4cac81SBjoern A. Zeeb 			return (sta);
67596b4cac81SBjoern A. Zeeb 		}
67606b4cac81SBjoern A. Zeeb 	}
6761a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67626b4cac81SBjoern A. Zeeb 	return (NULL);
67636b4cac81SBjoern A. Zeeb }
67646b4cac81SBjoern A. Zeeb 
67656b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67662e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
67672e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
67686b4cac81SBjoern A. Zeeb {
67696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67716b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
67736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67746b4cac81SBjoern A. Zeeb 
67756b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
67766b4cac81SBjoern A. Zeeb 	sta = NULL;
67776b4cac81SBjoern A. Zeeb 
67788891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
67796b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
67806b4cac81SBjoern A. Zeeb 
67816b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
67826b4cac81SBjoern A. Zeeb 
67836b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
67846b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
67856b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
67866b4cac81SBjoern A. Zeeb 			continue;
67876b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
67886b4cac81SBjoern A. Zeeb 		if (sta != NULL)
67896b4cac81SBjoern A. Zeeb 			break;
67906b4cac81SBjoern A. Zeeb 	}
67918891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
67926b4cac81SBjoern A. Zeeb 
67936b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
67946b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
67956b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
67966b4cac81SBjoern A. Zeeb 			return (NULL);
67976b4cac81SBjoern A. Zeeb 	}
67986b4cac81SBjoern A. Zeeb 
67996b4cac81SBjoern A. Zeeb 	return (sta);
68006b4cac81SBjoern A. Zeeb }
68016b4cac81SBjoern A. Zeeb 
68026b4cac81SBjoern A. Zeeb struct sk_buff *
68036b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
68046b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
68056b4cac81SBjoern A. Zeeb {
68066b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68070cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
68086b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
68096b4cac81SBjoern A. Zeeb 
68100cbcfa19SBjoern A. Zeeb 	skb = NULL;
68116b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68126b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
68136b4cac81SBjoern A. Zeeb 
68140cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
68150cbcfa19SBjoern A. Zeeb 		goto stopped;
68160cbcfa19SBjoern A. Zeeb 
68170cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
68180cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
68190cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
68200cbcfa19SBjoern A. Zeeb 		goto stopped;
68210cbcfa19SBjoern A. Zeeb 	}
68220cbcfa19SBjoern A. Zeeb 
6823eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6824eac3646fSBjoern A. Zeeb 
6825eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68266b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6827eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68286b4cac81SBjoern A. Zeeb 
68290cbcfa19SBjoern A. Zeeb stopped:
68306b4cac81SBjoern A. Zeeb 	return (skb);
68316b4cac81SBjoern A. Zeeb }
68326b4cac81SBjoern A. Zeeb 
68336b4cac81SBjoern A. Zeeb void
68346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
683586220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
68366b4cac81SBjoern A. Zeeb {
68376b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68386b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
683986220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
68406b4cac81SBjoern A. Zeeb 
68416b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68426b4cac81SBjoern A. Zeeb 
68436b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6844eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68456b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
68466b4cac81SBjoern A. Zeeb 		fc++;
68476b4cac81SBjoern A. Zeeb 		bc += skb->len;
68486b4cac81SBjoern A. Zeeb 	}
6849eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68506b4cac81SBjoern A. Zeeb 	if (frame_cnt)
68516b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
68526b4cac81SBjoern A. Zeeb 	if (byte_cnt)
68536b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
68546b4cac81SBjoern A. Zeeb 
68556b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
68566b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
68576b4cac81SBjoern A. Zeeb 	IMPROVE();
68586b4cac81SBjoern A. Zeeb }
68596b4cac81SBjoern A. Zeeb 
68606b4cac81SBjoern A. Zeeb /*
68616b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
68626b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
68636b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
68646b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
68656b4cac81SBjoern A. Zeeb  */
6866a8397571SBjoern A. Zeeb static void
6867a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
68686b4cac81SBjoern A. Zeeb     int status)
68696b4cac81SBjoern A. Zeeb {
68706b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
68716b4cac81SBjoern A. Zeeb 	struct mbuf *m;
68726b4cac81SBjoern A. Zeeb 
68736b4cac81SBjoern A. Zeeb 	m = skb->m;
68746b4cac81SBjoern A. Zeeb 	skb->m = NULL;
68756b4cac81SBjoern A. Zeeb 
68766b4cac81SBjoern A. Zeeb 	if (m != NULL) {
68776b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
68786b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
68796b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
68806b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
68816b4cac81SBjoern A. Zeeb 	}
6882a8397571SBjoern A. Zeeb }
68836b4cac81SBjoern A. Zeeb 
6884a8397571SBjoern A. Zeeb void
6885a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6886a8397571SBjoern A. Zeeb     int status)
6887a8397571SBjoern A. Zeeb {
6888a8397571SBjoern A. Zeeb 
6889a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
68906b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
68916b4cac81SBjoern A. Zeeb }
68926b4cac81SBjoern A. Zeeb 
6893383b3e8fSBjoern A. Zeeb void
6894a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6895a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6896383b3e8fSBjoern A. Zeeb {
6897a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6898383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6899383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6900383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6901383b3e8fSBjoern A. Zeeb 	int status;
6902383b3e8fSBjoern A. Zeeb 
6903a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6904383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6905383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6906383b3e8fSBjoern A. Zeeb 
6907383b3e8fSBjoern A. Zeeb 		m = skb->m;
6908383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6909383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6910383b3e8fSBjoern A. Zeeb 	} else {
6911383b3e8fSBjoern A. Zeeb 		ni = NULL;
6912383b3e8fSBjoern A. Zeeb 	}
6913383b3e8fSBjoern A. Zeeb 
6914a8397571SBjoern A. Zeeb 	info = txstat->info;
6915383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6916383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6917383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6918383b3e8fSBjoern A. Zeeb 	} else {
6919383b3e8fSBjoern A. Zeeb 		status = 1;
6920383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6921383b3e8fSBjoern A. Zeeb 	}
6922383b3e8fSBjoern A. Zeeb 
6923383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6924e2c5ab09SJohn Baldwin 		int ridx __unused;
6925383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6926383b3e8fSBjoern A. Zeeb 		int old_rate;
6927383b3e8fSBjoern A. Zeeb 
6928383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6929383b3e8fSBjoern A. Zeeb #endif
6930383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6931383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6932383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6933383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6934383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6935383b3e8fSBjoern A. Zeeb 		}
6936383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6937a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6938383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6939383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6940383b3e8fSBjoern A. Zeeb #endif
6941adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6942383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6943383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6944383b3e8fSBjoern A. Zeeb 		}
6945383b3e8fSBjoern A. Zeeb 
6946383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6947383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6948383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6949383b3e8fSBjoern A. Zeeb 
6950383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6951383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6952383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6953383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6954383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6955383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6956383b3e8fSBjoern A. Zeeb 		}
6957383b3e8fSBjoern A. Zeeb #endif
6958383b3e8fSBjoern A. Zeeb 	}
6959383b3e8fSBjoern A. Zeeb 
6960383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6961383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6962383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6963383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6964383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6965383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6966adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6967383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6968383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6969383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6970383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6971383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6972383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6973383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6974383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6975383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6976383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6977383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6978383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6979383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6980adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6981383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6982383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6983383b3e8fSBjoern A. Zeeb #endif
6984383b3e8fSBjoern A. Zeeb 
6985a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6986a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6987a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6988a8397571SBjoern A. Zeeb 	} else {
6989383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6990383b3e8fSBjoern A. Zeeb 	}
6991a8397571SBjoern A. Zeeb }
6992a8397571SBjoern A. Zeeb 
6993a8397571SBjoern A. Zeeb void
6994a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6995a8397571SBjoern A. Zeeb {
6996a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6997a8397571SBjoern A. Zeeb 
6998a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6999a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7000a8397571SBjoern A. Zeeb 	status.skb = skb;
7001a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7002a8397571SBjoern A. Zeeb 
7003a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7004a8397571SBjoern A. Zeeb }
7005383b3e8fSBjoern A. Zeeb 
70066b4cac81SBjoern A. Zeeb /*
70076b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
70086b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
70096b4cac81SBjoern A. Zeeb  * mbufs this should go away.
70106b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
70116b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
70126b4cac81SBjoern A. Zeeb  */
70136b4cac81SBjoern A. Zeeb static void
70146b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
70156b4cac81SBjoern A. Zeeb {
70166b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
70176b4cac81SBjoern A. Zeeb 	struct mbuf *m;
70186b4cac81SBjoern A. Zeeb 
70196b4cac81SBjoern A. Zeeb 	if (p == NULL)
70206b4cac81SBjoern A. Zeeb 		return;
70216b4cac81SBjoern A. Zeeb 
70226b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
70236b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
70246b4cac81SBjoern A. Zeeb 
70256b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
70266b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
70276b4cac81SBjoern A. Zeeb 	if (ni != NULL)
70286b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
70296b4cac81SBjoern A. Zeeb 	m_freem(m);
70306b4cac81SBjoern A. Zeeb }
70316b4cac81SBjoern A. Zeeb 
70326b4cac81SBjoern A. Zeeb void
70336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
70346b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
70356b4cac81SBjoern A. Zeeb {
70366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70376b4cac81SBjoern A. Zeeb 
70386b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70396b4cac81SBjoern A. Zeeb 	IMPROVE();
70406b4cac81SBjoern A. Zeeb 
70416b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70426b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
70436b4cac81SBjoern A. Zeeb }
70446b4cac81SBjoern A. Zeeb 
70456b4cac81SBjoern A. Zeeb void
70466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
70476b4cac81SBjoern A. Zeeb     struct work_struct *w)
70486b4cac81SBjoern A. Zeeb {
70496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70506b4cac81SBjoern A. Zeeb 
70516b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70526b4cac81SBjoern A. Zeeb 	IMPROVE();
70536b4cac81SBjoern A. Zeeb 
70546b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70556b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
70566b4cac81SBjoern A. Zeeb }
70576b4cac81SBjoern A. Zeeb 
70586b4cac81SBjoern A. Zeeb struct sk_buff *
7059ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7060ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7061ade774b1SBjoern A. Zeeb {
7062ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7063ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7064ade774b1SBjoern A. Zeeb 	uint8_t *p;
7065ade774b1SBjoern A. Zeeb 	size_t len;
7066ade774b1SBjoern A. Zeeb 
7067ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7068ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7069ade774b1SBjoern A. Zeeb 
7070ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7071ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7072ade774b1SBjoern A. Zeeb 		return (NULL);
7073ade774b1SBjoern A. Zeeb 
7074ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7075ade774b1SBjoern A. Zeeb 
7076ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7077ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7078ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7079ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7080ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7081ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7082ade774b1SBjoern A. Zeeb 
7083ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7084ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7085ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7086ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7087ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7088ade774b1SBjoern A. Zeeb 
7089ade774b1SBjoern A. Zeeb 	return (skb);
7090ade774b1SBjoern A. Zeeb }
7091ade774b1SBjoern A. Zeeb 
7092ade774b1SBjoern A. Zeeb struct sk_buff *
70936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
70946b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
70956b4cac81SBjoern A. Zeeb {
70966b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70976b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70986b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70996b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
71006b4cac81SBjoern A. Zeeb 	uint16_t v;
71016b4cac81SBjoern A. Zeeb 
71026b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
71036b4cac81SBjoern A. Zeeb 	if (skb == NULL)
71046b4cac81SBjoern A. Zeeb 		return (NULL);
71056b4cac81SBjoern A. Zeeb 
71066b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71076b4cac81SBjoern A. Zeeb 
71086b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71096b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71106b4cac81SBjoern A. Zeeb 
71116b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
71126b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
71136b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7114616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
71156b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
71166b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
71176b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
71186b4cac81SBjoern A. Zeeb 
71196b4cac81SBjoern A. Zeeb 	return (skb);
71206b4cac81SBjoern A. Zeeb }
71216b4cac81SBjoern A. Zeeb 
71226b4cac81SBjoern A. Zeeb struct sk_buff *
71236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7124adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
71256b4cac81SBjoern A. Zeeb {
71266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71276b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71286b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
71296b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
71306b4cac81SBjoern A. Zeeb 
7131adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7132adff403fSBjoern A. Zeeb 
71336b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
71346b4cac81SBjoern A. Zeeb 	if (skb == NULL)
71356b4cac81SBjoern A. Zeeb 		return (NULL);
71366b4cac81SBjoern A. Zeeb 
71376b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71386b4cac81SBjoern A. Zeeb 
71396b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71406b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71416b4cac81SBjoern A. Zeeb 
71426b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
71436b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
71446b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
71456b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
71466b4cac81SBjoern A. Zeeb 
71476b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
71486b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
71496b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
71506b4cac81SBjoern A. Zeeb 
71516b4cac81SBjoern A. Zeeb 	return (skb);
71526b4cac81SBjoern A. Zeeb }
71536b4cac81SBjoern A. Zeeb 
71546b4cac81SBjoern A. Zeeb struct wireless_dev *
71556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
71566b4cac81SBjoern A. Zeeb {
71576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71586b4cac81SBjoern A. Zeeb 
71596b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71606b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
71616b4cac81SBjoern A. Zeeb }
71626b4cac81SBjoern A. Zeeb 
71636b4cac81SBjoern A. Zeeb void
71646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
71656b4cac81SBjoern A. Zeeb {
71666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71676b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71686b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
71696b4cac81SBjoern A. Zeeb 	int arg;
71706b4cac81SBjoern A. Zeeb 
71716b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71726b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71736b4cac81SBjoern A. Zeeb 
71746b4cac81SBjoern A. Zeeb 	/*
7175f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
71766b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
71776b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
71786b4cac81SBjoern A. Zeeb 	 */
7179f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7180bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
71816b4cac81SBjoern A. Zeeb 
7182018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7183018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71846b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
71856b4cac81SBjoern A. Zeeb }
71866b4cac81SBjoern A. Zeeb 
7187bb81db90SBjoern A. Zeeb void
7188bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7189bb81db90SBjoern A. Zeeb {
7190bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7191bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7192bb81db90SBjoern A. Zeeb 
7193bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7194bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7195bb81db90SBjoern A. Zeeb 
7196bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7197bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71983540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7199bb81db90SBjoern A. Zeeb }
7200bb81db90SBjoern A. Zeeb 
72015edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
72025edde07cSBjoern A. Zeeb 
72035a9a0d78SBjoern A. Zeeb void
72045a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
72055a9a0d78SBjoern A. Zeeb {
72065a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72075a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72085a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
72095a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
72105a9a0d78SBjoern A. Zeeb 
72115a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
72125a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
72135a9a0d78SBjoern A. Zeeb 
72145a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72155a9a0d78SBjoern A. Zeeb 
72165a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72175a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72185a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72195a9a0d78SBjoern A. Zeeb 	else
72205a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72215a9a0d78SBjoern A. Zeeb 
72225a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72235a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72245a9a0d78SBjoern A. Zeeb 
72255a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72265a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72275a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
72285a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
72290d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72305a9a0d78SBjoern A. Zeeb 				/*
72315a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72325a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72335a9a0d78SBjoern A. Zeeb 				 */
72340d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
72350d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72365a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72375a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
72385a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
72395a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
72400d2cb6a6SBjoern A. Zeeb #endif
72415a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
72425a9a0d78SBjoern A. Zeeb 			}
72435a9a0d78SBjoern A. Zeeb 		}
72445a9a0d78SBjoern A. Zeeb 	}
72455a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72465a9a0d78SBjoern A. Zeeb }
72475a9a0d78SBjoern A. Zeeb 
72485a9a0d78SBjoern A. Zeeb void
72495a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
72505a9a0d78SBjoern A. Zeeb {
72515a9a0d78SBjoern A. Zeeb 	int i;
72525a9a0d78SBjoern A. Zeeb 
72535a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
72545a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72555a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
72565a9a0d78SBjoern A. Zeeb }
72575a9a0d78SBjoern A. Zeeb 
72585a9a0d78SBjoern A. Zeeb 
72595a9a0d78SBjoern A. Zeeb static void
72605a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
72615a9a0d78SBjoern A. Zeeb {
72625a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72635a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72645a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
72655a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
72665a9a0d78SBjoern A. Zeeb 
72675a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72685a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72695a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72705a9a0d78SBjoern A. Zeeb 	else
72715a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72725a9a0d78SBjoern A. Zeeb 
72735a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72745a9a0d78SBjoern A. Zeeb 
72755a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
72765a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72775a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72785a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
72795a9a0d78SBjoern A. Zeeb 
72805a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72815a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72825a9a0d78SBjoern A. Zeeb 
72835a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
72845a9a0d78SBjoern A. Zeeb 
72855a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
72865a9a0d78SBjoern A. Zeeb 
72870d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72885a9a0d78SBjoern A. Zeeb 				/*
72895a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72905a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72915a9a0d78SBjoern A. Zeeb 				 */
72920d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
72930d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72945a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72955a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
72965a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
72975a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
72980d2cb6a6SBjoern A. Zeeb #endif
72995a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
73005a9a0d78SBjoern A. Zeeb 
7301a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7302a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
73035a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
73045a9a0d78SBjoern A. Zeeb 
73055a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
73065a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
73075a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
73085a9a0d78SBjoern A. Zeeb 
73095a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
73105a9a0d78SBjoern A. Zeeb 							continue;
73115a9a0d78SBjoern A. Zeeb 
73125a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
73135a9a0d78SBjoern A. Zeeb 							continue;
73145a9a0d78SBjoern A. Zeeb 
73155a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
73165a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
73175a9a0d78SBjoern A. Zeeb 							continue;
73185a9a0d78SBjoern A. Zeeb 
73195a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
73205a9a0d78SBjoern A. Zeeb 
73215a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
73225a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
73235a9a0d78SBjoern A. Zeeb 					}
73245a9a0d78SBjoern A. Zeeb 				}
7325a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
73265a9a0d78SBjoern A. Zeeb 			}
73275a9a0d78SBjoern A. Zeeb 		}
73285a9a0d78SBjoern A. Zeeb 	}
73295a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
73305a9a0d78SBjoern A. Zeeb }
73315a9a0d78SBjoern A. Zeeb 
73325a9a0d78SBjoern A. Zeeb void
73335a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
73345a9a0d78SBjoern A. Zeeb {
73355a9a0d78SBjoern A. Zeeb 	int i;
73365a9a0d78SBjoern A. Zeeb 
73375a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
73385a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
73395a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
73405a9a0d78SBjoern A. Zeeb }
73415a9a0d78SBjoern A. Zeeb 
73425a9a0d78SBjoern A. Zeeb void
73435a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
73445a9a0d78SBjoern A. Zeeb {
73455a9a0d78SBjoern A. Zeeb 
73465a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
73475a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
73485a9a0d78SBjoern A. Zeeb 
73495a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
73505a9a0d78SBjoern A. Zeeb }
73515a9a0d78SBjoern A. Zeeb 
73525a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
73535a9a0d78SBjoern A. Zeeb void
73545a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
73555a9a0d78SBjoern A. Zeeb {
73565a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73575a9a0d78SBjoern A. Zeeb 
73585a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73595a9a0d78SBjoern A. Zeeb 
73605a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
73615a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73625a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
73635a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
73645a9a0d78SBjoern A. Zeeb }
73655a9a0d78SBjoern A. Zeeb 
73665a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
73675a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
73685a9a0d78SBjoern A. Zeeb {
73695a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73705a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
73715a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73725a9a0d78SBjoern A. Zeeb 
73735a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73745a9a0d78SBjoern A. Zeeb 	txq = NULL;
73755a9a0d78SBjoern A. Zeeb 
73765a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73775a9a0d78SBjoern A. Zeeb 
73785a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
73795a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
73805a9a0d78SBjoern A. Zeeb 		goto out;
73815a9a0d78SBjoern A. Zeeb 
73825a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
73835a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
73845a9a0d78SBjoern A. Zeeb 		goto out;
73855a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
73865a9a0d78SBjoern A. Zeeb 		goto out;
73875a9a0d78SBjoern A. Zeeb 
73885a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
73895a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
73905a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
73915a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
73925a9a0d78SBjoern A. Zeeb 
73935a9a0d78SBjoern A. Zeeb out:
73945a9a0d78SBjoern A. Zeeb 	return (txq);
73955a9a0d78SBjoern A. Zeeb }
73965a9a0d78SBjoern A. Zeeb 
73975a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
73985a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
73995a9a0d78SBjoern A. Zeeb {
74005a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74015a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7402eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
74035a9a0d78SBjoern A. Zeeb 
74045a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
74055a9a0d78SBjoern A. Zeeb 
74065a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
74075a9a0d78SBjoern A. Zeeb 
74085a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7409eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7410eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7411eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7412eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
74135a9a0d78SBjoern A. Zeeb 		goto out;
74145a9a0d78SBjoern A. Zeeb 
741541b746e0SAustin Shafer 	/*
741641b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
741741b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
741841b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
741941b746e0SAustin Shafer 	 */
742041b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
74215a9a0d78SBjoern A. Zeeb 		goto out;
74225a9a0d78SBjoern A. Zeeb 
74235a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74245a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
74255a9a0d78SBjoern A. Zeeb out:
74265a9a0d78SBjoern A. Zeeb 	return;
74275a9a0d78SBjoern A. Zeeb }
74285a9a0d78SBjoern A. Zeeb 
7429eac3646fSBjoern A. Zeeb void
7430eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7431eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7432eac3646fSBjoern A. Zeeb {
7433eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7434eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7435eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7436eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7437eac3646fSBjoern A. Zeeb 
7438eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7439eac3646fSBjoern A. Zeeb 
7440eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7441eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7442eac3646fSBjoern A. Zeeb 	do {
7443eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7444eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7445eac3646fSBjoern A. Zeeb 			break;
7446eac3646fSBjoern A. Zeeb 
7447eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7448eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7449eac3646fSBjoern A. Zeeb 		do {
7450eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7451eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7452eac3646fSBjoern A. Zeeb 				break;
7453eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7454eac3646fSBjoern A. Zeeb 		} while(1);
7455eac3646fSBjoern A. Zeeb 
7456eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7457eac3646fSBjoern A. Zeeb 	} while (1);
7458eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7459eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7460eac3646fSBjoern A. Zeeb }
7461eac3646fSBjoern A. Zeeb 
74625a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
74635a9a0d78SBjoern A. Zeeb 
74645edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
74655edde07cSBjoern A. Zeeb 	u_int refcnt;
74665edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
74675edde07cSBjoern A. Zeeb };
74685edde07cSBjoern A. Zeeb 
74695edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
74705edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
74715edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
74725edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
74735edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
74745edde07cSBjoern A. Zeeb 	size_t ssid_len;
74755edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
74765edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
74775edde07cSBjoern A. Zeeb 
74785edde07cSBjoern A. Zeeb 	/*
74795edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
74805edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
74815edde07cSBjoern A. Zeeb 	 */
74825edde07cSBjoern A. Zeeb 	bool match;
74835edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
74845edde07cSBjoern A. Zeeb };
74855edde07cSBjoern A. Zeeb 
74865edde07cSBjoern A. Zeeb static void
74875edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
74885edde07cSBjoern A. Zeeb {
74895edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
74905edde07cSBjoern A. Zeeb 	size_t ielen;
74915edde07cSBjoern A. Zeeb 
74925edde07cSBjoern A. Zeeb 	lookup = arg;
74935edde07cSBjoern A. Zeeb 
74945edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
74955edde07cSBjoern A. Zeeb 	if (lookup->match)
74965edde07cSBjoern A. Zeeb 		return;
74975edde07cSBjoern A. Zeeb 
74985edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
74995edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
75005edde07cSBjoern A. Zeeb 		return;
75015edde07cSBjoern A. Zeeb 
75025edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
75035edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
75045edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
75055edde07cSBjoern A. Zeeb 		return;
75065edde07cSBjoern A. Zeeb 	}
75075edde07cSBjoern A. Zeeb 
75085edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
75095edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
75105edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
75115edde07cSBjoern A. Zeeb 		return;
75125edde07cSBjoern A. Zeeb 	}
75135edde07cSBjoern A. Zeeb 
75145edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
75155edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
75165edde07cSBjoern A. Zeeb 
75175edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
75185edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
75195edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
75205edde07cSBjoern A. Zeeb 			return;
75215edde07cSBjoern A. Zeeb 	}
75225edde07cSBjoern A. Zeeb 
75235edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
75245edde07cSBjoern A. Zeeb 		return;
75255edde07cSBjoern A. Zeeb 
75265edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
75275edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
75285edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
75295edde07cSBjoern A. Zeeb 			return;
75305edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
75315edde07cSBjoern A. Zeeb 			return;
75325edde07cSBjoern A. Zeeb 	}
75335edde07cSBjoern A. Zeeb 
75345edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
75355edde07cSBjoern A. Zeeb 
75365edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
75375edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
75385edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
75395edde07cSBjoern A. Zeeb 		return;
75405edde07cSBjoern A. Zeeb 
75415edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
75425edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
75435edde07cSBjoern A. Zeeb 	if (ielen)
75445edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
75455edde07cSBjoern A. Zeeb 
75465edde07cSBjoern A. Zeeb 	lookup->match = true;
75475edde07cSBjoern A. Zeeb }
75485edde07cSBjoern A. Zeeb 
75495edde07cSBjoern A. Zeeb struct cfg80211_bss *
75505edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
75515edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
75525edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
75535edde07cSBjoern A. Zeeb {
75545edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75555edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
75565edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75575edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
75585edde07cSBjoern A. Zeeb 
75595edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
75605edde07cSBjoern A. Zeeb 
75615edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
75625edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
75635edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
75645edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
75655edde07cSBjoern A. Zeeb 		return (NULL);
75665edde07cSBjoern A. Zeeb 	}
75675edde07cSBjoern A. Zeeb 
75685edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
75695edde07cSBjoern A. Zeeb 	lookup.chan = chan;
75705edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
75715edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
75725edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
75735edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
75745edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
75755edde07cSBjoern A. Zeeb 	lookup.match = false;
75765edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
75775edde07cSBjoern A. Zeeb 
75785edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
75795edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
75805edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
75815edde07cSBjoern A. Zeeb 	if (!lookup.match) {
75825edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75835edde07cSBjoern A. Zeeb 		return (NULL);
75845edde07cSBjoern A. Zeeb 	}
75855edde07cSBjoern A. Zeeb 
75865edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
75875edde07cSBjoern A. Zeeb 	return (&lbss->bss);
75885edde07cSBjoern A. Zeeb }
75895edde07cSBjoern A. Zeeb 
75905edde07cSBjoern A. Zeeb void
75915edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
75925edde07cSBjoern A. Zeeb {
75935edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75945edde07cSBjoern A. Zeeb 
75955edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
75965edde07cSBjoern A. Zeeb 
75975edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
75985edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
75995edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
76005edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
76015edde07cSBjoern A. Zeeb 	}
76025edde07cSBjoern A. Zeeb }
76035edde07cSBjoern A. Zeeb 
76045edde07cSBjoern A. Zeeb void
76055edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
76065edde07cSBjoern A. Zeeb {
76075edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
76085edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
76095edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
76105edde07cSBjoern A. Zeeb 
76115edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
76125edde07cSBjoern A. Zeeb 	ic = lhw->ic;
76135edde07cSBjoern A. Zeeb 
76145edde07cSBjoern A. Zeeb 	/*
76155edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
76165edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
76175edde07cSBjoern A. Zeeb 	 */
76185edde07cSBjoern A. Zeeb 	if (ic == NULL ||
76195edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
76205edde07cSBjoern A. Zeeb 		return;
76215edde07cSBjoern A. Zeeb 
76225edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
76235edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
76245edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
76255edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
76265edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
76275edde07cSBjoern A. Zeeb }
76285edde07cSBjoern A. Zeeb 
76295edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
76305edde07cSBjoern A. Zeeb 
7631ac1d519cSBjoern A. Zeeb /*
7632ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7633ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7634ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7635ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7636ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7637ac1d519cSBjoern A. Zeeb  */
7638ac1d519cSBjoern A. Zeeb 
7639ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7640ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7641ac1d519cSBjoern A. Zeeb {
7642ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7643ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7644ac1d519cSBjoern A. Zeeb 	int error;
7645ac1d519cSBjoern A. Zeeb 
7646ac1d519cSBjoern A. Zeeb 	changed = 0;
7647ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7648ac1d519cSBjoern A. Zeeb 		cd = NULL;
7649ac1d519cSBjoern A. Zeeb 	else
7650ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7651ac1d519cSBjoern A. Zeeb 
7652ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7653ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7654ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7655ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7656ac1d519cSBjoern A. Zeeb 	}
7657ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7658ac1d519cSBjoern A. Zeeb 
7659ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7660ac1d519cSBjoern A. Zeeb 		return (0);
7661ac1d519cSBjoern A. Zeeb 
7662ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7663ac1d519cSBjoern A. Zeeb 	return (error);
7664ac1d519cSBjoern A. Zeeb }
7665ac1d519cSBjoern A. Zeeb 
7666ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7667ac1d519cSBjoern A. Zeeb 
76686b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
76696b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
76706b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7671