xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 75d23d882389e1bb011bda37022aaa17c068cbf8)
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
82*75d23d88SBjoern A. Zeeb #define	LKPI_80211_HT
8311db70b6SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
31740839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
31840839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
31940839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32140839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32240839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
32340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
32440839418SBjoern A. Zeeb 
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
32640839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
32940839418SBjoern A. Zeeb 
33040839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33140839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33240839418SBjoern A. Zeeb 
33340839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
33440839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
33540839418SBjoern A. Zeeb 
33640839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
33740839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
33840839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
33940839418SBjoern A. Zeeb 		}
34040839418SBjoern A. Zeeb 
34140839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
34440839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
34540839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
34640839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
34740839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
34840839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
34940839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35040839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35140839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35240839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35340839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
35440839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
35540839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
35640839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35740839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
35840839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
35940839418SBjoern A. Zeeb 	}
36040839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36140839418SBjoern A. Zeeb 
36240839418SBjoern A. Zeeb 	sbuf_finish(&s);
36340839418SBjoern A. Zeeb 	sbuf_delete(&s);
36440839418SBjoern A. Zeeb 
36540839418SBjoern A. Zeeb 	return (0);
36640839418SBjoern A. Zeeb }
36740839418SBjoern A. Zeeb 
3689fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3699fb91463SBjoern A. Zeeb static void
370f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
3719fb91463SBjoern A. Zeeb {
3729fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
3739fb91463SBjoern A. Zeeb 	uint8_t *ie;
3749fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
3759fb91463SBjoern A. Zeeb 	int i, rx_nss;
3769fb91463SBjoern A. Zeeb 
377f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
378f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
3799fb91463SBjoern A. Zeeb 		return;
380f9c7a07dSBjoern A. Zeeb 	}
3819fb91463SBjoern A. Zeeb 
3829fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
3839fb91463SBjoern A. Zeeb 
3849fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
3859fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
3869fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3879fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
3889fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
3899fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3909fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
3919fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
3929fb91463SBjoern A. Zeeb 
3939fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
3949fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
3959fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
3969fb91463SBjoern A. Zeeb 		ie += 4;
3979fb91463SBjoern A. Zeeb 	ie += 2;
3989fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
3999fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4009fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4019fb91463SBjoern A. Zeeb 
402f9c7a07dSBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0)
403f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
404f9c7a07dSBjoern A. Zeeb 
405f9c7a07dSBjoern A. Zeeb 	/*
406f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
407f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
408f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
409f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
410f9c7a07dSBjoern A. Zeeb 	 */
4119fb91463SBjoern A. Zeeb 	rx_nss = 0;
412f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4139fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4149fb91463SBjoern A. Zeeb 			rx_nss++;
4159fb91463SBjoern A. Zeeb 	}
416f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
417f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
4189fb91463SBjoern A. Zeeb 
419f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
420f9c7a07dSBjoern A. Zeeb 
421f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
422f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
423f9c7a07dSBjoern A. Zeeb 	else
424f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
425f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
426f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
427f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
428f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
429f9c7a07dSBjoern A. Zeeb 	}
430f9c7a07dSBjoern A. Zeeb #endif
4319fb91463SBjoern A. Zeeb }
4329fb91463SBjoern A. Zeeb #endif
4339fb91463SBjoern A. Zeeb 
4349fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4359fb91463SBjoern A. Zeeb static void
436f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
4379fb91463SBjoern A. Zeeb {
438f9c7a07dSBjoern A. Zeeb 	uint32_t width;
439f9c7a07dSBjoern A. Zeeb 	int rx_nss;
440f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
441f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
4429fb91463SBjoern A. Zeeb 
443f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) {
444f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
4459fb91463SBjoern A. Zeeb 		return;
446f9c7a07dSBjoern A. Zeeb 	}
4479fb91463SBjoern A. Zeeb 
448f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
449f9c7a07dSBjoern A. Zeeb 
450f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
451f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
452f9c7a07dSBjoern A. Zeeb 
453f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
454f9c7a07dSBjoern A. Zeeb 	switch (width) {
455f9c7a07dSBjoern A. Zeeb #if 0
456f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
457f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
4589fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
459f9c7a07dSBjoern A. Zeeb 		break;
460f9c7a07dSBjoern A. Zeeb #endif
461f9c7a07dSBjoern A. Zeeb 	default:
462f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
463f9c7a07dSBjoern A. Zeeb #if 0
464f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
465f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
466f9c7a07dSBjoern A. Zeeb 		else
467f9c7a07dSBjoern A. Zeeb #endif
4689fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4699fb91463SBjoern A. Zeeb 	}
4709fb91463SBjoern A. Zeeb 
471f9c7a07dSBjoern A. Zeeb 
472f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
473f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
474f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
475f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
476f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
477f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
478f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
479f9c7a07dSBjoern A. Zeeb 			break;
480f9c7a07dSBjoern A. Zeeb 		}
481f9c7a07dSBjoern A. Zeeb 	}
482f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
483f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
484f9c7a07dSBjoern A. Zeeb 
485f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
486f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
487f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
488f9c7a07dSBjoern A. Zeeb 		break;
489f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
490f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
491f9c7a07dSBjoern A. Zeeb 		break;
492f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
493f9c7a07dSBjoern A. Zeeb 	default:
494f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
495f9c7a07dSBjoern A. Zeeb 		break;
496f9c7a07dSBjoern A. Zeeb 	}
4979fb91463SBjoern A. Zeeb }
4989fb91463SBjoern A. Zeeb #endif
4999fb91463SBjoern A. Zeeb 
500d9f59799SBjoern A. Zeeb static void
501f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
502f9c7a07dSBjoern A. Zeeb {
503f9c7a07dSBjoern A. Zeeb 
504f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
505f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni);
506f9c7a07dSBjoern A. Zeeb #endif
507f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
508f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni);
509f9c7a07dSBjoern A. Zeeb #endif
510f9c7a07dSBjoern A. Zeeb }
511f9c7a07dSBjoern A. Zeeb 
512389265e3SBjoern A. Zeeb static uint8_t
513389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
514389265e3SBjoern A. Zeeb {
515389265e3SBjoern A. Zeeb 	uint8_t chains;
516389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
517389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
518389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
519389265e3SBjoern A. Zeeb 
520389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
521389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
522389265e3SBjoern A. Zeeb #endif
523389265e3SBjoern A. Zeeb 
524389265e3SBjoern A. Zeeb 	chains = 1;
525389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
526389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
527389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
528389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
529389265e3SBjoern A. Zeeb #endif
530389265e3SBjoern A. Zeeb 
531389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
532389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
533389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
534389265e3SBjoern A. Zeeb #endif
535389265e3SBjoern A. Zeeb 
536389265e3SBjoern A. Zeeb 	return (chains);
537389265e3SBjoern A. Zeeb }
538389265e3SBjoern A. Zeeb 
539f9c7a07dSBjoern A. Zeeb static void
54067674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
54167674c1cSBjoern A. Zeeb     const char *_f, int _l)
542d9f59799SBjoern A. Zeeb {
543d9f59799SBjoern A. Zeeb 
5449d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5459d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
546d9f59799SBjoern A. Zeeb 		return;
547d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
548d9f59799SBjoern A. Zeeb 		return;
549d9f59799SBjoern A. Zeeb 
550d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
55167674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
55267674c1cSBjoern A. Zeeb 	if (ni != NULL)
55367674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
554d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
555d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
55611db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
5579d9ba2b7SBjoern A. Zeeb #endif
558d9f59799SBjoern A. Zeeb }
559d9f59799SBjoern A. Zeeb 
560d9f59799SBjoern A. Zeeb static void
561d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
562d9f59799SBjoern A. Zeeb {
563d9f59799SBjoern A. Zeeb 
564d9f59799SBjoern A. Zeeb 
565a8f735a6SBjoern A. Zeeb 	wiphy_lock(lsta->hw->wiphy);
566a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
567a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
568a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
569a8f735a6SBjoern A. Zeeb 	wiphy_unlock(lsta->hw->wiphy);
570d9f59799SBjoern A. Zeeb }
571d9f59799SBjoern A. Zeeb 
5724f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
5734f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
5744f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
5754f61ef8bSBjoern A. Zeeb {
5764f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
5774f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
5784f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
5794f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
580b6b352e4SBjoern A. Zeeb 	int band, i, tid;
5814f61ef8bSBjoern A. Zeeb 
5824f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
5834f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
5844f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
5854f61ef8bSBjoern A. Zeeb 		return (NULL);
5864f61ef8bSBjoern A. Zeeb 
587a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
5884f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
5894f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
5904f61ef8bSBjoern A. Zeeb 	/*
5910936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
5920936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
5930936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
5940936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
5950936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
5960936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
5970936c648SBjoern A. Zeeb 	 * two separate allocations.
5984f61ef8bSBjoern A. Zeeb 	 */
5990936c648SBjoern A. Zeeb 	lsta->ni = ni;
6004f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
6014f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
6024f61ef8bSBjoern A. Zeeb 
6034f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
6044f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
6054f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
6064f61ef8bSBjoern A. Zeeb 
6074f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
608b6b352e4SBjoern A. Zeeb 
609b6b352e4SBjoern A. Zeeb 	/* TXQ */
6104f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
6114f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
6124f61ef8bSBjoern A. Zeeb 
613d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
6144f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
6154f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
6164f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
6174f61ef8bSBjoern A. Zeeb 			goto cleanup;
6184f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
6194f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
620d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
621d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
622d0d29110SBjoern A. Zeeb 				continue;
623d0d29110SBjoern A. Zeeb 			}
624d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
6254f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
6264f61ef8bSBjoern A. Zeeb 		} else {
627fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
6284f61ef8bSBjoern A. Zeeb 		}
629d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
6300cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
631d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
6324f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
6334f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
6340cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
635d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
636eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
6374f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
6384f61ef8bSBjoern A. Zeeb 	}
6394f61ef8bSBjoern A. Zeeb 
640b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
641b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
642b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
643b6b352e4SBjoern A. Zeeb 
644b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
645b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
646b6b352e4SBjoern A. Zeeb 			continue;
647b6b352e4SBjoern A. Zeeb 
648b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
64973cd1c5dSBjoern A. Zeeb 			switch (band) {
65073cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
65173cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
65273cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
65373cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
65473cd1c5dSBjoern A. Zeeb 				case 110:
65573cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
65673cd1c5dSBjoern A. Zeeb 				case 55:
65773cd1c5dSBjoern A. Zeeb 				case 20:
65873cd1c5dSBjoern A. Zeeb 				case 10:
659b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
66073cd1c5dSBjoern A. Zeeb 					break;
66173cd1c5dSBjoern A. Zeeb 				}
66273cd1c5dSBjoern A. Zeeb 				break;
66373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
66473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
66573cd1c5dSBjoern A. Zeeb 				case 240:
66673cd1c5dSBjoern A. Zeeb 				case 120:
66773cd1c5dSBjoern A. Zeeb 				case 60:
66873cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
66973cd1c5dSBjoern A. Zeeb 					break;
67073cd1c5dSBjoern A. Zeeb 				}
67173cd1c5dSBjoern A. Zeeb 				break;
67273cd1c5dSBjoern A. Zeeb 			}
673b6b352e4SBjoern A. Zeeb 		}
674b6b352e4SBjoern A. Zeeb 	}
6759fb91463SBjoern A. Zeeb 
6766ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
6779fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
678f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
6799fb91463SBjoern A. Zeeb 
680f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
6819fb91463SBjoern A. Zeeb 
682f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
683b6b352e4SBjoern A. Zeeb 
6846ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
6856ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
6866ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
6876ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
6886ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
6896ffb7bd4SBjoern A. Zeeb 	}
6906ffb7bd4SBjoern A. Zeeb 
6914f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
692fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
6934f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
6944f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
6950936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
6964f61ef8bSBjoern A. Zeeb 
6974f61ef8bSBjoern A. Zeeb 	return (lsta);
6984f61ef8bSBjoern A. Zeeb 
6994f61ef8bSBjoern A. Zeeb cleanup:
700eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
701eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
702eac3646fSBjoern A. Zeeb 
703eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
704eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
7054f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
706eac3646fSBjoern A. Zeeb 	}
7074f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
7084f61ef8bSBjoern A. Zeeb 	return (NULL);
7094f61ef8bSBjoern A. Zeeb }
7104f61ef8bSBjoern A. Zeeb 
7110936c648SBjoern A. Zeeb static void
7120936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
7130936c648SBjoern A. Zeeb {
7140936c648SBjoern A. Zeeb 	struct mbuf *m;
7150936c648SBjoern A. Zeeb 
7160936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
7170936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
7180936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
7190936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
7200936c648SBjoern A. Zeeb 
7210936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
7220936c648SBjoern A. Zeeb 	IMPROVE();
7230936c648SBjoern A. Zeeb 
724fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
725fa4e4257SBjoern A. Zeeb 
726fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
7270936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
728fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
7290936c648SBjoern A. Zeeb 
7300936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
7310936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
7320936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
7330936c648SBjoern A. Zeeb 
7340936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
7350936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
7360936c648SBjoern A. Zeeb 	while (m != NULL) {
7370936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
7380936c648SBjoern A. Zeeb 
7390936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
7400936c648SBjoern A. Zeeb 		if (nim != NULL)
7410936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
7420936c648SBjoern A. Zeeb 		m_freem(m);
7430936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
7440936c648SBjoern A. Zeeb 	}
7450936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
7460936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
747fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
7480936c648SBjoern A. Zeeb 
7490936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
7500936c648SBjoern A. Zeeb 
7510936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
7520936c648SBjoern A. Zeeb 
7530936c648SBjoern A. Zeeb 	/* Free lsta. */
7540936c648SBjoern A. Zeeb 	lsta->ni = NULL;
7550936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
7560936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
7570936c648SBjoern A. Zeeb }
7580936c648SBjoern A. Zeeb 
7590936c648SBjoern A. Zeeb 
7606b4cac81SBjoern A. Zeeb static enum nl80211_band
7616b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
7626b4cac81SBjoern A. Zeeb {
7636b4cac81SBjoern A. Zeeb 
7646b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
7656b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
7666b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
7676b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
7686b4cac81SBjoern A. Zeeb #ifdef __notyet__
7696b4cac81SBjoern A. Zeeb 	else if ()
7706b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
7716b4cac81SBjoern A. Zeeb 	else if ()
7726b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
7736b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
7746b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
7756b4cac81SBjoern A. Zeeb #endif
7766b4cac81SBjoern A. Zeeb 	else
7776b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
7786b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
7796b4cac81SBjoern A. Zeeb }
7806b4cac81SBjoern A. Zeeb 
7816b4cac81SBjoern A. Zeeb static uint32_t
7826b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
7836b4cac81SBjoern A. Zeeb {
7846b4cac81SBjoern A. Zeeb 
7856b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
7866b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
7876b4cac81SBjoern A. Zeeb 	switch (band) {
7886b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
7896b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
7906b4cac81SBjoern A. Zeeb 		break;
7916b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
7926b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
7936b4cac81SBjoern A. Zeeb 		break;
7946b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
7956b4cac81SBjoern A. Zeeb 		break;
7966b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
7976b4cac81SBjoern A. Zeeb 		break;
7986b4cac81SBjoern A. Zeeb 	default:
7996b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
8006b4cac81SBjoern A. Zeeb 		break;
8016b4cac81SBjoern A. Zeeb 	}
8026b4cac81SBjoern A. Zeeb 
8036b4cac81SBjoern A. Zeeb 	IMPROVE();
8046b4cac81SBjoern A. Zeeb 	return (0x00);
8056b4cac81SBjoern A. Zeeb }
8066b4cac81SBjoern A. Zeeb 
807e3a0b120SBjoern A. Zeeb #if 0
8086b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
8096b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
8106b4cac81SBjoern A. Zeeb {
8116b4cac81SBjoern A. Zeeb 
8126b4cac81SBjoern A. Zeeb 	switch (ac) {
8136b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
8146b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
8156b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
8166b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
8176b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
8186b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
8196b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
8206b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
8216b4cac81SBjoern A. Zeeb 	default:
8226b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
8236b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
8246b4cac81SBjoern A. Zeeb 	}
8256b4cac81SBjoern A. Zeeb }
826e3a0b120SBjoern A. Zeeb #endif
8276b4cac81SBjoern A. Zeeb 
8286b4cac81SBjoern A. Zeeb static enum nl80211_iftype
8296b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
8306b4cac81SBjoern A. Zeeb {
8316b4cac81SBjoern A. Zeeb 
8326b4cac81SBjoern A. Zeeb 	switch (opmode) {
8336b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
8346b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
8356b4cac81SBjoern A. Zeeb 		break;
8366b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
8376b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
8386b4cac81SBjoern A. Zeeb 		break;
8396b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
8406b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
8416b4cac81SBjoern A. Zeeb 		break;
8426b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
8436b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
8446b4cac81SBjoern A. Zeeb 		break;
8456b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
8466b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
8476b4cac81SBjoern A. Zeeb 		break;
8486b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
8496b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
8506b4cac81SBjoern A. Zeeb 		break;
8516b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
8526b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8536b4cac81SBjoern A. Zeeb 	default:
8546b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
8556b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8566b4cac81SBjoern A. Zeeb 	}
8576b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
8586b4cac81SBjoern A. Zeeb }
8596b4cac81SBjoern A. Zeeb 
860b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
86112a511c8SBjoern A. Zeeb static const char *
86212a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
86312a511c8SBjoern A. Zeeb {
86412a511c8SBjoern A. Zeeb 
86512a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
86612a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
86712a511c8SBjoern A. Zeeb 		return ("WEP40");
86812a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
86912a511c8SBjoern A. Zeeb 		return ("TKIP");
87012a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
87112a511c8SBjoern A. Zeeb 		return ("CCMP");
87212a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
87312a511c8SBjoern A. Zeeb 		return ("WEP104");
87412a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
87512a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
87612a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
87712a511c8SBjoern A. Zeeb 		return ("GCMP");
87812a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
87912a511c8SBjoern A. Zeeb 		return ("GCMP_256");
88012a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
88112a511c8SBjoern A. Zeeb 		return ("CCMP_256");
88212a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
88312a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
88412a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
88512a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
88612a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
88712a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
88812a511c8SBjoern A. Zeeb 	default:
88912a511c8SBjoern A. Zeeb 		return ("??");
89012a511c8SBjoern A. Zeeb 	}
89112a511c8SBjoern A. Zeeb }
89212a511c8SBjoern A. Zeeb 
8936b4cac81SBjoern A. Zeeb static uint32_t
8946b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
8956b4cac81SBjoern A. Zeeb {
8966b4cac81SBjoern A. Zeeb 
8976b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
8986b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
8996b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
9006b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
9016b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
9026b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
903906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
9046b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
9056b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
9066b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
9076b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
9086b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
9096b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
9106b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
9116b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
9126b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
91312a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
91412a511c8SBjoern A. Zeeb 		    __func__,
91512a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
91612a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
9176b4cac81SBjoern A. Zeeb 		break;
9186b4cac81SBjoern A. Zeeb 	default:
91912a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
92012a511c8SBjoern A. Zeeb 		    __func__,
92112a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
92212a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
9236b4cac81SBjoern A. Zeeb 	}
9246b4cac81SBjoern A. Zeeb 
9256b4cac81SBjoern A. Zeeb 	return (0);
9266b4cac81SBjoern A. Zeeb }
9276b4cac81SBjoern A. Zeeb 
9286b4cac81SBjoern A. Zeeb static uint32_t
9296b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
9306b4cac81SBjoern A. Zeeb {
9316b4cac81SBjoern A. Zeeb 
9326b4cac81SBjoern A. Zeeb 	switch (cipher) {
9336b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
9346b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
9356b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
9366b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
9376b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
9386b4cac81SBjoern A. Zeeb 		if (keylen < 8)
9396b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
9406b4cac81SBjoern A. Zeeb 		else
9416b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
9426b4cac81SBjoern A. Zeeb 		break;
9436b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
9446b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
9456b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
9466b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
9476b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
9486b4cac81SBjoern A. Zeeb 		break;
9496b4cac81SBjoern A. Zeeb 	default:
9506b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
9516b4cac81SBjoern A. Zeeb 	};
9526b4cac81SBjoern A. Zeeb 	return (0);
9536b4cac81SBjoern A. Zeeb }
9546b4cac81SBjoern A. Zeeb #endif
9556b4cac81SBjoern A. Zeeb 
9566b4cac81SBjoern A. Zeeb #ifdef __notyet__
9576b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
9586b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
9596b4cac81SBjoern A. Zeeb {
9606b4cac81SBjoern A. Zeeb 
9616b4cac81SBjoern A. Zeeb 	/*
9626b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
9636b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
9646b4cac81SBjoern A. Zeeb 	 */
9656b4cac81SBjoern A. Zeeb 	switch (state) {
9666b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
9676b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
9686b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
9696b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
9706b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
9716b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
9726b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
9736b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
9746b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
9756b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
9766b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
9776b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
9786b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
9796b4cac81SBjoern A. Zeeb 	default:
9806b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
9816b4cac81SBjoern A. Zeeb 	};
9826b4cac81SBjoern A. Zeeb 
9836b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
9846b4cac81SBjoern A. Zeeb }
9856b4cac81SBjoern A. Zeeb #endif
9866b4cac81SBjoern A. Zeeb 
9876b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9886b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
9896b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
9906b4cac81SBjoern A. Zeeb {
9916b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9926b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
9936b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
9946b4cac81SBjoern A. Zeeb 	int i, nchans;
9956b4cac81SBjoern A. Zeeb 
9966b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9976b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
9986b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
9996b4cac81SBjoern A. Zeeb 		return (NULL);
10006b4cac81SBjoern A. Zeeb 
10016b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
10026b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
10036b4cac81SBjoern A. Zeeb 		return (NULL);
10046b4cac81SBjoern A. Zeeb 
10056b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
10066b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
10076b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
10086b4cac81SBjoern A. Zeeb 			return (&channels[i]);
10096b4cac81SBjoern A. Zeeb 	}
10106b4cac81SBjoern A. Zeeb 
10116b4cac81SBjoern A. Zeeb 	return (NULL);
10126b4cac81SBjoern A. Zeeb }
10136b4cac81SBjoern A. Zeeb 
10142ac8a218SBjoern A. Zeeb #if 0
10156b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
10166b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
10176b4cac81SBjoern A. Zeeb {
10186b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
10196b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
10206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10216b4cac81SBjoern A. Zeeb 
10226b4cac81SBjoern A. Zeeb 	chan = NULL;
10236b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
10246b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
10256b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
10266b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
10276b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
10286b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
10296b4cac81SBjoern A. Zeeb 	else
10306b4cac81SBjoern A. Zeeb 		c = NULL;
10316b4cac81SBjoern A. Zeeb 
10326b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
10336b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
10346b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
10356b4cac81SBjoern A. Zeeb 	}
10366b4cac81SBjoern A. Zeeb 
10376b4cac81SBjoern A. Zeeb 	return (chan);
10386b4cac81SBjoern A. Zeeb }
10392ac8a218SBjoern A. Zeeb #endif
10406b4cac81SBjoern A. Zeeb 
10412e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
10422e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
10432e183d99SBjoern A. Zeeb {
10442e183d99SBjoern A. Zeeb 	enum nl80211_band band;
10452e183d99SBjoern A. Zeeb 
10462e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
10472e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
10482e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
10492e183d99SBjoern A. Zeeb 		int i;
10502e183d99SBjoern A. Zeeb 
10512e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
10522e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
10532e183d99SBjoern A. Zeeb 			continue;
10542e183d99SBjoern A. Zeeb 
10552e183d99SBjoern A. Zeeb 		channels = supband->channels;
10562e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
10572e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
10582e183d99SBjoern A. Zeeb 				return (&channels[i]);
10592e183d99SBjoern A. Zeeb 		}
10602e183d99SBjoern A. Zeeb 	}
10612e183d99SBjoern A. Zeeb 
10622e183d99SBjoern A. Zeeb 	return (NULL);
10632e183d99SBjoern A. Zeeb }
10642e183d99SBjoern A. Zeeb 
1065b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
10666b4cac81SBjoern A. Zeeb static int
106711db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
106811db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
106911db70b6SBjoern A. Zeeb {
107011db70b6SBjoern A. Zeeb 	int error;
107111db70b6SBjoern A. Zeeb 
107211db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
107311db70b6SBjoern A. Zeeb 		return (0);
107411db70b6SBjoern A. Zeeb 
107511db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
107611db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
107711db70b6SBjoern A. Zeeb 
107811db70b6SBjoern A. Zeeb 	error = 0;
107911db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
108011db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
108111db70b6SBjoern A. Zeeb 		int err;
108211db70b6SBjoern A. Zeeb 
108311db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
108411db70b6SBjoern A. Zeeb 			continue;
108511db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
108611db70b6SBjoern A. Zeeb 
108711db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
108811db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
108911db70b6SBjoern A. Zeeb 		if (err != 0) {
109011db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
109111db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
109211db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
109311db70b6SBjoern A. Zeeb 			error++;
109411db70b6SBjoern A. Zeeb 
109511db70b6SBjoern A. Zeeb 			/*
109611db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
109711db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
109811db70b6SBjoern A. Zeeb 			 * crash (firmware).
109911db70b6SBjoern A. Zeeb 			 */
110011db70b6SBjoern A. Zeeb 			continue;
110111db70b6SBjoern A. Zeeb 		}
110211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
110311db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
110411db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
110511db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
110611db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
110711db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
110811db70b6SBjoern A. Zeeb #endif
110911db70b6SBjoern A. Zeeb 
111011db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
111111db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
111211db70b6SBjoern A. Zeeb 	}
111311db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
111411db70b6SBjoern A. Zeeb 	return (error);
111511db70b6SBjoern A. Zeeb }
111611db70b6SBjoern A. Zeeb 
111711db70b6SBjoern A. Zeeb static int
111811db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
11196b4cac81SBjoern A. Zeeb {
11206b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
11216b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11226b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11236b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
112411db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11256b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
11276b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11286b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
112911db70b6SBjoern A. Zeeb 	struct ieee80211_node_table *nt;
11306b4cac81SBjoern A. Zeeb 	int error;
113111db70b6SBjoern A. Zeeb 	bool islocked;
11326b4cac81SBjoern A. Zeeb 
11336b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
11346b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
11356b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11366b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11376b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11386b4cac81SBjoern A. Zeeb 
113911db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
114011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
114111db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
114211db70b6SBjoern A. Zeeb 		return (0);
114311db70b6SBjoern A. Zeeb 	}
114411db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
114511db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
114611db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
114711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
114811db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
114911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
115011db70b6SBjoern A. Zeeb 		return (0);
115111db70b6SBjoern A. Zeeb 	}
115211db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
115311db70b6SBjoern A. Zeeb 
115411db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
115511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
115611db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
115711db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
115811db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
115911db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
116011db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
116111db70b6SBjoern A. Zeeb #endif
116211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
116311db70b6SBjoern A. Zeeb 		return (1);
116411db70b6SBjoern A. Zeeb 	}
116511db70b6SBjoern A. Zeeb 
116611db70b6SBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
116711db70b6SBjoern A. Zeeb 	nt = &ic->ic_sta;
116811db70b6SBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
116911db70b6SBjoern A. Zeeb 	if (islocked)
117011db70b6SBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
117111db70b6SBjoern A. Zeeb 
117211db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
117311db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
117411db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
117511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
117611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
117711db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
117811db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
117911db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
118011db70b6SBjoern A. Zeeb #endif
118111db70b6SBjoern A. Zeeb 		error = 1;
118211db70b6SBjoern A. Zeeb 		goto out;
118311db70b6SBjoern A. Zeeb 	}
118411db70b6SBjoern A. Zeeb 
118511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
118611db70b6SBjoern A. Zeeb 	if (error != 0) {
118711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
118811db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
118911db70b6SBjoern A. Zeeb 		error = 0;
119011db70b6SBjoern A. Zeeb 		goto out;
119111db70b6SBjoern A. Zeeb 	}
119211db70b6SBjoern A. Zeeb 
119311db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
119411db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
119511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
119611db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
119711db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
119811db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
119911db70b6SBjoern A. Zeeb #endif
120011db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
120111db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
120211db70b6SBjoern A. Zeeb 	error = 1;
120311db70b6SBjoern A. Zeeb out:
120411db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
120511db70b6SBjoern A. Zeeb 	if (islocked)
120611db70b6SBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
120711db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
120811db70b6SBjoern A. Zeeb 	return (error);
120911db70b6SBjoern A. Zeeb }
121011db70b6SBjoern A. Zeeb 
121111db70b6SBjoern A. Zeeb static int
121211db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
121311db70b6SBjoern A. Zeeb {
121411db70b6SBjoern A. Zeeb 
121511db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
121611db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
121711db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
121811db70b6SBjoern A. Zeeb }
121911db70b6SBjoern A. Zeeb 
122011db70b6SBjoern A. Zeeb static int
122111db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
122211db70b6SBjoern A. Zeeb {
122311db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
122411db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
122511db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
122611db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
122711db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
122811db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
122911db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
123011db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
123111db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
123211db70b6SBjoern A. Zeeb 	uint32_t lcipher;
123311db70b6SBjoern A. Zeeb 	int error;
123411db70b6SBjoern A. Zeeb 
123511db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
123611db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
123711db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
123811db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
123911db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
124011db70b6SBjoern A. Zeeb 
124111db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
124211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
124311db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
124411db70b6SBjoern A. Zeeb 		return (0);
124511db70b6SBjoern A. Zeeb 	}
124611db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
124711db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
124811db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
124911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
125011db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
125111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
125211db70b6SBjoern A. Zeeb 		return (0);
125311db70b6SBjoern A. Zeeb 	}
125411db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
125511db70b6SBjoern A. Zeeb 
125611db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
125711db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
125811db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
125911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
126011db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
126111db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
126211db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
126311db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
126411db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
126511db70b6SBjoern A. Zeeb 	}
126611db70b6SBjoern A. Zeeb 
126711db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
12686b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
126911db70b6SBjoern A. Zeeb 	switch (lcipher) {
127011db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
127111db70b6SBjoern A. Zeeb 		break;
127211db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
127311db70b6SBjoern A. Zeeb 	default:
127412a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
127512a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
127611db70b6SBjoern A. Zeeb 		IMPROVE();
127711db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
127811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
127911db70b6SBjoern A. Zeeb 		return (0);
128011db70b6SBjoern A. Zeeb 	}
128111db70b6SBjoern A. Zeeb 
128211db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
128311db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
128411db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
128511db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
12866b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
12876b4cac81SBjoern A. Zeeb #if 0
12886b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
12896b4cac81SBjoern A. Zeeb #endif
12906b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
12916b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
12926b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
12936b4cac81SBjoern A. Zeeb 
129411db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
129511db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
129611db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
129711db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
129811db70b6SBjoern A. Zeeb 
12996b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
13006b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
13016b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
13026b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
13036b4cac81SBjoern A. Zeeb 		break;
13046b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
13056b4cac81SBjoern A. Zeeb 	default:
130611db70b6SBjoern A. Zeeb 		/* currently UNREACH */
13076b4cac81SBjoern A. Zeeb 		IMPROVE();
130811db70b6SBjoern A. Zeeb 		break;
13096b4cac81SBjoern A. Zeeb 	};
131011db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
13116b4cac81SBjoern A. Zeeb 
131211db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
131311db70b6SBjoern A. Zeeb 	if (error != 0) {
131411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
131511db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
131611db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
131711db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
131811db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
131911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
132011db70b6SBjoern A. Zeeb 		return (0);
13216b4cac81SBjoern A. Zeeb 	}
13226b4cac81SBjoern A. Zeeb 
132311db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
132411db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
132511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
132611db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
132711db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
132811db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
132911db70b6SBjoern A. Zeeb #endif
133011db70b6SBjoern A. Zeeb 
133111db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
133211db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
13336b4cac81SBjoern A. Zeeb 	return (1);
13346b4cac81SBjoern A. Zeeb }
13356b4cac81SBjoern A. Zeeb 
13366b4cac81SBjoern A. Zeeb static  int
13376b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
13386b4cac81SBjoern A. Zeeb {
13396b4cac81SBjoern A. Zeeb 
134011db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
13416b4cac81SBjoern A. Zeeb }
13426b4cac81SBjoern A. Zeeb #endif
13436b4cac81SBjoern A. Zeeb 
13446b4cac81SBjoern A. Zeeb static u_int
13456b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
13466b4cac81SBjoern A. Zeeb {
13476b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
13486b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13496b4cac81SBjoern A. Zeeb 
13506b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
13516b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
13526b4cac81SBjoern A. Zeeb 
13536b4cac81SBjoern A. Zeeb 	mc_list = arg;
13546b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
13556b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
13566b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
13576b4cac81SBjoern A. Zeeb 			return (0);
13586b4cac81SBjoern A. Zeeb 	}
13596b4cac81SBjoern A. Zeeb 
13606b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
13616b4cac81SBjoern A. Zeeb 	if (addr == NULL)
13626b4cac81SBjoern A. Zeeb 		return (0);
13636b4cac81SBjoern A. Zeeb 
13646b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
13656b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
13666b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
13676b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
13686b4cac81SBjoern A. Zeeb 	mc_list->count++;
13696b4cac81SBjoern A. Zeeb 
13709d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13719d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13726b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
13736b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
13749d9ba2b7SBjoern A. Zeeb #endif
13756b4cac81SBjoern A. Zeeb 
13766b4cac81SBjoern A. Zeeb 	return (1);
13776b4cac81SBjoern A. Zeeb }
13786b4cac81SBjoern A. Zeeb 
13796b4cac81SBjoern A. Zeeb static void
13806b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
13816b4cac81SBjoern A. Zeeb {
13826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13846b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
13856b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
13866b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13876b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
13886b4cac81SBjoern A. Zeeb 	u64 mc;
13896b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
13906b4cac81SBjoern A. Zeeb 
13916b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
13926b4cac81SBjoern A. Zeeb 
13936b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
13946b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
13956b4cac81SBjoern A. Zeeb 		return;
13966b4cac81SBjoern A. Zeeb 
13976b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
13986b4cac81SBjoern A. Zeeb 		return;
13996b4cac81SBjoern A. Zeeb 
14006b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
14016b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
14026b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
14036b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
14046b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
14056b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
14066b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
14076b4cac81SBjoern A. Zeeb 	} else {
14086b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
14096b4cac81SBjoern A. Zeeb 	}
14106b4cac81SBjoern A. Zeeb 
14116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14126b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
14136b4cac81SBjoern A. Zeeb 	/*
14146b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
14156b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
14166b4cac81SBjoern A. Zeeb 	 */
14176b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
14186b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
14196b4cac81SBjoern A. Zeeb 
14209d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14219d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
14226b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
14236b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
14249d9ba2b7SBjoern A. Zeeb #endif
14256b4cac81SBjoern A. Zeeb 
14266b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
14276b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
14286b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
14296b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
14306b4cac81SBjoern A. Zeeb 			mc_list.count--;
14316b4cac81SBjoern A. Zeeb 		}
14326b4cac81SBjoern A. Zeeb 	}
14336b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
14346b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
14356b4cac81SBjoern A. Zeeb }
14366b4cac81SBjoern A. Zeeb 
1437fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1438fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1439fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1440fa8f007dSBjoern A. Zeeb {
1441fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1442fa8f007dSBjoern A. Zeeb 
1443fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1444fa8f007dSBjoern A. Zeeb 
14459d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14469d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1447fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1448fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1449ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1450fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1451616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1452fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1453fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1454fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1455fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1456ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14579d9ba2b7SBjoern A. Zeeb #endif
1458fa8f007dSBjoern A. Zeeb 
1459fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1460fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1461fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1462fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1463fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1464fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1465fa8f007dSBjoern A. Zeeb 	}
1466fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1467fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1468fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1469fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1470fa8f007dSBjoern A. Zeeb 	}
1471fa8f007dSBjoern A. Zeeb 
1472fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1473fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1474fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1475fa8f007dSBjoern A. Zeeb 
14769d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14779d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1478fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1479fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1480ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1481fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1482616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1483fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1484fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1485fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1486fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1487ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14889d9ba2b7SBjoern A. Zeeb #endif
1489fa8f007dSBjoern A. Zeeb 
1490fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1491fa8f007dSBjoern A. Zeeb }
1492fa8f007dSBjoern A. Zeeb 
14936b4cac81SBjoern A. Zeeb static void
14946b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
14956b4cac81SBjoern A. Zeeb {
14966b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14976b4cac81SBjoern A. Zeeb 	int error;
14988ac540d3SBjoern A. Zeeb 	bool cancel;
14996b4cac81SBjoern A. Zeeb 
15008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15018ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15038ac540d3SBjoern A. Zeeb 	if (!cancel)
15046b4cac81SBjoern A. Zeeb 		return;
15056b4cac81SBjoern A. Zeeb 
15066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15076b4cac81SBjoern A. Zeeb 
1508bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1509bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15106b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
15116b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
15128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15136b4cac81SBjoern A. Zeeb 
15146b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
15158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
15168ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
15178ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
15188ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
15198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
15208ac540d3SBjoern A. Zeeb 
1521bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
15226b4cac81SBjoern A. Zeeb 
15238ac540d3SBjoern A. Zeeb 	if (cancel)
15246b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
15256b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
15266b4cac81SBjoern A. Zeeb }
15276b4cac81SBjoern A. Zeeb 
15286b4cac81SBjoern A. Zeeb static void
1529086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1530086be6a8SBjoern A. Zeeb {
1531086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1532086be6a8SBjoern A. Zeeb 	int error;
1533086be6a8SBjoern A. Zeeb 	bool old;
1534086be6a8SBjoern A. Zeeb 
1535086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1536086be6a8SBjoern A. Zeeb 	if (old == new)
1537086be6a8SBjoern A. Zeeb 		return;
1538086be6a8SBjoern A. Zeeb 
1539086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1540086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1541086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1542086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1543086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1544086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1545086be6a8SBjoern A. Zeeb 	}
1546086be6a8SBjoern A. Zeeb }
1547086be6a8SBjoern A. Zeeb 
15485a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
15496b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
15506b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
15516b4cac81SBjoern A. Zeeb {
15525a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
15535a4d2461SBjoern A. Zeeb 
15545a4d2461SBjoern A. Zeeb 	changed = 0;
15556b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1556616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
15576b4cac81SBjoern A. Zeeb 
15586b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
15596b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
15606b4cac81SBjoern A. Zeeb 
1561616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1562616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
15636b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
15646b4cac81SBjoern A. Zeeb 		IMPROVE();
1565086be6a8SBjoern A. Zeeb 
15665a4d2461SBjoern A. Zeeb 		/*
15675a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
15685a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
15695a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
15705a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
15715a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
15725a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
15735a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
15745a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
15755a4d2461SBjoern A. Zeeb 		 */
15766b4cac81SBjoern A. Zeeb 	}
15775a4d2461SBjoern A. Zeeb 
15785a4d2461SBjoern A. Zeeb 	return (changed);
15796b4cac81SBjoern A. Zeeb }
15806b4cac81SBjoern A. Zeeb 
15816b4cac81SBjoern A. Zeeb static void
15826b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
15836b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
15846b4cac81SBjoern A. Zeeb {
15856b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
15866b4cac81SBjoern A. Zeeb 	int tid;
1587eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
15886b4cac81SBjoern A. Zeeb 
15896b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
15906b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
15916b4cac81SBjoern A. Zeeb 
15926b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
15936b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
15946b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
15956b4cac81SBjoern A. Zeeb 				continue;
15966b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
15976b4cac81SBjoern A. Zeeb 			continue;
15986b4cac81SBjoern A. Zeeb 
15996b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
16006b4cac81SBjoern A. Zeeb 			continue;
16016b4cac81SBjoern A. Zeeb 
16026b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
16036b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
16046b4cac81SBjoern A. Zeeb 			continue;
16056b4cac81SBjoern A. Zeeb 
1606eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1607eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1608eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1609eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
16106b4cac81SBjoern A. Zeeb 			continue;
16116b4cac81SBjoern A. Zeeb 
16126b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
16136b4cac81SBjoern A. Zeeb 	}
16146b4cac81SBjoern A. Zeeb }
16156b4cac81SBjoern A. Zeeb 
161688665349SBjoern A. Zeeb /*
161788665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
161888665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
161988665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
162088665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
162188665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
162288665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
162388665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
162488665349SBjoern A. Zeeb  * re-used.
162588665349SBjoern A. Zeeb  */
162688665349SBjoern A. Zeeb static void
162788665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
162888665349SBjoern A. Zeeb {
162988665349SBjoern A. Zeeb 	struct mbufq mq;
163088665349SBjoern A. Zeeb 	struct mbuf *m;
163188665349SBjoern A. Zeeb 	int len;
163288665349SBjoern A. Zeeb 
163388665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
163488665349SBjoern A. Zeeb 
163588665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
163688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
163788665349SBjoern A. Zeeb 	lsta->txq_ready = false;
163888665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
163988665349SBjoern A. Zeeb 
164088665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
164188665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
164288665349SBjoern A. Zeeb 
164388665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
164488665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
164588665349SBjoern A. Zeeb 	if (len <= 0) {
164688665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
164788665349SBjoern A. Zeeb 		return;
164888665349SBjoern A. Zeeb 	}
164988665349SBjoern A. Zeeb 
165088665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
165188665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
165288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
165388665349SBjoern A. Zeeb 
165488665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
165588665349SBjoern A. Zeeb 	while (m != NULL) {
165688665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
165788665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
165888665349SBjoern A. Zeeb 	}
165988665349SBjoern A. Zeeb }
166088665349SBjoern A. Zeeb 
16616b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
16626b4cac81SBjoern A. Zeeb 
16636b4cac81SBjoern A. Zeeb static int
16646b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16656b4cac81SBjoern A. Zeeb {
16666b4cac81SBjoern A. Zeeb 
16676b4cac81SBjoern A. Zeeb 	return (0);
16686b4cac81SBjoern A. Zeeb }
16696b4cac81SBjoern A. Zeeb 
16706b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
16716b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
16726b4cac81SBjoern A. Zeeb 
16736b4cac81SBjoern A. Zeeb static int
16746b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16756b4cac81SBjoern A. Zeeb {
16766b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1677c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1678d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
16796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16836b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16846b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16856b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
16866b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16876b4cac81SBjoern A. Zeeb 	uint32_t changed;
16886b4cac81SBjoern A. Zeeb 	int error;
16896b4cac81SBjoern A. Zeeb 
16902ac8a218SBjoern A. Zeeb 	/*
16912ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
16922ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
16932ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
16942ac8a218SBjoern A. Zeeb 	 */
16952ac8a218SBjoern A. Zeeb 
16962ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
16972ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
16982ac8a218SBjoern A. Zeeb 		return (EINVAL);
16992ac8a218SBjoern A. Zeeb 	}
17000936c648SBjoern A. Zeeb 	/*
17010936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
17020936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
17030936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
17040936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
17050936c648SBjoern A. Zeeb 	 */
17062ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
17072ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
17082ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
17092ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
17100936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17112ac8a218SBjoern A. Zeeb 		return (EINVAL);
17126b4cac81SBjoern A. Zeeb 	}
17136b4cac81SBjoern A. Zeeb 
17146b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17152ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
17162ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
17172ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
17182ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
17190936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17202ac8a218SBjoern A. Zeeb 		return (ESRCH);
17212ac8a218SBjoern A. Zeeb 	}
17222ac8a218SBjoern A. Zeeb 
17236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17246b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17256b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17266b4cac81SBjoern A. Zeeb 
17270936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17280936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
17290936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
17300936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17310936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17320936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17330936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17340936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
173592690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
173692690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17370936c648SBjoern A. Zeeb 		return (EBUSY);
17380936c648SBjoern A. Zeeb 	}
17390936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17400936c648SBjoern A. Zeeb 
17416b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17428ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17436b4cac81SBjoern A. Zeeb 
17446b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
17456b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1746d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1747d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
17486b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
17496b4cac81SBjoern A. Zeeb 	} else {
17506b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1751c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
17526b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1753d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
17546b4cac81SBjoern A. Zeeb 	}
17556b4cac81SBjoern A. Zeeb 
1756d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1757389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1758d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
17596b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1760d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1761d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
176290d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
176390d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
17649fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
17652ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
17662ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
17679fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
17689fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
176990d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1770d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
177190d8e307SBjoern A. Zeeb 		else
1772d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
17739fb91463SBjoern A. Zeeb 	}
17749fb91463SBjoern A. Zeeb #endif
17759fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
17769fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
17779fb91463SBjoern A. Zeeb #ifdef __notyet__
177890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1779d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
178090d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1781d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
178290d8e307SBjoern A. Zeeb 		else
178390d8e307SBjoern A. Zeeb #endif
178490d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1785d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
17869fb91463SBjoern A. Zeeb 	}
17879fb91463SBjoern A. Zeeb #endif
1788389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
17896b4cac81SBjoern A. Zeeb 	/* Responder ... */
179090d8e307SBjoern A. Zeeb #if 0
179190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
1792d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
179390d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
179490d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
179590d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
179690d8e307SBjoern A. Zeeb #endif
179790d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
179890d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
179990d8e307SBjoern A. Zeeb #else
180090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
180190d8e307SBjoern A. Zeeb #endif
18026b4cac81SBjoern A. Zeeb 
18036ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
18046ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
18056ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
18066ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
18076ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
18086ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
18096ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
18106ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
18116ffb7bd4SBjoern A. Zeeb 
18126ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
18136ffb7bd4SBjoern A. Zeeb 
18146ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
18156ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
18166ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
18176ffb7bd4SBjoern A. Zeeb 
18186ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
18196ffb7bd4SBjoern A. Zeeb 
18206b4cac81SBjoern A. Zeeb 	error = 0;
18216b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
18226b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
18236b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
18246b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
18256b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1826d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
18276b4cac81SBjoern A. Zeeb 	} else {
1828d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
18296b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
18307b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
18317b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
18327b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1833d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
18347b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1835d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
18366b4cac81SBjoern A. Zeeb 		} else {
1837018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1838018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
18396b4cac81SBjoern A. Zeeb 			goto out;
18406b4cac81SBjoern A. Zeeb 		}
18416ffb7bd4SBjoern A. Zeeb 
1842d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
18436ffb7bd4SBjoern A. Zeeb 
18446b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
18456b4cac81SBjoern A. Zeeb 		if (error == 0)
184668541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1847d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
18486b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
18496b4cac81SBjoern A. Zeeb 			error = 0;
18506b4cac81SBjoern A. Zeeb 		if (error != 0) {
1851018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1852018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1853d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1854d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1855c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
18566b4cac81SBjoern A. Zeeb 			goto out;
18576b4cac81SBjoern A. Zeeb 		}
18586b4cac81SBjoern A. Zeeb 	}
18596b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
18606b4cac81SBjoern A. Zeeb 
18616b4cac81SBjoern A. Zeeb 	/* RATES */
18626b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
18636b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
18646b4cac81SBjoern A. Zeeb 
1865d9f59799SBjoern A. Zeeb 	/*
18660936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
18670936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
18680936c648SBjoern A. Zeeb 	 * under the hoods.
1869d9f59799SBjoern A. Zeeb 	 */
18700936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
18710936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
18726b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1873e24e8103SBjoern A. Zeeb 
187488665349SBjoern A. Zeeb 	/*
187588665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
187688665349SBjoern A. Zeeb 	 * that we can tx again.
187788665349SBjoern A. Zeeb 	 */
187888665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
187988665349SBjoern A. Zeeb 	lsta->txq_ready = true;
188088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
188188665349SBjoern A. Zeeb 
1882a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
18832ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1884a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1885a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1886e24e8103SBjoern A. Zeeb 
1887d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
18886b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18896b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
18906b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1891e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
18926b4cac81SBjoern A. Zeeb 	if (error != 0) {
18936b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1894018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1895018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18966b4cac81SBjoern A. Zeeb 		goto out;
18976b4cac81SBjoern A. Zeeb 	}
18986b4cac81SBjoern A. Zeeb #if 0
18996b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
19006b4cac81SBjoern A. Zeeb #endif
19016b4cac81SBjoern A. Zeeb 
19029d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19039d9ba2b7SBjoern A. Zeeb 
19041665ef97SBjoern A. Zeeb #if 0
19056b4cac81SBjoern A. Zeeb 	/*
19066b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
19076b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
19086b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
19096b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1910d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1911d9f59799SBjoern A. Zeeb 	 * for all queues.
19126b4cac81SBjoern A. Zeeb 	 */
1913e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
19141665ef97SBjoern A. Zeeb #endif
19156b4cac81SBjoern A. Zeeb 
19166b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
19176b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19186b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
19196b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
19206b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
19216b4cac81SBjoern A. Zeeb 
19226b4cac81SBjoern A. Zeeb 	/*
19236b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
19246b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
19256b4cac81SBjoern A. Zeeb 	 * - event_callback
19266b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
19276b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
19286b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
19296b4cac81SBjoern A. Zeeb 	 */
19306b4cac81SBjoern A. Zeeb 
1931105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1932105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1933105b9df2SBjoern A. Zeeb 
1934105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1935105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1936105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1937105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1938105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1939105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1940105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1941105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1942105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1943105b9df2SBjoern A. Zeeb 
1944105b9df2SBjoern A. Zeeb 	/*
1945105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1946105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1947105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1948105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1949105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1950105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1951105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1952105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1953105b9df2SBjoern A. Zeeb 	 */
1954105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1955105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1956105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1957105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1958105b9df2SBjoern A. Zeeb 	} else {
1959105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1960105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1961105b9df2SBjoern A. Zeeb 		/*
1962105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1963105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1964105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1965105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1966105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1967105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1968105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1969105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1970105b9df2SBjoern A. Zeeb 		 */
1971105b9df2SBjoern A. Zeeb 	}
1972105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1973105b9df2SBjoern A. Zeeb 	goto out_relocked;
1974105b9df2SBjoern A. Zeeb 
19756b4cac81SBjoern A. Zeeb out:
19768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19776b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1978105b9df2SBjoern A. Zeeb out_relocked:
19790936c648SBjoern A. Zeeb 	/*
1980105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
19810936c648SBjoern A. Zeeb 	 * during the work of this function.
19820936c648SBjoern A. Zeeb 	 */
19836b4cac81SBjoern A. Zeeb 	if (ni != NULL)
19846b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
19856b4cac81SBjoern A. Zeeb 	return (error);
19866b4cac81SBjoern A. Zeeb }
19876b4cac81SBjoern A. Zeeb 
19886b4cac81SBjoern A. Zeeb static int
19896b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19906b4cac81SBjoern A. Zeeb {
19916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19936b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19946b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19956b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19966b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19976b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19986b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19996b4cac81SBjoern A. Zeeb 	int error;
20006b4cac81SBjoern A. Zeeb 
20016b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20026b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20036b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20046b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20056b4cac81SBjoern A. Zeeb 
20062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20072ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20082ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
20092ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
20102ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20112ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20122ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20132ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20142ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20152ac8a218SBjoern A. Zeeb #endif
20162ac8a218SBjoern A. Zeeb 
20172ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20182ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20192ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
20202ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
20212ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
20222ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
20236b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
20246b4cac81SBjoern A. Zeeb 
202567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20266b4cac81SBjoern A. Zeeb 
20276b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
20296b4cac81SBjoern A. Zeeb 
2030d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2031d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2032d9f59799SBjoern A. Zeeb 
20336b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
203488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
20356b4cac81SBjoern A. Zeeb 
20366b4cac81SBjoern A. Zeeb 	/* flush, no drop */
20376b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
20386b4cac81SBjoern A. Zeeb 
20396b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20406b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20416b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20426b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
20436b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20446b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20456b4cac81SBjoern A. Zeeb 	}
20466b4cac81SBjoern A. Zeeb 
20476b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
20486b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
20496b4cac81SBjoern A. Zeeb 
20506b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
20516b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2052d9f59799SBjoern A. Zeeb 
2053d9f59799SBjoern A. Zeeb 	/* Take the station down. */
20546b4cac81SBjoern A. Zeeb 
20556b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
20566b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20576b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2058d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2059e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
20606b4cac81SBjoern A. Zeeb 	if (error != 0) {
20616b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2062018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2063018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20646b4cac81SBjoern A. Zeeb 		goto out;
20656b4cac81SBjoern A. Zeeb 	}
20666b4cac81SBjoern A. Zeeb #if 0
20676b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
20686b4cac81SBjoern A. Zeeb #endif
20696b4cac81SBjoern A. Zeeb 
207067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20716b4cac81SBjoern A. Zeeb 
20722ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20732ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
20742ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
20752ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
20762ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2077d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
20780936c648SBjoern A. Zeeb 	/*
20790936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
20800936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
20810936c648SBjoern A. Zeeb 	 * and potentially freed.
20820936c648SBjoern A. Zeeb 	 */
20830936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2084d9f59799SBjoern A. Zeeb 
2085d9f59799SBjoern A. Zeeb 	/* conf_tx */
2086d9f59799SBjoern A. Zeeb 
2087d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
20886b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2089c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2090d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
20916b4cac81SBjoern A. Zeeb 
2092d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
20936b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
209468541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
20956b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
20966b4cac81SBjoern A. Zeeb 
20975a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
20985a4d2461SBjoern A. Zeeb 
20996b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
2100d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2101d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2102c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
21036b4cac81SBjoern A. Zeeb 	}
21046b4cac81SBjoern A. Zeeb 
21056b4cac81SBjoern A. Zeeb out:
21068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21086b4cac81SBjoern A. Zeeb 	return (error);
21096b4cac81SBjoern A. Zeeb }
21106b4cac81SBjoern A. Zeeb 
21116b4cac81SBjoern A. Zeeb static int
21126b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21136b4cac81SBjoern A. Zeeb {
21146b4cac81SBjoern A. Zeeb 	int error;
21156b4cac81SBjoern A. Zeeb 
21166b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
21176b4cac81SBjoern A. Zeeb 	if (error == 0)
21186b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
21196b4cac81SBjoern A. Zeeb 	return (error);
21206b4cac81SBjoern A. Zeeb }
21216b4cac81SBjoern A. Zeeb 
21226b4cac81SBjoern A. Zeeb static int
21236b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21246b4cac81SBjoern A. Zeeb {
21256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21296b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21306b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21316b4cac81SBjoern A. Zeeb 	int error;
21326b4cac81SBjoern A. Zeeb 
21336b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21356b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21366b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21376b4cac81SBjoern A. Zeeb 
21386b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21398ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21402ac8a218SBjoern A. Zeeb 
21412ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21422ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21432ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21442ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21452ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21462ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21472ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21482ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21492ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21502ac8a218SBjoern A. Zeeb #endif
21512ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21522ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21532ac8a218SBjoern A. Zeeb 		goto out;
21542ac8a218SBjoern A. Zeeb 	}
21552ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21562ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21572ac8a218SBjoern A. Zeeb 
21582ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
21596b4cac81SBjoern A. Zeeb 
21606b4cac81SBjoern A. Zeeb 	/* Finish auth. */
21616b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
21626b4cac81SBjoern A. Zeeb 
21636b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
21646b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
21656b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2166e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2167018d93ecSBjoern A. Zeeb 	if (error != 0) {
2168018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2169018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21706b4cac81SBjoern A. Zeeb 		goto out;
2171018d93ecSBjoern A. Zeeb 	}
21726b4cac81SBjoern A. Zeeb 
21736b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21746b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21756b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21766b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
21776b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21786b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21796b4cac81SBjoern A. Zeeb 	}
21806b4cac81SBjoern A. Zeeb 
21816b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
21826b4cac81SBjoern A. Zeeb 
21836b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21846b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21856b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21866b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21876b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21886b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21896b4cac81SBjoern A. Zeeb 	}
21906b4cac81SBjoern A. Zeeb 
21916b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
219288665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
21936b4cac81SBjoern A. Zeeb 
21946b4cac81SBjoern A. Zeeb 	/*
21956b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
21966b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
21976b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
21986b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
21996b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
22006b4cac81SBjoern A. Zeeb 	 * - event_callback
22016b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
22026b4cac81SBjoern A. Zeeb 	 */
22036b4cac81SBjoern A. Zeeb 
22046b4cac81SBjoern A. Zeeb out:
22058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22066b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22076b4cac81SBjoern A. Zeeb 	return (error);
22086b4cac81SBjoern A. Zeeb }
22096b4cac81SBjoern A. Zeeb 
22106b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
22116b4cac81SBjoern A. Zeeb static int
22126b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22136b4cac81SBjoern A. Zeeb {
22146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22176b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22196b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22202ac8a218SBjoern A. Zeeb 	int error;
22216b4cac81SBjoern A. Zeeb 
22226b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22246b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22256b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22266b4cac81SBjoern A. Zeeb 
22276b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22292ac8a218SBjoern A. Zeeb 
22302ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22312ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
22322ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
22332ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22342ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22352ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22362ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22372ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22382ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22392ac8a218SBjoern A. Zeeb #endif
22402ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
22412ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
22422ac8a218SBjoern A. Zeeb 		goto out;
22432ac8a218SBjoern A. Zeeb 	}
22442ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22452ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22462ac8a218SBjoern A. Zeeb 
22472ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
22482ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
22496b4cac81SBjoern A. Zeeb 
22506b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
22516b4cac81SBjoern A. Zeeb 
22526b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22536b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22546b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22556b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
22566b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22576b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22586b4cac81SBjoern A. Zeeb 	}
22596b4cac81SBjoern A. Zeeb 
22606b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
22616b4cac81SBjoern A. Zeeb 
22626b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
22636b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
22646b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22656b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
22666b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
22676b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
22686b4cac81SBjoern A. Zeeb 	}
22696b4cac81SBjoern A. Zeeb 
22702ac8a218SBjoern A. Zeeb 	error = 0;
22712ac8a218SBjoern A. Zeeb out:
22728ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22736b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22746b4cac81SBjoern A. Zeeb 
22752ac8a218SBjoern A. Zeeb 	return (error);
22766b4cac81SBjoern A. Zeeb }
22776b4cac81SBjoern A. Zeeb 
22786b4cac81SBjoern A. Zeeb static int
2279d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22806b4cac81SBjoern A. Zeeb {
22816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22826b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22856b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22866b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22876b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22886b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2289d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
22906b4cac81SBjoern A. Zeeb 	int error;
22916b4cac81SBjoern A. Zeeb 
22926b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22936b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22946b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22956b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22966b4cac81SBjoern A. Zeeb 
22972ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22982ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22992ac8a218SBjoern A. Zeeb 
23002ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23012ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23022ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23032ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23042ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23052ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23062ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23072ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23082ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23092ac8a218SBjoern A. Zeeb #endif
23102ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23122ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
23132ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
23142ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
23152ac8a218SBjoern A. Zeeb 
23162ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
23176b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
23186b4cac81SBjoern A. Zeeb 
231967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2320d9f59799SBjoern A. Zeeb 
2321d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2322d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2323d9f59799SBjoern A. Zeeb 
2324d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2325d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2326d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2327d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2328d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2329ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2330d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2331d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2332d9f59799SBjoern A. Zeeb 	}
2333d9f59799SBjoern A. Zeeb 
23348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2335d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2336d9f59799SBjoern A. Zeeb 
233788665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2338d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2339018d93ecSBjoern A. Zeeb 	if (error != 0) {
2340018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2341018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2342d9f59799SBjoern A. Zeeb 		goto outni;
2343018d93ecSBjoern A. Zeeb 	}
2344d9f59799SBjoern A. Zeeb 
2345d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
234688665349SBjoern A. Zeeb 
234788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
234888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
234988665349SBjoern A. Zeeb 
23508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2351d9f59799SBjoern A. Zeeb 
235267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2353d9f59799SBjoern A. Zeeb 
2354d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
235588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2356d9f59799SBjoern A. Zeeb 
2357d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2358d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2359d9f59799SBjoern A. Zeeb 
23606b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23616b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23626b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23636b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2364ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
23656b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23666b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23676b4cac81SBjoern A. Zeeb 	}
23686b4cac81SBjoern A. Zeeb 
2369d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2370d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2371d9f59799SBjoern A. Zeeb 
2372d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2373d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2374d9f59799SBjoern A. Zeeb 
2375d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2376d9f59799SBjoern A. Zeeb 
23776b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
23786b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
23796b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
23806b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2381e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2382018d93ecSBjoern A. Zeeb 	if (error != 0) {
2383018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2384018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23856b4cac81SBjoern A. Zeeb 		goto out;
2386018d93ecSBjoern A. Zeeb 	}
23876b4cac81SBjoern A. Zeeb 
23885a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
23895a4d2461SBjoern A. Zeeb 	bss_changed = 0;
23905a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
23916b4cac81SBjoern A. Zeeb 
23925a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
239316e688b2SBjoern A. Zeeb 
2394d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2395d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2396d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2397d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2398e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2399d9f59799SBjoern A. Zeeb 	if (error != 0) {
2400d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2401018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2402018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2403d9f59799SBjoern A. Zeeb 		goto out;
2404d9f59799SBjoern A. Zeeb 	}
2405d9f59799SBjoern A. Zeeb 
240616e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2407d9f59799SBjoern A. Zeeb 
2408d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2409d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2410d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2411616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2412616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2413d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2414d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2415d9f59799SBjoern A. Zeeb 
24162ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24172ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24182ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24192ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2421d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24220936c648SBjoern A. Zeeb 	/*
24230936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24240936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24250936c648SBjoern A. Zeeb 	 * and potentially freed.
24260936c648SBjoern A. Zeeb 	 */
24270936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2428d9f59799SBjoern A. Zeeb 
2429d9f59799SBjoern A. Zeeb 	/* conf_tx */
2430d9f59799SBjoern A. Zeeb 
2431d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2432d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2433c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2434d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2435d9f59799SBjoern A. Zeeb 
2436d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2437d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
243868541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2439d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2440d9f59799SBjoern A. Zeeb 
24415a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24425a4d2461SBjoern A. Zeeb 
2443d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2444d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2445d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2446c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2447d9f59799SBjoern A. Zeeb 	}
2448d9f59799SBjoern A. Zeeb 
2449d9f59799SBjoern A. Zeeb 	error = EALREADY;
24506b4cac81SBjoern A. Zeeb out:
24518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
24526b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2453d9f59799SBjoern A. Zeeb outni:
24546b4cac81SBjoern A. Zeeb 	return (error);
24556b4cac81SBjoern A. Zeeb }
24566b4cac81SBjoern A. Zeeb 
24576b4cac81SBjoern A. Zeeb static int
2458d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2459d9f59799SBjoern A. Zeeb {
2460d9f59799SBjoern A. Zeeb 	int error;
2461d9f59799SBjoern A. Zeeb 
2462d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2463d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2464d9f59799SBjoern A. Zeeb 		return (error);
2465d9f59799SBjoern A. Zeeb 
2466d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2467d9f59799SBjoern A. Zeeb 
2468d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2469d9f59799SBjoern A. Zeeb 	return (error);
2470d9f59799SBjoern A. Zeeb }
2471d9f59799SBjoern A. Zeeb 
2472d9f59799SBjoern A. Zeeb static int
24736b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24746b4cac81SBjoern A. Zeeb {
24756b4cac81SBjoern A. Zeeb 	int error;
24766b4cac81SBjoern A. Zeeb 
2477d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24786b4cac81SBjoern A. Zeeb 	return (error);
24796b4cac81SBjoern A. Zeeb }
24806b4cac81SBjoern A. Zeeb 
24816b4cac81SBjoern A. Zeeb static int
24826b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(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_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24926b4cac81SBjoern A. Zeeb {
24936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24976b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
24986b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24996b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
25006b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
25016b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
25026b4cac81SBjoern A. Zeeb 	int error;
25036b4cac81SBjoern A. Zeeb 
25046b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25056b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25066b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25076b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25086b4cac81SBjoern A. Zeeb 
25096b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
25108ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
25112ac8a218SBjoern A. Zeeb 
25122ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25132ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
25142ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
25152ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25162ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25172ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25182ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25192ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25202ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25212ac8a218SBjoern A. Zeeb #endif
25222ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
25232ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
25242ac8a218SBjoern A. Zeeb 		goto out;
25252ac8a218SBjoern A. Zeeb 	}
25262ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25282ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25292ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25302ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25312ac8a218SBjoern A. Zeeb 
25322ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25336b4cac81SBjoern A. Zeeb 
25346b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
25356b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
25366b4cac81SBjoern A. Zeeb 
25379597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
25389597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
25399597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
25409597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
25416b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25429597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
25434a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25444a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
25454a67f1dfSBjoern A. Zeeb 		sta->wme = true;
25464a67f1dfSBjoern A. Zeeb #endif
2547e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2548018d93ecSBjoern A. Zeeb 	if (error != 0) {
2549018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2550018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25519597f7cbSBjoern A. Zeeb 		goto out;
2552018d93ecSBjoern A. Zeeb 	}
25536b4cac81SBjoern A. Zeeb 
25546b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
25556b4cac81SBjoern A. Zeeb 
25566b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
25576b4cac81SBjoern A. Zeeb 	bss_changed = 0;
25584a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25594a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
25604a67f1dfSBjoern A. Zeeb #endif
2561616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2562616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2563616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
25646b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
25656b4cac81SBjoern A. Zeeb 	}
25666b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2567616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2568616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
25696b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
25706b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
25716b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
25726b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25736b4cac81SBjoern A. Zeeb 	}
25746b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
25756b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
25766b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
25776b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25786b4cac81SBjoern A. Zeeb 	}
25796b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
25806b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
25816b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
25826b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
25836b4cac81SBjoern A. Zeeb 	}
2584fa8f007dSBjoern A. Zeeb 
2585fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2586fa8f007dSBjoern A. Zeeb 
25876b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
25886b4cac81SBjoern A. Zeeb 
25896b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
25906b4cac81SBjoern A. Zeeb 	 * - event_callback
25916b4cac81SBjoern A. Zeeb 	 */
25926b4cac81SBjoern A. Zeeb 
25936b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25946b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25956b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25966b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
25976b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25986b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25996b4cac81SBjoern A. Zeeb 	}
26006b4cac81SBjoern A. Zeeb 
2601086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2602086be6a8SBjoern A. Zeeb 
26036b4cac81SBjoern A. Zeeb 	/*
26046b4cac81SBjoern A. Zeeb 	 * And then:
26056b4cac81SBjoern A. Zeeb 	 * - (more packets)?
26066b4cac81SBjoern A. Zeeb 	 * - set_key
26076b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
26086b4cac81SBjoern A. Zeeb 	 * - set_key (?)
26096b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
26106b4cac81SBjoern A. Zeeb 	 */
26116b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
26126b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
26136b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
26146b4cac81SBjoern A. Zeeb 
26156b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
26166b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
26176b4cac81SBjoern A. Zeeb 	}
26186b4cac81SBjoern A. Zeeb 
2619f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
26209fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
2621f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
26229fb91463SBjoern A. Zeeb 
26236b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
26246b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26256b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
26266b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2627e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
26286b4cac81SBjoern A. Zeeb 	if (error != 0) {
26296b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2630018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2631018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26326b4cac81SBjoern A. Zeeb 		goto out;
26336b4cac81SBjoern A. Zeeb 	}
26346b4cac81SBjoern A. Zeeb 
26356b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
26366b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
26376b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
26386b4cac81SBjoern A. Zeeb 	 *
26396b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
26406b4cac81SBjoern A. Zeeb 	 */
26416b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
26426b4cac81SBjoern A. Zeeb 
2643fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2644fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2645fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2646fa8f007dSBjoern A. Zeeb 
26476b4cac81SBjoern A. Zeeb out:
26488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
26496b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
26506b4cac81SBjoern A. Zeeb 	return (error);
26516b4cac81SBjoern A. Zeeb }
26526b4cac81SBjoern A. Zeeb 
26536b4cac81SBjoern A. Zeeb static int
26546b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26556b4cac81SBjoern A. Zeeb {
26566b4cac81SBjoern A. Zeeb 	int error;
26576b4cac81SBjoern A. Zeeb 
26586b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
26596b4cac81SBjoern A. Zeeb 	if (error == 0)
26606b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
26616b4cac81SBjoern A. Zeeb 	return (error);
26626b4cac81SBjoern A. Zeeb }
26636b4cac81SBjoern A. Zeeb 
26646b4cac81SBjoern A. Zeeb static int
26656b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26666b4cac81SBjoern A. Zeeb {
26676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26686b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26696b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26706b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26716b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26726b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2674d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2675d9f59799SBjoern A. Zeeb #if 0
2676d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2677d9f59799SBjoern A. Zeeb #endif
26786b4cac81SBjoern A. Zeeb 	int error;
26796b4cac81SBjoern A. Zeeb 
26806b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26816b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26826b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26836b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26846b4cac81SBjoern A. Zeeb 
26852ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26862ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26872ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26882ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26892ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26902ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26912ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26922ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26932ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
26942ac8a218SBjoern A. Zeeb #endif
26952ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26962ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26972ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26982ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26992ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27002ac8a218SBjoern A. Zeeb 
27012ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27026b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27036b4cac81SBjoern A. Zeeb 
270467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2705d9f59799SBjoern A. Zeeb 
2706d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
27078ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2708d9f59799SBjoern A. Zeeb 
2709d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2710d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2711d9f59799SBjoern A. Zeeb 
2712d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2713d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2714d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2715d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2716d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2717ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2718d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2719d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2720d9f59799SBjoern A. Zeeb 	}
2721d9f59799SBjoern A. Zeeb 
27228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2723d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2724d9f59799SBjoern A. Zeeb 
2725d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2726d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2727018d93ecSBjoern A. Zeeb 	if (error != 0) {
2728018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2729018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2730d9f59799SBjoern A. Zeeb 		goto outni;
2731018d93ecSBjoern A. Zeeb 	}
2732d9f59799SBjoern A. Zeeb 
2733d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
273488665349SBjoern A. Zeeb 
273588665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
273688665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
273788665349SBjoern A. Zeeb 
27388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2739d9f59799SBjoern A. Zeeb 
274067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2741d9f59799SBjoern A. Zeeb 
2742d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
274388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2744d9f59799SBjoern A. Zeeb 
2745d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2746d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2747d9f59799SBjoern A. Zeeb 
2748d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2749d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2750d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2751d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2752ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2753d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2754d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2755d9f59799SBjoern A. Zeeb 	}
2756d9f59799SBjoern A. Zeeb 
2757d9f59799SBjoern A. Zeeb #if 0
2758d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2759d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2760d9f59799SBjoern A. Zeeb 
2761d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2762d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2763d9f59799SBjoern A. Zeeb #endif
2764d9f59799SBjoern A. Zeeb 
2765d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2766d9f59799SBjoern A. Zeeb 
27676b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
27686b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27696b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
27706b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2771e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2772018d93ecSBjoern A. Zeeb 	if (error != 0) {
2773018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2774018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27756b4cac81SBjoern A. Zeeb 		goto out;
2776018d93ecSBjoern A. Zeeb 	}
27776b4cac81SBjoern A. Zeeb 
277867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27796b4cac81SBjoern A. Zeeb 
278011db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
278111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
278211db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
278311db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
278411db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
278511db70b6SBjoern A. Zeeb 		if (error != 0) {
278611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
278711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
278811db70b6SBjoern A. Zeeb 			/*
278911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
279011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
279111db70b6SBjoern A. Zeeb 			 * less appropriate time).
279211db70b6SBjoern A. Zeeb 			 */
279311db70b6SBjoern A. Zeeb 			/* goto out; */
279411db70b6SBjoern A. Zeeb 		}
279511db70b6SBjoern A. Zeeb 	}
279611db70b6SBjoern A. Zeeb #endif
279711db70b6SBjoern A. Zeeb 
27986b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
27996b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28006b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28016b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2802e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2803018d93ecSBjoern A. Zeeb 	if (error != 0) {
2804018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2805018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28066b4cac81SBjoern A. Zeeb 		goto out;
2807018d93ecSBjoern A. Zeeb 	}
28086b4cac81SBjoern A. Zeeb 
280967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
28106b4cac81SBjoern A. Zeeb 
2811d9f59799SBjoern A. Zeeb #if 0
2812d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2813d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2814d9f59799SBjoern A. Zeeb #endif
2815d9f59799SBjoern A. Zeeb 
2816d9f59799SBjoern A. Zeeb 	error = EALREADY;
28176b4cac81SBjoern A. Zeeb out:
28188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
28196b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2820d9f59799SBjoern A. Zeeb outni:
28216b4cac81SBjoern A. Zeeb 	return (error);
28226b4cac81SBjoern A. Zeeb }
28236b4cac81SBjoern A. Zeeb 
28246b4cac81SBjoern A. Zeeb static int
2825d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2826d9f59799SBjoern A. Zeeb {
2827d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2828d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2829d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2830d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2831d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2832d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2833d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2834d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2835d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2836d9f59799SBjoern A. Zeeb 	int error;
2837d9f59799SBjoern A. Zeeb 
2838d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2839d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2840d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2841d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2842d9f59799SBjoern A. Zeeb 
28432ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
28442ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
28452ac8a218SBjoern A. Zeeb 
28462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28472ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28482ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
28492ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
28502ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28512ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28522ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28532ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28542ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28552ac8a218SBjoern A. Zeeb #endif
28562ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28582ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28592ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28602ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28612ac8a218SBjoern A. Zeeb 
28622ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2863d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2864d9f59799SBjoern A. Zeeb 
286567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2866d9f59799SBjoern A. Zeeb 
2867d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2868d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2869d9f59799SBjoern A. Zeeb 
2870d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2871d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2872d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2873d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2874d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2875ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2876d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2877d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2878d9f59799SBjoern A. Zeeb 	}
2879d9f59799SBjoern A. Zeeb 
28808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2881d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2882d9f59799SBjoern A. Zeeb 
2883d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2884d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2885018d93ecSBjoern A. Zeeb 	if (error != 0) {
2886018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2887018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2888d9f59799SBjoern A. Zeeb 		goto outni;
2889018d93ecSBjoern A. Zeeb 	}
2890d9f59799SBjoern A. Zeeb 
2891d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
289288665349SBjoern A. Zeeb 
289388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
289488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
289588665349SBjoern A. Zeeb 
28968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2897d9f59799SBjoern A. Zeeb 
289867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2899d9f59799SBjoern A. Zeeb 
2900d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
290188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2902d9f59799SBjoern A. Zeeb 
2903d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2904d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2905d9f59799SBjoern A. Zeeb 
2906d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2907d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2908d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2909d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2910ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2911d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2912d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2913d9f59799SBjoern A. Zeeb 	}
2914d9f59799SBjoern A. Zeeb 
2915d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2916d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2917d9f59799SBjoern A. Zeeb 
2918d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2919d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2920d9f59799SBjoern A. Zeeb 
2921d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2922d9f59799SBjoern A. Zeeb 
2923d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2924d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2925d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2926d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2927e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2928018d93ecSBjoern A. Zeeb 	if (error != 0) {
2929018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2930018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2931d9f59799SBjoern A. Zeeb 		goto out;
2932018d93ecSBjoern A. Zeeb 	}
2933d9f59799SBjoern A. Zeeb 
293467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2935d9f59799SBjoern A. Zeeb 
293611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
293711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
293811db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
293911db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
294011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
294111db70b6SBjoern A. Zeeb 		if (error != 0) {
294211db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
294311db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
294411db70b6SBjoern A. Zeeb 			/*
294511db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
294611db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
294711db70b6SBjoern A. Zeeb 			 * less appropriate time).
294811db70b6SBjoern A. Zeeb 			 */
294911db70b6SBjoern A. Zeeb 			/* goto out; */
295011db70b6SBjoern A. Zeeb 		}
295111db70b6SBjoern A. Zeeb 	}
295211db70b6SBjoern A. Zeeb #endif
295311db70b6SBjoern A. Zeeb 
2954d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2955d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2956d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2957d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2958e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2959018d93ecSBjoern A. Zeeb 	if (error != 0) {
2960018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2961018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2962d9f59799SBjoern A. Zeeb 		goto out;
2963018d93ecSBjoern A. Zeeb 	}
2964d9f59799SBjoern A. Zeeb 
296567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2966d9f59799SBjoern A. Zeeb 
2967d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2968d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2969d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2970d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2971e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2972018d93ecSBjoern A. Zeeb 	if (error != 0) {
2973018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2974018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2975d9f59799SBjoern A. Zeeb 		goto out;
2976018d93ecSBjoern A. Zeeb 	}
2977d9f59799SBjoern A. Zeeb 
29785a4d2461SBjoern A. Zeeb 	bss_changed = 0;
297916e688b2SBjoern A. Zeeb 	/*
29805a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
29815a4d2461SBjoern A. Zeeb 	 *
298216e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
29835a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
29845a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
29855a4d2461SBjoern A. Zeeb 	 *
29865a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
29875a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
29885a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
29895a4d2461SBjoern A. Zeeb 	 * a fw assert.
29905a4d2461SBjoern A. Zeeb 	 *
29915a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
29925a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
29935a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
29945a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
29955a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
29965a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
29975a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
29985a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
29995a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
30005a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
300116e688b2SBjoern A. Zeeb 	 */
30025a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
30035a4d2461SBjoern A. Zeeb 
30045a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
300516e688b2SBjoern A. Zeeb 
3006d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3007d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3008d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3009d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3010e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3011d9f59799SBjoern A. Zeeb 	if (error != 0) {
3012d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3013018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3014018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3015d9f59799SBjoern A. Zeeb 		goto out;
3016d9f59799SBjoern A. Zeeb 	}
3017d9f59799SBjoern A. Zeeb 
30185a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
30195a4d2461SBjoern A. Zeeb 
302016e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3021d9f59799SBjoern A. Zeeb 
3022d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3023d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3024d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3025616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3026616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3027d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
30285a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
30295a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
30305a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3031d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3032d9f59799SBjoern A. Zeeb 
30332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30342ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
30352ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
30362ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
30372ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30380936c648SBjoern A. Zeeb 	/*
30390936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
30400936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
30410936c648SBjoern A. Zeeb 	 * and potentially freed.
30420936c648SBjoern A. Zeeb 	 */
30430936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3044d9f59799SBjoern A. Zeeb 
3045d9f59799SBjoern A. Zeeb 	/* conf_tx */
3046d9f59799SBjoern A. Zeeb 
3047d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
3048d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
3049c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
3050d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
3051d9f59799SBjoern A. Zeeb 
3052d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
3053d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
305468541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
3055d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
3056d9f59799SBjoern A. Zeeb 
30575a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
30585a4d2461SBjoern A. Zeeb 
3059d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
3060d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
3061d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
3062c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
3063d9f59799SBjoern A. Zeeb 	}
3064d9f59799SBjoern A. Zeeb 
3065d9f59799SBjoern A. Zeeb 	error = EALREADY;
3066d9f59799SBjoern A. Zeeb out:
30678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3068d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3069d9f59799SBjoern A. Zeeb outni:
3070d9f59799SBjoern A. Zeeb 	return (error);
3071d9f59799SBjoern A. Zeeb }
3072d9f59799SBjoern A. Zeeb 
3073d9f59799SBjoern A. Zeeb static int
3074d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3075d9f59799SBjoern A. Zeeb {
3076d9f59799SBjoern A. Zeeb 
3077d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3078d9f59799SBjoern A. Zeeb }
3079d9f59799SBjoern A. Zeeb 
3080d9f59799SBjoern A. Zeeb static int
30816b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
30826b4cac81SBjoern A. Zeeb {
30836b4cac81SBjoern A. Zeeb 	int error;
30846b4cac81SBjoern A. Zeeb 
3085d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3086d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3087d9f59799SBjoern A. Zeeb 		return (error);
3088d9f59799SBjoern A. Zeeb 
3089d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3090d9f59799SBjoern A. Zeeb 
3091d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
30926b4cac81SBjoern A. Zeeb 	return (error);
30936b4cac81SBjoern A. Zeeb }
30946b4cac81SBjoern A. Zeeb 
3095d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
30966b4cac81SBjoern A. Zeeb 
30976b4cac81SBjoern A. Zeeb /*
30986b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
30996b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
31006b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
31016b4cac81SBjoern A. Zeeb  */
31026b4cac81SBjoern A. Zeeb struct fsm_state {
31036b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
31046b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31056b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
31066b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
31076b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
31086b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
31096b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
31106b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3111d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3112d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
31136b4cac81SBjoern A. Zeeb 
31146b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31156b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
31166b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
31176b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3118d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
31196b4cac81SBjoern A. Zeeb 
3120d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3121d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3122d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3123d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3124d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
31256b4cac81SBjoern A. Zeeb 
3126d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3127d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3128d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
31296b4cac81SBjoern A. Zeeb 
31306b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
31316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
31326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
31336b4cac81SBjoern A. Zeeb 
31346b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
31356b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
31366b4cac81SBjoern A. Zeeb };
31376b4cac81SBjoern A. Zeeb 
31386b4cac81SBjoern A. Zeeb static int
31396b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
31406b4cac81SBjoern A. Zeeb {
31416b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
31426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
31436b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31446b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31456b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
31466b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31476b4cac81SBjoern A. Zeeb 	int error;
31486b4cac81SBjoern A. Zeeb 
31496b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
31506b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
31516b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
31526b4cac81SBjoern A. Zeeb 
31539d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31549d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
31556b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
31566b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31579d9ba2b7SBjoern A. Zeeb #endif
31586b4cac81SBjoern A. Zeeb 
31596b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
31606b4cac81SBjoern A. Zeeb 
31616b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
31626b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
31636b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
31646b4cac81SBjoern A. Zeeb 
31656b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
31666b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
31676b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
31686b4cac81SBjoern A. Zeeb 
31696b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
31706b4cac81SBjoern A. Zeeb 
31716b4cac81SBjoern A. Zeeb 	} else {
31726b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
31736b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
31746b4cac81SBjoern A. Zeeb 		return (ENOSYS);
31756b4cac81SBjoern A. Zeeb 	}
31766b4cac81SBjoern A. Zeeb 
31776b4cac81SBjoern A. Zeeb 	error = 0;
31786b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
31796b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
31809d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31819d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3182d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3183d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3184d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3185d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
31869d9ba2b7SBjoern A. Zeeb #endif
31876b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
31886b4cac81SBjoern A. Zeeb 			break;
31896b4cac81SBjoern A. Zeeb 		}
31906b4cac81SBjoern A. Zeeb 	}
31916b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
31926b4cac81SBjoern A. Zeeb 
31936b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3194d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
31956b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
31966b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
31976b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31986b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
31996b4cac81SBjoern A. Zeeb 		return (ENOSYS);
32006b4cac81SBjoern A. Zeeb 	}
32016b4cac81SBjoern A. Zeeb 
32026b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
32039d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32049d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3205d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3206d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3207d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3208d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
32099d9ba2b7SBjoern A. Zeeb #endif
32106b4cac81SBjoern A. Zeeb 		return (0);
32116b4cac81SBjoern A. Zeeb 	}
32126b4cac81SBjoern A. Zeeb 
32136b4cac81SBjoern A. Zeeb 	if (error != 0) {
32146b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
32156b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
32166b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
32176b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
321845c27ad5SBjoern A. Zeeb 		return (error);
32196b4cac81SBjoern A. Zeeb 	}
32206b4cac81SBjoern A. Zeeb 
32219d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
32229d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3223d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3224d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
32256b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
32269d9ba2b7SBjoern A. Zeeb #endif
32276b4cac81SBjoern A. Zeeb 
32286b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
32296b4cac81SBjoern A. Zeeb }
32306b4cac81SBjoern A. Zeeb 
32316b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32326b4cac81SBjoern A. Zeeb 
3233d9f59799SBjoern A. Zeeb /*
3234d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3235d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3236d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3237d9f59799SBjoern A. Zeeb  */
3238d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3239d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3240d9f59799SBjoern A. Zeeb {
3241d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32422ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3243d9f59799SBjoern A. Zeeb 
32442ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3245d9f59799SBjoern A. Zeeb 
3246ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32472ac8a218SBjoern A. Zeeb 
32482ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32492ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32512ac8a218SBjoern A. Zeeb 
32522ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
32532ac8a218SBjoern A. Zeeb 	return (rni);
3254d9f59799SBjoern A. Zeeb }
3255d9f59799SBjoern A. Zeeb 
32564a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
32576b4cac81SBjoern A. Zeeb static int
32584a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
32596b4cac81SBjoern A. Zeeb {
32604a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
32616b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32626b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32646b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
32656b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
32666b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
32676b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
32686b4cac81SBjoern A. Zeeb 	int error;
32696b4cac81SBjoern A. Zeeb 	uint16_t ac;
32706b4cac81SBjoern A. Zeeb 
32716b4cac81SBjoern A. Zeeb 	IMPROVE();
32726b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
32736b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
32746b4cac81SBjoern A. Zeeb 
32756b4cac81SBjoern A. Zeeb 	if (vap == NULL)
32766b4cac81SBjoern A. Zeeb 		return (0);
32776b4cac81SBjoern A. Zeeb 
32784a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
32794a67f1dfSBjoern A. Zeeb 		return (0);
32804a67f1dfSBjoern A. Zeeb 
32816b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
32826b4cac81SBjoern A. Zeeb 		return (0);
32836b4cac81SBjoern A. Zeeb 
32844a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
32854a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
32864a67f1dfSBjoern A. Zeeb 		return (0);
32874a67f1dfSBjoern A. Zeeb 	}
32884a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
32896b4cac81SBjoern A. Zeeb 
32904a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
32916b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
32926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
32936b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
32946b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
32956b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
32966b4cac81SBjoern A. Zeeb 
32974a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32984a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32994a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33004a67f1dfSBjoern A. Zeeb 
33016b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
33026b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
33036b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
33046b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
33056b4cac81SBjoern A. Zeeb 
33066b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
33076b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
33086b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
33096b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
33106b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
33116b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
331268541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
33136b4cac81SBjoern A. Zeeb 		if (error != 0)
33143f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
33156b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
33166b4cac81SBjoern A. Zeeb 	}
33176b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
33186b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
33194a67f1dfSBjoern A. Zeeb 	if (!planned)
33206b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
33214a67f1dfSBjoern A. Zeeb 
33224a67f1dfSBjoern A. Zeeb 	return (changed);
33234a67f1dfSBjoern A. Zeeb }
33246b4cac81SBjoern A. Zeeb #endif
33256b4cac81SBjoern A. Zeeb 
33264a67f1dfSBjoern A. Zeeb static int
33274a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
33284a67f1dfSBjoern A. Zeeb {
33294a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
33304a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
33314a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33324a67f1dfSBjoern A. Zeeb 
33334a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
33344a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
33354a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
33366b4cac81SBjoern A. Zeeb 		return (0);
33374a67f1dfSBjoern A. Zeeb 
33384a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
33394a67f1dfSBjoern A. Zeeb 
33404a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
33414a67f1dfSBjoern A. Zeeb #endif
33424a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
33436b4cac81SBjoern A. Zeeb }
33446b4cac81SBjoern A. Zeeb 
33454aff4048SBjoern A. Zeeb /*
33464aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
33474aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
33484aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
33494aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
33504aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
33514aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3352edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3353edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3354edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3355edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
33564aff4048SBjoern A. Zeeb  */
33574aff4048SBjoern A. Zeeb static void
33584aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
33594aff4048SBjoern A. Zeeb {
33604aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
33614aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33624aff4048SBjoern A. Zeeb 
33634aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3364edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3365edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3366edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
33674aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
33684aff4048SBjoern A. Zeeb 		return;
33694aff4048SBjoern A. Zeeb 	}
33704aff4048SBjoern A. Zeeb 
33714aff4048SBjoern A. Zeeb 	vif = arg;
337257609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
33734aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
33744aff4048SBjoern A. Zeeb }
33754aff4048SBjoern A. Zeeb 
33766b4cac81SBjoern A. Zeeb static struct ieee80211vap *
33776b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
33786b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
33796b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
33806b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
33816b4cac81SBjoern A. Zeeb {
33826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33846b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33856b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
33866b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3387a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
33886b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
338940839418SBjoern A. Zeeb 	struct sysctl_oid *node;
33906b4cac81SBjoern A. Zeeb 	size_t len;
3391e3a0b120SBjoern A. Zeeb 	int error, i;
3392a6042e17SBjoern A. Zeeb 	uint16_t ac;
33936b4cac81SBjoern A. Zeeb 
33946b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
33956b4cac81SBjoern A. Zeeb 		return (NULL);
33966b4cac81SBjoern A. Zeeb 
33976b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33986b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33996b4cac81SBjoern A. Zeeb 
34006b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
34016b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
34026b4cac81SBjoern A. Zeeb 
34036b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
34046b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3405a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
34062ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
34072ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34086b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
34096b4cac81SBjoern A. Zeeb 
34106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
34116b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
34126b4cac81SBjoern A. Zeeb 	vif->p2p = false;
34136b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
34146b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
34156b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
34166b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
34176b4cac81SBjoern A. Zeeb 	IMPROVE();
34186b4cac81SBjoern A. Zeeb 
34196b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
34206b4cac81SBjoern A. Zeeb #if 1
34216b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3422adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
34236ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
34246ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
34254aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
34264aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
34276ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
34287b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
34296b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
34306b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
34316b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
34326b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
34336b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3434616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3435616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3436616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3437616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
34386ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3439caaa79c3SBjoern A. Zeeb 	/*
3440caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3441caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3442caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3443caaa79c3SBjoern A. Zeeb 	 */
34446ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
34456b4cac81SBjoern A. Zeeb #endif
34466b4cac81SBjoern A. Zeeb #if 0
34476b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
34486b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
34496b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
34506b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
34516b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
34526b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
34536b4cac81SBjoern A. Zeeb #endif
3454e3a0b120SBjoern A. Zeeb 
34556ffb7bd4SBjoern A. Zeeb 	/* Link Config */
34566ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
34576ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
34586ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
34596ffb7bd4SBjoern A. Zeeb 	}
34606ffb7bd4SBjoern A. Zeeb 
3461e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3462e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3463e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3464e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3465e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3466e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3467e3a0b120SBjoern A. Zeeb 		else
3468e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
34690cbcfa19SBjoern A. Zeeb 
34700cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
34710cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3472e3a0b120SBjoern A. Zeeb 	}
3473e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3474e3a0b120SBjoern A. Zeeb 
34756b4cac81SBjoern A. Zeeb 	IMPROVE();
34766b4cac81SBjoern A. Zeeb 
34776b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
34786b4cac81SBjoern A. Zeeb 	if (error != 0) {
34793f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
34806b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34816b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
34826b4cac81SBjoern A. Zeeb 		return (NULL);
34836b4cac81SBjoern A. Zeeb 	}
34846b4cac81SBjoern A. Zeeb 
34856b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
34866b4cac81SBjoern A. Zeeb 	if (error != 0) {
34876b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
34883f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %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 
34948891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
34956b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
34968891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
34976b4cac81SBjoern A. Zeeb 
34986b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
34996b4cac81SBjoern A. Zeeb 	changed = 0;
35006b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35016b4cac81SBjoern A. Zeeb 
3502a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3503a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3504a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3505a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3506a6042e17SBjoern A. Zeeb 
3507a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3508a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3509a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3510a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3511a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3512a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3513a6042e17SBjoern A. Zeeb 		if (error != 0)
3514a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3515a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3516a6042e17SBjoern A. Zeeb 	}
3517a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3518a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3519a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35206b4cac81SBjoern A. Zeeb 
35216b4cac81SBjoern A. Zeeb 	/* Force MC init. */
35226b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
35236b4cac81SBjoern A. Zeeb 
35246b4cac81SBjoern A. Zeeb 	IMPROVE();
35256b4cac81SBjoern A. Zeeb 
35266b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
35276b4cac81SBjoern A. Zeeb 
35286b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
35296b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
35306b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3531d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3532d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
35336b4cac81SBjoern A. Zeeb 
3534b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
353511db70b6SBjoern A. Zeeb 	/* Key management. */
353611db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
35376b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
35386b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
35396b4cac81SBjoern A. Zeeb 	}
354011db70b6SBjoern A. Zeeb #endif
35416b4cac81SBjoern A. Zeeb 
35429fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
35439fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
35449fb91463SBjoern A. Zeeb #endif
35459fb91463SBjoern A. Zeeb 
35466b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
35476b4cac81SBjoern A. Zeeb 
35486b4cac81SBjoern A. Zeeb 	/* Complete setup. */
35496b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
35506b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
35516b4cac81SBjoern A. Zeeb 
355211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
355311db70b6SBjoern A. Zeeb 	/*
355411db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
355511db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
355611db70b6SBjoern A. Zeeb 	 */
355711db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
355811db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
355911db70b6SBjoern A. Zeeb #endif
3560ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3561ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3562ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3563ac2c7271SBjoern A. Zeeb #endif
356411db70b6SBjoern A. Zeeb 
35656b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
35666b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
35676b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
35686b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
35696b4cac81SBjoern A. Zeeb 
35706b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
35716b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
35726b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
35736b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
35746b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
35756b4cac81SBjoern A. Zeeb 	/* any others? */
357640839418SBjoern A. Zeeb 
357740839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
357840839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
357940839418SBjoern A. Zeeb 
358040839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
358140839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
358240839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
358340839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
358440839418SBjoern A. Zeeb 
358540839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
358640839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
358740839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
358840839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
358940839418SBjoern A. Zeeb 
35906b4cac81SBjoern A. Zeeb 	IMPROVE();
35916b4cac81SBjoern A. Zeeb 
35926b4cac81SBjoern A. Zeeb 	return (vap);
35936b4cac81SBjoern A. Zeeb }
35946b4cac81SBjoern A. Zeeb 
35954b0af114SBjoern A. Zeeb void
35964b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
35974b0af114SBjoern A. Zeeb {
35984b0af114SBjoern A. Zeeb 
35994b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
36004b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
36014b0af114SBjoern A. Zeeb 
36024b0af114SBjoern A. Zeeb 	IMPROVE();
36034b0af114SBjoern A. Zeeb }
36044b0af114SBjoern A. Zeeb 
36054b0af114SBjoern A. Zeeb void
36064b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
36074b0af114SBjoern A. Zeeb {
36084b0af114SBjoern A. Zeeb 
36094b0af114SBjoern A. Zeeb 	TODO();
36104b0af114SBjoern A. Zeeb }
36114b0af114SBjoern A. Zeeb 
36126b4cac81SBjoern A. Zeeb static void
36136b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
36146b4cac81SBjoern A. Zeeb {
36156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36196b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36206b4cac81SBjoern A. Zeeb 
36216b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
36226b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36236b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
36246b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36256b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36266b4cac81SBjoern A. Zeeb 
36274aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
36284aff4048SBjoern A. Zeeb 
362940839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
363040839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
363140839418SBjoern A. Zeeb 
36328891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36336b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
36348891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36356b4cac81SBjoern A. Zeeb 
36366b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
36376b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3638dbf76919SBjoern A. Zeeb 
3639dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3640dbf76919SBjoern A. Zeeb 
3641dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3642dbf76919SBjoern A. Zeeb 
36436c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
36447b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
36456c38c6b1SBjoern A. Zeeb 
36466b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
36476b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
36486b4cac81SBjoern A. Zeeb }
36496b4cac81SBjoern A. Zeeb 
36506b4cac81SBjoern A. Zeeb static void
36516b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
36526b4cac81SBjoern A. Zeeb {
36536b4cac81SBjoern A. Zeeb 
36546b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
36556b4cac81SBjoern A. Zeeb 	TRACEOK();
36566b4cac81SBjoern A. Zeeb }
36576b4cac81SBjoern A. Zeeb 
36586b4cac81SBjoern A. Zeeb static void
36596b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
36606b4cac81SBjoern A. Zeeb {
36616b4cac81SBjoern A. Zeeb 
36626b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36636b4cac81SBjoern A. Zeeb }
36646b4cac81SBjoern A. Zeeb 
36656b4cac81SBjoern A. Zeeb static void
36666b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
36676b4cac81SBjoern A. Zeeb {
36686b4cac81SBjoern A. Zeeb 
36696b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36706b4cac81SBjoern A. Zeeb }
36716b4cac81SBjoern A. Zeeb 
36726b4cac81SBjoern A. Zeeb /* Start / stop device. */
36736b4cac81SBjoern A. Zeeb static void
36746b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
36756b4cac81SBjoern A. Zeeb {
36766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36778d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36786b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36796b4cac81SBjoern A. Zeeb 	int error;
36808d58a057SBjoern A. Zeeb #endif
36816b4cac81SBjoern A. Zeeb 	bool start_all;
36826b4cac81SBjoern A. Zeeb 
36836b4cac81SBjoern A. Zeeb 	IMPROVE();
36846b4cac81SBjoern A. Zeeb 
36856b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36868d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36876b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36888d58a057SBjoern A. Zeeb #endif
36896b4cac81SBjoern A. Zeeb 	start_all = false;
36906b4cac81SBjoern A. Zeeb 
36918ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
36928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
36936b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
36948d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36956b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
36966b4cac81SBjoern A. Zeeb 		if (error == 0)
36978d58a057SBjoern A. Zeeb #endif
36986b4cac81SBjoern A. Zeeb 			start_all = true;
36996b4cac81SBjoern A. Zeeb 	} else {
37008d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
37017b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
37028d58a057SBjoern A. Zeeb #endif
37036b4cac81SBjoern A. Zeeb 	}
37048ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
37058ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
37066b4cac81SBjoern A. Zeeb 
37076b4cac81SBjoern A. Zeeb 	if (start_all)
37086b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
37096b4cac81SBjoern A. Zeeb }
37106b4cac81SBjoern A. Zeeb 
37116b4cac81SBjoern A. Zeeb bool
37126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
37136b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
37146b4cac81SBjoern A. Zeeb {
37156b4cac81SBjoern A. Zeeb 	int i;
37166b4cac81SBjoern A. Zeeb 
37176b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
37186b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
37196b4cac81SBjoern A. Zeeb 			return (true);
37206b4cac81SBjoern A. Zeeb 	}
37216b4cac81SBjoern A. Zeeb 
37226b4cac81SBjoern A. Zeeb 	return (false);
37236b4cac81SBjoern A. Zeeb }
37246b4cac81SBjoern A. Zeeb 
37256b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
37266b4cac81SBjoern A. Zeeb bool
37276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
37286b4cac81SBjoern A. Zeeb {
37296b4cac81SBjoern A. Zeeb 	size_t x;
37306b4cac81SBjoern A. Zeeb 	uint8_t l;
37316b4cac81SBjoern A. Zeeb 
37326b4cac81SBjoern A. Zeeb 	x = *xp;
37336b4cac81SBjoern A. Zeeb 
37346b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
37356b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
37366b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
37376b4cac81SBjoern A. Zeeb 	x += 2 + l;
37386b4cac81SBjoern A. Zeeb 
37396b4cac81SBjoern A. Zeeb 	if (x > ies_len)
37406b4cac81SBjoern A. Zeeb 		return (false);
37416b4cac81SBjoern A. Zeeb 
37426b4cac81SBjoern A. Zeeb 	*xp = x;
37436b4cac81SBjoern A. Zeeb 	return (true);
37446b4cac81SBjoern A. Zeeb }
37456b4cac81SBjoern A. Zeeb 
3746d9945d78SBjoern A. Zeeb static uint8_t *
3747d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3748d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
37496b4cac81SBjoern A. Zeeb {
3750d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3751d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
37523dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3753d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3754d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3755d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3756d9945d78SBjoern A. Zeeb 	int band, i;
37576b4cac81SBjoern A. Zeeb 
37583dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3759d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3760d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3761d9945d78SBjoern A. Zeeb 			continue;
3762d9945d78SBjoern A. Zeeb 
3763d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3764d9945d78SBjoern A. Zeeb 		/*
3765d9945d78SBjoern A. Zeeb 		 * This should not happen;
3766d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3767d9945d78SBjoern A. Zeeb 		 */
3768d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3769d9945d78SBjoern A. Zeeb 			continue;
3770d9945d78SBjoern A. Zeeb 
3771d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3772d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3773d9945d78SBjoern A. Zeeb 		chan = NULL;
3774d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3775d9945d78SBjoern A. Zeeb 
3776d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3777d9945d78SBjoern A. Zeeb 				continue;
3778d9945d78SBjoern A. Zeeb 
37793dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3780d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3781d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3782d9945d78SBjoern A. Zeeb 				break;
3783d9945d78SBjoern A. Zeeb 		}
3784d9945d78SBjoern A. Zeeb 
3785d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3786d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3787d9945d78SBjoern A. Zeeb 			continue;
3788d9945d78SBjoern A. Zeeb 
3789d9945d78SBjoern A. Zeeb 		pb = p;
37903dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3791d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3792d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3793d9945d78SBjoern A. Zeeb 
37943dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
37953dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
37963dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
37973dd98026SBjoern A. Zeeb 
37983dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37993dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
38003dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
38013dd98026SBjoern A. Zeeb 		}
38023dd98026SBjoern A. Zeeb #endif
38033dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
38043dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 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 			c = ieee80211_vht_adjust_channel(ic, c,
38103dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
38113dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
38123dd98026SBjoern A. Zeeb 		}
38133dd98026SBjoern A. Zeeb #endif
38143dd98026SBjoern A. Zeeb 
3815d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3816d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3817d9945d78SBjoern A. Zeeb 	}
3818d9945d78SBjoern A. Zeeb 
3819d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3820d9945d78SBjoern A. Zeeb 	pb = p;
3821d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3822d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3823d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3824d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3825d9945d78SBjoern A. Zeeb 	}
3826d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3827d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3828d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3829d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3830d9945d78SBjoern A. Zeeb 	}
3831d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3832d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3833d9945d78SBjoern A. Zeeb 
3834d9945d78SBjoern A. Zeeb 	return (p);
38356b4cac81SBjoern A. Zeeb }
38366b4cac81SBjoern A. Zeeb 
38376b4cac81SBjoern A. Zeeb static void
38386b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
38396b4cac81SBjoern A. Zeeb {
38406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38446b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
38456b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
38466b4cac81SBjoern A. Zeeb 	int error;
38478ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
38486b4cac81SBjoern A. Zeeb 
38496b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3851a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
38526b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
38538ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38546b4cac81SBjoern A. Zeeb 		return;
38556b4cac81SBjoern A. Zeeb 	}
38568ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
38578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38586b4cac81SBjoern A. Zeeb 
38596b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
38606b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
38616b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3862d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
38636b4cac81SBjoern A. Zeeb 		return;
38646b4cac81SBjoern A. Zeeb 	}
38656b4cac81SBjoern A. Zeeb 
38666b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38678ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3868a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3869a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3870d3ef7fb4SBjoern A. Zeeb sw_scan:
38716b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
38726b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3873086be6a8SBjoern A. Zeeb 
3874086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3875086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3876086be6a8SBjoern A. Zeeb 
38776b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
38786b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
38796b4cac81SBjoern A. Zeeb 		IMPROVE();
38806b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
38816b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
38826b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
38836b4cac81SBjoern A. Zeeb 
38846b4cac81SBjoern A. Zeeb 	} else {
38856b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
38866b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
38876b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
38886b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
38896b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3890d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3891d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3892d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
38933206587aSBjoern A. Zeeb 		bool running;
3894d9945d78SBjoern A. Zeeb 
3895d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3896d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
38976b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
38986b4cac81SBjoern A. Zeeb 
3899d9945d78SBjoern A. Zeeb 		band_mask = 0;
39006b4cac81SBjoern A. Zeeb 		nchan = 0;
3901c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3902c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3903d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
39046b4cac81SBjoern A. Zeeb 				nchan++;
3905d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3906d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3907d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3908d9945d78SBjoern A. Zeeb 			}
3909c272abc5SBjoern A. Zeeb #else
3910c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3911c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3912c272abc5SBjoern A. Zeeb 				switch (band) {
3913c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3914c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3915c272abc5SBjoern A. Zeeb 					break;
3916c272abc5SBjoern A. Zeeb 				default:
3917c272abc5SBjoern A. Zeeb 					continue;
3918c272abc5SBjoern A. Zeeb 				}
3919c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3920c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3921c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3922c272abc5SBjoern A. Zeeb 				}
3923c272abc5SBjoern A. Zeeb 			}
3924c272abc5SBjoern A. Zeeb #endif
3925c272abc5SBjoern A. Zeeb 		} else {
39263206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
39273206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
39283206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
39293206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
39303206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
39313206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
39323206587aSBjoern A. Zeeb 			 */
39333206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
39343206587aSBjoern A. Zeeb 		}
39353206587aSBjoern A. Zeeb 
39366b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
39376b4cac81SBjoern A. Zeeb 
3938d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3939d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3940d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3941d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3942d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3943d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3944d9945d78SBjoern A. Zeeb 
3945d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3946d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3947d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3948d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3949d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3950d9945d78SBjoern A. Zeeb 		}
3951d9945d78SBjoern A. Zeeb 
39523206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3953d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3954d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
39556b4cac81SBjoern A. Zeeb 
39566b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
39576b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
39586b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
39596b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
39606b4cac81SBjoern A. Zeeb #if 0
39616b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
39626b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
39636b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
39646b4cac81SBjoern A. Zeeb #endif
39656b4cac81SBjoern A. Zeeb #ifdef __notyet__
39666b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
39676b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
39686b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
39696b4cac81SBjoern A. Zeeb #endif
3970e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
39716b4cac81SBjoern A. Zeeb 
39726b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
39736b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
39746b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
39756b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
39766b4cac81SBjoern A. Zeeb 			*(cpp + i) =
39776b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
39786b4cac81SBjoern A. Zeeb 		}
3979c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
39806b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3981c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
39826b4cac81SBjoern A. Zeeb 
3983c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
39846b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
39853206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
39866b4cac81SBjoern A. Zeeb 			/* lc->flags */
39876b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
39886b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
39896b4cac81SBjoern A. Zeeb 			/* lc-> ... */
39906b4cac81SBjoern A. Zeeb 			lc++;
39916b4cac81SBjoern A. Zeeb 		}
3992c272abc5SBjoern A. Zeeb #else
3993c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3994c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3995c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3996c272abc5SBjoern A. Zeeb 
3997c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3998c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3999c272abc5SBjoern A. Zeeb 				continue;
4000c272abc5SBjoern A. Zeeb 
4001c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4002c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4003c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4004c272abc5SBjoern A. Zeeb 				continue;
4005c272abc5SBjoern A. Zeeb 
4006c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4007c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4008c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4009c272abc5SBjoern A. Zeeb 				lc++;
4010c272abc5SBjoern A. Zeeb 			}
4011c272abc5SBjoern A. Zeeb 		}
4012c272abc5SBjoern A. Zeeb #endif
40136b4cac81SBjoern A. Zeeb 
4014d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
40156b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
40166b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
40176b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4018d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
40196b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
40206b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
40216b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
40226b4cac81SBjoern A. Zeeb 				ssids++;
40236b4cac81SBjoern A. Zeeb 			}
40246b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
40256b4cac81SBjoern A. Zeeb 		} else {
40266b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
40276b4cac81SBjoern A. Zeeb 		}
40286b4cac81SBjoern A. Zeeb 
40296b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
40306b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
40316b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
40326b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
40336b4cac81SBjoern A. Zeeb 		/* s6gp->... */
40346b4cac81SBjoern A. Zeeb 
4035d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4036d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4037d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4038d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4039d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4040d9945d78SBjoern A. Zeeb 
40416b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40426b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
40433206587aSBjoern A. Zeeb 
40443206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
40453206587aSBjoern A. Zeeb 		/* Re-check under lock. */
40463206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
40473206587aSBjoern A. Zeeb 		if (!running) {
40483206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
40493206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
40503206587aSBjoern A. Zeeb 
40513206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
40523206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
40533206587aSBjoern A. Zeeb 		}
40543206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40553206587aSBjoern A. Zeeb 		if (running) {
40563206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
40573206587aSBjoern A. Zeeb 			return;
40583206587aSBjoern A. Zeeb 		}
40593206587aSBjoern A. Zeeb 
40606b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
40616b4cac81SBjoern A. Zeeb 		if (error != 0) {
4062d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4063d9945d78SBjoern A. Zeeb 
40643206587aSBjoern A. Zeeb 			/*
40653206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
40663206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
40673206587aSBjoern A. Zeeb 			 * and only there.
40683206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
40693206587aSBjoern A. Zeeb 			 * behave differently:
40703206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
40713206587aSBjoern A. Zeeb 			 *        and can then still return an error.
40723206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
40733206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
40743206587aSBjoern A. Zeeb 			 * ...
40753206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
40763206587aSBjoern A. Zeeb 			 * and balance between both code paths.
40773206587aSBjoern A. Zeeb 			 */
40783206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
40793206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40803206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
40816b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
40823206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
40833206587aSBjoern A. Zeeb 			}
40843206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4085d3ef7fb4SBjoern A. Zeeb 
4086d3ef7fb4SBjoern A. Zeeb 			/*
4087d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4088d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4089d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4090d3ef7fb4SBjoern A. Zeeb 			 */
4091196cfd0bSBjoern A. Zeeb 			if (error == 1) {
40928ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4093a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
40948ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40953206587aSBjoern A. Zeeb 				/*
40963206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
40973206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
40983206587aSBjoern A. Zeeb 				 * currently not re-enable it?
40993206587aSBjoern A. Zeeb 				 */
41003206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4101196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4102196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4103196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4104196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4105196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4106196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4107196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4108196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4109d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4110196cfd0bSBjoern A. Zeeb 			}
4111d3ef7fb4SBjoern A. Zeeb 
4112d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4113d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
41146b4cac81SBjoern A. Zeeb 		}
41156b4cac81SBjoern A. Zeeb 	}
41166b4cac81SBjoern A. Zeeb }
41176b4cac81SBjoern A. Zeeb 
41186b4cac81SBjoern A. Zeeb static void
41196b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
41206b4cac81SBjoern A. Zeeb {
41216b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41228ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
41236b4cac81SBjoern A. Zeeb 
41246b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4126a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
41278ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41286b4cac81SBjoern A. Zeeb 		return;
41296b4cac81SBjoern A. Zeeb 	}
41308ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41318ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41326b4cac81SBjoern A. Zeeb 
41338ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4134a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4135a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
41366b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
41376b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
41386b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
41396b4cac81SBjoern A. Zeeb 
4140a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4141a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
41426b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
41436b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41446b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4145a486fbbdSBjoern A. Zeeb 
41466b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
41476b4cac81SBjoern A. Zeeb 
41486b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4149086be6a8SBjoern A. Zeeb 
4150086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4151086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
41526b4cac81SBjoern A. Zeeb 	}
41536b4cac81SBjoern A. Zeeb }
41546b4cac81SBjoern A. Zeeb 
41556b4cac81SBjoern A. Zeeb static void
4156a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4157a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4158a486fbbdSBjoern A. Zeeb {
4159a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41608ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4161a486fbbdSBjoern A. Zeeb 
4162a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41648ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41668ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4167a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4168a486fbbdSBjoern A. Zeeb }
4169a486fbbdSBjoern A. Zeeb 
4170a486fbbdSBjoern A. Zeeb static void
4171a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4172a486fbbdSBjoern A. Zeeb {
4173a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41748ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4175a486fbbdSBjoern A. Zeeb 
4176a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41788ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41808ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4181a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4182a486fbbdSBjoern A. Zeeb }
4183a486fbbdSBjoern A. Zeeb 
4184a486fbbdSBjoern A. Zeeb static void
41856b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
41866b4cac81SBjoern A. Zeeb {
41876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4189b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4190b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
41916b4cac81SBjoern A. Zeeb 	int error;
41928ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
41936b4cac81SBjoern A. Zeeb 
41946b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4195b2cf3c21SBjoern A. Zeeb 
4196b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4197b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
41986b4cac81SBjoern A. Zeeb 		return;
41996b4cac81SBjoern A. Zeeb 
4200a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
42018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
42028ac540d3SBjoern A. Zeeb 	hw_scan_running =
42038ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
42048ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
42058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42068ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4207a486fbbdSBjoern A. Zeeb 		return;
4208a486fbbdSBjoern A. Zeeb 
4209b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4210b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
42116b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
42126b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
42136b4cac81SBjoern A. Zeeb 		return;
42146b4cac81SBjoern A. Zeeb 	}
42156b4cac81SBjoern A. Zeeb 
42166b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
42176b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
42186b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
42196b4cac81SBjoern A. Zeeb 		    c, chan);
42206b4cac81SBjoern A. Zeeb 		return;
42216b4cac81SBjoern A. Zeeb 	}
42226b4cac81SBjoern A. Zeeb 
42236b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
42246b4cac81SBjoern A. Zeeb 	IMPROVE();
42256b4cac81SBjoern A. Zeeb 
42266b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42274a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
42289fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
422911450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
42309fb91463SBjoern A. Zeeb #endif
42314a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
42326b4cac81SBjoern A. Zeeb 
42336b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
42346b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
42356b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
42366b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
42376b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
42386b4cac81SBjoern A. Zeeb 		IMPROVE();
42396b4cac81SBjoern A. Zeeb 	} else {
42406b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
42416b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
42426b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
42436b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
42446b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
42456b4cac81SBjoern A. Zeeb 	}
42466b4cac81SBjoern A. Zeeb 
42476b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
42486b4cac81SBjoern A. Zeeb 	IMPROVE();
42496b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
42506b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
42516b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
42526b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
42536b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
42546b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
42556b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
42566b4cac81SBjoern A. Zeeb 			    error);
42576b4cac81SBjoern A. Zeeb 	}
42586b4cac81SBjoern A. Zeeb }
42596b4cac81SBjoern A. Zeeb 
42606b4cac81SBjoern A. Zeeb static struct ieee80211_node *
42616b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
42626b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
42636b4cac81SBjoern A. Zeeb {
42646b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42664f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
42676b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
42686b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42696b4cac81SBjoern A. Zeeb 
42706b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
42716b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42726b4cac81SBjoern A. Zeeb 
42736b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
42744f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
42754f61ef8bSBjoern A. Zeeb 		return (NULL);
42764f61ef8bSBjoern A. Zeeb 
42776b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
42786b4cac81SBjoern A. Zeeb 	if (ni == NULL)
42796b4cac81SBjoern A. Zeeb 		return (NULL);
42806b4cac81SBjoern A. Zeeb 
42816b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42824f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
42836b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
42846b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
42856b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
42866b4cac81SBjoern A. Zeeb 		return (NULL);
42876b4cac81SBjoern A. Zeeb 	}
42886b4cac81SBjoern A. Zeeb 
42896b4cac81SBjoern A. Zeeb 	return (ni);
42906b4cac81SBjoern A. Zeeb }
42916b4cac81SBjoern A. Zeeb 
42926b4cac81SBjoern A. Zeeb static int
42936b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
42946b4cac81SBjoern A. Zeeb {
42956b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42976b4cac81SBjoern A. Zeeb 	int error;
42986b4cac81SBjoern A. Zeeb 
42996b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43006b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43016b4cac81SBjoern A. Zeeb 
43026b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
43036b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
43046b4cac81SBjoern A. Zeeb 		if (error != 0)
43056b4cac81SBjoern A. Zeeb 			return (error);
43066b4cac81SBjoern A. Zeeb 	}
43076b4cac81SBjoern A. Zeeb 
43086b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
43096b4cac81SBjoern A. Zeeb 	IMPROVE();
43106b4cac81SBjoern A. Zeeb 
43116b4cac81SBjoern A. Zeeb 	return (0);
43126b4cac81SBjoern A. Zeeb }
43136b4cac81SBjoern A. Zeeb 
43146b4cac81SBjoern A. Zeeb static void
43156b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
43166b4cac81SBjoern A. Zeeb {
43176b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43186b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43196b4cac81SBjoern A. Zeeb 
43206b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43216b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43226b4cac81SBjoern A. Zeeb 
43236b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
43246b4cac81SBjoern A. Zeeb 	IMPROVE();
43256b4cac81SBjoern A. Zeeb 
43266b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
43276b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
43286b4cac81SBjoern A. Zeeb }
43296b4cac81SBjoern A. Zeeb 
43306b4cac81SBjoern A. Zeeb static void
43316b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
43326b4cac81SBjoern A. Zeeb {
43336b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43356b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43366b4cac81SBjoern A. Zeeb 
43376b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43386b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43396b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
43406b4cac81SBjoern A. Zeeb 
43410936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
43426b4cac81SBjoern A. Zeeb 
43430936c648SBjoern A. Zeeb 	/*
43440936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
43450936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
43460936c648SBjoern A. Zeeb 	 */
43470936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
43486b4cac81SBjoern A. Zeeb 
43496b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
43506b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
43516b4cac81SBjoern A. Zeeb }
43526b4cac81SBjoern A. Zeeb 
43536b4cac81SBjoern A. Zeeb static int
43546b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
43556b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
43566b4cac81SBjoern A. Zeeb {
43576b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43586b4cac81SBjoern A. Zeeb 
43596b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4360fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
43612372f8ccSBjoern A. Zeeb #if 0
436288665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
43632372f8ccSBjoern A. Zeeb #else
43642372f8ccSBjoern A. Zeeb 	/*
43652372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
43662372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
43672372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
43682372f8ccSBjoern A. Zeeb 	 */
43692372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
43702372f8ccSBjoern A. Zeeb #endif
4371fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4372fa4e4257SBjoern A. Zeeb 		/*
4373fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4374fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4375fa4e4257SBjoern A. Zeeb 		 */
43760936c648SBjoern A. Zeeb 		m_free(m);
43770936c648SBjoern A. Zeeb 		return (ENETDOWN);
43780936c648SBjoern A. Zeeb 	}
43796b4cac81SBjoern A. Zeeb 
43806b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
43816b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4382fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4383fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
43846b4cac81SBjoern A. Zeeb 
43859d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
43869d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
43876b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
43886b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
43896b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
43909d9ba2b7SBjoern A. Zeeb #endif
43916b4cac81SBjoern A. Zeeb 
43926b4cac81SBjoern A. Zeeb 	return (0);
43936b4cac81SBjoern A. Zeeb }
43946b4cac81SBjoern A. Zeeb 
439511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
439611db70b6SBjoern A. Zeeb static int
439711db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
439811db70b6SBjoern A. Zeeb     struct sk_buff *skb)
439911db70b6SBjoern A. Zeeb {
440011db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
440111db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
440211db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
440311db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
440411db70b6SBjoern A. Zeeb 	uint8_t *p;
440511db70b6SBjoern A. Zeeb 
440611db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
440711db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
440811db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
440911db70b6SBjoern A. Zeeb 
441011db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
441111db70b6SBjoern A. Zeeb 
441211db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
441311db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
441411db70b6SBjoern A. Zeeb 
441511db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
441611db70b6SBjoern A. Zeeb 	if (kc == NULL) {
441711db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
441811db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
441911db70b6SBjoern A. Zeeb 		return (ENXIO);
442011db70b6SBjoern A. Zeeb 	}
442111db70b6SBjoern A. Zeeb 
442211db70b6SBjoern A. Zeeb 
442311db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
442411db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
442511db70b6SBjoern A. Zeeb 
442611db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
442711db70b6SBjoern A. Zeeb 
442811db70b6SBjoern A. Zeeb 	/*
442911db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
443011db70b6SBjoern A. Zeeb 	 * or if we are done?
443111db70b6SBjoern A. Zeeb 	 */
443211db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
443311db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
443411db70b6SBjoern A. Zeeb 	    /* MFP */
443511db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
443611db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
443711db70b6SBjoern A. Zeeb 			return (0);
443811db70b6SBjoern A. Zeeb 
443911db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
444011db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
444111db70b6SBjoern A. Zeeb 		return (ENOSPC);
444211db70b6SBjoern A. Zeeb 
444311db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
444411db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
444511db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
444611db70b6SBjoern A. Zeeb 
444711db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
444811db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
444911db70b6SBjoern A. Zeeb 		return (0);
445011db70b6SBjoern A. Zeeb 
445111db70b6SBjoern A. Zeeb 	p += hdrlen;
445211db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
445311db70b6SBjoern A. Zeeb 
445411db70b6SBjoern A. Zeeb 	return (0);
445511db70b6SBjoern A. Zeeb }
445611db70b6SBjoern A. Zeeb #endif
445711db70b6SBjoern A. Zeeb 
44586b4cac81SBjoern A. Zeeb static void
44596b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
44606b4cac81SBjoern A. Zeeb {
44616b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
44626b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
44636b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
44646b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
44656b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44676b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44686b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
44696b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
44706b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
44716b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
44726b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
44736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4474e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4475ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
44766b4cac81SBjoern A. Zeeb 	void *buf;
447711db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4478e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
44796b4cac81SBjoern A. Zeeb 
44806b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
44816b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
44829d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
44836b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
44846b4cac81SBjoern A. Zeeb #endif
44856b4cac81SBjoern A. Zeeb 
44866b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4487b35f6cd0SBjoern A. Zeeb 	k = NULL;
448811db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
44896b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
44906b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
449111db70b6SBjoern A. Zeeb 
449211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
449311db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
449411db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
449511db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
449611db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
449711db70b6SBjoern A. Zeeb 		}
449811db70b6SBjoern A. Zeeb #endif
449911db70b6SBjoern A. Zeeb 
450011db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
450111db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
45026b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
45036b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
45046b4cac81SBjoern A. Zeeb 			if (k == NULL) {
45056b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
45066b4cac81SBjoern A. Zeeb 				m_freem(m);
45076b4cac81SBjoern A. Zeeb 				return;
45086b4cac81SBjoern A. Zeeb 			}
45096b4cac81SBjoern A. Zeeb 		}
451011db70b6SBjoern A. Zeeb 	}
45116b4cac81SBjoern A. Zeeb 
45126b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45156b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
45166b4cac81SBjoern A. Zeeb 
45176b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
45186b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
45196b4cac81SBjoern A. Zeeb 
45206b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
45216b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
45226b4cac81SBjoern A. Zeeb 		if (k != NULL)
45236b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
45246b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
45256b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
45266b4cac81SBjoern A. Zeeb 		IMPROVE();
45276b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
45286b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
45296b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
45306b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
45316b4cac81SBjoern A. Zeeb 		}
45326b4cac81SBjoern A. Zeeb 
45336b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
45346b4cac81SBjoern A. Zeeb 	}
45356b4cac81SBjoern A. Zeeb 
45366b4cac81SBjoern A. Zeeb 	/*
45376b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
45386b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
45393d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
45403d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
45416b4cac81SBjoern A. Zeeb 	 */
45426b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
45436b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4544bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4545bcf1d8eeSBjoern A. Zeeb 
4546bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4547bcf1d8eeSBjoern A. Zeeb 			int tid;
4548bcf1d8eeSBjoern A. Zeeb 
4549bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4550bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4551bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4552bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4553bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4554bcf1d8eeSBjoern A. Zeeb 					continue;
4555bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4556bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4557bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4558bcf1d8eeSBjoern A. Zeeb 			}
4559bcf1d8eeSBjoern A. Zeeb 		}
45606b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
45616b4cac81SBjoern A. Zeeb 		m_freem(m);
45626b4cac81SBjoern A. Zeeb 		return;
45636b4cac81SBjoern A. Zeeb 	}
45646b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
45656b4cac81SBjoern A. Zeeb 
45666b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
45676b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
45686b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
45696b4cac81SBjoern A. Zeeb 	skb->m = m;
45706b4cac81SBjoern A. Zeeb #if 0
45716b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
45726b4cac81SBjoern A. Zeeb #else
45736b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
45746b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
45756b4cac81SBjoern A. Zeeb #endif
45766b4cac81SBjoern A. Zeeb 	/* Save the ni. */
45776b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
45786b4cac81SBjoern A. Zeeb 
45796b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
45806b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
45816b4cac81SBjoern A. Zeeb 
4582e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4583e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4584e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
45851665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
45861665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
45871665ef97SBjoern A. Zeeb 			skb->priority = 7;
45881665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
45891665ef97SBjoern A. Zeeb 		} else {
45901665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
45911665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4592e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4593e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
45941665ef97SBjoern A. Zeeb 		}
4595e3a0b120SBjoern A. Zeeb 	} else {
4596e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4597fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4598e3a0b120SBjoern A. Zeeb 	}
45996b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
46006b4cac81SBjoern A. Zeeb 
46016b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
46026b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
46036b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
46046b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
46056b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
46066b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4607e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
46086b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
46096b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
46106b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
46116b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
46129fb91463SBjoern A. Zeeb #ifdef __notyet__
46139fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46149fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
46159fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
46169fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
46179fb91463SBjoern A. Zeeb #endif
46189fb91463SBjoern A. Zeeb #endif
46196b4cac81SBjoern A. Zeeb 
46206b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4621b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
462211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
462311db70b6SBjoern A. Zeeb 		int error;
462411db70b6SBjoern A. Zeeb 
462511db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
462611db70b6SBjoern A. Zeeb 		if (error != 0) {
462711db70b6SBjoern A. Zeeb 			/*
462811db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
462911db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
463011db70b6SBjoern A. Zeeb 			 */
463111db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
463211db70b6SBjoern A. Zeeb 			return;
463311db70b6SBjoern A. Zeeb 		}
463411db70b6SBjoern A. Zeeb 	}
46356b4cac81SBjoern A. Zeeb #endif
46366b4cac81SBjoern A. Zeeb 
46376b4cac81SBjoern A. Zeeb 	IMPROVE();
46386b4cac81SBjoern A. Zeeb 
4639e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4640e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4641e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4642e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4643e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4644d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4645e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4646e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4647e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4648e3a0b120SBjoern A. Zeeb 	}
4649e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4650d0d29110SBjoern A. Zeeb 		goto ops_tx;
4651e3a0b120SBjoern A. Zeeb 
4652d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4653d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4654d0d29110SBjoern A. Zeeb 
4655eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
46566b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
46579d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46589d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4659d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4660d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4661d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4662fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4663d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4664d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4665d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4666d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
46679d9ba2b7SBjoern A. Zeeb #endif
4668eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
466945bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4670d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
467145bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46726b4cac81SBjoern A. Zeeb 	return;
46736b4cac81SBjoern A. Zeeb 
46746b4cac81SBjoern A. Zeeb ops_tx:
46759d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46769d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4677d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4678d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
46796b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
46806b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
46819d9ba2b7SBjoern A. Zeeb #endif
46826b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
46836b4cac81SBjoern A. Zeeb 	control.sta = sta;
468445bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
46856b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
468645bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46876b4cac81SBjoern A. Zeeb }
46886b4cac81SBjoern A. Zeeb 
46896b4cac81SBjoern A. Zeeb static void
46906b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
46916b4cac81SBjoern A. Zeeb {
46926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46936b4cac81SBjoern A. Zeeb 	struct mbufq mq;
46946b4cac81SBjoern A. Zeeb 	struct mbuf *m;
469588665349SBjoern A. Zeeb 	bool shall_tx;
46966b4cac81SBjoern A. Zeeb 
46976b4cac81SBjoern A. Zeeb 	lsta = ctx;
46986b4cac81SBjoern A. Zeeb 
46999d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47009d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
47016b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
47029d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
47036b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
47049d9ba2b7SBjoern A. Zeeb #endif
47056b4cac81SBjoern A. Zeeb 
47066b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
47076b4cac81SBjoern A. Zeeb 
4708fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4709fa4e4257SBjoern A. Zeeb 	/*
4710fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
471188665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
471288665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
471388665349SBjoern A. Zeeb 	 * point to try to TX.
471488665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
471588665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4716fa4e4257SBjoern A. Zeeb 	 */
47172372f8ccSBjoern A. Zeeb #if 0
471888665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
47192372f8ccSBjoern A. Zeeb #else
47202372f8ccSBjoern A. Zeeb 	/*
47212372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
47222372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
47232372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
47242372f8ccSBjoern A. Zeeb 	 */
47252372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
47262372f8ccSBjoern A. Zeeb #endif
472788665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
47286b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
472988665349SBjoern A. Zeeb 	/*
473088665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
473188665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
473288665349SBjoern A. Zeeb 	 */
4733fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47346b4cac81SBjoern A. Zeeb 
47356b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
47366b4cac81SBjoern A. Zeeb 	while (m != NULL) {
47376b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
47386b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
47396b4cac81SBjoern A. Zeeb 	}
47406b4cac81SBjoern A. Zeeb }
47416b4cac81SBjoern A. Zeeb 
47426b4cac81SBjoern A. Zeeb static int
47436b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
47446b4cac81SBjoern A. Zeeb {
47456b4cac81SBjoern A. Zeeb 
47466b4cac81SBjoern A. Zeeb 	/* XXX TODO */
47476b4cac81SBjoern A. Zeeb 	IMPROVE();
47486b4cac81SBjoern A. Zeeb 
47496b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
47506b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47516b4cac81SBjoern A. Zeeb 
47526b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
47536b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
47546b4cac81SBjoern A. Zeeb }
47556b4cac81SBjoern A. Zeeb 
47569fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
47579fb91463SBjoern A. Zeeb static int
47589fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
47599fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
47609fb91463SBjoern A. Zeeb {
47619fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47629fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47639fb91463SBjoern A. Zeeb 
47649fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47659fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47669fb91463SBjoern A. Zeeb 
4767310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
47689fb91463SBjoern A. Zeeb 
47699fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
47709fb91463SBjoern A. Zeeb }
47719fb91463SBjoern A. Zeeb 
47729fb91463SBjoern A. Zeeb static int
47739fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
47749fb91463SBjoern A. Zeeb {
47759fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47769fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47779fb91463SBjoern A. Zeeb 
47789fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47799fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47809fb91463SBjoern A. Zeeb 
4781310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
47829fb91463SBjoern A. Zeeb 
47839fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
47849fb91463SBjoern A. Zeeb }
47859fb91463SBjoern A. Zeeb 
47869fb91463SBjoern A. Zeeb 
47879fb91463SBjoern A. Zeeb static int
47889fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
47899fb91463SBjoern A. Zeeb {
47909fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47919fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47929fb91463SBjoern A. Zeeb 
47939fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47949fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47959fb91463SBjoern A. Zeeb 
4796310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
47979fb91463SBjoern A. Zeeb 
47989fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
47999fb91463SBjoern A. Zeeb }
48009fb91463SBjoern A. Zeeb 
4801310743c4SBjoern A. Zeeb /*
4802310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4803310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4804310743c4SBjoern A. Zeeb  *
4805310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4806310743c4SBjoern A. Zeeb  */
48079fb91463SBjoern A. Zeeb static int
48089fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48099fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
48109fb91463SBjoern A. Zeeb {
48119fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48129fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4813310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4814310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4815310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4816310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4817310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4818310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4819310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4820310743c4SBjoern A. Zeeb 	int error;
48219fb91463SBjoern A. Zeeb 
48229fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48239fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4824310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4825310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4826310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4827310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4828310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4829310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48309fb91463SBjoern A. Zeeb 
4831310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4832310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4833310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4834310743c4SBjoern A. Zeeb 		return (0);
4835310743c4SBjoern A. Zeeb 	}
4836310743c4SBjoern A. Zeeb 
4837310743c4SBjoern A. Zeeb 	params.sta = sta;
4838310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4839310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4840310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4841310743c4SBjoern A. Zeeb 	params.timeout = 0;
4842310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4843310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4844310743c4SBjoern A. Zeeb 	params.amsdu = false;
4845310743c4SBjoern A. Zeeb 
4846310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4847310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4848310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4849310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4850310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4851310743c4SBjoern A. Zeeb 	if (error != 0) {
4852310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4853310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4854310743c4SBjoern A. Zeeb 		return (0);
4855310743c4SBjoern A. Zeeb 	}
48569fb91463SBjoern A. Zeeb 
48579fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
48589fb91463SBjoern A. Zeeb }
48599fb91463SBjoern A. Zeeb 
4860310743c4SBjoern A. Zeeb /*
4861310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4862310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4863310743c4SBjoern A. Zeeb  *
4864310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4865310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4866310743c4SBjoern A. Zeeb  */
48679fb91463SBjoern A. Zeeb static int
48689fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48699fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
48709fb91463SBjoern A. Zeeb {
48719fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48729fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4873310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4874310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4875310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4876310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4877310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4878310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4879310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4880310743c4SBjoern A. Zeeb 	int error;
48819fb91463SBjoern A. Zeeb 
48829fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48839fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4884310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4885310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4886310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4887310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4888310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4889310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48909fb91463SBjoern A. Zeeb 
4891310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4892310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4893310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4894310743c4SBjoern A. Zeeb 		return (0);
4895310743c4SBjoern A. Zeeb 	}
4896310743c4SBjoern A. Zeeb 
4897310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4898310743c4SBjoern A. Zeeb 		params.sta = sta;
4899310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4900310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4901310743c4SBjoern A. Zeeb 		params.timeout = 0;
4902310743c4SBjoern A. Zeeb 		params.ssn = 0;
4903310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4904310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4905310743c4SBjoern A. Zeeb 			params.amsdu = true;
4906310743c4SBjoern A. Zeeb 		else
4907310743c4SBjoern A. Zeeb 			params.amsdu = false;
4908310743c4SBjoern A. Zeeb 	} else {
4909310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4910310743c4SBjoern A. Zeeb 		params.sta = sta;
4911310743c4SBjoern A. Zeeb 		switch (status) {
4912310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4913310743c4SBjoern A. Zeeb 		default:
4914310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4915310743c4SBjoern A. Zeeb 			break;
4916310743c4SBjoern A. Zeeb 		}
4917310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4918310743c4SBjoern A. Zeeb 		params.timeout = 0;
4919310743c4SBjoern A. Zeeb 		params.ssn = 0;
4920310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4921310743c4SBjoern A. Zeeb 		params.amsdu = false;
4922310743c4SBjoern A. Zeeb 	}
4923310743c4SBjoern A. Zeeb 
4924310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4925310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4926310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4927310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4928310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4929310743c4SBjoern A. Zeeb 	if (error != 0) {
4930310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4931310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4932310743c4SBjoern A. Zeeb 		return (0);
4933310743c4SBjoern A. Zeeb 	}
4934310743c4SBjoern A. Zeeb 
4935310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
49369fb91463SBjoern A. Zeeb 
49379fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
49389fb91463SBjoern A. Zeeb }
49399fb91463SBjoern A. Zeeb 
4940310743c4SBjoern A. Zeeb /*
4941310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4942310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4943310743c4SBjoern A. Zeeb  */
49449fb91463SBjoern A. Zeeb static void
49459fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49469fb91463SBjoern A. Zeeb {
49479fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49489fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4949310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4950310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4951310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4952310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4953310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4954310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4955310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4956310743c4SBjoern A. Zeeb 	int error;
49579fb91463SBjoern A. Zeeb 
49589fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49599fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4960310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4961310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4962310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4963310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4964310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4965310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49669fb91463SBjoern A. Zeeb 
4967310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4968310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4969310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4970310743c4SBjoern A. Zeeb 		goto n80211;
4971310743c4SBjoern A. Zeeb 	}
49729fb91463SBjoern A. Zeeb 
4973310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4974310743c4SBjoern A. Zeeb 	params.sta = sta;
4975310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4976310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4977310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4978310743c4SBjoern A. Zeeb 	params.timeout = 0;
4979310743c4SBjoern A. Zeeb 	params.ssn = 0;
4980310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4981310743c4SBjoern A. Zeeb 	params.amsdu = false;
4982310743c4SBjoern A. Zeeb 
4983310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4984310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4985310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4986310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4987310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4988310743c4SBjoern A. Zeeb 	if (error != 0) {
4989310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4990310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4991310743c4SBjoern A. Zeeb 		goto n80211;
4992310743c4SBjoern A. Zeeb 	}
4993310743c4SBjoern A. Zeeb 
4994310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4995310743c4SBjoern A. Zeeb 
4996310743c4SBjoern A. Zeeb n80211:
49979fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
49989fb91463SBjoern A. Zeeb }
49999fb91463SBjoern A. Zeeb 
50009fb91463SBjoern A. Zeeb static void
50019fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
50029fb91463SBjoern A. Zeeb {
50039fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50049fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50059fb91463SBjoern A. Zeeb 
50069fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50079fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50089fb91463SBjoern A. Zeeb 
50099fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50109fb91463SBjoern A. Zeeb 
50119fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
50129fb91463SBjoern A. Zeeb }
50139fb91463SBjoern A. Zeeb 
50149fb91463SBjoern A. Zeeb static void
50159fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
50169fb91463SBjoern A. Zeeb     int status)
50179fb91463SBjoern A. Zeeb {
50189fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50199fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50209fb91463SBjoern A. Zeeb 
50219fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50229fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50239fb91463SBjoern A. Zeeb 
50249fb91463SBjoern A. Zeeb 	IMPROVE_HT();
50259fb91463SBjoern A. Zeeb 
50269fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
50279fb91463SBjoern A. Zeeb }
50289fb91463SBjoern A. Zeeb 
50299fb91463SBjoern A. Zeeb static int
50309fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
50319fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
50329fb91463SBjoern A. Zeeb {
50339fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50349fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50359fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50369fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50379fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50389fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50399fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50409fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5041310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50429fb91463SBjoern A. Zeeb 	int error;
50439fb91463SBjoern A. Zeeb 
50449fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50459fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50469fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
50479fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
50489fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
50499fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50509fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
50519fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50529fb91463SBjoern A. Zeeb 
5053310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5054310743c4SBjoern A. Zeeb 
5055310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5056310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5057310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5058310743c4SBjoern A. Zeeb 		return (-ENXIO);
5059310743c4SBjoern A. Zeeb 	}
5060310743c4SBjoern A. Zeeb 
50619fb91463SBjoern A. Zeeb 	params.sta = sta;
50629fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
50639fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
50649fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
50659fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
50669fb91463SBjoern A. Zeeb 	else
50679fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5068310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5069310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
50709fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
50719fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
50729fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
50739fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5074310743c4SBjoern A. Zeeb 
5075310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5076310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5077310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5078310743c4SBjoern A. Zeeb 		params.amsdu = true;
5079310743c4SBjoern A. Zeeb 	else
50809fb91463SBjoern A. Zeeb 		params.amsdu = false;
50819fb91463SBjoern A. Zeeb 
5082310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
50839fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5084310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
50859fb91463SBjoern A. Zeeb 	if (error != 0) {
50869fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
50879fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50889fb91463SBjoern A. Zeeb 		return (error);
50899fb91463SBjoern A. Zeeb 	}
5090310743c4SBjoern A. Zeeb 
5091310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5092310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5093310743c4SBjoern A. Zeeb 	}
5094310743c4SBjoern A. Zeeb 
50959fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
50969fb91463SBjoern A. Zeeb 
50979fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
50989fb91463SBjoern A. Zeeb 	return (error);
50999fb91463SBjoern A. Zeeb }
51009fb91463SBjoern A. Zeeb 
51019fb91463SBjoern A. Zeeb static void
51029fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
51039fb91463SBjoern A. Zeeb {
51049fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51059fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51069fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
51079fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
51089fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51099fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
51109fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51119fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5112310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
51139fb91463SBjoern A. Zeeb 	int error;
51149fb91463SBjoern A. Zeeb 	uint8_t tid;
511565c573e4SBjoern A. Zeeb 	bool ic_locked;
51169fb91463SBjoern A. Zeeb 
51179fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51189fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51199fb91463SBjoern A. Zeeb 
51209fb91463SBjoern A. Zeeb 	/*
51219fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
51229fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
51239fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
51249fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
51259fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
51269fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
51279fb91463SBjoern A. Zeeb 	 * track some state itself.
51289fb91463SBjoern A. Zeeb 	 */
51299fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
51309fb91463SBjoern A. Zeeb 		goto net80211_only;
51319fb91463SBjoern A. Zeeb 
51329fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51339fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
51349fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
51359fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
51369fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
51379fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51389fb91463SBjoern A. Zeeb 
51399fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
51409fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
51419fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
51429fb91463SBjoern A. Zeeb 			break;
51439fb91463SBjoern A. Zeeb 	}
51449fb91463SBjoern A. Zeeb 
51459fb91463SBjoern A. Zeeb 	params.sta = sta;
51469fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
51479fb91463SBjoern A. Zeeb 	params.buf_size = 0;
51489fb91463SBjoern A. Zeeb 	params.timeout = 0;
51499fb91463SBjoern A. Zeeb 	params.ssn = 0;
51509fb91463SBjoern A. Zeeb 	params.tid = tid;
51519fb91463SBjoern A. Zeeb 	params.amsdu = false;
51529fb91463SBjoern A. Zeeb 
515365c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
515465c573e4SBjoern A. Zeeb 	if (ic_locked)
515565c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5156310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
51579fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5158310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
515965c573e4SBjoern A. Zeeb 	if (ic_locked)
516065c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
51619fb91463SBjoern A. Zeeb 	if (error != 0)
51629fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
51639fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
51649fb91463SBjoern A. Zeeb 
51659fb91463SBjoern A. Zeeb net80211_only:
51669fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
51679fb91463SBjoern A. Zeeb }
51689fb91463SBjoern A. Zeeb #endif
51699fb91463SBjoern A. Zeeb 
51709fb91463SBjoern A. Zeeb static void
51719fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
51729fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
51739fb91463SBjoern A. Zeeb {
51749fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
51759fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
51769fb91463SBjoern A. Zeeb 
51779fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
51789fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
51799fb91463SBjoern A. Zeeb 		return;
51809fb91463SBjoern A. Zeeb 
51819fb91463SBjoern A. Zeeb 	switch (band) {
51829fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
51839fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
51849fb91463SBjoern A. Zeeb 		break;
51859fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
51869fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
51879fb91463SBjoern A. Zeeb 		break;
51889fb91463SBjoern A. Zeeb 	default:
51899fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
51909fb91463SBjoern A. Zeeb 		return;
51919fb91463SBjoern A. Zeeb 	}
51929fb91463SBjoern A. Zeeb 
51939fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
51949fb91463SBjoern A. Zeeb 
51959fb91463SBjoern A. Zeeb 	/*
51969fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
51979fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
51989fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
51999fb91463SBjoern A. Zeeb 	 */
52009fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
52019fb91463SBjoern A. Zeeb 
52029fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
52039fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
52049fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
52059fb91463SBjoern A. Zeeb #ifdef __notyet__
52069fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
52079fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
52089fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
52099fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
52109fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
52119fb91463SBjoern A. Zeeb #endif
52129fb91463SBjoern A. Zeeb 
52139fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
52149fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
52159fb91463SBjoern A. Zeeb 
52169fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
52179fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
52189fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
52199fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
52209fb91463SBjoern A. Zeeb #endif
52219fb91463SBjoern A. Zeeb }
52229fb91463SBjoern A. Zeeb 
52236b4cac81SBjoern A. Zeeb static void
52246b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
52256b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
52266b4cac81SBjoern A. Zeeb {
52276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52286b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52296b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
52306b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
52316b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
52326b4cac81SBjoern A. Zeeb 
52336b4cac81SBjoern A. Zeeb 	/* Channels */
52346b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
52356b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
52366b4cac81SBjoern A. Zeeb 
52376b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
52386b4cac81SBjoern A. Zeeb 	nchans = 0;
52396b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
52406b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
52416b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52426b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52436b4cac81SBjoern A. Zeeb 		chan_flags = 0;
52446b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
52456b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
52469fb91463SBjoern A. Zeeb 
52479fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
52486b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
52499fb91463SBjoern A. Zeeb 
52509fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
52519fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
52526b4cac81SBjoern A. Zeeb 
52536b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5254cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52556b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
52566b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
52576b4cac81SBjoern A. Zeeb 
52586b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
52593f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
52603f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
52616b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
52626b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
52636b4cac81SBjoern A. Zeeb 				continue;
52646b4cac81SBjoern A. Zeeb 			}
52656b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
52666b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
52676b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
52686b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
52696b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
52706b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
52716b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
52726b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
52736b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
52746b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
52756b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
52766b4cac81SBjoern A. Zeeb 
52776b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
52786b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
52796b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
52805856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5281cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5282cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
52833f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
52843f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
52856b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
52866b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
52876b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5288cee56e77SBjoern A. Zeeb 			if (error != 0)
52896b4cac81SBjoern A. Zeeb 				break;
52906b4cac81SBjoern A. Zeeb 		}
52916b4cac81SBjoern A. Zeeb 	}
52926b4cac81SBjoern A. Zeeb 
52936b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
52946b4cac81SBjoern A. Zeeb 	nchans = 0;
52956b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
52966b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
52976b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52986b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52996b4cac81SBjoern A. Zeeb 		chan_flags = 0;
53006b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
53019fb91463SBjoern A. Zeeb 
53029fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
53039fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
53049fb91463SBjoern A. Zeeb 
53059fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
53066b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
53076b4cac81SBjoern A. Zeeb 
53086b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5309562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
53106b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5311ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5312ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
53136b4cac81SBjoern A. Zeeb 
53146b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
53156b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
53166b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5317562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53186b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
53196b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5320562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
53216b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
53226b4cac81SBjoern A. Zeeb 		}
53236b4cac81SBjoern A. Zeeb #endif
53246b4cac81SBjoern A. Zeeb 
53256b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5326cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
53276b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
53286b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
53296b4cac81SBjoern A. Zeeb 
53306b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
53313f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
53323f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
53336b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
53346b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
53356b4cac81SBjoern A. Zeeb 				continue;
53366b4cac81SBjoern A. Zeeb 			}
53376b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
53386b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
53396b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
53406b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
53416b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
53426b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
53436b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
53446b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
53456b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
53466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
53476b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
53486b4cac81SBjoern A. Zeeb 
53496b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
53506b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
53516b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
53525856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5353cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5354cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
53553f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
53563f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
53576b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
53586b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
53596b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5360cee56e77SBjoern A. Zeeb 			if (error != 0)
53616b4cac81SBjoern A. Zeeb 				break;
53626b4cac81SBjoern A. Zeeb 		}
53636b4cac81SBjoern A. Zeeb 	}
53646b4cac81SBjoern A. Zeeb }
53656b4cac81SBjoern A. Zeeb 
53666b4cac81SBjoern A. Zeeb static void *
53676b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
53686b4cac81SBjoern A. Zeeb {
53696b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53706b4cac81SBjoern A. Zeeb 
53716b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
53726b4cac81SBjoern A. Zeeb 
53736b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
53746b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
53756b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
53766b4cac81SBjoern A. Zeeb 
53776b4cac81SBjoern A. Zeeb 	return (ic);
53786b4cac81SBjoern A. Zeeb }
53796b4cac81SBjoern A. Zeeb 
53806b4cac81SBjoern A. Zeeb struct ieee80211_hw *
53816b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
53826b4cac81SBjoern A. Zeeb {
53836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
53846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53856b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5386a5ae63edSBjoern A. Zeeb 	int ac;
53876b4cac81SBjoern A. Zeeb 
53886b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
53896b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
53906b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
53916b4cac81SBjoern A. Zeeb 		return (NULL);
53926b4cac81SBjoern A. Zeeb 
53936b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
53946b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5395652e22d3SBjoern A. Zeeb 
53968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
53978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5398eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
53998891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
54006b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5401a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5402a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5403a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5404a5ae63edSBjoern A. Zeeb 	}
54056b4cac81SBjoern A. Zeeb 
5406759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5407759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5408759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5409759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5410759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5411759a996dSBjoern A. Zeeb 
54126b4cac81SBjoern A. Zeeb 	/*
54136b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
54146b4cac81SBjoern A. Zeeb 	 * not initialized.
54156b4cac81SBjoern A. Zeeb 	 */
54166b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54176b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5418086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
54196b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
54206b4cac81SBjoern A. Zeeb 
54216b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
54226b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
54236b4cac81SBjoern A. Zeeb 
54246b4cac81SBjoern A. Zeeb 	IMPROVE();
54256b4cac81SBjoern A. Zeeb 
54266b4cac81SBjoern A. Zeeb 	return (hw);
54276b4cac81SBjoern A. Zeeb }
54286b4cac81SBjoern A. Zeeb 
54296b4cac81SBjoern A. Zeeb void
54306b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
54316b4cac81SBjoern A. Zeeb {
54326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5433759a996dSBjoern A. Zeeb 	struct mbuf *m;
54346b4cac81SBjoern A. Zeeb 
54356b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54366b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
54376b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
54386b4cac81SBjoern A. Zeeb 
5439759a996dSBjoern A. Zeeb 	/*
5440759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5441759a996dSBjoern A. Zeeb 	 */
5442759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5443759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5444759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5445759a996dSBjoern A. Zeeb 
5446759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5447759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5448759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5449759a996dSBjoern A. Zeeb 
5450759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5451759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5452759a996dSBjoern A. Zeeb 	while (m != NULL) {
5453759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5454759a996dSBjoern A. Zeeb 
5455759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5456759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5457759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5458759a996dSBjoern A. Zeeb 
5459759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5460759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5461759a996dSBjoern A. Zeeb 		}
5462759a996dSBjoern A. Zeeb 		m_freem(m);
5463759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5464759a996dSBjoern A. Zeeb 	}
5465759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5466759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5467759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5468759a996dSBjoern A. Zeeb 
54696b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5470eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
54718ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5472eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5473eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
54746b4cac81SBjoern A. Zeeb 	IMPROVE();
54756b4cac81SBjoern A. Zeeb }
54766b4cac81SBjoern A. Zeeb 
54776b4cac81SBjoern A. Zeeb void
54786b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
54796b4cac81SBjoern A. Zeeb {
54806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54826b4cac81SBjoern A. Zeeb 
54836b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54846b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54856b4cac81SBjoern A. Zeeb 
54866b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
54876b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
54886b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
54896b4cac81SBjoern A. Zeeb 
54906b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
54916b4cac81SBjoern A. Zeeb }
54926b4cac81SBjoern A. Zeeb 
54936b4cac81SBjoern A. Zeeb struct ieee80211_hw *
54946b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
54956b4cac81SBjoern A. Zeeb {
54966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54976b4cac81SBjoern A. Zeeb 
54986b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54996b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
55006b4cac81SBjoern A. Zeeb }
55016b4cac81SBjoern A. Zeeb 
55026b4cac81SBjoern A. Zeeb static void
55036b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
55046b4cac81SBjoern A. Zeeb {
55056b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55066b4cac81SBjoern A. Zeeb 
55076b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55086b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
55096b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
55106b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
55116b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
55126b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
55136b4cac81SBjoern A. Zeeb }
55146b4cac81SBjoern A. Zeeb 
55152e183d99SBjoern A. Zeeb int
55166b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
55176b4cac81SBjoern A. Zeeb {
55186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5520c5b96b3eSBjoern A. Zeeb 	int band, i;
55216b4cac81SBjoern A. Zeeb 
55226b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55236b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
55246b4cac81SBjoern A. Zeeb 
5525652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5526652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5527652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5528652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5529652e22d3SBjoern A. Zeeb 
55306b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
55316b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
55326b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55336b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
55346b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
55356b4cac81SBjoern A. Zeeb 		/* We take the first one. */
55366b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55376b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
55386b4cac81SBjoern A. Zeeb 	} else {
55396b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
55406b4cac81SBjoern A. Zeeb 	}
55416b4cac81SBjoern A. Zeeb 
55423d09d310SBjoern A. Zeeb #ifdef __not_yet__
55433d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
55446b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
55453d09d310SBjoern A. Zeeb #endif
55466b4cac81SBjoern A. Zeeb 
55476b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
55486b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
55496b4cac81SBjoern A. Zeeb 
55506b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
55516b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
55526b4cac81SBjoern A. Zeeb 	ic->ic_caps =
55536b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
55546b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
55556b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
55564a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
55576b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
55584a67f1dfSBjoern A. Zeeb #endif
55596b4cac81SBjoern A. Zeeb #if 0
55606b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
55616b4cac81SBjoern A. Zeeb #endif
55626b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
55636b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
55646b4cac81SBjoern A. Zeeb 	    ;
55656b4cac81SBjoern A. Zeeb #if 0
55666b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
55676b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
55686b4cac81SBjoern A. Zeeb #endif
5569cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5570cc4e78d5SBjoern A. Zeeb 		/*
5571cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5572cc4e78d5SBjoern A. Zeeb 		 *
5573cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5574cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5575cc4e78d5SBjoern A. Zeeb 		 * the flag.
5576cc4e78d5SBjoern A. Zeeb 		 */
55776b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5578a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
55796b4cac81SBjoern A. Zeeb 	}
55806b4cac81SBjoern A. Zeeb 
5581527687a9SBjoern A. Zeeb 	/*
5582527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5583527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5584527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5585527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5586527687a9SBjoern A. Zeeb 	 */
5587527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5588527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5589527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5590527687a9SBjoern A. Zeeb 
5591527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5592527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5593527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5594527687a9SBjoern A. Zeeb 		}
5595527687a9SBjoern A. Zeeb 	}
5596527687a9SBjoern A. Zeeb 
55976b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5598b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
559911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
56006b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
56016b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
56026b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
56036b4cac81SBjoern A. Zeeb 	}
56046b4cac81SBjoern A. Zeeb #endif
56056b4cac81SBjoern A. Zeeb 
56066b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
56076b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
56086b4cac81SBjoern A. Zeeb 
56096b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
56106b4cac81SBjoern A. Zeeb 
56116b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
56126b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
56136b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
56146b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
56156b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
56166b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
56176b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
56186b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
56196b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
56206b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
56216b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
56226b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
56236b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
56246b4cac81SBjoern A. Zeeb 
5625a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5626a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5627a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5628a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5629a486fbbdSBjoern A. Zeeb 
56306b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
56316b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
56326b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
56336b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
56346b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
56356b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
56366b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
56376b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
56386b4cac81SBjoern A. Zeeb 
56399fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
564086bc7259SBjoern A. Zeeb 	/*
564186bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
564286bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
564386bc7259SBjoern A. Zeeb 	 */
564486bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
56459fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
56469fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
56479fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
56489fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
56499fb91463SBjoern A. Zeeb 
56509fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
56519fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
56529fb91463SBjoern A. Zeeb 
56539fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
56549fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
56559fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
56569fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
56579fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
56589fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
56599fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
56609fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
56619fb91463SBjoern A. Zeeb 
56629fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
56639fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
56649fb91463SBjoern A. Zeeb 
56659fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
56669fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
56679fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
56689fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
566986bc7259SBjoern A. Zeeb 	}
56709fb91463SBjoern A. Zeeb #endif
56719fb91463SBjoern A. Zeeb 
56726b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
56736b4cac81SBjoern A. Zeeb 
5674c5b96b3eSBjoern A. Zeeb 	/*
5675c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5676c5b96b3eSBjoern A. Zeeb 	 * expect one.
5677d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5678d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5679c5b96b3eSBjoern A. Zeeb 	 */
5680d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5681f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5682c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5683c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5684c5b96b3eSBjoern A. Zeeb 
5685c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5686c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5687c5b96b3eSBjoern A. Zeeb 			continue;
5688c5b96b3eSBjoern A. Zeeb 
5689d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5690d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5691d9945d78SBjoern A. Zeeb 
5692f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5693f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5694f454a4a1SBjoern A. Zeeb 			continue;
5695f454a4a1SBjoern A. Zeeb 
5696c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5697c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5698c5b96b3eSBjoern A. Zeeb 
5699c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5700c5b96b3eSBjoern A. Zeeb 				continue;
5701c5b96b3eSBjoern A. Zeeb 
57024a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
57039fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
570411450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
57059fb91463SBjoern A. Zeeb #endif
5706c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5707c5b96b3eSBjoern A. Zeeb 			break;
5708c5b96b3eSBjoern A. Zeeb 		}
5709c5b96b3eSBjoern A. Zeeb 	}
5710c5b96b3eSBjoern A. Zeeb 
5711d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5712d9945d78SBjoern A. Zeeb 
5713d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5714d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5715d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5716d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5717d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5718d9945d78SBjoern A. Zeeb 	}
5719d9945d78SBjoern A. Zeeb 
5720d9945d78SBjoern A. Zeeb 	/*
5721d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5722d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5723d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5724d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5725d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5726d9945d78SBjoern A. Zeeb 	 */
5727d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5728d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5729d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
573078ca45dfSBjoern A. Zeeb 
573178ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5732d9945d78SBjoern A. Zeeb 		/*
573378ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
573478ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
573578ca45dfSBjoern A. Zeeb 		 * space in.
5736d9945d78SBjoern A. Zeeb 		 */
5737d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
573878ca45dfSBjoern A. Zeeb 	}
5739d9945d78SBjoern A. Zeeb 
57409fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
57419fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
57429fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
57439fb91463SBjoern A. Zeeb #endif
57449fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
57451f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
57469fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
57479fb91463SBjoern A. Zeeb #endif
57489fb91463SBjoern A. Zeeb 
5749d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
575078ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
575178ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
575278ca45dfSBjoern A. Zeeb 			goto err;
5753d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
575478ca45dfSBjoern A. Zeeb 	}
5755d9945d78SBjoern A. Zeeb 
57566b4cac81SBjoern A. Zeeb 	if (bootverbose)
57576b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
57582e183d99SBjoern A. Zeeb 
57592e183d99SBjoern A. Zeeb 	return (0);
576078ca45dfSBjoern A. Zeeb err:
576178ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
576278ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
57636b4cac81SBjoern A. Zeeb }
57646b4cac81SBjoern A. Zeeb 
57656b4cac81SBjoern A. Zeeb void
57666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
57676b4cac81SBjoern A. Zeeb {
57686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57696b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57706b4cac81SBjoern A. Zeeb 
57716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57726b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57736b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
57746b4cac81SBjoern A. Zeeb }
57756b4cac81SBjoern A. Zeeb 
57766b4cac81SBjoern A. Zeeb void
57776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
57786b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
57796b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
57806b4cac81SBjoern A. Zeeb     void *arg)
57816b4cac81SBjoern A. Zeeb {
57826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
578561a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
57866b4cac81SBjoern A. Zeeb 
57876b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57886b4cac81SBjoern A. Zeeb 
57896b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
57906b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
579161a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5792adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
57936b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
57946b4cac81SBjoern A. Zeeb 		    __func__, flags);
57956b4cac81SBjoern A. Zeeb 	}
57966b4cac81SBjoern A. Zeeb 
5797adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
57986b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
579961a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
58006b4cac81SBjoern A. Zeeb 
58016b4cac81SBjoern A. Zeeb 	if (atomic)
58028891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
58036b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58046b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
58056b4cac81SBjoern A. Zeeb 
58066b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
58076b4cac81SBjoern A. Zeeb 
58086b4cac81SBjoern A. Zeeb 		/*
58096b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
58106b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
58116b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
58126b4cac81SBjoern A. Zeeb 		 * driver does not know about.
58136b4cac81SBjoern A. Zeeb 		 */
58146b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
58156b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
58166b4cac81SBjoern A. Zeeb 			continue;
58176b4cac81SBjoern A. Zeeb 
58186b4cac81SBjoern A. Zeeb 		/*
581961a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
582061a68e50SBjoern A. Zeeb 		 * if we haven't yet.
582161a68e50SBjoern A. Zeeb 		 */
582261a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
582361a68e50SBjoern A. Zeeb 			continue;
582461a68e50SBjoern A. Zeeb 
582561a68e50SBjoern A. Zeeb 		/*
58266b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
58276b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
58286b4cac81SBjoern A. Zeeb 		 */
58296b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
58306b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
58316b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
58326b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
58336b4cac81SBjoern A. Zeeb 	}
58346b4cac81SBjoern A. Zeeb 	if (atomic)
58358891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58366b4cac81SBjoern A. Zeeb }
58376b4cac81SBjoern A. Zeeb 
583811db70b6SBjoern A. Zeeb static void
583911db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
584011db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
584111db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
584211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
584311db70b6SBjoern A. Zeeb     void *arg)
584411db70b6SBjoern A. Zeeb {
584511db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
584611db70b6SBjoern A. Zeeb 		return;
584711db70b6SBjoern A. Zeeb 
584811db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
584911db70b6SBjoern A. Zeeb 		return;
585011db70b6SBjoern A. Zeeb 
585111db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
585211db70b6SBjoern A. Zeeb }
585311db70b6SBjoern A. Zeeb 
58546b4cac81SBjoern A. Zeeb void
58556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
58566b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
58576b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
58586b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
585911db70b6SBjoern A. Zeeb     void *arg, bool rcu)
58606b4cac81SBjoern A. Zeeb {
586111db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
586211db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58636b4cac81SBjoern A. Zeeb 
586411db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
586511db70b6SBjoern A. Zeeb 
586611db70b6SBjoern A. Zeeb 	if (rcu) {
5867a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5868a8f735a6SBjoern A. Zeeb 
586911db70b6SBjoern A. Zeeb 		if (vif == NULL) {
587011db70b6SBjoern A. Zeeb 			TODO();
587111db70b6SBjoern A. Zeeb 		} else {
5872a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
587311db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
587411db70b6SBjoern A. Zeeb 				    keyix++)
587511db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
587611db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
587711db70b6SBjoern A. Zeeb 			}
587811db70b6SBjoern A. Zeeb 		}
587911db70b6SBjoern A. Zeeb 	} else {
588011db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
588111db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
588211db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
588311db70b6SBjoern A. Zeeb 
588411db70b6SBjoern A. Zeeb 		if (vif == NULL) {
588511db70b6SBjoern A. Zeeb 			TODO();
588611db70b6SBjoern A. Zeeb 		} else {
5887a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
588811db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
588911db70b6SBjoern A. Zeeb 				    keyix++)
589011db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
589111db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
589211db70b6SBjoern A. Zeeb 			}
589311db70b6SBjoern A. Zeeb 		}
589411db70b6SBjoern A. Zeeb 	}
58956b4cac81SBjoern A. Zeeb }
58966b4cac81SBjoern A. Zeeb 
58976b4cac81SBjoern A. Zeeb void
58986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
58996b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
59006b4cac81SBjoern A. Zeeb 	void *),
59016b4cac81SBjoern A. Zeeb     void *arg)
59026b4cac81SBjoern A. Zeeb {
5903c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5904c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5905c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5906c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
59076b4cac81SBjoern A. Zeeb 
5908c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5909c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5910c5e25798SBjoern A. Zeeb 
5911c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5912c5e25798SBjoern A. Zeeb 
5913c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5914c5e25798SBjoern A. Zeeb 
5915c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5916c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5917c5e25798SBjoern A. Zeeb 
5918c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5919c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5920c5e25798SBjoern A. Zeeb 			continue;
5921c5e25798SBjoern A. Zeeb 
5922c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5923c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5924c5e25798SBjoern A. Zeeb 			continue;
5925c5e25798SBjoern A. Zeeb 
5926d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5927c5e25798SBjoern A. Zeeb 	}
5928c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59296b4cac81SBjoern A. Zeeb }
59306b4cac81SBjoern A. Zeeb 
59316b4cac81SBjoern A. Zeeb void
59326b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
59336b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
59346b4cac81SBjoern A. Zeeb {
59356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59376b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
59386b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
59396b4cac81SBjoern A. Zeeb 
59406b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
59416b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
59426b4cac81SBjoern A. Zeeb 
59436b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59446b4cac81SBjoern A. Zeeb 
59458891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
59466b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
59476b4cac81SBjoern A. Zeeb 
5948a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
5949a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
59506b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
59516b4cac81SBjoern A. Zeeb 				continue;
59526b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
59536b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
59546b4cac81SBjoern A. Zeeb 		}
5955a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
59566b4cac81SBjoern A. Zeeb 	}
59578891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59586b4cac81SBjoern A. Zeeb }
59596b4cac81SBjoern A. Zeeb 
5960673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5961673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5962673d62fcSBjoern A. Zeeb {
5963673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5964673d62fcSBjoern A. Zeeb 
5965673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5966673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5967673d62fcSBjoern A. Zeeb 	return (regd);
5968673d62fcSBjoern A. Zeeb }
5969673d62fcSBjoern A. Zeeb 
59706b4cac81SBjoern A. Zeeb int
59716b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
59726b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
59736b4cac81SBjoern A. Zeeb {
59746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59756b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59766b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
59776b4cac81SBjoern A. Zeeb 
59786b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59796b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59806b4cac81SBjoern A. Zeeb 
59816b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
59826b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
59836b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
59846b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
59856b4cac81SBjoern A. Zeeb 	}
59866b4cac81SBjoern A. Zeeb 
59876b4cac81SBjoern A. Zeeb 	TODO();
59886b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
59896b4cac81SBjoern A. Zeeb 
59906b4cac81SBjoern A. Zeeb 	return (0);
59916b4cac81SBjoern A. Zeeb }
59926b4cac81SBjoern A. Zeeb 
59936b4cac81SBjoern A. Zeeb void
59946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
59956b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
59966b4cac81SBjoern A. Zeeb {
59976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59986b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59996b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
60006b4cac81SBjoern A. Zeeb 
60016b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
60026b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
60036b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
60046b4cac81SBjoern A. Zeeb 
60056b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
60066b4cac81SBjoern A. Zeeb 
60078ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
60086b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
60096b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6010a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
60116b4cac81SBjoern A. Zeeb 	wakeup(lhw);
60128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
60136b4cac81SBjoern A. Zeeb 
60146b4cac81SBjoern A. Zeeb 	return;
60156b4cac81SBjoern A. Zeeb }
60166b4cac81SBjoern A. Zeeb 
6017759a996dSBjoern A. Zeeb static void
6018759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6019759a996dSBjoern A. Zeeb {
6020759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6021759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6022759a996dSBjoern A. Zeeb 	int ok;
6023759a996dSBjoern A. Zeeb 
6024759a996dSBjoern A. Zeeb 	ni = NULL;
6025759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6026759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6027759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6028759a996dSBjoern A. Zeeb 
6029759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6030759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6031759a996dSBjoern A. Zeeb 	}
6032759a996dSBjoern A. Zeeb 
6033759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6034759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6035759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6036759a996dSBjoern A. Zeeb 		if (ok < 0)
6037759a996dSBjoern A. Zeeb 			m_freem(m);
6038759a996dSBjoern A. Zeeb 	} else {
6039759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6040759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6041759a996dSBjoern A. Zeeb 	}
6042759a996dSBjoern A. Zeeb 
6043759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6044759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6045bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6046759a996dSBjoern A. Zeeb #endif
6047759a996dSBjoern A. Zeeb }
6048759a996dSBjoern A. Zeeb 
6049759a996dSBjoern A. Zeeb static void
6050759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6051759a996dSBjoern A. Zeeb {
6052759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6053759a996dSBjoern A. Zeeb 	struct mbufq mq;
6054759a996dSBjoern A. Zeeb 	struct mbuf *m;
6055759a996dSBjoern A. Zeeb 
6056759a996dSBjoern A. Zeeb 	lhw = ctx;
6057759a996dSBjoern A. Zeeb 
6058759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6059759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6060bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6061bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6062759a996dSBjoern A. Zeeb #endif
6063759a996dSBjoern A. Zeeb 
6064759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6065759a996dSBjoern A. Zeeb 
6066759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6067759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6068759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6069759a996dSBjoern A. Zeeb 
6070759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6071759a996dSBjoern A. Zeeb 	while (m != NULL) {
6072759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6073759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6074759a996dSBjoern A. Zeeb 	}
6075759a996dSBjoern A. Zeeb }
6076759a996dSBjoern A. Zeeb 
607749010ba7SBjoern A. Zeeb static void
607849010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
607949010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
608049010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
608149010ba7SBjoern A. Zeeb     uint8_t *rssip)
608249010ba7SBjoern A. Zeeb {
608349010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
608449010ba7SBjoern A. Zeeb 	int i;
608549010ba7SBjoern A. Zeeb 	uint8_t rssi;
608649010ba7SBjoern A. Zeeb 
608749010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
608849010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
608949010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
609049010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
609149010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
609249010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
609349010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
609449010ba7SBjoern A. Zeeb 	else
609549010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
609649010ba7SBjoern A. Zeeb 	/*
609749010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
609849010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
609949010ba7SBjoern A. Zeeb 	 */
610049010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
610149010ba7SBjoern A. Zeeb 	if (rssip != NULL)
610249010ba7SBjoern A. Zeeb 		*rssip = rssi;
610349010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
610449010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
610549010ba7SBjoern A. Zeeb 	rx_stats->c_band =
610649010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
610749010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
610849010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
610949010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
611049010ba7SBjoern A. Zeeb 
611149010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
611249010ba7SBjoern A. Zeeb 
611349010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
611449010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
611549010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
611649010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
611749010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
611849010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
611949010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
612049010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
612149010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
612249010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
612349010ba7SBjoern A. Zeeb 	}
612449010ba7SBjoern A. Zeeb 
612549010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
612649010ba7SBjoern A. Zeeb 		int cc;
612749010ba7SBjoern A. Zeeb 		int8_t crssi;
612849010ba7SBjoern A. Zeeb 
612949010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
613049010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
613149010ba7SBjoern A. Zeeb 
613249010ba7SBjoern A. Zeeb 		cc = 0;
613349010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
613449010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
613549010ba7SBjoern A. Zeeb 				continue;
613649010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
613749010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
613849010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
613949010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
614049010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
614149010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
614249010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
614349010ba7SBjoern A. Zeeb 			cc++;
614449010ba7SBjoern A. Zeeb 		}
614549010ba7SBjoern A. Zeeb 		if (cc > 0)
614649010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
614749010ba7SBjoern A. Zeeb 	}
614849010ba7SBjoern A. Zeeb 
614949010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
615049010ba7SBjoern A. Zeeb 
615149010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
615249010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
615349010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
615449010ba7SBjoern A. Zeeb 		if (supband != NULL)
615549010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
615649010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
615749010ba7SBjoern A. Zeeb 		break;
615849010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
615949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
616049010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
616149010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
616249010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
616349010ba7SBjoern A. Zeeb 		break;
616449010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
616549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
616649010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
616749010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
616849010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
616949010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
617049010ba7SBjoern A. Zeeb 		break;
617149010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
617249010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
617349010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
617449010ba7SBjoern A. Zeeb 		break;
617549010ba7SBjoern A. Zeeb 	}
617649010ba7SBjoern A. Zeeb 
617749010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
617849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
617949010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
618049010ba7SBjoern A. Zeeb 		break;
618149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
618249010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
618349010ba7SBjoern A. Zeeb 		break;
618449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
618549010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
618649010ba7SBjoern A. Zeeb 		break;
618749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
618849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
618949010ba7SBjoern A. Zeeb 		break;
619049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
619149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
619249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
619349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
619449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
619549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
619649010ba7SBjoern A. Zeeb 		break;
619749010ba7SBjoern A. Zeeb 	}
619849010ba7SBjoern A. Zeeb 
619949010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
620049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
620149010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
620249010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
620349010ba7SBjoern A. Zeeb 
620449010ba7SBjoern A. Zeeb 	/*
620549010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
620649010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
620749010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
620849010ba7SBjoern A. Zeeb 	 */
620949010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
621049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
621149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
621249010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
621349010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
621449010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
621549010ba7SBjoern A. Zeeb 	}
621649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
621749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
621849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
621949010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
622049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
622149010ba7SBjoern A. Zeeb 	}
622249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
622349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
622449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
622549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
622649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
622749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
622849010ba7SBjoern A. Zeeb #endif
622949010ba7SBjoern A. Zeeb }
623049010ba7SBjoern A. Zeeb 
6231e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
62326b4cac81SBjoern A. Zeeb void
62336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6234e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6235e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
62366b4cac81SBjoern A. Zeeb {
62376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62386b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
62396b4cac81SBjoern A. Zeeb 	struct mbuf *m;
62406b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
62416b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
62426b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
62436b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
62446b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
62456b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
62466b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
62479d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
624849010ba7SBjoern A. Zeeb 	uint8_t rssi;
6249c0cadd99SBjoern A. Zeeb 	bool is_beacon;
62506b4cac81SBjoern A. Zeeb 
62516b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
62526b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
62536b4cac81SBjoern A. Zeeb 		IMPROVE();
62546b4cac81SBjoern A. Zeeb 		goto err;
62556b4cac81SBjoern A. Zeeb 	}
62566b4cac81SBjoern A. Zeeb 
62576b4cac81SBjoern A. Zeeb 	/*
62586b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
62596b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
62606b4cac81SBjoern A. Zeeb 	 */
62616b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
62626b4cac81SBjoern A. Zeeb 	if (m == NULL)
62636b4cac81SBjoern A. Zeeb 		goto err;
62646b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
62656b4cac81SBjoern A. Zeeb 
62666b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
62676b4cac81SBjoern A. Zeeb 	offset = m->m_len;
62686b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
62696b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
62706b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
62716b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
62726b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
62736b4cac81SBjoern A. Zeeb 	}
62746b4cac81SBjoern A. Zeeb 
62756b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
62766b4cac81SBjoern A. Zeeb 
62776b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6278c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6279c0cadd99SBjoern A. Zeeb 
6280c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
62819d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
62826b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
62836b4cac81SBjoern A. Zeeb 
62849d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
62856b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6286c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
62876b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
62886b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
62896b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6290c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
62916b4cac81SBjoern A. Zeeb 
62929d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
62936b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
62946b4cac81SBjoern A. Zeeb 
62956b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
62969d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6297f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
629860970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
62996b4cac81SBjoern A. Zeeb 			__func__,
6300c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6301c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
63026b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6303f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
63046b4cac81SBjoern A. Zeeb 			rx_status->freq,
63056b4cac81SBjoern A. Zeeb 			rx_status->bw,
63066b4cac81SBjoern A. Zeeb 			rx_status->encoding,
63076b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
63086b4cac81SBjoern A. Zeeb 			rx_status->band,
63096b4cac81SBjoern A. Zeeb 			rx_status->chains,
63106b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
63116b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
63126b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
631360970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
63146b4cac81SBjoern A. Zeeb 			rx_status->signal,
63156b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
63166b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
63176b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
63186b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
63196b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
63206b4cac81SBjoern A. Zeeb 			rx_status->nss,
63216b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
63226b4cac81SBjoern A. Zeeb no_trace_beacons:
63236b4cac81SBjoern A. Zeeb #endif
63246b4cac81SBjoern A. Zeeb 
632549010ba7SBjoern A. Zeeb 	rssi = 0;
632649010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
63276b4cac81SBjoern A. Zeeb 
63286b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63296b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63306b4cac81SBjoern A. Zeeb 
63316b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
63326b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6333dbc06dd9SBjoern A. Zeeb 		m_freem(m);
63346b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
63356b4cac81SBjoern A. Zeeb 		goto err;
63366b4cac81SBjoern A. Zeeb 	}
63376b4cac81SBjoern A. Zeeb 
633858246901SBjoern A. Zeeb 	lsta = NULL;
63396b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
63406b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
63416b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
63426b4cac81SBjoern A. Zeeb 	} else {
6343c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6344c8dafefaSBjoern A. Zeeb 
63456b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
63466b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
63476b4cac81SBjoern A. Zeeb 		if (ni != NULL)
63486b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
63496b4cac81SBjoern A. Zeeb 	}
63506b4cac81SBjoern A. Zeeb 
63516b4cac81SBjoern A. Zeeb 	if (ni != NULL)
63526b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
63536b4cac81SBjoern A. Zeeb 	else
63546b4cac81SBjoern A. Zeeb 		/*
63556b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
63566b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
63576b4cac81SBjoern A. Zeeb 		 */
63586b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
63596b4cac81SBjoern A. Zeeb 
63609d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63619d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6362bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6363c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6364c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
63659d9ba2b7SBjoern A. Zeeb #endif
63666b4cac81SBjoern A. Zeeb 
6367c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6368c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6369c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6370c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6371c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6372c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6373c8dafefaSBjoern A. Zeeb 
6374c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6375c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6376c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6377c8dafefaSBjoern A. Zeeb 
6378c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6379c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6380c8dafefaSBjoern A. Zeeb 
6381c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6382c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6383c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6384c8dafefaSBjoern A. Zeeb 		/*
6385c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6386c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6387c8dafefaSBjoern A. Zeeb 		 */
6388c8dafefaSBjoern A. Zeeb skip_device_ts:
63899fb91463SBjoern A. Zeeb 		;
63909fb91463SBjoern A. Zeeb 	}
6391c8dafefaSBjoern A. Zeeb 
63926b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
63936b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
63946b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
63956b4cac81SBjoern A. Zeeb 
63966b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
63976b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
63986b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
63996b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
64006b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
64016b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
64026b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
64036b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
64046b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64056b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
64066b4cac81SBjoern A. Zeeb #endif
64076b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
64086b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
64096b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
64106b4cac81SBjoern A. Zeeb 		IMPROVE();
64116b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
64126b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
64136b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
64146b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6415170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
64166b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
64176b4cac81SBjoern A. Zeeb 	}
64186b4cac81SBjoern A. Zeeb 
64196b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
64206b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
64216b4cac81SBjoern A. Zeeb 
6422e30e05d3SBjoern A. Zeeb #if 0
6423e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6424e30e05d3SBjoern A. Zeeb 		/*
6425e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6426e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6427e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6428e30e05d3SBjoern A. Zeeb 		*/
6429e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6430e30e05d3SBjoern A. Zeeb 	}
6431e30e05d3SBjoern A. Zeeb #endif
6432e30e05d3SBjoern A. Zeeb 
6433759a996dSBjoern A. Zeeb 	/*
6434759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6435759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6436759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6437759a996dSBjoern A. Zeeb 	 */
64386b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6439759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6440759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6441759a996dSBjoern A. Zeeb 
6442759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6443759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6444759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6445759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6446759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6447759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6448759a996dSBjoern A. Zeeb 		}
64496b4cac81SBjoern A. Zeeb 	}
64506b4cac81SBjoern A. Zeeb 
6451759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6452759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6453759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6454759a996dSBjoern A. Zeeb 		m_freem(m);
6455759a996dSBjoern A. Zeeb 		goto err;
6456759a996dSBjoern A. Zeeb 	}
6457759a996dSBjoern A. Zeeb 
6458759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6459759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6460759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
64616b4cac81SBjoern A. Zeeb 
64626b4cac81SBjoern A. Zeeb 	IMPROVE();
64636b4cac81SBjoern A. Zeeb 
64646b4cac81SBjoern A. Zeeb err:
64656b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
64666b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
64676b4cac81SBjoern A. Zeeb }
64686b4cac81SBjoern A. Zeeb 
64696b4cac81SBjoern A. Zeeb uint8_t
6470ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
64716b4cac81SBjoern A. Zeeb {
64726b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6473ec190d91SBjoern A. Zeeb 	uint8_t tid;
6474ec190d91SBjoern A. Zeeb 
6475ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6476ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6477ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6478ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
64796b4cac81SBjoern A. Zeeb 
64806b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6481ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6482ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6483ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6484ec190d91SBjoern A. Zeeb 
6485ec190d91SBjoern A. Zeeb 	return (tid);
64866b4cac81SBjoern A. Zeeb }
64876b4cac81SBjoern A. Zeeb 
6488ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6489ac1d519cSBjoern A. Zeeb 
6490ac1d519cSBjoern A. Zeeb static void
6491ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6492ac1d519cSBjoern A. Zeeb {
6493ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6494ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6495ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6496ac1d519cSBjoern A. Zeeb 
6497ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6498ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6499ac1d519cSBjoern A. Zeeb 
6500ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6501ac1d519cSBjoern A. Zeeb 
6502ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6503ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6504ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6505ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6506ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6507ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6508ac1d519cSBjoern A. Zeeb 		return;
6509ac1d519cSBjoern A. Zeeb 	}
6510ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6511ac1d519cSBjoern A. Zeeb 
6512ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6513ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6514ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6515ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6516ac1d519cSBjoern A. Zeeb 
6517ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6518ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6519ac1d519cSBjoern A. Zeeb 
6520ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6521ac1d519cSBjoern A. Zeeb }
6522ac1d519cSBjoern A. Zeeb 
6523ac1d519cSBjoern A. Zeeb void
6524ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6525ac1d519cSBjoern A. Zeeb {
6526ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6527ac1d519cSBjoern A. Zeeb 
6528ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6529ac1d519cSBjoern A. Zeeb 
6530ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6531ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6532ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6533ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6534ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6535ac1d519cSBjoern A. Zeeb 
6536ac1d519cSBjoern A. Zeeb 	/*
6537ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6538ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6539ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6540ac1d519cSBjoern A. Zeeb 	 */
6541ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6542ac1d519cSBjoern A. Zeeb }
6543ac1d519cSBjoern A. Zeeb 
6544ac1d519cSBjoern A. Zeeb void
6545ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6546ac1d519cSBjoern A. Zeeb {
6547ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6548ac1d519cSBjoern A. Zeeb 
6549ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6550ac1d519cSBjoern A. Zeeb 
6551ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6552ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6553ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6554ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6555ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6556ac1d519cSBjoern A. Zeeb }
6557ac1d519cSBjoern A. Zeeb 
6558ac1d519cSBjoern A. Zeeb void
6559ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6560ac1d519cSBjoern A. Zeeb {
6561ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6562ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6563ac1d519cSBjoern A. Zeeb 
6564ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6565ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6566ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6567ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6568ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6569ac1d519cSBjoern A. Zeeb 		return;
6570ac1d519cSBjoern A. Zeeb 	}
6571ac1d519cSBjoern A. Zeeb 
6572ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6573ac1d519cSBjoern A. Zeeb 
6574ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6575ac1d519cSBjoern A. Zeeb 		    entry);
6576ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6577ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6578ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6579ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6580ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6581ac1d519cSBjoern A. Zeeb 			break;
6582ac1d519cSBjoern A. Zeeb 	}
6583ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6584ac1d519cSBjoern A. Zeeb }
6585ac1d519cSBjoern A. Zeeb 
6586ac1d519cSBjoern A. Zeeb void
6587ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6588ac1d519cSBjoern A. Zeeb {
6589ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6590ac1d519cSBjoern A. Zeeb 
6591ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6592ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6593ac1d519cSBjoern A. Zeeb }
6594ac1d519cSBjoern A. Zeeb 
6595ac1d519cSBjoern A. Zeeb void
6596ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6597ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6598ac1d519cSBjoern A. Zeeb {
6599ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6600ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6601ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6602ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6603ac1d519cSBjoern A. Zeeb 	} else {
6604ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6605ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6606ac1d519cSBjoern A. Zeeb 	}
6607ac1d519cSBjoern A. Zeeb }
6608ac1d519cSBjoern A. Zeeb 
6609ac1d519cSBjoern A. Zeeb void
6610ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6611ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6612ac1d519cSBjoern A. Zeeb {
6613ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6614ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6615ac1d519cSBjoern A. Zeeb }
6616ac1d519cSBjoern A. Zeeb 
6617ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6618ac1d519cSBjoern A. Zeeb 
66196b4cac81SBjoern A. Zeeb struct wiphy *
66206b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
66216b4cac81SBjoern A. Zeeb {
66226b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6623ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
66246b4cac81SBjoern A. Zeeb 
66256b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
66266b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
66276b4cac81SBjoern A. Zeeb 		return (NULL);
66286b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
66296b4cac81SBjoern A. Zeeb 
6630ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6631ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6632ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6633ac1d519cSBjoern A. Zeeb 
6634ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6635ac1d519cSBjoern A. Zeeb 
6636ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6637ac1d519cSBjoern A. Zeeb 	TODO();
6638ac1d519cSBjoern A. Zeeb 
6639ac1d519cSBjoern A. Zeeb 	return (wiphy);
66406b4cac81SBjoern A. Zeeb }
66416b4cac81SBjoern A. Zeeb 
66426b4cac81SBjoern A. Zeeb void
66436b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
66446b4cac81SBjoern A. Zeeb {
66456b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
66466b4cac81SBjoern A. Zeeb 
66476b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
66486b4cac81SBjoern A. Zeeb 		return;
66496b4cac81SBjoern A. Zeeb 
6650ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6651ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6652ac1d519cSBjoern A. Zeeb 
66536b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6654ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6655ac1d519cSBjoern A. Zeeb 
66566b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
66576b4cac81SBjoern A. Zeeb }
66586b4cac81SBjoern A. Zeeb 
6659a7c19b8aSBjoern A. Zeeb static uint32_t
6660a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6661a7c19b8aSBjoern A. Zeeb {
6662a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6663a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6664a7c19b8aSBjoern A. Zeeb }
6665a7c19b8aSBjoern A. Zeeb 
6666a7c19b8aSBjoern A. Zeeb static uint32_t
6667a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6668a7c19b8aSBjoern A. Zeeb {
6669a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6670a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6671a7c19b8aSBjoern A. Zeeb }
6672a7c19b8aSBjoern A. Zeeb 
6673a7c19b8aSBjoern A. Zeeb uint32_t
6674a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6675a7c19b8aSBjoern A. Zeeb {
6676a7c19b8aSBjoern A. Zeeb 
6677a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6678a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6679a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6680a7c19b8aSBjoern A. Zeeb 
6681a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6682a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6683a7c19b8aSBjoern A. Zeeb 
6684a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6685a7c19b8aSBjoern A. Zeeb 
6686a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6687a7c19b8aSBjoern A. Zeeb }
6688a7c19b8aSBjoern A. Zeeb 
66896b4cac81SBjoern A. Zeeb uint32_t
66906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
66916b4cac81SBjoern A. Zeeb     enum nl80211_band band)
66926b4cac81SBjoern A. Zeeb {
66936b4cac81SBjoern A. Zeeb 
66946b4cac81SBjoern A. Zeeb 	switch (band) {
66956b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
66966b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
66976b4cac81SBjoern A. Zeeb 		break;
66986b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
66996b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
67006b4cac81SBjoern A. Zeeb 		break;
67016b4cac81SBjoern A. Zeeb 	default:
67026b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
67036b4cac81SBjoern A. Zeeb 		break;
67046b4cac81SBjoern A. Zeeb 	}
67056b4cac81SBjoern A. Zeeb 
67066b4cac81SBjoern A. Zeeb 	return (0);
67076b4cac81SBjoern A. Zeeb }
67086b4cac81SBjoern A. Zeeb 
67096b4cac81SBjoern A. Zeeb uint32_t
67106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
67116b4cac81SBjoern A. Zeeb {
67126b4cac81SBjoern A. Zeeb 
67136b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
67146b4cac81SBjoern A. Zeeb }
67156b4cac81SBjoern A. Zeeb 
6716ac867c20SBjoern A. Zeeb #if 0
67176b4cac81SBjoern A. Zeeb static struct lkpi_sta *
67186b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
67196b4cac81SBjoern A. Zeeb {
67206b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
67216b4cac81SBjoern A. Zeeb 
6722a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6723a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67246b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6725a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67266b4cac81SBjoern A. Zeeb 			return (lsta);
67276b4cac81SBjoern A. Zeeb 		}
67286b4cac81SBjoern A. Zeeb 	}
6729a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67306b4cac81SBjoern A. Zeeb 
67316b4cac81SBjoern A. Zeeb 	return (NULL);
67326b4cac81SBjoern A. Zeeb }
6733ac867c20SBjoern A. Zeeb #endif
67346b4cac81SBjoern A. Zeeb 
67356b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
67376b4cac81SBjoern A. Zeeb {
67386b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6739a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67406b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67416b4cac81SBjoern A. Zeeb 
67426b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
67436b4cac81SBjoern A. Zeeb 
6744a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6745a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67466b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
67476b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6748a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67496b4cac81SBjoern A. Zeeb 			return (sta);
67506b4cac81SBjoern A. Zeeb 		}
67516b4cac81SBjoern A. Zeeb 	}
6752a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67536b4cac81SBjoern A. Zeeb 	return (NULL);
67546b4cac81SBjoern A. Zeeb }
67556b4cac81SBjoern A. Zeeb 
67566b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67572e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
67582e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
67596b4cac81SBjoern A. Zeeb {
67606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67616b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67626b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
67646b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67656b4cac81SBjoern A. Zeeb 
67666b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
67676b4cac81SBjoern A. Zeeb 	sta = NULL;
67686b4cac81SBjoern A. Zeeb 
67698891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
67706b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
67716b4cac81SBjoern A. Zeeb 
67726b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
67736b4cac81SBjoern A. Zeeb 
67746b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
67756b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
67766b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
67776b4cac81SBjoern A. Zeeb 			continue;
67786b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
67796b4cac81SBjoern A. Zeeb 		if (sta != NULL)
67806b4cac81SBjoern A. Zeeb 			break;
67816b4cac81SBjoern A. Zeeb 	}
67828891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
67836b4cac81SBjoern A. Zeeb 
67846b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
67856b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
67866b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
67876b4cac81SBjoern A. Zeeb 			return (NULL);
67886b4cac81SBjoern A. Zeeb 	}
67896b4cac81SBjoern A. Zeeb 
67906b4cac81SBjoern A. Zeeb 	return (sta);
67916b4cac81SBjoern A. Zeeb }
67926b4cac81SBjoern A. Zeeb 
67936b4cac81SBjoern A. Zeeb struct sk_buff *
67946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
67956b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
67966b4cac81SBjoern A. Zeeb {
67976b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
67980cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67996b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
68006b4cac81SBjoern A. Zeeb 
68010cbcfa19SBjoern A. Zeeb 	skb = NULL;
68026b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68036b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
68046b4cac81SBjoern A. Zeeb 
68050cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
68060cbcfa19SBjoern A. Zeeb 		goto stopped;
68070cbcfa19SBjoern A. Zeeb 
68080cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
68090cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
68100cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
68110cbcfa19SBjoern A. Zeeb 		goto stopped;
68120cbcfa19SBjoern A. Zeeb 	}
68130cbcfa19SBjoern A. Zeeb 
6814eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6815eac3646fSBjoern A. Zeeb 
6816eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68176b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6818eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68196b4cac81SBjoern A. Zeeb 
68200cbcfa19SBjoern A. Zeeb stopped:
68216b4cac81SBjoern A. Zeeb 	return (skb);
68226b4cac81SBjoern A. Zeeb }
68236b4cac81SBjoern A. Zeeb 
68246b4cac81SBjoern A. Zeeb void
68256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
682686220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
68276b4cac81SBjoern A. Zeeb {
68286b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68296b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
683086220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
68316b4cac81SBjoern A. Zeeb 
68326b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68336b4cac81SBjoern A. Zeeb 
68346b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6835eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68366b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
68376b4cac81SBjoern A. Zeeb 		fc++;
68386b4cac81SBjoern A. Zeeb 		bc += skb->len;
68396b4cac81SBjoern A. Zeeb 	}
6840eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68416b4cac81SBjoern A. Zeeb 	if (frame_cnt)
68426b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
68436b4cac81SBjoern A. Zeeb 	if (byte_cnt)
68446b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
68456b4cac81SBjoern A. Zeeb 
68466b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
68476b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
68486b4cac81SBjoern A. Zeeb 	IMPROVE();
68496b4cac81SBjoern A. Zeeb }
68506b4cac81SBjoern A. Zeeb 
68516b4cac81SBjoern A. Zeeb /*
68526b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
68536b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
68546b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
68556b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
68566b4cac81SBjoern A. Zeeb  */
6857a8397571SBjoern A. Zeeb static void
6858a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
68596b4cac81SBjoern A. Zeeb     int status)
68606b4cac81SBjoern A. Zeeb {
68616b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
68626b4cac81SBjoern A. Zeeb 	struct mbuf *m;
68636b4cac81SBjoern A. Zeeb 
68646b4cac81SBjoern A. Zeeb 	m = skb->m;
68656b4cac81SBjoern A. Zeeb 	skb->m = NULL;
68666b4cac81SBjoern A. Zeeb 
68676b4cac81SBjoern A. Zeeb 	if (m != NULL) {
68686b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
68696b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
68706b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
68716b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
68726b4cac81SBjoern A. Zeeb 	}
6873a8397571SBjoern A. Zeeb }
68746b4cac81SBjoern A. Zeeb 
6875a8397571SBjoern A. Zeeb void
6876a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6877a8397571SBjoern A. Zeeb     int status)
6878a8397571SBjoern A. Zeeb {
6879a8397571SBjoern A. Zeeb 
6880a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
68816b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
68826b4cac81SBjoern A. Zeeb }
68836b4cac81SBjoern A. Zeeb 
6884383b3e8fSBjoern A. Zeeb void
6885a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6886a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6887383b3e8fSBjoern A. Zeeb {
6888a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6889383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6890383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6891383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6892383b3e8fSBjoern A. Zeeb 	int status;
6893383b3e8fSBjoern A. Zeeb 
6894a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6895383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6896383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6897383b3e8fSBjoern A. Zeeb 
6898383b3e8fSBjoern A. Zeeb 		m = skb->m;
6899383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6900383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6901383b3e8fSBjoern A. Zeeb 	} else {
6902383b3e8fSBjoern A. Zeeb 		ni = NULL;
6903383b3e8fSBjoern A. Zeeb 	}
6904383b3e8fSBjoern A. Zeeb 
6905a8397571SBjoern A. Zeeb 	info = txstat->info;
6906383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6907383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6908383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6909383b3e8fSBjoern A. Zeeb 	} else {
6910383b3e8fSBjoern A. Zeeb 		status = 1;
6911383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6912383b3e8fSBjoern A. Zeeb 	}
6913383b3e8fSBjoern A. Zeeb 
6914383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6915e2c5ab09SJohn Baldwin 		int ridx __unused;
6916383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6917383b3e8fSBjoern A. Zeeb 		int old_rate;
6918383b3e8fSBjoern A. Zeeb 
6919383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6920383b3e8fSBjoern A. Zeeb #endif
6921383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6922383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6923383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6924383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6925383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6926383b3e8fSBjoern A. Zeeb 		}
6927383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6928a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6929383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6930383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6931383b3e8fSBjoern A. Zeeb #endif
6932adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6933383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6934383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6935383b3e8fSBjoern A. Zeeb 		}
6936383b3e8fSBjoern A. Zeeb 
6937383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6938383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6939383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6940383b3e8fSBjoern A. Zeeb 
6941383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6942383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6943383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6944383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6945383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6946383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6947383b3e8fSBjoern A. Zeeb 		}
6948383b3e8fSBjoern A. Zeeb #endif
6949383b3e8fSBjoern A. Zeeb 	}
6950383b3e8fSBjoern A. Zeeb 
6951383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6952383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6953383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6954383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6955383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6956383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6957adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6958383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6959383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6960383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6961383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6962383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6963383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6964383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6965383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6966383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6967383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6968383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6969383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6970383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6971adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6972383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6973383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6974383b3e8fSBjoern A. Zeeb #endif
6975383b3e8fSBjoern A. Zeeb 
6976a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6977a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6978a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6979a8397571SBjoern A. Zeeb 	} else {
6980383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6981383b3e8fSBjoern A. Zeeb 	}
6982a8397571SBjoern A. Zeeb }
6983a8397571SBjoern A. Zeeb 
6984a8397571SBjoern A. Zeeb void
6985a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6986a8397571SBjoern A. Zeeb {
6987a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6988a8397571SBjoern A. Zeeb 
6989a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6990a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6991a8397571SBjoern A. Zeeb 	status.skb = skb;
6992a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6993a8397571SBjoern A. Zeeb 
6994a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6995a8397571SBjoern A. Zeeb }
6996383b3e8fSBjoern A. Zeeb 
69976b4cac81SBjoern A. Zeeb /*
69986b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
69996b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
70006b4cac81SBjoern A. Zeeb  * mbufs this should go away.
70016b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
70026b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
70036b4cac81SBjoern A. Zeeb  */
70046b4cac81SBjoern A. Zeeb static void
70056b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
70066b4cac81SBjoern A. Zeeb {
70076b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
70086b4cac81SBjoern A. Zeeb 	struct mbuf *m;
70096b4cac81SBjoern A. Zeeb 
70106b4cac81SBjoern A. Zeeb 	if (p == NULL)
70116b4cac81SBjoern A. Zeeb 		return;
70126b4cac81SBjoern A. Zeeb 
70136b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
70146b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
70156b4cac81SBjoern A. Zeeb 
70166b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
70176b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
70186b4cac81SBjoern A. Zeeb 	if (ni != NULL)
70196b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
70206b4cac81SBjoern A. Zeeb 	m_freem(m);
70216b4cac81SBjoern A. Zeeb }
70226b4cac81SBjoern A. Zeeb 
70236b4cac81SBjoern A. Zeeb void
70246b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
70256b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
70266b4cac81SBjoern A. Zeeb {
70276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70286b4cac81SBjoern A. Zeeb 
70296b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70306b4cac81SBjoern A. Zeeb 	IMPROVE();
70316b4cac81SBjoern A. Zeeb 
70326b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70336b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
70346b4cac81SBjoern A. Zeeb }
70356b4cac81SBjoern A. Zeeb 
70366b4cac81SBjoern A. Zeeb void
70376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
70386b4cac81SBjoern A. Zeeb     struct work_struct *w)
70396b4cac81SBjoern A. Zeeb {
70406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70416b4cac81SBjoern A. Zeeb 
70426b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70436b4cac81SBjoern A. Zeeb 	IMPROVE();
70446b4cac81SBjoern A. Zeeb 
70456b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70466b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
70476b4cac81SBjoern A. Zeeb }
70486b4cac81SBjoern A. Zeeb 
70496b4cac81SBjoern A. Zeeb struct sk_buff *
7050ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7051ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7052ade774b1SBjoern A. Zeeb {
7053ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7054ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7055ade774b1SBjoern A. Zeeb 	uint8_t *p;
7056ade774b1SBjoern A. Zeeb 	size_t len;
7057ade774b1SBjoern A. Zeeb 
7058ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7059ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7060ade774b1SBjoern A. Zeeb 
7061ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7062ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7063ade774b1SBjoern A. Zeeb 		return (NULL);
7064ade774b1SBjoern A. Zeeb 
7065ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7066ade774b1SBjoern A. Zeeb 
7067ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7068ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7069ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7070ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7071ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7072ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7073ade774b1SBjoern A. Zeeb 
7074ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7075ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7076ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7077ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7078ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7079ade774b1SBjoern A. Zeeb 
7080ade774b1SBjoern A. Zeeb 	return (skb);
7081ade774b1SBjoern A. Zeeb }
7082ade774b1SBjoern A. Zeeb 
7083ade774b1SBjoern A. Zeeb struct sk_buff *
70846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
70856b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
70866b4cac81SBjoern A. Zeeb {
70876b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70886b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70896b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70906b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
70916b4cac81SBjoern A. Zeeb 	uint16_t v;
70926b4cac81SBjoern A. Zeeb 
70936b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
70946b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70956b4cac81SBjoern A. Zeeb 		return (NULL);
70966b4cac81SBjoern A. Zeeb 
70976b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
70986b4cac81SBjoern A. Zeeb 
70996b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71006b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71016b4cac81SBjoern A. Zeeb 
71026b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
71036b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
71046b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7105616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
71066b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
71076b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
71086b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
71096b4cac81SBjoern A. Zeeb 
71106b4cac81SBjoern A. Zeeb 	return (skb);
71116b4cac81SBjoern A. Zeeb }
71126b4cac81SBjoern A. Zeeb 
71136b4cac81SBjoern A. Zeeb struct sk_buff *
71146b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7115adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
71166b4cac81SBjoern A. Zeeb {
71176b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71186b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71196b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
71206b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
71216b4cac81SBjoern A. Zeeb 
7122adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7123adff403fSBjoern A. Zeeb 
71246b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
71256b4cac81SBjoern A. Zeeb 	if (skb == NULL)
71266b4cac81SBjoern A. Zeeb 		return (NULL);
71276b4cac81SBjoern A. Zeeb 
71286b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71296b4cac81SBjoern A. Zeeb 
71306b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71316b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71326b4cac81SBjoern A. Zeeb 
71336b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
71346b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
71356b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
71366b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
71376b4cac81SBjoern A. Zeeb 
71386b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
71396b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
71406b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
71416b4cac81SBjoern A. Zeeb 
71426b4cac81SBjoern A. Zeeb 	return (skb);
71436b4cac81SBjoern A. Zeeb }
71446b4cac81SBjoern A. Zeeb 
71456b4cac81SBjoern A. Zeeb struct wireless_dev *
71466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
71476b4cac81SBjoern A. Zeeb {
71486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71496b4cac81SBjoern A. Zeeb 
71506b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71516b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
71526b4cac81SBjoern A. Zeeb }
71536b4cac81SBjoern A. Zeeb 
71546b4cac81SBjoern A. Zeeb void
71556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
71566b4cac81SBjoern A. Zeeb {
71576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71586b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71596b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
71606b4cac81SBjoern A. Zeeb 	int arg;
71616b4cac81SBjoern A. Zeeb 
71626b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71636b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71646b4cac81SBjoern A. Zeeb 
71656b4cac81SBjoern A. Zeeb 	/*
7166f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
71676b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
71686b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
71696b4cac81SBjoern A. Zeeb 	 */
7170f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7171bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
71726b4cac81SBjoern A. Zeeb 
7173018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7174018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71756b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
71766b4cac81SBjoern A. Zeeb }
71776b4cac81SBjoern A. Zeeb 
7178bb81db90SBjoern A. Zeeb void
7179bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7180bb81db90SBjoern A. Zeeb {
7181bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7182bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7183bb81db90SBjoern A. Zeeb 
7184bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7185bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7186bb81db90SBjoern A. Zeeb 
7187bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7188bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71893540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7190bb81db90SBjoern A. Zeeb }
7191bb81db90SBjoern A. Zeeb 
71925edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
71935edde07cSBjoern A. Zeeb 
71945a9a0d78SBjoern A. Zeeb void
71955a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
71965a9a0d78SBjoern A. Zeeb {
71975a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71985a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71995a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
72005a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
72015a9a0d78SBjoern A. Zeeb 
72025a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
72035a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
72045a9a0d78SBjoern A. Zeeb 
72055a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72065a9a0d78SBjoern A. Zeeb 
72075a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72085a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72095a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72105a9a0d78SBjoern A. Zeeb 	else
72115a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72125a9a0d78SBjoern A. Zeeb 
72135a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72145a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72155a9a0d78SBjoern A. Zeeb 
72165a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72175a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72185a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
72195a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
72200d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72215a9a0d78SBjoern A. Zeeb 				/*
72225a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72235a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72245a9a0d78SBjoern A. Zeeb 				 */
72250d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
72260d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72275a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72285a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
72295a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
72305a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
72310d2cb6a6SBjoern A. Zeeb #endif
72325a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
72335a9a0d78SBjoern A. Zeeb 			}
72345a9a0d78SBjoern A. Zeeb 		}
72355a9a0d78SBjoern A. Zeeb 	}
72365a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72375a9a0d78SBjoern A. Zeeb }
72385a9a0d78SBjoern A. Zeeb 
72395a9a0d78SBjoern A. Zeeb void
72405a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
72415a9a0d78SBjoern A. Zeeb {
72425a9a0d78SBjoern A. Zeeb 	int i;
72435a9a0d78SBjoern A. Zeeb 
72445a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
72455a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72465a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
72475a9a0d78SBjoern A. Zeeb }
72485a9a0d78SBjoern A. Zeeb 
72495a9a0d78SBjoern A. Zeeb 
72505a9a0d78SBjoern A. Zeeb static void
72515a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
72525a9a0d78SBjoern A. Zeeb {
72535a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72545a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72555a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
72565a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
72575a9a0d78SBjoern A. Zeeb 
72585a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72595a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72605a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72615a9a0d78SBjoern A. Zeeb 	else
72625a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72635a9a0d78SBjoern A. Zeeb 
72645a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72655a9a0d78SBjoern A. Zeeb 
72665a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
72675a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72685a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72695a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
72705a9a0d78SBjoern A. Zeeb 
72715a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72725a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72735a9a0d78SBjoern A. Zeeb 
72745a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
72755a9a0d78SBjoern A. Zeeb 
72765a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
72775a9a0d78SBjoern A. Zeeb 
72780d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72795a9a0d78SBjoern A. Zeeb 				/*
72805a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72815a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72825a9a0d78SBjoern A. Zeeb 				 */
72830d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
72840d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72855a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72865a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
72875a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
72885a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
72890d2cb6a6SBjoern A. Zeeb #endif
72905a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
72915a9a0d78SBjoern A. Zeeb 
7292a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7293a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72945a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
72955a9a0d78SBjoern A. Zeeb 
72965a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
72975a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
72985a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
72995a9a0d78SBjoern A. Zeeb 
73005a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
73015a9a0d78SBjoern A. Zeeb 							continue;
73025a9a0d78SBjoern A. Zeeb 
73035a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
73045a9a0d78SBjoern A. Zeeb 							continue;
73055a9a0d78SBjoern A. Zeeb 
73065a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
73075a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
73085a9a0d78SBjoern A. Zeeb 							continue;
73095a9a0d78SBjoern A. Zeeb 
73105a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
73115a9a0d78SBjoern A. Zeeb 
73125a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
73135a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
73145a9a0d78SBjoern A. Zeeb 					}
73155a9a0d78SBjoern A. Zeeb 				}
7316a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
73175a9a0d78SBjoern A. Zeeb 			}
73185a9a0d78SBjoern A. Zeeb 		}
73195a9a0d78SBjoern A. Zeeb 	}
73205a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
73215a9a0d78SBjoern A. Zeeb }
73225a9a0d78SBjoern A. Zeeb 
73235a9a0d78SBjoern A. Zeeb void
73245a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
73255a9a0d78SBjoern A. Zeeb {
73265a9a0d78SBjoern A. Zeeb 	int i;
73275a9a0d78SBjoern A. Zeeb 
73285a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
73295a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
73305a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
73315a9a0d78SBjoern A. Zeeb }
73325a9a0d78SBjoern A. Zeeb 
73335a9a0d78SBjoern A. Zeeb void
73345a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
73355a9a0d78SBjoern A. Zeeb {
73365a9a0d78SBjoern A. Zeeb 
73375a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
73385a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
73395a9a0d78SBjoern A. Zeeb 
73405a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
73415a9a0d78SBjoern A. Zeeb }
73425a9a0d78SBjoern A. Zeeb 
73435a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
73445a9a0d78SBjoern A. Zeeb void
73455a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
73465a9a0d78SBjoern A. Zeeb {
73475a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73485a9a0d78SBjoern A. Zeeb 
73495a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73505a9a0d78SBjoern A. Zeeb 
73515a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
73525a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73535a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
73545a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
73555a9a0d78SBjoern A. Zeeb }
73565a9a0d78SBjoern A. Zeeb 
73575a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
73585a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
73595a9a0d78SBjoern A. Zeeb {
73605a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73615a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
73625a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73635a9a0d78SBjoern A. Zeeb 
73645a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73655a9a0d78SBjoern A. Zeeb 	txq = NULL;
73665a9a0d78SBjoern A. Zeeb 
73675a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73685a9a0d78SBjoern A. Zeeb 
73695a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
73705a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
73715a9a0d78SBjoern A. Zeeb 		goto out;
73725a9a0d78SBjoern A. Zeeb 
73735a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
73745a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
73755a9a0d78SBjoern A. Zeeb 		goto out;
73765a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
73775a9a0d78SBjoern A. Zeeb 		goto out;
73785a9a0d78SBjoern A. Zeeb 
73795a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
73805a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
73815a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
73825a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
73835a9a0d78SBjoern A. Zeeb 
73845a9a0d78SBjoern A. Zeeb out:
73855a9a0d78SBjoern A. Zeeb 	return (txq);
73865a9a0d78SBjoern A. Zeeb }
73875a9a0d78SBjoern A. Zeeb 
73885a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
73895a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
73905a9a0d78SBjoern A. Zeeb {
73915a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73925a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7393eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
73945a9a0d78SBjoern A. Zeeb 
73955a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73965a9a0d78SBjoern A. Zeeb 
73975a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73985a9a0d78SBjoern A. Zeeb 
73995a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7400eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7401eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7402eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7403eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
74045a9a0d78SBjoern A. Zeeb 		goto out;
74055a9a0d78SBjoern A. Zeeb 
740641b746e0SAustin Shafer 	/*
740741b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
740841b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
740941b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
741041b746e0SAustin Shafer 	 */
741141b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
74125a9a0d78SBjoern A. Zeeb 		goto out;
74135a9a0d78SBjoern A. Zeeb 
74145a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74155a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
74165a9a0d78SBjoern A. Zeeb out:
74175a9a0d78SBjoern A. Zeeb 	return;
74185a9a0d78SBjoern A. Zeeb }
74195a9a0d78SBjoern A. Zeeb 
7420eac3646fSBjoern A. Zeeb void
7421eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7422eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7423eac3646fSBjoern A. Zeeb {
7424eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7425eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7426eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7427eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7428eac3646fSBjoern A. Zeeb 
7429eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7430eac3646fSBjoern A. Zeeb 
7431eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7432eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7433eac3646fSBjoern A. Zeeb 	do {
7434eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7435eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7436eac3646fSBjoern A. Zeeb 			break;
7437eac3646fSBjoern A. Zeeb 
7438eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7439eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7440eac3646fSBjoern A. Zeeb 		do {
7441eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7442eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7443eac3646fSBjoern A. Zeeb 				break;
7444eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7445eac3646fSBjoern A. Zeeb 		} while(1);
7446eac3646fSBjoern A. Zeeb 
7447eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7448eac3646fSBjoern A. Zeeb 	} while (1);
7449eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7450eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7451eac3646fSBjoern A. Zeeb }
7452eac3646fSBjoern A. Zeeb 
74535a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
74545a9a0d78SBjoern A. Zeeb 
74555edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
74565edde07cSBjoern A. Zeeb 	u_int refcnt;
74575edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
74585edde07cSBjoern A. Zeeb };
74595edde07cSBjoern A. Zeeb 
74605edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
74615edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
74625edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
74635edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
74645edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
74655edde07cSBjoern A. Zeeb 	size_t ssid_len;
74665edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
74675edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
74685edde07cSBjoern A. Zeeb 
74695edde07cSBjoern A. Zeeb 	/*
74705edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
74715edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
74725edde07cSBjoern A. Zeeb 	 */
74735edde07cSBjoern A. Zeeb 	bool match;
74745edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
74755edde07cSBjoern A. Zeeb };
74765edde07cSBjoern A. Zeeb 
74775edde07cSBjoern A. Zeeb static void
74785edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
74795edde07cSBjoern A. Zeeb {
74805edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
74815edde07cSBjoern A. Zeeb 	size_t ielen;
74825edde07cSBjoern A. Zeeb 
74835edde07cSBjoern A. Zeeb 	lookup = arg;
74845edde07cSBjoern A. Zeeb 
74855edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
74865edde07cSBjoern A. Zeeb 	if (lookup->match)
74875edde07cSBjoern A. Zeeb 		return;
74885edde07cSBjoern A. Zeeb 
74895edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
74905edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
74915edde07cSBjoern A. Zeeb 		return;
74925edde07cSBjoern A. Zeeb 
74935edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
74945edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
74955edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
74965edde07cSBjoern A. Zeeb 		return;
74975edde07cSBjoern A. Zeeb 	}
74985edde07cSBjoern A. Zeeb 
74995edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
75005edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
75015edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
75025edde07cSBjoern A. Zeeb 		return;
75035edde07cSBjoern A. Zeeb 	}
75045edde07cSBjoern A. Zeeb 
75055edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
75065edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
75075edde07cSBjoern A. Zeeb 
75085edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
75095edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
75105edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
75115edde07cSBjoern A. Zeeb 			return;
75125edde07cSBjoern A. Zeeb 	}
75135edde07cSBjoern A. Zeeb 
75145edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
75155edde07cSBjoern A. Zeeb 		return;
75165edde07cSBjoern A. Zeeb 
75175edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
75185edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
75195edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
75205edde07cSBjoern A. Zeeb 			return;
75215edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
75225edde07cSBjoern A. Zeeb 			return;
75235edde07cSBjoern A. Zeeb 	}
75245edde07cSBjoern A. Zeeb 
75255edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
75265edde07cSBjoern A. Zeeb 
75275edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
75285edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
75295edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
75305edde07cSBjoern A. Zeeb 		return;
75315edde07cSBjoern A. Zeeb 
75325edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
75335edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
75345edde07cSBjoern A. Zeeb 	if (ielen)
75355edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
75365edde07cSBjoern A. Zeeb 
75375edde07cSBjoern A. Zeeb 	lookup->match = true;
75385edde07cSBjoern A. Zeeb }
75395edde07cSBjoern A. Zeeb 
75405edde07cSBjoern A. Zeeb struct cfg80211_bss *
75415edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
75425edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
75435edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
75445edde07cSBjoern A. Zeeb {
75455edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75465edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
75475edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75485edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
75495edde07cSBjoern A. Zeeb 
75505edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
75515edde07cSBjoern A. Zeeb 
75525edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
75535edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
75545edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
75555edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
75565edde07cSBjoern A. Zeeb 		return (NULL);
75575edde07cSBjoern A. Zeeb 	}
75585edde07cSBjoern A. Zeeb 
75595edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
75605edde07cSBjoern A. Zeeb 	lookup.chan = chan;
75615edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
75625edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
75635edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
75645edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
75655edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
75665edde07cSBjoern A. Zeeb 	lookup.match = false;
75675edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
75685edde07cSBjoern A. Zeeb 
75695edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
75705edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
75715edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
75725edde07cSBjoern A. Zeeb 	if (!lookup.match) {
75735edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75745edde07cSBjoern A. Zeeb 		return (NULL);
75755edde07cSBjoern A. Zeeb 	}
75765edde07cSBjoern A. Zeeb 
75775edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
75785edde07cSBjoern A. Zeeb 	return (&lbss->bss);
75795edde07cSBjoern A. Zeeb }
75805edde07cSBjoern A. Zeeb 
75815edde07cSBjoern A. Zeeb void
75825edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
75835edde07cSBjoern A. Zeeb {
75845edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75855edde07cSBjoern A. Zeeb 
75865edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
75875edde07cSBjoern A. Zeeb 
75885edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
75895edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
75905edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
75915edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75925edde07cSBjoern A. Zeeb 	}
75935edde07cSBjoern A. Zeeb }
75945edde07cSBjoern A. Zeeb 
75955edde07cSBjoern A. Zeeb void
75965edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
75975edde07cSBjoern A. Zeeb {
75985edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75995edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
76005edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
76015edde07cSBjoern A. Zeeb 
76025edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
76035edde07cSBjoern A. Zeeb 	ic = lhw->ic;
76045edde07cSBjoern A. Zeeb 
76055edde07cSBjoern A. Zeeb 	/*
76065edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
76075edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
76085edde07cSBjoern A. Zeeb 	 */
76095edde07cSBjoern A. Zeeb 	if (ic == NULL ||
76105edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
76115edde07cSBjoern A. Zeeb 		return;
76125edde07cSBjoern A. Zeeb 
76135edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
76145edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
76155edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
76165edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
76175edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
76185edde07cSBjoern A. Zeeb }
76195edde07cSBjoern A. Zeeb 
76205edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
76215edde07cSBjoern A. Zeeb 
7622ac1d519cSBjoern A. Zeeb /*
7623ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7624ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7625ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7626ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7627ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7628ac1d519cSBjoern A. Zeeb  */
7629ac1d519cSBjoern A. Zeeb 
7630ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7631ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7632ac1d519cSBjoern A. Zeeb {
7633ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7634ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7635ac1d519cSBjoern A. Zeeb 	int error;
7636ac1d519cSBjoern A. Zeeb 
7637ac1d519cSBjoern A. Zeeb 	changed = 0;
7638ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7639ac1d519cSBjoern A. Zeeb 		cd = NULL;
7640ac1d519cSBjoern A. Zeeb 	else
7641ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7642ac1d519cSBjoern A. Zeeb 
7643ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7644ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7645ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7646ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7647ac1d519cSBjoern A. Zeeb 	}
7648ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7649ac1d519cSBjoern A. Zeeb 
7650ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7651ac1d519cSBjoern A. Zeeb 		return (0);
7652ac1d519cSBjoern A. Zeeb 
7653ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7654ac1d519cSBjoern A. Zeeb 	return (error);
7655ac1d519cSBjoern A. Zeeb }
7656ac1d519cSBjoern A. Zeeb 
7657ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7658ac1d519cSBjoern A. Zeeb 
76596b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
76606b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
76616b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7662