xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 408394185fe9be0c158189b7671bde316ac14ac9)
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 
426b4cac81SBjoern A. Zeeb #include <sys/param.h>
436b4cac81SBjoern A. Zeeb #include <sys/types.h>
446b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
456b4cac81SBjoern A. Zeeb #include <sys/errno.h>
466b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
476b4cac81SBjoern A. Zeeb #include <sys/module.h>
486b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
49*40839418SBjoern A. Zeeb #include <sys/sbuf.h>
506b4cac81SBjoern A. Zeeb #include <sys/socket.h>
516b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
526b4cac81SBjoern A. Zeeb #include <sys/queue.h>
536b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
54527687a9SBjoern A. Zeeb #include <sys/libkern.h>
556b4cac81SBjoern A. Zeeb 
566b4cac81SBjoern A. Zeeb #include <net/if.h>
576b4cac81SBjoern A. Zeeb #include <net/if_var.h>
586b4cac81SBjoern A. Zeeb #include <net/if_media.h>
596b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
606b4cac81SBjoern A. Zeeb 
616b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
626b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
646b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
659fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
686b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
696b4cac81SBjoern A. Zeeb 
706b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
716b4cac81SBjoern A. Zeeb #include "linux_80211.h"
726b4cac81SBjoern A. Zeeb 
734a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
74b35f6cd0SBjoern A. Zeeb /* #define	LKPI_80211_HW_CRYPTO */
759fb91463SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
769fb91463SBjoern A. Zeeb /* #define	LKPI_80211_HT */
779fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
789fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
799fb91463SBjoern A. Zeeb #endif
80b35f6cd0SBjoern A. Zeeb 
816b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
826b4cac81SBjoern A. Zeeb 
835a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
845a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
855a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
865a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
875a9a0d78SBjoern A. Zeeb } while (0)
885a9a0d78SBjoern A. Zeeb 
896b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
906b4cac81SBjoern A. Zeeb 
919d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
929d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
939d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
949d9ba2b7SBjoern A. Zeeb 
95*40839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
96*40839418SBjoern A. Zeeb int linuxkpi_debug_80211;
97*40839418SBjoern A. Zeeb 
98*40839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
999d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1009d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1019d9ba2b7SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1036b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1049d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1056b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1066b4cac81SBjoern A. Zeeb #else
1076b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1086b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1096b4cac81SBjoern A. Zeeb #endif
1106b4cac81SBjoern A. Zeeb 
1116b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1126b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1136b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1146b4cac81SBjoern A. Zeeb #endif
1156b4cac81SBjoern A. Zeeb 
1166b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1176b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1186b4cac81SBjoern A. Zeeb 
119b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
120b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
121b0f73768SBjoern A. Zeeb 
122fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
123fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1244f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1254f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1264f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1274f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1284f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1294f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1304f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1314f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1324f61ef8bSBjoern A. Zeeb #if 0
1334f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1344f61ef8bSBjoern A. Zeeb #endif
1354f61ef8bSBjoern A. Zeeb };
1364f61ef8bSBjoern A. Zeeb 
1376b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1386b4cac81SBjoern A. Zeeb 	/*
1396b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1406b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1416b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1426b4cac81SBjoern A. Zeeb 	 */
1436b4cac81SBjoern A. Zeeb };
1446b4cac81SBjoern A. Zeeb 
145ac867c20SBjoern A. Zeeb #if 0
1466b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1476b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
148ac867c20SBjoern A. Zeeb #endif
14988665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1506b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
151759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1526b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1534a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1544a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1554a67f1dfSBjoern A. Zeeb #endif
1566b4cac81SBjoern A. Zeeb 
157*40839418SBjoern A. Zeeb static const char *
158*40839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
159*40839418SBjoern A. Zeeb {
160*40839418SBjoern A. Zeeb 
161*40839418SBjoern A. Zeeb 	switch (bw) {
162*40839418SBjoern A. Zeeb 
163*40839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
164*40839418SBjoern A. Zeeb 		return ("20");
165*40839418SBjoern A. Zeeb 		break;
166*40839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
167*40839418SBjoern A. Zeeb 		return ("5");
168*40839418SBjoern A. Zeeb 		break;
169*40839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
170*40839418SBjoern A. Zeeb 		return ("10");
171*40839418SBjoern A. Zeeb 		break;
172*40839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
173*40839418SBjoern A. Zeeb 		return ("40");
174*40839418SBjoern A. Zeeb 		break;
175*40839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
176*40839418SBjoern A. Zeeb 		return ("80");
177*40839418SBjoern A. Zeeb 		break;
178*40839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
179*40839418SBjoern A. Zeeb 		return ("160");
180*40839418SBjoern A. Zeeb 		break;
181*40839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
182*40839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
183*40839418SBjoern A. Zeeb 		return ("HE_RU");
184*40839418SBjoern A. Zeeb 		break;
185*40839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
186*40839418SBjoern A. Zeeb 		return ("320");
187*40839418SBjoern A. Zeeb 		break;
188*40839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
189*40839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
190*40839418SBjoern A. Zeeb 		return ("EHT_RU");
191*40839418SBjoern A. Zeeb 		break;
192*40839418SBjoern A. Zeeb 	default:
193*40839418SBjoern A. Zeeb 		return ("?");
194*40839418SBjoern A. Zeeb 		break;
195*40839418SBjoern A. Zeeb 	}
196*40839418SBjoern A. Zeeb }
197*40839418SBjoern A. Zeeb 
198*40839418SBjoern A. Zeeb static void
199*40839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
200*40839418SBjoern A. Zeeb     const uint64_t flags)
201*40839418SBjoern A. Zeeb {
202*40839418SBjoern A. Zeeb 	int bit, i;
203*40839418SBjoern A. Zeeb 
204*40839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
205*40839418SBjoern A. Zeeb 
206*40839418SBjoern A. Zeeb 	i = 0;
207*40839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
208*40839418SBjoern A. Zeeb 
209*40839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
210*40839418SBjoern A. Zeeb 			continue;
211*40839418SBjoern A. Zeeb 
212*40839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
213*40839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
214*40839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
215*40839418SBjoern A. Zeeb 		i++;							\
216*40839418SBjoern A. Zeeb 		break;
217*40839418SBjoern A. Zeeb 
218*40839418SBjoern A. Zeeb 		switch (bit) {
219*40839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
220*40839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
221*40839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
222*40839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
223*40839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
224*40839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
225*40839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
226*40839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
227*40839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
228*40839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
229*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
230*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
231*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
232*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
233*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
234*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
235*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
236*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
237*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
238*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
239*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
240*40839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
241*40839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
242*40839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
243*40839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
244*40839418SBjoern A. Zeeb 		default:
245*40839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
246*40839418SBjoern A. Zeeb 			break;
247*40839418SBjoern A. Zeeb 		}
248*40839418SBjoern A. Zeeb 	}
249*40839418SBjoern A. Zeeb #undef	EXPAND_CASE
250*40839418SBjoern A. Zeeb 	if (i > 0)
251*40839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
252*40839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
253*40839418SBjoern A. Zeeb }
254*40839418SBjoern A. Zeeb 
255*40839418SBjoern A. Zeeb static int
256*40839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
257*40839418SBjoern A. Zeeb {
258*40839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
259*40839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
260*40839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
261*40839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
262*40839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
263*40839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
264*40839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
265*40839418SBjoern A. Zeeb 	struct station_info sinfo;
266*40839418SBjoern A. Zeeb 	struct sbuf s;
267*40839418SBjoern A. Zeeb 	int error;
268*40839418SBjoern A. Zeeb 
269*40839418SBjoern A. Zeeb 	if (req->newptr)
270*40839418SBjoern A. Zeeb 		return (EPERM);
271*40839418SBjoern A. Zeeb 
272*40839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
273*40839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
274*40839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
275*40839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
276*40839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
277*40839418SBjoern A. Zeeb 
278*40839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
279*40839418SBjoern A. Zeeb 
280*40839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
281*40839418SBjoern A. Zeeb 	TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
282*40839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
283*40839418SBjoern A. Zeeb 
284*40839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
285*40839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
286*40839418SBjoern A. Zeeb 
287*40839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
288*40839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
289*40839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
290*40839418SBjoern A. Zeeb 			continue;
291*40839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
292*40839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
293*40839418SBjoern A. Zeeb 			continue;
294*40839418SBjoern A. Zeeb 		}
295*40839418SBjoern A. Zeeb 		if (error != 0) {
296*40839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
297*40839418SBjoern A. Zeeb 			continue;
298*40839418SBjoern A. Zeeb 		}
299*40839418SBjoern A. Zeeb 
300*40839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
301*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
302*40839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
303*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
304*40839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
305*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
306*40839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
307*40839418SBjoern A. Zeeb 
308*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
309*40839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
310*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
311*40839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
312*40839418SBjoern A. Zeeb 
313*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
314*40839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
315*40839418SBjoern A. Zeeb 
316*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
317*40839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
318*40839418SBjoern A. Zeeb 
319*40839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
320*40839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
321*40839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
322*40839418SBjoern A. Zeeb 		}
323*40839418SBjoern A. Zeeb 
324*40839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
325*40839418SBjoern A. Zeeb 
326*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
327*40839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
328*40839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
329*40839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
330*40839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
331*40839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
332*40839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
333*40839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
334*40839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
335*40839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
336*40839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
337*40839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
338*40839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
339*40839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
340*40839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
341*40839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
342*40839418SBjoern A. Zeeb 	}
343*40839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
344*40839418SBjoern A. Zeeb 
345*40839418SBjoern A. Zeeb 	sbuf_finish(&s);
346*40839418SBjoern A. Zeeb 	sbuf_delete(&s);
347*40839418SBjoern A. Zeeb 
348*40839418SBjoern A. Zeeb 	return (0);
349*40839418SBjoern A. Zeeb }
350*40839418SBjoern A. Zeeb 
3519fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3529fb91463SBjoern A. Zeeb static void
3539fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
3549fb91463SBjoern A. Zeeb {
3559fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
3569fb91463SBjoern A. Zeeb 	uint8_t *ie;
3579fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
3589fb91463SBjoern A. Zeeb 	int i, rx_nss;
3599fb91463SBjoern A. Zeeb 
3609fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
3619fb91463SBjoern A. Zeeb 		return;
3629fb91463SBjoern A. Zeeb 
3639fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3649fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3659fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
3669fb91463SBjoern A. Zeeb 
3679fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
3689fb91463SBjoern A. Zeeb 
3699fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
3709fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
3719fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3729fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
3739fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
3749fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3759fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
3769fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
3779fb91463SBjoern A. Zeeb 
3789fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
3799fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
3809fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
3819fb91463SBjoern A. Zeeb 		ie += 4;
3829fb91463SBjoern A. Zeeb 	ie += 2;
3839fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
3849fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
3859fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
3869fb91463SBjoern A. Zeeb 
3879fb91463SBjoern A. Zeeb 	rx_nss = 0;
3889fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
3899fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
3909fb91463SBjoern A. Zeeb 			rx_nss++;
3919fb91463SBjoern A. Zeeb 	}
3929fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
3939fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
3949fb91463SBjoern A. Zeeb 
3959fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
3969fb91463SBjoern A. Zeeb }
3979fb91463SBjoern A. Zeeb #endif
3989fb91463SBjoern A. Zeeb 
3999fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4009fb91463SBjoern A. Zeeb static void
4019fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
4029fb91463SBjoern A. Zeeb {
4039fb91463SBjoern A. Zeeb 
4049fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
4059fb91463SBjoern A. Zeeb 		return;
4069fb91463SBjoern A. Zeeb 
4079fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
4089fb91463SBjoern A. Zeeb #ifdef __notyet__
4099fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
4109fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
4119fb91463SBjoern A. Zeeb 		} else
4129fb91463SBjoern A. Zeeb #endif
4139fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
4149fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
4159fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
4169fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4179fb91463SBjoern A. Zeeb 	}
4189fb91463SBjoern A. Zeeb 
4199fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
4209fb91463SBjoern A. Zeeb 	return;
4219fb91463SBjoern A. Zeeb }
4229fb91463SBjoern A. Zeeb #endif
4239fb91463SBjoern A. Zeeb 
424d9f59799SBjoern A. Zeeb static void
42567674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
42667674c1cSBjoern A. Zeeb     const char *_f, int _l)
427d9f59799SBjoern A. Zeeb {
428d9f59799SBjoern A. Zeeb 
4299d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4309d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
431d9f59799SBjoern A. Zeeb 		return;
432d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
433d9f59799SBjoern A. Zeeb 		return;
434d9f59799SBjoern A. Zeeb 
435d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
43667674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
43767674c1cSBjoern A. Zeeb 	if (ni != NULL)
43867674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
439d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
440d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
441d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
4429d9ba2b7SBjoern A. Zeeb #endif
443d9f59799SBjoern A. Zeeb }
444d9f59799SBjoern A. Zeeb 
445d9f59799SBjoern A. Zeeb static void
446d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
447d9f59799SBjoern A. Zeeb {
448d9f59799SBjoern A. Zeeb 
449d9f59799SBjoern A. Zeeb 
450d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
4510936c648SBjoern A. Zeeb 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
4520936c648SBjoern A. Zeeb 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
4530936c648SBjoern A. Zeeb 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
454d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
455d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
456d9f59799SBjoern A. Zeeb }
457d9f59799SBjoern A. Zeeb 
4584f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
4594f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
4604f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
4614f61ef8bSBjoern A. Zeeb {
4624f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
4634f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
4644f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
4654f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
466b6b352e4SBjoern A. Zeeb 	int band, i, tid;
4679fb91463SBjoern A. Zeeb 	int ht_rx_nss;
4689fb91463SBjoern A. Zeeb 	int vht_rx_nss;
4694f61ef8bSBjoern A. Zeeb 
4704f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
4714f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
4724f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
4734f61ef8bSBjoern A. Zeeb 		return (NULL);
4744f61ef8bSBjoern A. Zeeb 
4754f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
4764f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
4774f61ef8bSBjoern A. Zeeb 	/*
4780936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
4790936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
4800936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
4810936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
4820936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
4830936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
4840936c648SBjoern A. Zeeb 	 * two separate allocations.
4854f61ef8bSBjoern A. Zeeb 	 */
4860936c648SBjoern A. Zeeb 	lsta->ni = ni;
4874f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
4884f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
4894f61ef8bSBjoern A. Zeeb 
4904f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4914f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4924f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4934f61ef8bSBjoern A. Zeeb 
4944f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
495b6b352e4SBjoern A. Zeeb 
496b6b352e4SBjoern A. Zeeb 	/* TXQ */
4974f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
4984f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
4994f61ef8bSBjoern A. Zeeb 
500d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
5014f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
5024f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
5034f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
5044f61ef8bSBjoern A. Zeeb 			goto cleanup;
5054f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
5064f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
507d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
508d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
509d0d29110SBjoern A. Zeeb 				continue;
510d0d29110SBjoern A. Zeeb 			}
511d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
5124f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
5134f61ef8bSBjoern A. Zeeb 		} else {
514fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
5154f61ef8bSBjoern A. Zeeb 		}
516d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
5170cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
518d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
5194f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
5204f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
5210cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
522d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
523eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
5244f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
5254f61ef8bSBjoern A. Zeeb 	}
5264f61ef8bSBjoern A. Zeeb 
527b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
528b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
529b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
530b6b352e4SBjoern A. Zeeb 
531b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
532b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
533b6b352e4SBjoern A. Zeeb 			continue;
534b6b352e4SBjoern A. Zeeb 
535b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
536b6b352e4SBjoern A. Zeeb 
537b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
538b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
539b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
540b6b352e4SBjoern A. Zeeb 		}
541b6b352e4SBjoern A. Zeeb 	}
5429fb91463SBjoern A. Zeeb 
5436ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
5449fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
545f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
5469fb91463SBjoern A. Zeeb 
5479fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
5489fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
5499fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
5509fb91463SBjoern A. Zeeb #endif
5519fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
5529fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5539fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
5549fb91463SBjoern A. Zeeb #endif
5559fb91463SBjoern A. Zeeb 
5569fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
5579fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
5589fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
559b6b352e4SBjoern A. Zeeb 
5606ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
5616ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
5626ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
5636ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
5646ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
5656ffb7bd4SBjoern A. Zeeb 	}
5666ffb7bd4SBjoern A. Zeeb 
5674f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
568fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
5694f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
5704f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
5710936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
5724f61ef8bSBjoern A. Zeeb 
5734f61ef8bSBjoern A. Zeeb 	return (lsta);
5744f61ef8bSBjoern A. Zeeb 
5754f61ef8bSBjoern A. Zeeb cleanup:
576eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
577eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
578eac3646fSBjoern A. Zeeb 
579eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
580eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
5814f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
582eac3646fSBjoern A. Zeeb 	}
5834f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
5844f61ef8bSBjoern A. Zeeb 	return (NULL);
5854f61ef8bSBjoern A. Zeeb }
5864f61ef8bSBjoern A. Zeeb 
5870936c648SBjoern A. Zeeb static void
5880936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
5890936c648SBjoern A. Zeeb {
5900936c648SBjoern A. Zeeb 	struct mbuf *m;
5910936c648SBjoern A. Zeeb 
5920936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
5930936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
5940936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
5950936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
5960936c648SBjoern A. Zeeb 
5970936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
5980936c648SBjoern A. Zeeb 	IMPROVE();
5990936c648SBjoern A. Zeeb 
600fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
601fa4e4257SBjoern A. Zeeb 
602fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
6030936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
604fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
6050936c648SBjoern A. Zeeb 
6060936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
6070936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
6080936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
6090936c648SBjoern A. Zeeb 
6100936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
6110936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
6120936c648SBjoern A. Zeeb 	while (m != NULL) {
6130936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
6140936c648SBjoern A. Zeeb 
6150936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
6160936c648SBjoern A. Zeeb 		if (nim != NULL)
6170936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
6180936c648SBjoern A. Zeeb 		m_freem(m);
6190936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
6200936c648SBjoern A. Zeeb 	}
6210936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
6220936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
623fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
6240936c648SBjoern A. Zeeb 
6250936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
6260936c648SBjoern A. Zeeb 
6270936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
6280936c648SBjoern A. Zeeb 
6290936c648SBjoern A. Zeeb 	/* Free lsta. */
6300936c648SBjoern A. Zeeb 	lsta->ni = NULL;
6310936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
6320936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6330936c648SBjoern A. Zeeb }
6340936c648SBjoern A. Zeeb 
6350936c648SBjoern A. Zeeb 
6366b4cac81SBjoern A. Zeeb static enum nl80211_band
6376b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
6386b4cac81SBjoern A. Zeeb {
6396b4cac81SBjoern A. Zeeb 
6406b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
6416b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
6426b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
6436b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
6446b4cac81SBjoern A. Zeeb #ifdef __notyet__
6456b4cac81SBjoern A. Zeeb 	else if ()
6466b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
6476b4cac81SBjoern A. Zeeb 	else if ()
6486b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
6496b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
6506b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
6516b4cac81SBjoern A. Zeeb #endif
6526b4cac81SBjoern A. Zeeb 	else
6536b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
6546b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
6556b4cac81SBjoern A. Zeeb }
6566b4cac81SBjoern A. Zeeb 
6576b4cac81SBjoern A. Zeeb static uint32_t
6586b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
6596b4cac81SBjoern A. Zeeb {
6606b4cac81SBjoern A. Zeeb 
6616b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
6626b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
6636b4cac81SBjoern A. Zeeb 	switch (band) {
6646b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
6656b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
6666b4cac81SBjoern A. Zeeb 		break;
6676b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
6686b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
6696b4cac81SBjoern A. Zeeb 		break;
6706b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
6716b4cac81SBjoern A. Zeeb 		break;
6726b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
6736b4cac81SBjoern A. Zeeb 		break;
6746b4cac81SBjoern A. Zeeb 	default:
6756b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
6766b4cac81SBjoern A. Zeeb 		break;
6776b4cac81SBjoern A. Zeeb 	}
6786b4cac81SBjoern A. Zeeb 
6796b4cac81SBjoern A. Zeeb 	IMPROVE();
6806b4cac81SBjoern A. Zeeb 	return (0x00);
6816b4cac81SBjoern A. Zeeb }
6826b4cac81SBjoern A. Zeeb 
683e3a0b120SBjoern A. Zeeb #if 0
6846b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
6856b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
6866b4cac81SBjoern A. Zeeb {
6876b4cac81SBjoern A. Zeeb 
6886b4cac81SBjoern A. Zeeb 	switch (ac) {
6896b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
6906b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
6916b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
6926b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
6936b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
6946b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
6956b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
6966b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
6976b4cac81SBjoern A. Zeeb 	default:
6986b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
6996b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7006b4cac81SBjoern A. Zeeb 	}
7016b4cac81SBjoern A. Zeeb }
702e3a0b120SBjoern A. Zeeb #endif
7036b4cac81SBjoern A. Zeeb 
7046b4cac81SBjoern A. Zeeb static enum nl80211_iftype
7056b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
7066b4cac81SBjoern A. Zeeb {
7076b4cac81SBjoern A. Zeeb 
7086b4cac81SBjoern A. Zeeb 	switch (opmode) {
7096b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
7106b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
7116b4cac81SBjoern A. Zeeb 		break;
7126b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
7136b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
7146b4cac81SBjoern A. Zeeb 		break;
7156b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
7166b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
7176b4cac81SBjoern A. Zeeb 		break;
7186b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
7196b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
7206b4cac81SBjoern A. Zeeb 		break;
7216b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
7226b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
7236b4cac81SBjoern A. Zeeb 		break;
7246b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
7256b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
7266b4cac81SBjoern A. Zeeb 		break;
7276b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
7286b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7296b4cac81SBjoern A. Zeeb 	default:
7306b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
7316b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7326b4cac81SBjoern A. Zeeb 	}
7336b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
7346b4cac81SBjoern A. Zeeb }
7356b4cac81SBjoern A. Zeeb 
736b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
7376b4cac81SBjoern A. Zeeb static uint32_t
7386b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
7396b4cac81SBjoern A. Zeeb {
7406b4cac81SBjoern A. Zeeb 
7416b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
7426b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
7436b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
7446b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7456b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
7466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
747906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
7486b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
7496b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
7506b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
7516b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
7526b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
7536b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
7546b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
7556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
7566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
7576b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
7586b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
7596b4cac81SBjoern A. Zeeb 		break;
7606b4cac81SBjoern A. Zeeb 	default:
7616b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
7626b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
7636b4cac81SBjoern A. Zeeb 	}
7646b4cac81SBjoern A. Zeeb 
7656b4cac81SBjoern A. Zeeb 	return (0);
7666b4cac81SBjoern A. Zeeb }
7676b4cac81SBjoern A. Zeeb 
7686b4cac81SBjoern A. Zeeb static uint32_t
7696b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
7706b4cac81SBjoern A. Zeeb {
7716b4cac81SBjoern A. Zeeb 
7726b4cac81SBjoern A. Zeeb 	switch (cipher) {
7736b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
7746b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
7756b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
7766b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
7776b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
7786b4cac81SBjoern A. Zeeb 		if (keylen < 8)
7796b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
7806b4cac81SBjoern A. Zeeb 		else
7816b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
7826b4cac81SBjoern A. Zeeb 		break;
7836b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
7846b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
7856b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
7866b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
7876b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
7886b4cac81SBjoern A. Zeeb 		break;
7896b4cac81SBjoern A. Zeeb 	default:
7906b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
7916b4cac81SBjoern A. Zeeb 	};
7926b4cac81SBjoern A. Zeeb 	return (0);
7936b4cac81SBjoern A. Zeeb }
7946b4cac81SBjoern A. Zeeb #endif
7956b4cac81SBjoern A. Zeeb 
7966b4cac81SBjoern A. Zeeb #ifdef __notyet__
7976b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
7986b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
7996b4cac81SBjoern A. Zeeb {
8006b4cac81SBjoern A. Zeeb 
8016b4cac81SBjoern A. Zeeb 	/*
8026b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
8036b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
8046b4cac81SBjoern A. Zeeb 	 */
8056b4cac81SBjoern A. Zeeb 	switch (state) {
8066b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
8076b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
8086b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
8096b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
8106b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
8116b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
8126b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
8136b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
8146b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
8156b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
8166b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
8176b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
8186b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
8196b4cac81SBjoern A. Zeeb 	default:
8206b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
8216b4cac81SBjoern A. Zeeb 	};
8226b4cac81SBjoern A. Zeeb 
8236b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
8246b4cac81SBjoern A. Zeeb }
8256b4cac81SBjoern A. Zeeb #endif
8266b4cac81SBjoern A. Zeeb 
8276b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
8286b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
8296b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
8306b4cac81SBjoern A. Zeeb {
8316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8326b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
8336b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
8346b4cac81SBjoern A. Zeeb 	int i, nchans;
8356b4cac81SBjoern A. Zeeb 
8366b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8376b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
8386b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
8396b4cac81SBjoern A. Zeeb 		return (NULL);
8406b4cac81SBjoern A. Zeeb 
8416b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
8426b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
8436b4cac81SBjoern A. Zeeb 		return (NULL);
8446b4cac81SBjoern A. Zeeb 
8456b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
8466b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
8476b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
8486b4cac81SBjoern A. Zeeb 			return (&channels[i]);
8496b4cac81SBjoern A. Zeeb 	}
8506b4cac81SBjoern A. Zeeb 
8516b4cac81SBjoern A. Zeeb 	return (NULL);
8526b4cac81SBjoern A. Zeeb }
8536b4cac81SBjoern A. Zeeb 
8542ac8a218SBjoern A. Zeeb #if 0
8556b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
8566b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
8576b4cac81SBjoern A. Zeeb {
8586b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
8596b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
8606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8616b4cac81SBjoern A. Zeeb 
8626b4cac81SBjoern A. Zeeb 	chan = NULL;
8636b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
8646b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
8656b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
8666b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
8676b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
8686b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
8696b4cac81SBjoern A. Zeeb 	else
8706b4cac81SBjoern A. Zeeb 		c = NULL;
8716b4cac81SBjoern A. Zeeb 
8726b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
8736b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
8746b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
8756b4cac81SBjoern A. Zeeb 	}
8766b4cac81SBjoern A. Zeeb 
8776b4cac81SBjoern A. Zeeb 	return (chan);
8786b4cac81SBjoern A. Zeeb }
8792ac8a218SBjoern A. Zeeb #endif
8806b4cac81SBjoern A. Zeeb 
8812e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
8822e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
8832e183d99SBjoern A. Zeeb {
8842e183d99SBjoern A. Zeeb 	enum nl80211_band band;
8852e183d99SBjoern A. Zeeb 
8862e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
8872e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
8882e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
8892e183d99SBjoern A. Zeeb 		int i;
8902e183d99SBjoern A. Zeeb 
8912e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
8922e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
8932e183d99SBjoern A. Zeeb 			continue;
8942e183d99SBjoern A. Zeeb 
8952e183d99SBjoern A. Zeeb 		channels = supband->channels;
8962e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
8972e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
8982e183d99SBjoern A. Zeeb 				return (&channels[i]);
8992e183d99SBjoern A. Zeeb 		}
9002e183d99SBjoern A. Zeeb 	}
9012e183d99SBjoern A. Zeeb 
9022e183d99SBjoern A. Zeeb 	return (NULL);
9032e183d99SBjoern A. Zeeb }
9042e183d99SBjoern A. Zeeb 
905b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
9066b4cac81SBjoern A. Zeeb static int
9076b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
9086b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
9096b4cac81SBjoern A. Zeeb {
9106b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
9116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9126b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
9146b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
9156b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
9166b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
9176b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
9186b4cac81SBjoern A. Zeeb 	int error;
9196b4cac81SBjoern A. Zeeb 
9206b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
9216b4cac81SBjoern A. Zeeb 
9226b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
9236b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
9246b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9256b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
9266b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
9276b4cac81SBjoern A. Zeeb 
9286b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
9296b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
9306b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
9316b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
9326b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
9336b4cac81SBjoern A. Zeeb #if 0
9346b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
9356b4cac81SBjoern A. Zeeb #endif
9366b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
9376b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
9386b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
9396b4cac81SBjoern A. Zeeb 
9406b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
9416b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
9426b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
9436b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
9446b4cac81SBjoern A. Zeeb 		break;
9456b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
9466b4cac81SBjoern A. Zeeb 	default:
9476b4cac81SBjoern A. Zeeb 		IMPROVE();
9486b4cac81SBjoern A. Zeeb 		return (0);
9496b4cac81SBjoern A. Zeeb 	};
9506b4cac81SBjoern A. Zeeb 
9516b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
9526b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
9536b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
9546b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
9556b4cac81SBjoern A. Zeeb 
9566b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
9576b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
9586b4cac81SBjoern A. Zeeb 	}
9596b4cac81SBjoern A. Zeeb 
9606b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
9616b4cac81SBjoern A. Zeeb 	if (error != 0) {
9626b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
9636b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
9646b4cac81SBjoern A. Zeeb 		return (0);
9656b4cac81SBjoern A. Zeeb 	} else {
9666b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
9676b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
9686b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
9696b4cac81SBjoern A. Zeeb 		return (1);
9706b4cac81SBjoern A. Zeeb 	}
9716b4cac81SBjoern A. Zeeb }
9726b4cac81SBjoern A. Zeeb 
9736b4cac81SBjoern A. Zeeb static int
9746b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
9756b4cac81SBjoern A. Zeeb {
9766b4cac81SBjoern A. Zeeb 
9776b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
9786b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
9796b4cac81SBjoern A. Zeeb }
9806b4cac81SBjoern A. Zeeb static  int
9816b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
9826b4cac81SBjoern A. Zeeb {
9836b4cac81SBjoern A. Zeeb 
9846b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
9856b4cac81SBjoern A. Zeeb }
9866b4cac81SBjoern A. Zeeb #endif
9876b4cac81SBjoern A. Zeeb 
9886b4cac81SBjoern A. Zeeb static u_int
9896b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
9906b4cac81SBjoern A. Zeeb {
9916b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
9926b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
9936b4cac81SBjoern A. Zeeb 
9946b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
9956b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
9966b4cac81SBjoern A. Zeeb 
9976b4cac81SBjoern A. Zeeb 	mc_list = arg;
9986b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
9996b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
10006b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
10016b4cac81SBjoern A. Zeeb 			return (0);
10026b4cac81SBjoern A. Zeeb 	}
10036b4cac81SBjoern A. Zeeb 
10046b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
10056b4cac81SBjoern A. Zeeb 	if (addr == NULL)
10066b4cac81SBjoern A. Zeeb 		return (0);
10076b4cac81SBjoern A. Zeeb 
10086b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
10096b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
10106b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
10116b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
10126b4cac81SBjoern A. Zeeb 	mc_list->count++;
10136b4cac81SBjoern A. Zeeb 
10149d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
10159d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
10166b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
10176b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
10189d9ba2b7SBjoern A. Zeeb #endif
10196b4cac81SBjoern A. Zeeb 
10206b4cac81SBjoern A. Zeeb 	return (1);
10216b4cac81SBjoern A. Zeeb }
10226b4cac81SBjoern A. Zeeb 
10236b4cac81SBjoern A. Zeeb static void
10246b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
10256b4cac81SBjoern A. Zeeb {
10266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10276b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10286b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
10296b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
10306b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
10316b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
10326b4cac81SBjoern A. Zeeb 	u64 mc;
10336b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
10346b4cac81SBjoern A. Zeeb 
10356b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
10366b4cac81SBjoern A. Zeeb 
10376b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
10386b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
10396b4cac81SBjoern A. Zeeb 		return;
10406b4cac81SBjoern A. Zeeb 
10416b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
10426b4cac81SBjoern A. Zeeb 		return;
10436b4cac81SBjoern A. Zeeb 
10446b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
10456b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
10466b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
10476b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
10486b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
10496b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
10506b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
10516b4cac81SBjoern A. Zeeb 	} else {
10526b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
10536b4cac81SBjoern A. Zeeb 	}
10546b4cac81SBjoern A. Zeeb 
10556b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10566b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
10576b4cac81SBjoern A. Zeeb 	/*
10586b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
10596b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
10606b4cac81SBjoern A. Zeeb 	 */
10616b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
10626b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
10636b4cac81SBjoern A. Zeeb 
10649d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
10659d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
10666b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
10676b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
10689d9ba2b7SBjoern A. Zeeb #endif
10696b4cac81SBjoern A. Zeeb 
10706b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
10716b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
10726b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
10736b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
10746b4cac81SBjoern A. Zeeb 			mc_list.count--;
10756b4cac81SBjoern A. Zeeb 		}
10766b4cac81SBjoern A. Zeeb 	}
10776b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
10786b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
10796b4cac81SBjoern A. Zeeb }
10806b4cac81SBjoern A. Zeeb 
1081fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1082fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1083fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1084fa8f007dSBjoern A. Zeeb {
1085fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1086fa8f007dSBjoern A. Zeeb 
1087fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1088fa8f007dSBjoern A. Zeeb 
10899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
10909d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1091fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1092fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1093ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1094fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1095616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1096fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1097fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1098fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1099fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1100ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
11019d9ba2b7SBjoern A. Zeeb #endif
1102fa8f007dSBjoern A. Zeeb 
1103fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1104fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1105fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1106fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1107fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1108fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1109fa8f007dSBjoern A. Zeeb 	}
1110fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1111fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1112fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1113fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1114fa8f007dSBjoern A. Zeeb 	}
1115fa8f007dSBjoern A. Zeeb 
1116fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1117fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1118fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1119fa8f007dSBjoern A. Zeeb 
11209d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
11219d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1122fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1123fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1124ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1125fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1126616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1127fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1128fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1129fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1130fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1131ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
11329d9ba2b7SBjoern A. Zeeb #endif
1133fa8f007dSBjoern A. Zeeb 
1134fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1135fa8f007dSBjoern A. Zeeb }
1136fa8f007dSBjoern A. Zeeb 
11376b4cac81SBjoern A. Zeeb static void
11386b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
11396b4cac81SBjoern A. Zeeb {
11406b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11416b4cac81SBjoern A. Zeeb 	int error;
11428ac540d3SBjoern A. Zeeb 	bool cancel;
11436b4cac81SBjoern A. Zeeb 
11448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
11458ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
11468ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
11478ac540d3SBjoern A. Zeeb 	if (!cancel)
11486b4cac81SBjoern A. Zeeb 		return;
11496b4cac81SBjoern A. Zeeb 
11506b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11516b4cac81SBjoern A. Zeeb 
1152bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1153bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
11546b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
11556b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
11568ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
11576b4cac81SBjoern A. Zeeb 
11586b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
11598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
11608ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
11618ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
11628ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
11638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
11648ac540d3SBjoern A. Zeeb 
1165bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
11666b4cac81SBjoern A. Zeeb 
11678ac540d3SBjoern A. Zeeb 	if (cancel)
11686b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
11696b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
11706b4cac81SBjoern A. Zeeb }
11716b4cac81SBjoern A. Zeeb 
11726b4cac81SBjoern A. Zeeb static void
1173086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1174086be6a8SBjoern A. Zeeb {
1175086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1176086be6a8SBjoern A. Zeeb 	int error;
1177086be6a8SBjoern A. Zeeb 	bool old;
1178086be6a8SBjoern A. Zeeb 
1179086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1180086be6a8SBjoern A. Zeeb 	if (old == new)
1181086be6a8SBjoern A. Zeeb 		return;
1182086be6a8SBjoern A. Zeeb 
1183086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1184086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1185086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1186086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1187086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1188086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1189086be6a8SBjoern A. Zeeb 	}
1190086be6a8SBjoern A. Zeeb }
1191086be6a8SBjoern A. Zeeb 
11925a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
11936b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
11946b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
11956b4cac81SBjoern A. Zeeb {
11965a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
11975a4d2461SBjoern A. Zeeb 
11985a4d2461SBjoern A. Zeeb 	changed = 0;
11996b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1200616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
12016b4cac81SBjoern A. Zeeb 
12026b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
12036b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
12046b4cac81SBjoern A. Zeeb 
1205616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1206616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
12076b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
12086b4cac81SBjoern A. Zeeb 		IMPROVE();
1209086be6a8SBjoern A. Zeeb 
12105a4d2461SBjoern A. Zeeb 		/*
12115a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
12125a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
12135a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
12145a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
12155a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
12165a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
12175a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
12185a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
12195a4d2461SBjoern A. Zeeb 		 */
12206b4cac81SBjoern A. Zeeb 	}
12215a4d2461SBjoern A. Zeeb 
12225a4d2461SBjoern A. Zeeb 	return (changed);
12236b4cac81SBjoern A. Zeeb }
12246b4cac81SBjoern A. Zeeb 
12256b4cac81SBjoern A. Zeeb static void
12266b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
12276b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
12286b4cac81SBjoern A. Zeeb {
12296b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
12306b4cac81SBjoern A. Zeeb 	int tid;
1231eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
12326b4cac81SBjoern A. Zeeb 
12336b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
12346b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
12356b4cac81SBjoern A. Zeeb 
12366b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
12376b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
12386b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
12396b4cac81SBjoern A. Zeeb 				continue;
12406b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
12416b4cac81SBjoern A. Zeeb 			continue;
12426b4cac81SBjoern A. Zeeb 
12436b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
12446b4cac81SBjoern A. Zeeb 			continue;
12456b4cac81SBjoern A. Zeeb 
12466b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
12476b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
12486b4cac81SBjoern A. Zeeb 			continue;
12496b4cac81SBjoern A. Zeeb 
1250eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1251eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1252eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1253eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
12546b4cac81SBjoern A. Zeeb 			continue;
12556b4cac81SBjoern A. Zeeb 
12566b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
12576b4cac81SBjoern A. Zeeb 	}
12586b4cac81SBjoern A. Zeeb }
12596b4cac81SBjoern A. Zeeb 
126088665349SBjoern A. Zeeb /*
126188665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
126288665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
126388665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
126488665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
126588665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
126688665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
126788665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
126888665349SBjoern A. Zeeb  * re-used.
126988665349SBjoern A. Zeeb  */
127088665349SBjoern A. Zeeb static void
127188665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
127288665349SBjoern A. Zeeb {
127388665349SBjoern A. Zeeb 	struct mbufq mq;
127488665349SBjoern A. Zeeb 	struct mbuf *m;
127588665349SBjoern A. Zeeb 	int len;
127688665349SBjoern A. Zeeb 
127788665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
127888665349SBjoern A. Zeeb 
127988665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
128088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
128188665349SBjoern A. Zeeb 	lsta->txq_ready = false;
128288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
128388665349SBjoern A. Zeeb 
128488665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
128588665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
128688665349SBjoern A. Zeeb 
128788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
128888665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
128988665349SBjoern A. Zeeb 	if (len <= 0) {
129088665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
129188665349SBjoern A. Zeeb 		return;
129288665349SBjoern A. Zeeb 	}
129388665349SBjoern A. Zeeb 
129488665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
129588665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
129688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
129788665349SBjoern A. Zeeb 
129888665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
129988665349SBjoern A. Zeeb 	while (m != NULL) {
130088665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
130188665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
130288665349SBjoern A. Zeeb 	}
130388665349SBjoern A. Zeeb }
130488665349SBjoern A. Zeeb 
13056b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
13066b4cac81SBjoern A. Zeeb 
13076b4cac81SBjoern A. Zeeb static int
13086b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13096b4cac81SBjoern A. Zeeb {
13106b4cac81SBjoern A. Zeeb 
13116b4cac81SBjoern A. Zeeb 	return (0);
13126b4cac81SBjoern A. Zeeb }
13136b4cac81SBjoern A. Zeeb 
13146b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
13156b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
13166b4cac81SBjoern A. Zeeb 
13176b4cac81SBjoern A. Zeeb static int
13186b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13196b4cac81SBjoern A. Zeeb {
13206b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1321c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1322d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
13236b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13246b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13256b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13266b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13276b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13286b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13296b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
13306b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
13316b4cac81SBjoern A. Zeeb 	uint32_t changed;
13326b4cac81SBjoern A. Zeeb 	int error;
13336b4cac81SBjoern A. Zeeb 
13342ac8a218SBjoern A. Zeeb 	/*
13352ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
13362ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
13372ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
13382ac8a218SBjoern A. Zeeb 	 */
13392ac8a218SBjoern A. Zeeb 
13402ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
13412ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
13422ac8a218SBjoern A. Zeeb 		return (EINVAL);
13432ac8a218SBjoern A. Zeeb 	}
13440936c648SBjoern A. Zeeb 	/*
13450936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
13460936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
13470936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
13480936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
13490936c648SBjoern A. Zeeb 	 */
13502ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
13512ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
13522ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
13532ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
13540936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
13552ac8a218SBjoern A. Zeeb 		return (EINVAL);
13566b4cac81SBjoern A. Zeeb 	}
13576b4cac81SBjoern A. Zeeb 
13586b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13592ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
13602ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
13612ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
13622ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
13630936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
13642ac8a218SBjoern A. Zeeb 		return (ESRCH);
13652ac8a218SBjoern A. Zeeb 	}
13662ac8a218SBjoern A. Zeeb 
13676b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13686b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13696b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13706b4cac81SBjoern A. Zeeb 
13710936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
13720936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
13730936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
13740936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
13750936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
13760936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
13770936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
13780936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
137992690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
138092690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
13810936c648SBjoern A. Zeeb 		return (EBUSY);
13820936c648SBjoern A. Zeeb 	}
13830936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
13840936c648SBjoern A. Zeeb 
13856b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
13868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
13876b4cac81SBjoern A. Zeeb 
13886b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
13896b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1390d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1391d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
13926b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
13936b4cac81SBjoern A. Zeeb 	} else {
13946b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1395c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
13966b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1397d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
13986b4cac81SBjoern A. Zeeb 	}
13996b4cac81SBjoern A. Zeeb 
1400d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1401d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1402d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
14036b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1404d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1405d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1406d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1407d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
14089fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
14092ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
14102ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
14119fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
14129fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
14139fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1414d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
14159fb91463SBjoern A. Zeeb 		} else
1416d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
14179fb91463SBjoern A. Zeeb 	}
14189fb91463SBjoern A. Zeeb #endif
14199fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
14209fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
14219fb91463SBjoern A. Zeeb #ifdef __notyet__
14229fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1423d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1424d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
14259fb91463SBjoern A. Zeeb 		} else
14269fb91463SBjoern A. Zeeb #endif
14279fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1428d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
14299fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1430d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
14319fb91463SBjoern A. Zeeb 	}
14329fb91463SBjoern A. Zeeb #endif
14336b4cac81SBjoern A. Zeeb 	/* Responder ... */
1434d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1435d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1436d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1437d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
14389fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
14396b4cac81SBjoern A. Zeeb 
14406ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
14416ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
14426ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
14436ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
14446ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
14456ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
14466ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
14476ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
14486ffb7bd4SBjoern A. Zeeb 
14496ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
14506ffb7bd4SBjoern A. Zeeb 
14516ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
14526ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
14536ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
14546ffb7bd4SBjoern A. Zeeb 
14556ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
14566ffb7bd4SBjoern A. Zeeb 
14576b4cac81SBjoern A. Zeeb 	error = 0;
14586b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
14596b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
14606b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
14616b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
14626b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1463d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
14646b4cac81SBjoern A. Zeeb 	} else {
1465d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
14666b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
14677b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
14687b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
14697b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1470d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
14719fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
1472943a19c6SBjoern A. Zeeb 			if (vif->bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_40) {
14739fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
14749fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1475943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 += 10;
14769fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1477943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 -= 10;
14789fb91463SBjoern A. Zeeb 			}
14799fb91463SBjoern A. Zeeb #endif
14807b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1481d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
14826b4cac81SBjoern A. Zeeb 		} else {
1483018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1484018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
14856b4cac81SBjoern A. Zeeb 			goto out;
14866b4cac81SBjoern A. Zeeb 		}
14876ffb7bd4SBjoern A. Zeeb 
1488d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
14896ffb7bd4SBjoern A. Zeeb 
14906b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
14916b4cac81SBjoern A. Zeeb 		if (error == 0)
149268541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1493d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
14946b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
14956b4cac81SBjoern A. Zeeb 			error = 0;
14966b4cac81SBjoern A. Zeeb 		if (error != 0) {
1497018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1498018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1499d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1500d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1501c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
15026b4cac81SBjoern A. Zeeb 			goto out;
15036b4cac81SBjoern A. Zeeb 		}
15046b4cac81SBjoern A. Zeeb 	}
15056b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
15066b4cac81SBjoern A. Zeeb 
15076b4cac81SBjoern A. Zeeb 	/* RATES */
15086b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
15096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
15106b4cac81SBjoern A. Zeeb 
1511d9f59799SBjoern A. Zeeb 	/*
15120936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
15130936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
15140936c648SBjoern A. Zeeb 	 * under the hoods.
1515d9f59799SBjoern A. Zeeb 	 */
15160936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
15170936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
15186b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1519e24e8103SBjoern A. Zeeb 
152088665349SBjoern A. Zeeb 	/*
152188665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
152288665349SBjoern A. Zeeb 	 * that we can tx again.
152388665349SBjoern A. Zeeb 	 */
152488665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
152588665349SBjoern A. Zeeb 	lsta->txq_ready = true;
152688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
152788665349SBjoern A. Zeeb 
1528e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15292ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1530e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1531e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1532e24e8103SBjoern A. Zeeb 
1533d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
15346b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15356b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
15366b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1537e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
15386b4cac81SBjoern A. Zeeb 	if (error != 0) {
15396b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1540018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1541018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
15426b4cac81SBjoern A. Zeeb 		goto out;
15436b4cac81SBjoern A. Zeeb 	}
15446b4cac81SBjoern A. Zeeb #if 0
15456b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
15466b4cac81SBjoern A. Zeeb #endif
15476b4cac81SBjoern A. Zeeb 
15489d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
15499d9ba2b7SBjoern A. Zeeb 
15501665ef97SBjoern A. Zeeb #if 0
15516b4cac81SBjoern A. Zeeb 	/*
15526b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
15536b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
15546b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
15556b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1556d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1557d9f59799SBjoern A. Zeeb 	 * for all queues.
15586b4cac81SBjoern A. Zeeb 	 */
1559e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
15601665ef97SBjoern A. Zeeb #endif
15616b4cac81SBjoern A. Zeeb 
15626b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
15636b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15646b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
15656b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
15666b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
15676b4cac81SBjoern A. Zeeb 
15686b4cac81SBjoern A. Zeeb 	/*
15696b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
15706b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
15716b4cac81SBjoern A. Zeeb 	 * - event_callback
15726b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
15736b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
15746b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
15756b4cac81SBjoern A. Zeeb 	 */
15766b4cac81SBjoern A. Zeeb 
1577105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1578105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1579105b9df2SBjoern A. Zeeb 
1580105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1581105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1582105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1583105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1584105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1585105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1586105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1587105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1588105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1589105b9df2SBjoern A. Zeeb 
1590105b9df2SBjoern A. Zeeb 	/*
1591105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1592105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1593105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1594105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1595105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1596105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1597105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1598105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1599105b9df2SBjoern A. Zeeb 	 */
1600105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1601105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1602105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1603105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1604105b9df2SBjoern A. Zeeb 	} else {
1605105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1606105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1607105b9df2SBjoern A. Zeeb 		/*
1608105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1609105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1610105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1611105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1612105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1613105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1614105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1615105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1616105b9df2SBjoern A. Zeeb 		 */
1617105b9df2SBjoern A. Zeeb 	}
1618105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1619105b9df2SBjoern A. Zeeb 	goto out_relocked;
1620105b9df2SBjoern A. Zeeb 
16216b4cac81SBjoern A. Zeeb out:
16228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16236b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1624105b9df2SBjoern A. Zeeb out_relocked:
16250936c648SBjoern A. Zeeb 	/*
1626105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
16270936c648SBjoern A. Zeeb 	 * during the work of this function.
16280936c648SBjoern A. Zeeb 	 */
16296b4cac81SBjoern A. Zeeb 	if (ni != NULL)
16306b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
16316b4cac81SBjoern A. Zeeb 	return (error);
16326b4cac81SBjoern A. Zeeb }
16336b4cac81SBjoern A. Zeeb 
16346b4cac81SBjoern A. Zeeb static int
16356b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16366b4cac81SBjoern A. Zeeb {
16376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16406b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16416b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16426b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16436b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
16446b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16456b4cac81SBjoern A. Zeeb 	int error;
16466b4cac81SBjoern A. Zeeb 
16476b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16486b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16496b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16506b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16516b4cac81SBjoern A. Zeeb 
16522ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16532ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16542ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
16552ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
16562ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16572ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16582ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16592ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16602ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16612ac8a218SBjoern A. Zeeb #endif
16622ac8a218SBjoern A. Zeeb 
16632ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16642ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16652ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
16662ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
16672ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
16682ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
16696b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
16706b4cac81SBjoern A. Zeeb 
167167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
16726b4cac81SBjoern A. Zeeb 
16736b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16748ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16756b4cac81SBjoern A. Zeeb 
1676d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1677d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1678d9f59799SBjoern A. Zeeb 
16796b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
168088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
16816b4cac81SBjoern A. Zeeb 
16826b4cac81SBjoern A. Zeeb 	/* flush, no drop */
16836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
16846b4cac81SBjoern A. Zeeb 
16856b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16866b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16876b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16886b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
16896b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16906b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16916b4cac81SBjoern A. Zeeb 	}
16926b4cac81SBjoern A. Zeeb 
16936b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
16946b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
16956b4cac81SBjoern A. Zeeb 
16966b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
16976b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1698d9f59799SBjoern A. Zeeb 
1699d9f59799SBjoern A. Zeeb 	/* Take the station down. */
17006b4cac81SBjoern A. Zeeb 
17016b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
17026b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
17036b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1704d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1705e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
17066b4cac81SBjoern A. Zeeb 	if (error != 0) {
17076b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1708018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1709018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
17106b4cac81SBjoern A. Zeeb 		goto out;
17116b4cac81SBjoern A. Zeeb 	}
17126b4cac81SBjoern A. Zeeb #if 0
17136b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
17146b4cac81SBjoern A. Zeeb #endif
17156b4cac81SBjoern A. Zeeb 
171667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
17176b4cac81SBjoern A. Zeeb 
17182ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17192ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
17202ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
17212ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
17222ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1723d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
17240936c648SBjoern A. Zeeb 	/*
17250936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
17260936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
17270936c648SBjoern A. Zeeb 	 * and potentially freed.
17280936c648SBjoern A. Zeeb 	 */
17290936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1730d9f59799SBjoern A. Zeeb 
1731d9f59799SBjoern A. Zeeb 	/* conf_tx */
1732d9f59799SBjoern A. Zeeb 
1733d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
17346b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1735c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1736d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
17376b4cac81SBjoern A. Zeeb 
1738d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
17396b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
174068541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
17416b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
17426b4cac81SBjoern A. Zeeb 
17435a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
17445a4d2461SBjoern A. Zeeb 
17456b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
1746d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1747d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1748c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
17496b4cac81SBjoern A. Zeeb 	}
17506b4cac81SBjoern A. Zeeb 
17516b4cac81SBjoern A. Zeeb out:
17528ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17536b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
17546b4cac81SBjoern A. Zeeb 	return (error);
17556b4cac81SBjoern A. Zeeb }
17566b4cac81SBjoern A. Zeeb 
17576b4cac81SBjoern A. Zeeb static int
17586b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17596b4cac81SBjoern A. Zeeb {
17606b4cac81SBjoern A. Zeeb 	int error;
17616b4cac81SBjoern A. Zeeb 
17626b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
17636b4cac81SBjoern A. Zeeb 	if (error == 0)
17646b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
17656b4cac81SBjoern A. Zeeb 	return (error);
17666b4cac81SBjoern A. Zeeb }
17676b4cac81SBjoern A. Zeeb 
17686b4cac81SBjoern A. Zeeb static int
17696b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17706b4cac81SBjoern A. Zeeb {
17716b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17726b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17736b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17746b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17756b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
17766b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
17776b4cac81SBjoern A. Zeeb 	int error;
17786b4cac81SBjoern A. Zeeb 
17796b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17806b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17816b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17826b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17836b4cac81SBjoern A. Zeeb 
17846b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17862ac8a218SBjoern A. Zeeb 
17872ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17882ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
17892ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
17902ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17912ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17922ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17932ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17942ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17952ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
17962ac8a218SBjoern A. Zeeb #endif
17972ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
17982ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
17992ac8a218SBjoern A. Zeeb 		goto out;
18002ac8a218SBjoern A. Zeeb 	}
18012ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
18022ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
18032ac8a218SBjoern A. Zeeb 
18042ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
18056b4cac81SBjoern A. Zeeb 
18066b4cac81SBjoern A. Zeeb 	/* Finish auth. */
18076b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
18086b4cac81SBjoern A. Zeeb 
18096b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
18106b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
18116b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1812e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1813018d93ecSBjoern A. Zeeb 	if (error != 0) {
1814018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1815018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18166b4cac81SBjoern A. Zeeb 		goto out;
1817018d93ecSBjoern A. Zeeb 	}
18186b4cac81SBjoern A. Zeeb 
18196b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
18206b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
18216b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18226b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
18236b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
18246b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
18256b4cac81SBjoern A. Zeeb 	}
18266b4cac81SBjoern A. Zeeb 
18276b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
18286b4cac81SBjoern A. Zeeb 
18296b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
18306b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
18316b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18326b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
18336b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
18346b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
18356b4cac81SBjoern A. Zeeb 	}
18366b4cac81SBjoern A. Zeeb 
18376b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
183888665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
18396b4cac81SBjoern A. Zeeb 
18406b4cac81SBjoern A. Zeeb 	/*
18416b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
18426b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
18436b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
18446b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
18456b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
18466b4cac81SBjoern A. Zeeb 	 * - event_callback
18476b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
18486b4cac81SBjoern A. Zeeb 	 */
18496b4cac81SBjoern A. Zeeb 
18506b4cac81SBjoern A. Zeeb out:
18518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
18526b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
18536b4cac81SBjoern A. Zeeb 	return (error);
18546b4cac81SBjoern A. Zeeb }
18556b4cac81SBjoern A. Zeeb 
18566b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
18576b4cac81SBjoern A. Zeeb static int
18586b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18596b4cac81SBjoern A. Zeeb {
18606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18616b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18626b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18646b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
18656b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
18662ac8a218SBjoern A. Zeeb 	int error;
18676b4cac81SBjoern A. Zeeb 
18686b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
18696b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18706b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18716b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
18726b4cac81SBjoern A. Zeeb 
18736b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
18748ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
18752ac8a218SBjoern A. Zeeb 
18762ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18772ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
18782ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
18792ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
18802ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
18812ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
18822ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
18832ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
18842ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
18852ac8a218SBjoern A. Zeeb #endif
18862ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
18872ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
18882ac8a218SBjoern A. Zeeb 		goto out;
18892ac8a218SBjoern A. Zeeb 	}
18902ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
18912ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
18922ac8a218SBjoern A. Zeeb 
18932ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
18942ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
18956b4cac81SBjoern A. Zeeb 
18966b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
18976b4cac81SBjoern A. Zeeb 
18986b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
18996b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
19006b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19016b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
19026b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
19036b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
19046b4cac81SBjoern A. Zeeb 	}
19056b4cac81SBjoern A. Zeeb 
19066b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
19076b4cac81SBjoern A. Zeeb 
19086b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
19096b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
19106b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19116b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
19126b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
19136b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
19146b4cac81SBjoern A. Zeeb 	}
19156b4cac81SBjoern A. Zeeb 
19162ac8a218SBjoern A. Zeeb 	error = 0;
19172ac8a218SBjoern A. Zeeb out:
19188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19196b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
19206b4cac81SBjoern A. Zeeb 
19212ac8a218SBjoern A. Zeeb 	return (error);
19226b4cac81SBjoern A. Zeeb }
19236b4cac81SBjoern A. Zeeb 
19246b4cac81SBjoern A. Zeeb static int
1925d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19266b4cac81SBjoern A. Zeeb {
19276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19286b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19296b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19306b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19316b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19326b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19336b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19346b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1935d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
19366b4cac81SBjoern A. Zeeb 	int error;
19376b4cac81SBjoern A. Zeeb 
19386b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19396b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19406b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19416b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19426b4cac81SBjoern A. Zeeb 
19432ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
19442ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19452ac8a218SBjoern A. Zeeb 
19462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19472ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19482ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
19492ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
19502ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19512ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19522ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19532ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19542ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19552ac8a218SBjoern A. Zeeb #endif
19562ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19582ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19592ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19602ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19612ac8a218SBjoern A. Zeeb 
19622ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
19636b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19646b4cac81SBjoern A. Zeeb 
196567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1966d9f59799SBjoern A. Zeeb 
1967d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1968d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1969d9f59799SBjoern A. Zeeb 
1970d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1971d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1972d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1973d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1974d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1975ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
1976d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1977d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1978d9f59799SBjoern A. Zeeb 	}
1979d9f59799SBjoern A. Zeeb 
19808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1981d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1982d9f59799SBjoern A. Zeeb 
198388665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
1984d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1985018d93ecSBjoern A. Zeeb 	if (error != 0) {
1986018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1987018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1988d9f59799SBjoern A. Zeeb 		goto outni;
1989018d93ecSBjoern A. Zeeb 	}
1990d9f59799SBjoern A. Zeeb 
1991d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
199288665349SBjoern A. Zeeb 
199388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
199488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
199588665349SBjoern A. Zeeb 
19968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1997d9f59799SBjoern A. Zeeb 
199867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1999d9f59799SBjoern A. Zeeb 
2000d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
200188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2002d9f59799SBjoern A. Zeeb 
2003d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2004d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2005d9f59799SBjoern A. Zeeb 
20066b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20076b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20086b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20096b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2010ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
20116b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20126b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20136b4cac81SBjoern A. Zeeb 	}
20146b4cac81SBjoern A. Zeeb 
2015d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2016d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2017d9f59799SBjoern A. Zeeb 
2018d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2019d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2020d9f59799SBjoern A. Zeeb 
2021d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2022d9f59799SBjoern A. Zeeb 
20236b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
20246b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20256b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
20266b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2027e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2028018d93ecSBjoern A. Zeeb 	if (error != 0) {
2029018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2030018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20316b4cac81SBjoern A. Zeeb 		goto out;
2032018d93ecSBjoern A. Zeeb 	}
20336b4cac81SBjoern A. Zeeb 
20345a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
20355a4d2461SBjoern A. Zeeb 	bss_changed = 0;
20365a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
20376b4cac81SBjoern A. Zeeb 
20385a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
203916e688b2SBjoern A. Zeeb 
2040d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2041d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2042d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2043d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2044e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2045d9f59799SBjoern A. Zeeb 	if (error != 0) {
2046d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2047018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2048018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2049d9f59799SBjoern A. Zeeb 		goto out;
2050d9f59799SBjoern A. Zeeb 	}
2051d9f59799SBjoern A. Zeeb 
205216e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2053d9f59799SBjoern A. Zeeb 
2054d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2055d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2056d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2057616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2058616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2059d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2060d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2061d9f59799SBjoern A. Zeeb 
20622ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20632ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
20642ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
20652ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
20662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2067d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
20680936c648SBjoern A. Zeeb 	/*
20690936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
20700936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
20710936c648SBjoern A. Zeeb 	 * and potentially freed.
20720936c648SBjoern A. Zeeb 	 */
20730936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2074d9f59799SBjoern A. Zeeb 
2075d9f59799SBjoern A. Zeeb 	/* conf_tx */
2076d9f59799SBjoern A. Zeeb 
2077d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2078d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2079c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2080d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2081d9f59799SBjoern A. Zeeb 
2082d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2083d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
208468541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2085d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2086d9f59799SBjoern A. Zeeb 
20875a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
20885a4d2461SBjoern A. Zeeb 
2089d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2090d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2091d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2092c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2093d9f59799SBjoern A. Zeeb 	}
2094d9f59799SBjoern A. Zeeb 
2095d9f59799SBjoern A. Zeeb 	error = EALREADY;
20966b4cac81SBjoern A. Zeeb out:
20978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20986b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2099d9f59799SBjoern A. Zeeb outni:
21006b4cac81SBjoern A. Zeeb 	return (error);
21016b4cac81SBjoern A. Zeeb }
21026b4cac81SBjoern A. Zeeb 
21036b4cac81SBjoern A. Zeeb static int
2104d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2105d9f59799SBjoern A. Zeeb {
2106d9f59799SBjoern A. Zeeb 	int error;
2107d9f59799SBjoern A. Zeeb 
2108d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2109d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2110d9f59799SBjoern A. Zeeb 		return (error);
2111d9f59799SBjoern A. Zeeb 
2112d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2113d9f59799SBjoern A. Zeeb 
2114d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2115d9f59799SBjoern A. Zeeb 	return (error);
2116d9f59799SBjoern A. Zeeb }
2117d9f59799SBjoern A. Zeeb 
2118d9f59799SBjoern A. Zeeb static int
21196b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21206b4cac81SBjoern A. Zeeb {
21216b4cac81SBjoern A. Zeeb 	int error;
21226b4cac81SBjoern A. Zeeb 
2123d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
21246b4cac81SBjoern A. Zeeb 	return (error);
21256b4cac81SBjoern A. Zeeb }
21266b4cac81SBjoern A. Zeeb 
21276b4cac81SBjoern A. Zeeb static int
21286b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21296b4cac81SBjoern A. Zeeb {
21306b4cac81SBjoern A. Zeeb 	int error;
21316b4cac81SBjoern A. Zeeb 
2132d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
21336b4cac81SBjoern A. Zeeb 	return (error);
21346b4cac81SBjoern A. Zeeb }
21356b4cac81SBjoern A. Zeeb 
21366b4cac81SBjoern A. Zeeb static int
21376b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21386b4cac81SBjoern A. Zeeb {
21396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21406b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21416b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21426b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21436b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
21446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21456b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
21466b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21476b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
21486b4cac81SBjoern A. Zeeb 	int error;
21496b4cac81SBjoern A. Zeeb 
21506b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21516b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21526b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21536b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21546b4cac81SBjoern A. Zeeb 
21556b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21568ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21572ac8a218SBjoern A. Zeeb 
21582ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21592ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21602ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21612ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21622ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21632ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21642ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21652ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21662ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21672ac8a218SBjoern A. Zeeb #endif
21682ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21692ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21702ac8a218SBjoern A. Zeeb 		goto out;
21712ac8a218SBjoern A. Zeeb 	}
21722ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21732ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21742ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
21752ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
21762ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
21772ac8a218SBjoern A. Zeeb 
21782ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
21796b4cac81SBjoern A. Zeeb 
21806b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
21816b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
21826b4cac81SBjoern A. Zeeb 
21839597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
21849597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
21859597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
21869597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
21876b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
21889597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
21894a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
21904a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
21914a67f1dfSBjoern A. Zeeb 		sta->wme = true;
21924a67f1dfSBjoern A. Zeeb #endif
2193e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2194018d93ecSBjoern A. Zeeb 	if (error != 0) {
2195018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2196018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21979597f7cbSBjoern A. Zeeb 		goto out;
2198018d93ecSBjoern A. Zeeb 	}
21996b4cac81SBjoern A. Zeeb 
22006b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
22016b4cac81SBjoern A. Zeeb 
22026b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
22036b4cac81SBjoern A. Zeeb 	bss_changed = 0;
22044a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
22054a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
22064a67f1dfSBjoern A. Zeeb #endif
2207616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2208616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2209616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
22106b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
22116b4cac81SBjoern A. Zeeb 	}
22126b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2213616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2214616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
22156b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
22166b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
22176b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
22186b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
22196b4cac81SBjoern A. Zeeb 	}
22206b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
22216b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
22226b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
22236b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
22246b4cac81SBjoern A. Zeeb 	}
22256b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
22266b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
22276b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
22286b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
22296b4cac81SBjoern A. Zeeb 	}
2230fa8f007dSBjoern A. Zeeb 
2231fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2232fa8f007dSBjoern A. Zeeb 
22336b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
22346b4cac81SBjoern A. Zeeb 
22356b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
22366b4cac81SBjoern A. Zeeb 	 * - event_callback
22376b4cac81SBjoern A. Zeeb 	 */
22386b4cac81SBjoern A. Zeeb 
22396b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22406b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22416b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22426b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
22436b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22446b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22456b4cac81SBjoern A. Zeeb 	}
22466b4cac81SBjoern A. Zeeb 
2247086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2248086be6a8SBjoern A. Zeeb 
22496b4cac81SBjoern A. Zeeb 	/*
22506b4cac81SBjoern A. Zeeb 	 * And then:
22516b4cac81SBjoern A. Zeeb 	 * - (more packets)?
22526b4cac81SBjoern A. Zeeb 	 * - set_key
22536b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
22546b4cac81SBjoern A. Zeeb 	 * - set_key (?)
22556b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
22566b4cac81SBjoern A. Zeeb 	 */
22576b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
22586b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
22596b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
22606b4cac81SBjoern A. Zeeb 
22616b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
22626b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
22636b4cac81SBjoern A. Zeeb 	}
22646b4cac81SBjoern A. Zeeb 
22659fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
22669fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
22679fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
22689fb91463SBjoern A. Zeeb #endif
22699fb91463SBjoern A. Zeeb 
22706b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
22716b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22726b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
22736b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2274e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
22756b4cac81SBjoern A. Zeeb 	if (error != 0) {
22766b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2277018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2278018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22796b4cac81SBjoern A. Zeeb 		goto out;
22806b4cac81SBjoern A. Zeeb 	}
22816b4cac81SBjoern A. Zeeb 
22826b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
22836b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
22846b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
22856b4cac81SBjoern A. Zeeb 	 *
22866b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
22876b4cac81SBjoern A. Zeeb 	 */
22886b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
22896b4cac81SBjoern A. Zeeb 
2290fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2291fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2292fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2293fa8f007dSBjoern A. Zeeb 
22946b4cac81SBjoern A. Zeeb out:
22958ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22966b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22976b4cac81SBjoern A. Zeeb 	return (error);
22986b4cac81SBjoern A. Zeeb }
22996b4cac81SBjoern A. Zeeb 
23006b4cac81SBjoern A. Zeeb static int
23016b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23026b4cac81SBjoern A. Zeeb {
23036b4cac81SBjoern A. Zeeb 	int error;
23046b4cac81SBjoern A. Zeeb 
23056b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
23066b4cac81SBjoern A. Zeeb 	if (error == 0)
23076b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
23086b4cac81SBjoern A. Zeeb 	return (error);
23096b4cac81SBjoern A. Zeeb }
23106b4cac81SBjoern A. Zeeb 
23116b4cac81SBjoern A. Zeeb static int
23126b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23136b4cac81SBjoern A. Zeeb {
23146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23176b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23186b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23196b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23206b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2321d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2322d9f59799SBjoern A. Zeeb #if 0
2323d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2324d9f59799SBjoern A. Zeeb #endif
23256b4cac81SBjoern A. Zeeb 	int error;
23266b4cac81SBjoern A. Zeeb 
23276b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23296b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23306b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23316b4cac81SBjoern A. Zeeb 
23322ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23332ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23342ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23352ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23362ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23372ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23382ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23392ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23402ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23412ac8a218SBjoern A. Zeeb #endif
23422ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23432ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23442ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
23452ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
23462ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
23472ac8a218SBjoern A. Zeeb 
23482ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
23496b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
23506b4cac81SBjoern A. Zeeb 
235167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2352d9f59799SBjoern A. Zeeb 
2353d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
23548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2355d9f59799SBjoern A. Zeeb 
2356d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2357d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2358d9f59799SBjoern A. Zeeb 
2359d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2360d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2361d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2362d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2363d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2364ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2365d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2366d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2367d9f59799SBjoern A. Zeeb 	}
2368d9f59799SBjoern A. Zeeb 
23698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2370d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2371d9f59799SBjoern A. Zeeb 
2372d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2373d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2374018d93ecSBjoern A. Zeeb 	if (error != 0) {
2375018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2376018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2377d9f59799SBjoern A. Zeeb 		goto outni;
2378018d93ecSBjoern A. Zeeb 	}
2379d9f59799SBjoern A. Zeeb 
2380d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
238188665349SBjoern A. Zeeb 
238288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
238388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
238488665349SBjoern A. Zeeb 
23858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2386d9f59799SBjoern A. Zeeb 
238767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2388d9f59799SBjoern A. Zeeb 
2389d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
239088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2391d9f59799SBjoern A. Zeeb 
2392d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2393d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2394d9f59799SBjoern A. Zeeb 
2395d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2396d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2397d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2398d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2399ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2400d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2401d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2402d9f59799SBjoern A. Zeeb 	}
2403d9f59799SBjoern A. Zeeb 
2404d9f59799SBjoern A. Zeeb #if 0
2405d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2406d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2407d9f59799SBjoern A. Zeeb 
2408d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2409d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2410d9f59799SBjoern A. Zeeb #endif
2411d9f59799SBjoern A. Zeeb 
2412d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2413d9f59799SBjoern A. Zeeb 
24146b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
24156b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
24166b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
24176b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2418e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2419018d93ecSBjoern A. Zeeb 	if (error != 0) {
2420018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2421018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24226b4cac81SBjoern A. Zeeb 		goto out;
2423018d93ecSBjoern A. Zeeb 	}
24246b4cac81SBjoern A. Zeeb 
242567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
24266b4cac81SBjoern A. Zeeb 
24276b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
24286b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
24296b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
24306b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2431e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2432018d93ecSBjoern A. Zeeb 	if (error != 0) {
2433018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2434018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24356b4cac81SBjoern A. Zeeb 		goto out;
2436018d93ecSBjoern A. Zeeb 	}
24376b4cac81SBjoern A. Zeeb 
243867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
24396b4cac81SBjoern A. Zeeb 
2440d9f59799SBjoern A. Zeeb #if 0
2441d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2442d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2443d9f59799SBjoern A. Zeeb #endif
2444d9f59799SBjoern A. Zeeb 
2445d9f59799SBjoern A. Zeeb 	error = EALREADY;
24466b4cac81SBjoern A. Zeeb out:
24478ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
24486b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2449d9f59799SBjoern A. Zeeb outni:
24506b4cac81SBjoern A. Zeeb 	return (error);
24516b4cac81SBjoern A. Zeeb }
24526b4cac81SBjoern A. Zeeb 
24536b4cac81SBjoern A. Zeeb static int
2454d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2455d9f59799SBjoern A. Zeeb {
2456d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2457d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2458d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2459d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2460d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2461d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2462d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2463d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2464d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2465d9f59799SBjoern A. Zeeb 	int error;
2466d9f59799SBjoern A. Zeeb 
2467d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2468d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2469d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2470d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2471d9f59799SBjoern A. Zeeb 
24722ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
24732ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
24742ac8a218SBjoern A. Zeeb 
24752ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24762ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24772ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
24782ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
24792ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24802ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24812ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24822ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24832ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24842ac8a218SBjoern A. Zeeb #endif
24852ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24862ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24872ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
24882ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
24892ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
24902ac8a218SBjoern A. Zeeb 
24912ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2492d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2493d9f59799SBjoern A. Zeeb 
249467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2495d9f59799SBjoern A. Zeeb 
2496d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2497d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2498d9f59799SBjoern A. Zeeb 
2499d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2500d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2501d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2502d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2503d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2504ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2505d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2506d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2507d9f59799SBjoern A. Zeeb 	}
2508d9f59799SBjoern A. Zeeb 
25098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2510d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2511d9f59799SBjoern A. Zeeb 
2512d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2513d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2514018d93ecSBjoern A. Zeeb 	if (error != 0) {
2515018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2516018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2517d9f59799SBjoern A. Zeeb 		goto outni;
2518018d93ecSBjoern A. Zeeb 	}
2519d9f59799SBjoern A. Zeeb 
2520d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
252188665349SBjoern A. Zeeb 
252288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
252388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
252488665349SBjoern A. Zeeb 
25258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2526d9f59799SBjoern A. Zeeb 
252767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2528d9f59799SBjoern A. Zeeb 
2529d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
253088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2531d9f59799SBjoern A. Zeeb 
2532d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2533d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2534d9f59799SBjoern A. Zeeb 
2535d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2536d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2537d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2538d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2539ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2540d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2541d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2542d9f59799SBjoern A. Zeeb 	}
2543d9f59799SBjoern A. Zeeb 
2544d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2545d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2546d9f59799SBjoern A. Zeeb 
2547d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2548d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2549d9f59799SBjoern A. Zeeb 
2550d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2551d9f59799SBjoern A. Zeeb 
2552d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2553d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2554d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2555d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2556e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2557018d93ecSBjoern A. Zeeb 	if (error != 0) {
2558018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2559018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2560d9f59799SBjoern A. Zeeb 		goto out;
2561018d93ecSBjoern A. Zeeb 	}
2562d9f59799SBjoern A. Zeeb 
256367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2564d9f59799SBjoern A. Zeeb 
2565d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2566d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2567d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2568d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2569e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2570018d93ecSBjoern A. Zeeb 	if (error != 0) {
2571018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2572018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2573d9f59799SBjoern A. Zeeb 		goto out;
2574018d93ecSBjoern A. Zeeb 	}
2575d9f59799SBjoern A. Zeeb 
257667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2577d9f59799SBjoern A. Zeeb 
2578d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2579d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2580d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2581d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2582e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2583018d93ecSBjoern A. Zeeb 	if (error != 0) {
2584018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2585018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2586d9f59799SBjoern A. Zeeb 		goto out;
2587018d93ecSBjoern A. Zeeb 	}
2588d9f59799SBjoern A. Zeeb 
25895a4d2461SBjoern A. Zeeb 	bss_changed = 0;
259016e688b2SBjoern A. Zeeb 	/*
25915a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
25925a4d2461SBjoern A. Zeeb 	 *
259316e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
25945a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
25955a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
25965a4d2461SBjoern A. Zeeb 	 *
25975a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
25985a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
25995a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
26005a4d2461SBjoern A. Zeeb 	 * a fw assert.
26015a4d2461SBjoern A. Zeeb 	 *
26025a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
26035a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
26045a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
26055a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
26065a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
26075a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
26085a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
26095a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
26105a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
26115a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
261216e688b2SBjoern A. Zeeb 	 */
26135a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
26145a4d2461SBjoern A. Zeeb 
26155a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
261616e688b2SBjoern A. Zeeb 
2617d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2618d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2619d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2620d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2621e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2622d9f59799SBjoern A. Zeeb 	if (error != 0) {
2623d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2624018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2625018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2626d9f59799SBjoern A. Zeeb 		goto out;
2627d9f59799SBjoern A. Zeeb 	}
2628d9f59799SBjoern A. Zeeb 
26295a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
26305a4d2461SBjoern A. Zeeb 
263116e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2632d9f59799SBjoern A. Zeeb 
2633d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2634d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2635d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2636616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2637616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2638d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
26395a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
26405a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
26415a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2642d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2643d9f59799SBjoern A. Zeeb 
26442ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26452ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
26462ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
26472ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26482ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26490936c648SBjoern A. Zeeb 	/*
26500936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
26510936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
26520936c648SBjoern A. Zeeb 	 * and potentially freed.
26530936c648SBjoern A. Zeeb 	 */
26540936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2655d9f59799SBjoern A. Zeeb 
2656d9f59799SBjoern A. Zeeb 	/* conf_tx */
2657d9f59799SBjoern A. Zeeb 
2658d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2659d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2660c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2661d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2662d9f59799SBjoern A. Zeeb 
2663d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2664d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
266568541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2666d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2667d9f59799SBjoern A. Zeeb 
26685a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
26695a4d2461SBjoern A. Zeeb 
2670d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2671d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2672d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2673c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2674d9f59799SBjoern A. Zeeb 	}
2675d9f59799SBjoern A. Zeeb 
2676d9f59799SBjoern A. Zeeb 	error = EALREADY;
2677d9f59799SBjoern A. Zeeb out:
26788ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2679d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2680d9f59799SBjoern A. Zeeb outni:
2681d9f59799SBjoern A. Zeeb 	return (error);
2682d9f59799SBjoern A. Zeeb }
2683d9f59799SBjoern A. Zeeb 
2684d9f59799SBjoern A. Zeeb static int
2685d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2686d9f59799SBjoern A. Zeeb {
2687d9f59799SBjoern A. Zeeb 
2688d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2689d9f59799SBjoern A. Zeeb }
2690d9f59799SBjoern A. Zeeb 
2691d9f59799SBjoern A. Zeeb static int
26926b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26936b4cac81SBjoern A. Zeeb {
26946b4cac81SBjoern A. Zeeb 	int error;
26956b4cac81SBjoern A. Zeeb 
2696d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2697d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2698d9f59799SBjoern A. Zeeb 		return (error);
2699d9f59799SBjoern A. Zeeb 
2700d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2701d9f59799SBjoern A. Zeeb 
2702d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
27036b4cac81SBjoern A. Zeeb 	return (error);
27046b4cac81SBjoern A. Zeeb }
27056b4cac81SBjoern A. Zeeb 
2706d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
27076b4cac81SBjoern A. Zeeb 
27086b4cac81SBjoern A. Zeeb /*
27096b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
27106b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
27116b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
27126b4cac81SBjoern A. Zeeb  */
27136b4cac81SBjoern A. Zeeb struct fsm_state {
27146b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
27156b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
27166b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
27176b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
27186b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
27196b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
27206b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
27216b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2722d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2723d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
27246b4cac81SBjoern A. Zeeb 
27256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
27266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
27276b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
27286b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2729d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
27306b4cac81SBjoern A. Zeeb 
2731d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2732d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2733d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2734d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2735d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
27366b4cac81SBjoern A. Zeeb 
2737d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2738d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2739d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
27406b4cac81SBjoern A. Zeeb 
27416b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
27426b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
27436b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
27446b4cac81SBjoern A. Zeeb 
27456b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
27466b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
27476b4cac81SBjoern A. Zeeb };
27486b4cac81SBjoern A. Zeeb 
27496b4cac81SBjoern A. Zeeb static int
27506b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27516b4cac81SBjoern A. Zeeb {
27526b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27546b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27556b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27566b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
27576b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
27586b4cac81SBjoern A. Zeeb 	int error;
27596b4cac81SBjoern A. Zeeb 
27606b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
27616b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
27626b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
27636b4cac81SBjoern A. Zeeb 
27649d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27659d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
27666b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
27676b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
27689d9ba2b7SBjoern A. Zeeb #endif
27696b4cac81SBjoern A. Zeeb 
27706b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
27716b4cac81SBjoern A. Zeeb 
27726b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
27736b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
27746b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
27756b4cac81SBjoern A. Zeeb 
27766b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
27776b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
27786b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
27796b4cac81SBjoern A. Zeeb 
27806b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
27816b4cac81SBjoern A. Zeeb 
27826b4cac81SBjoern A. Zeeb 	} else {
27836b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
27846b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
27856b4cac81SBjoern A. Zeeb 		return (ENOSYS);
27866b4cac81SBjoern A. Zeeb 	}
27876b4cac81SBjoern A. Zeeb 
27886b4cac81SBjoern A. Zeeb 	error = 0;
27896b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
27906b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
27919d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27929d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2793d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2794d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2795d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2796d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
27979d9ba2b7SBjoern A. Zeeb #endif
27986b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
27996b4cac81SBjoern A. Zeeb 			break;
28006b4cac81SBjoern A. Zeeb 		}
28016b4cac81SBjoern A. Zeeb 	}
28026b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
28036b4cac81SBjoern A. Zeeb 
28046b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2805d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
28066b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
28076b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
28086b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
28096b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
28106b4cac81SBjoern A. Zeeb 		return (ENOSYS);
28116b4cac81SBjoern A. Zeeb 	}
28126b4cac81SBjoern A. Zeeb 
28136b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
28149d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28159d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2816d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2817d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2818d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2819d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
28209d9ba2b7SBjoern A. Zeeb #endif
28216b4cac81SBjoern A. Zeeb 		return (0);
28226b4cac81SBjoern A. Zeeb 	}
28236b4cac81SBjoern A. Zeeb 
28246b4cac81SBjoern A. Zeeb 	if (error != 0) {
28256b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
28266b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
28276b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
28286b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
282945c27ad5SBjoern A. Zeeb 		return (error);
28306b4cac81SBjoern A. Zeeb 	}
28316b4cac81SBjoern A. Zeeb 
28329d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28339d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2834d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2835d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
28366b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
28379d9ba2b7SBjoern A. Zeeb #endif
28386b4cac81SBjoern A. Zeeb 
28396b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
28406b4cac81SBjoern A. Zeeb }
28416b4cac81SBjoern A. Zeeb 
28426b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
28436b4cac81SBjoern A. Zeeb 
2844d9f59799SBjoern A. Zeeb /*
2845d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2846d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2847d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2848d9f59799SBjoern A. Zeeb  */
2849d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2850d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2851d9f59799SBjoern A. Zeeb {
2852d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28532ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2854d9f59799SBjoern A. Zeeb 
28552ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2856d9f59799SBjoern A. Zeeb 
2857ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28582ac8a218SBjoern A. Zeeb 
28592ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28602ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
28612ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28622ac8a218SBjoern A. Zeeb 
28632ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
28642ac8a218SBjoern A. Zeeb 	return (rni);
2865d9f59799SBjoern A. Zeeb }
2866d9f59799SBjoern A. Zeeb 
28674a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
28686b4cac81SBjoern A. Zeeb static int
28694a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
28706b4cac81SBjoern A. Zeeb {
28714a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
28726b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28736b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28746b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28756b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
28766b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
28776b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
28786b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
28796b4cac81SBjoern A. Zeeb 	int error;
28806b4cac81SBjoern A. Zeeb 	uint16_t ac;
28816b4cac81SBjoern A. Zeeb 
28826b4cac81SBjoern A. Zeeb 	IMPROVE();
28836b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
28846b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
28856b4cac81SBjoern A. Zeeb 
28866b4cac81SBjoern A. Zeeb 	if (vap == NULL)
28876b4cac81SBjoern A. Zeeb 		return (0);
28886b4cac81SBjoern A. Zeeb 
28894a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
28904a67f1dfSBjoern A. Zeeb 		return (0);
28914a67f1dfSBjoern A. Zeeb 
28926b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
28936b4cac81SBjoern A. Zeeb 		return (0);
28946b4cac81SBjoern A. Zeeb 
28954a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
28964a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
28974a67f1dfSBjoern A. Zeeb 		return (0);
28984a67f1dfSBjoern A. Zeeb 	}
28994a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
29006b4cac81SBjoern A. Zeeb 
29014a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
29026b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
29036b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
29046b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
29056b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
29066b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
29076b4cac81SBjoern A. Zeeb 
29084a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29094a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
29104a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29114a67f1dfSBjoern A. Zeeb 
29126b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
29136b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
29146b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
29156b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
29166b4cac81SBjoern A. Zeeb 
29176b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
29186b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
29196b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
29206b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
29216b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
29226b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
292368541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
29246b4cac81SBjoern A. Zeeb 		if (error != 0)
29253f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
29266b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
29276b4cac81SBjoern A. Zeeb 	}
29286b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
29296b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
29304a67f1dfSBjoern A. Zeeb 	if (!planned)
29316b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
29324a67f1dfSBjoern A. Zeeb 
29334a67f1dfSBjoern A. Zeeb 	return (changed);
29344a67f1dfSBjoern A. Zeeb }
29356b4cac81SBjoern A. Zeeb #endif
29366b4cac81SBjoern A. Zeeb 
29374a67f1dfSBjoern A. Zeeb static int
29384a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
29394a67f1dfSBjoern A. Zeeb {
29404a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
29414a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
29424a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
29434a67f1dfSBjoern A. Zeeb 
29444a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
29454a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
29464a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
29476b4cac81SBjoern A. Zeeb 		return (0);
29484a67f1dfSBjoern A. Zeeb 
29494a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
29504a67f1dfSBjoern A. Zeeb 
29514a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
29524a67f1dfSBjoern A. Zeeb #endif
29534a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
29546b4cac81SBjoern A. Zeeb }
29556b4cac81SBjoern A. Zeeb 
29564aff4048SBjoern A. Zeeb /*
29574aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
29584aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
29594aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
29604aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
29614aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
29624aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
2963edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
2964edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
2965edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
2966edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
29674aff4048SBjoern A. Zeeb  */
29684aff4048SBjoern A. Zeeb static void
29694aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
29704aff4048SBjoern A. Zeeb {
29714aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
29724aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29734aff4048SBjoern A. Zeeb 
29744aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
2975edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
2976edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
2977edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
29784aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
29794aff4048SBjoern A. Zeeb 		return;
29804aff4048SBjoern A. Zeeb 	}
29814aff4048SBjoern A. Zeeb 
29824aff4048SBjoern A. Zeeb 	vif = arg;
298357609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
29844aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
29854aff4048SBjoern A. Zeeb }
29864aff4048SBjoern A. Zeeb 
29876b4cac81SBjoern A. Zeeb static struct ieee80211vap *
29886b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
29896b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
29906b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
29916b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
29926b4cac81SBjoern A. Zeeb {
29936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29966b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
29976b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2998a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
29996b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
3000*40839418SBjoern A. Zeeb 	struct sysctl_oid *node;
30016b4cac81SBjoern A. Zeeb 	size_t len;
3002e3a0b120SBjoern A. Zeeb 	int error, i;
3003a6042e17SBjoern A. Zeeb 	uint16_t ac;
30046b4cac81SBjoern A. Zeeb 
30056b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
30066b4cac81SBjoern A. Zeeb 		return (NULL);
30076b4cac81SBjoern A. Zeeb 
30086b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30096b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30106b4cac81SBjoern A. Zeeb 
30116b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
30126b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
30136b4cac81SBjoern A. Zeeb 
30146b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
30156b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
30166b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
30172ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
30182ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
30196b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
30206b4cac81SBjoern A. Zeeb 
30216b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
30226b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
30236b4cac81SBjoern A. Zeeb 	vif->p2p = false;
30246b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
30256b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
30266b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
30276b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
30286b4cac81SBjoern A. Zeeb 	IMPROVE();
30296b4cac81SBjoern A. Zeeb 
30306b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
30316b4cac81SBjoern A. Zeeb #if 1
30326b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3033adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
30346ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
30356ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
30364aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
30374aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
30386ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
30397b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
30406b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
30416b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
30426b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
30436b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
30446b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3045616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3046616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3047616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3048616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
30496ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3050caaa79c3SBjoern A. Zeeb 	/*
3051caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3052caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3053caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3054caaa79c3SBjoern A. Zeeb 	 */
30556ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
30566b4cac81SBjoern A. Zeeb #endif
30576b4cac81SBjoern A. Zeeb #if 0
30586b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
30596b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
30606b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
30616b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
30626b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
30636b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
30646b4cac81SBjoern A. Zeeb #endif
3065e3a0b120SBjoern A. Zeeb 
30666ffb7bd4SBjoern A. Zeeb 	/* Link Config */
30676ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
30686ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
30696ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
30706ffb7bd4SBjoern A. Zeeb 	}
30716ffb7bd4SBjoern A. Zeeb 
3072e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3073e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3074e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3075e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3076e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3077e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3078e3a0b120SBjoern A. Zeeb 		else
3079e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
30800cbcfa19SBjoern A. Zeeb 
30810cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
30820cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3083e3a0b120SBjoern A. Zeeb 	}
3084e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3085e3a0b120SBjoern A. Zeeb 
30866b4cac81SBjoern A. Zeeb 	IMPROVE();
30876b4cac81SBjoern A. Zeeb 
30886b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
30896b4cac81SBjoern A. Zeeb 	if (error != 0) {
30903f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
30916b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
30926b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
30936b4cac81SBjoern A. Zeeb 		return (NULL);
30946b4cac81SBjoern A. Zeeb 	}
30956b4cac81SBjoern A. Zeeb 
30966b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
30976b4cac81SBjoern A. Zeeb 	if (error != 0) {
30986b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
30993f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
31006b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
31016b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
31026b4cac81SBjoern A. Zeeb 		return (NULL);
31036b4cac81SBjoern A. Zeeb 	}
31046b4cac81SBjoern A. Zeeb 
31058891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
31066b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
31078891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
31086b4cac81SBjoern A. Zeeb 
31096b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
31106b4cac81SBjoern A. Zeeb 	changed = 0;
31116b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
31126b4cac81SBjoern A. Zeeb 
3113a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3114a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3115a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3116a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3117a6042e17SBjoern A. Zeeb 
3118a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3119a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3120a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3121a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3122a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3123a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3124a6042e17SBjoern A. Zeeb 		if (error != 0)
3125a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3126a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3127a6042e17SBjoern A. Zeeb 	}
3128a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3129a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3130a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
31316b4cac81SBjoern A. Zeeb 
31326b4cac81SBjoern A. Zeeb 	/* Force MC init. */
31336b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
31346b4cac81SBjoern A. Zeeb 
31356b4cac81SBjoern A. Zeeb 	IMPROVE();
31366b4cac81SBjoern A. Zeeb 
31376b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
31386b4cac81SBjoern A. Zeeb 
31396b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
31406b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
31416b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3142d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3143d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
31446b4cac81SBjoern A. Zeeb 
31456b4cac81SBjoern A. Zeeb 	/* Key management. */
31466b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
3147b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
31486b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
31496b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
31506b4cac81SBjoern A. Zeeb #endif
31516b4cac81SBjoern A. Zeeb 	}
31526b4cac81SBjoern A. Zeeb 
31539fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
31549fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
31559fb91463SBjoern A. Zeeb #endif
31569fb91463SBjoern A. Zeeb 
31576b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
31586b4cac81SBjoern A. Zeeb 
31596b4cac81SBjoern A. Zeeb 	/* Complete setup. */
31606b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
31616b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
31626b4cac81SBjoern A. Zeeb 
31636b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
31646b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
31656b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
31666b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
31676b4cac81SBjoern A. Zeeb 
31686b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
31696b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
31706b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
31716b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
31726b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
31736b4cac81SBjoern A. Zeeb 	/* any others? */
3174*40839418SBjoern A. Zeeb 
3175*40839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
3176*40839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
3177*40839418SBjoern A. Zeeb 
3178*40839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
3179*40839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
3180*40839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
3181*40839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
3182*40839418SBjoern A. Zeeb 
3183*40839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
3184*40839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
3185*40839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
3186*40839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
3187*40839418SBjoern A. Zeeb 
31886b4cac81SBjoern A. Zeeb 	IMPROVE();
31896b4cac81SBjoern A. Zeeb 
31906b4cac81SBjoern A. Zeeb 	return (vap);
31916b4cac81SBjoern A. Zeeb }
31926b4cac81SBjoern A. Zeeb 
31934b0af114SBjoern A. Zeeb void
31944b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
31954b0af114SBjoern A. Zeeb {
31964b0af114SBjoern A. Zeeb 
31974b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
31984b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
31994b0af114SBjoern A. Zeeb 
32004b0af114SBjoern A. Zeeb 	IMPROVE();
32014b0af114SBjoern A. Zeeb }
32024b0af114SBjoern A. Zeeb 
32034b0af114SBjoern A. Zeeb void
32044b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
32054b0af114SBjoern A. Zeeb {
32064b0af114SBjoern A. Zeeb 
32074b0af114SBjoern A. Zeeb 	TODO();
32084b0af114SBjoern A. Zeeb }
32094b0af114SBjoern A. Zeeb 
32106b4cac81SBjoern A. Zeeb static void
32116b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
32126b4cac81SBjoern A. Zeeb {
32136b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
32146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32176b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32186b4cac81SBjoern A. Zeeb 
32196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
32216b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
32226b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32246b4cac81SBjoern A. Zeeb 
32254aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
32264aff4048SBjoern A. Zeeb 
3227*40839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
3228*40839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
3229*40839418SBjoern A. Zeeb 
32308891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
32316b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
32328891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
32336b4cac81SBjoern A. Zeeb 
32346b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
32356b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3236dbf76919SBjoern A. Zeeb 
3237dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3238dbf76919SBjoern A. Zeeb 
3239dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3240dbf76919SBjoern A. Zeeb 
32416c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
32427b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
32436c38c6b1SBjoern A. Zeeb 
32446b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
32456b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
32466b4cac81SBjoern A. Zeeb }
32476b4cac81SBjoern A. Zeeb 
32486b4cac81SBjoern A. Zeeb static void
32496b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
32506b4cac81SBjoern A. Zeeb {
32516b4cac81SBjoern A. Zeeb 
32526b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
32536b4cac81SBjoern A. Zeeb 	TRACEOK();
32546b4cac81SBjoern A. Zeeb }
32556b4cac81SBjoern A. Zeeb 
32566b4cac81SBjoern A. Zeeb static void
32576b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
32586b4cac81SBjoern A. Zeeb {
32596b4cac81SBjoern A. Zeeb 
32606b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
32616b4cac81SBjoern A. Zeeb }
32626b4cac81SBjoern A. Zeeb 
32636b4cac81SBjoern A. Zeeb static void
32646b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
32656b4cac81SBjoern A. Zeeb {
32666b4cac81SBjoern A. Zeeb 
32676b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
32686b4cac81SBjoern A. Zeeb }
32696b4cac81SBjoern A. Zeeb 
32706b4cac81SBjoern A. Zeeb /* Start / stop device. */
32716b4cac81SBjoern A. Zeeb static void
32726b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
32736b4cac81SBjoern A. Zeeb {
32746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32758d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
32766b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32776b4cac81SBjoern A. Zeeb 	int error;
32788d58a057SBjoern A. Zeeb #endif
32796b4cac81SBjoern A. Zeeb 	bool start_all;
32806b4cac81SBjoern A. Zeeb 
32816b4cac81SBjoern A. Zeeb 	IMPROVE();
32826b4cac81SBjoern A. Zeeb 
32836b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32848d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
32856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32868d58a057SBjoern A. Zeeb #endif
32876b4cac81SBjoern A. Zeeb 	start_all = false;
32886b4cac81SBjoern A. Zeeb 
32898ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
32908ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
32916b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
32928d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
32936b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
32946b4cac81SBjoern A. Zeeb 		if (error == 0)
32958d58a057SBjoern A. Zeeb #endif
32966b4cac81SBjoern A. Zeeb 			start_all = true;
32976b4cac81SBjoern A. Zeeb 	} else {
32988d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
32997b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
33008d58a057SBjoern A. Zeeb #endif
33016b4cac81SBjoern A. Zeeb 	}
33028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
33038ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
33046b4cac81SBjoern A. Zeeb 
33056b4cac81SBjoern A. Zeeb 	if (start_all)
33066b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
33076b4cac81SBjoern A. Zeeb }
33086b4cac81SBjoern A. Zeeb 
33096b4cac81SBjoern A. Zeeb bool
33106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
33116b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
33126b4cac81SBjoern A. Zeeb {
33136b4cac81SBjoern A. Zeeb 	int i;
33146b4cac81SBjoern A. Zeeb 
33156b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
33166b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
33176b4cac81SBjoern A. Zeeb 			return (true);
33186b4cac81SBjoern A. Zeeb 	}
33196b4cac81SBjoern A. Zeeb 
33206b4cac81SBjoern A. Zeeb 	return (false);
33216b4cac81SBjoern A. Zeeb }
33226b4cac81SBjoern A. Zeeb 
33236b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
33246b4cac81SBjoern A. Zeeb bool
33256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
33266b4cac81SBjoern A. Zeeb {
33276b4cac81SBjoern A. Zeeb 	size_t x;
33286b4cac81SBjoern A. Zeeb 	uint8_t l;
33296b4cac81SBjoern A. Zeeb 
33306b4cac81SBjoern A. Zeeb 	x = *xp;
33316b4cac81SBjoern A. Zeeb 
33326b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
33336b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
33346b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
33356b4cac81SBjoern A. Zeeb 	x += 2 + l;
33366b4cac81SBjoern A. Zeeb 
33376b4cac81SBjoern A. Zeeb 	if (x > ies_len)
33386b4cac81SBjoern A. Zeeb 		return (false);
33396b4cac81SBjoern A. Zeeb 
33406b4cac81SBjoern A. Zeeb 	*xp = x;
33416b4cac81SBjoern A. Zeeb 	return (true);
33426b4cac81SBjoern A. Zeeb }
33436b4cac81SBjoern A. Zeeb 
3344d9945d78SBjoern A. Zeeb static uint8_t *
3345d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3346d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
33476b4cac81SBjoern A. Zeeb {
3348d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3349d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
33503dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3351d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3352d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3353d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3354d9945d78SBjoern A. Zeeb 	int band, i;
33556b4cac81SBjoern A. Zeeb 
33563dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3357d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3358d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3359d9945d78SBjoern A. Zeeb 			continue;
3360d9945d78SBjoern A. Zeeb 
3361d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3362d9945d78SBjoern A. Zeeb 		/*
3363d9945d78SBjoern A. Zeeb 		 * This should not happen;
3364d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3365d9945d78SBjoern A. Zeeb 		 */
3366d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3367d9945d78SBjoern A. Zeeb 			continue;
3368d9945d78SBjoern A. Zeeb 
3369d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3370d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3371d9945d78SBjoern A. Zeeb 		chan = NULL;
3372d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3373d9945d78SBjoern A. Zeeb 
3374d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3375d9945d78SBjoern A. Zeeb 				continue;
3376d9945d78SBjoern A. Zeeb 
33773dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3378d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3379d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3380d9945d78SBjoern A. Zeeb 				break;
3381d9945d78SBjoern A. Zeeb 		}
3382d9945d78SBjoern A. Zeeb 
3383d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3384d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3385d9945d78SBjoern A. Zeeb 			continue;
3386d9945d78SBjoern A. Zeeb 
3387d9945d78SBjoern A. Zeeb 		pb = p;
33883dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3389d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3390d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3391d9945d78SBjoern A. Zeeb 
33923dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
33933dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
33943dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
33953dd98026SBjoern A. Zeeb 
33963dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
33973dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
33983dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
33993dd98026SBjoern A. Zeeb 		}
34003dd98026SBjoern A. Zeeb #endif
34013dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
34023dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
34033dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
34043dd98026SBjoern A. Zeeb 
34053dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
34063dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
34073dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
34083dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
34093dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
34103dd98026SBjoern A. Zeeb 		}
34113dd98026SBjoern A. Zeeb #endif
34123dd98026SBjoern A. Zeeb 
3413d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3414d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3415d9945d78SBjoern A. Zeeb 	}
3416d9945d78SBjoern A. Zeeb 
3417d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3418d9945d78SBjoern A. Zeeb 	pb = p;
3419d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3420d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3421d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3422d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3423d9945d78SBjoern A. Zeeb 	}
3424d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3425d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3426d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3427d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3428d9945d78SBjoern A. Zeeb 	}
3429d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3430d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3431d9945d78SBjoern A. Zeeb 
3432d9945d78SBjoern A. Zeeb 	return (p);
34336b4cac81SBjoern A. Zeeb }
34346b4cac81SBjoern A. Zeeb 
34356b4cac81SBjoern A. Zeeb static void
34366b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
34376b4cac81SBjoern A. Zeeb {
34386b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34396b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34406b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34416b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34426b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
34436b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
34446b4cac81SBjoern A. Zeeb 	int error;
34458ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
34466b4cac81SBjoern A. Zeeb 
34476b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3449a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
34506b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
34518ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34526b4cac81SBjoern A. Zeeb 		return;
34536b4cac81SBjoern A. Zeeb 	}
34548ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
34558ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
34586b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
34596b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3460d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
34616b4cac81SBjoern A. Zeeb 		return;
34626b4cac81SBjoern A. Zeeb 	}
34636b4cac81SBjoern A. Zeeb 
34646b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34658ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3466a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3467a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3468d3ef7fb4SBjoern A. Zeeb sw_scan:
34696b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
34706b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3471086be6a8SBjoern A. Zeeb 
3472086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3473086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3474086be6a8SBjoern A. Zeeb 
34756b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
34766b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
34776b4cac81SBjoern A. Zeeb 		IMPROVE();
34786b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
34796b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
34806b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
34816b4cac81SBjoern A. Zeeb 
34826b4cac81SBjoern A. Zeeb 	} else {
34836b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
34846b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
34856b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
34866b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
34876b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3488d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3489d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3490d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
34913206587aSBjoern A. Zeeb 		bool running;
3492d9945d78SBjoern A. Zeeb 
3493d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3494d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
34956b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
34966b4cac81SBjoern A. Zeeb 
3497d9945d78SBjoern A. Zeeb 		band_mask = 0;
34986b4cac81SBjoern A. Zeeb 		nchan = 0;
3499c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3500c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3501d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
35026b4cac81SBjoern A. Zeeb 				nchan++;
3503d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3504d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3505d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3506d9945d78SBjoern A. Zeeb 			}
3507c272abc5SBjoern A. Zeeb #else
3508c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3509c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3510c272abc5SBjoern A. Zeeb 				switch (band) {
3511c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3512c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3513c272abc5SBjoern A. Zeeb 					break;
3514c272abc5SBjoern A. Zeeb 				default:
3515c272abc5SBjoern A. Zeeb 					continue;
3516c272abc5SBjoern A. Zeeb 				}
3517c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3518c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3519c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3520c272abc5SBjoern A. Zeeb 				}
3521c272abc5SBjoern A. Zeeb 			}
3522c272abc5SBjoern A. Zeeb #endif
3523c272abc5SBjoern A. Zeeb 		} else {
35243206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
35253206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
35263206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
35273206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
35283206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
35293206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
35303206587aSBjoern A. Zeeb 			 */
35313206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
35323206587aSBjoern A. Zeeb 		}
35333206587aSBjoern A. Zeeb 
35346b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
35356b4cac81SBjoern A. Zeeb 
3536d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3537d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3538d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3539d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3540d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3541d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3542d9945d78SBjoern A. Zeeb 
3543d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3544d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3545d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3546d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3547d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3548d9945d78SBjoern A. Zeeb 		}
3549d9945d78SBjoern A. Zeeb 
35503206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3551d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3552d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
35536b4cac81SBjoern A. Zeeb 
35546b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
35556b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
35566b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
35576b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
35586b4cac81SBjoern A. Zeeb #if 0
35596b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
35606b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
35616b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
35626b4cac81SBjoern A. Zeeb #endif
35636b4cac81SBjoern A. Zeeb #ifdef __notyet__
35646b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
35656b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
35666b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
35676b4cac81SBjoern A. Zeeb #endif
3568e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
35696b4cac81SBjoern A. Zeeb 
35706b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
35716b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
35726b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
35736b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
35746b4cac81SBjoern A. Zeeb 			*(cpp + i) =
35756b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
35766b4cac81SBjoern A. Zeeb 		}
3577c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
35786b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3579c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
35806b4cac81SBjoern A. Zeeb 
3581c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
35826b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
35833206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
35846b4cac81SBjoern A. Zeeb 			/* lc->flags */
35856b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
35866b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
35876b4cac81SBjoern A. Zeeb 			/* lc-> ... */
35886b4cac81SBjoern A. Zeeb 			lc++;
35896b4cac81SBjoern A. Zeeb 		}
3590c272abc5SBjoern A. Zeeb #else
3591c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3592c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3593c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3594c272abc5SBjoern A. Zeeb 
3595c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3596c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3597c272abc5SBjoern A. Zeeb 				continue;
3598c272abc5SBjoern A. Zeeb 
3599c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
3600c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
3601c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
3602c272abc5SBjoern A. Zeeb 				continue;
3603c272abc5SBjoern A. Zeeb 
3604c272abc5SBjoern A. Zeeb 			channels = supband->channels;
3605c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
3606c272abc5SBjoern A. Zeeb 				*lc = channels[i];
3607c272abc5SBjoern A. Zeeb 				lc++;
3608c272abc5SBjoern A. Zeeb 			}
3609c272abc5SBjoern A. Zeeb 		}
3610c272abc5SBjoern A. Zeeb #endif
36116b4cac81SBjoern A. Zeeb 
3612d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
36136b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
36146b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
36156b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3616d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
36176b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
36186b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
36196b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
36206b4cac81SBjoern A. Zeeb 				ssids++;
36216b4cac81SBjoern A. Zeeb 			}
36226b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
36236b4cac81SBjoern A. Zeeb 		} else {
36246b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
36256b4cac81SBjoern A. Zeeb 		}
36266b4cac81SBjoern A. Zeeb 
36276b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
36286b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
36296b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
36306b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
36316b4cac81SBjoern A. Zeeb 		/* s6gp->... */
36326b4cac81SBjoern A. Zeeb 
3633d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3634d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3635d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3636d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3637d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3638d9945d78SBjoern A. Zeeb 
36396b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
36406b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
36413206587aSBjoern A. Zeeb 
36423206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
36433206587aSBjoern A. Zeeb 		/* Re-check under lock. */
36443206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
36453206587aSBjoern A. Zeeb 		if (!running) {
36463206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
36473206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
36483206587aSBjoern A. Zeeb 
36493206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
36503206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
36513206587aSBjoern A. Zeeb 		}
36523206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
36533206587aSBjoern A. Zeeb 		if (running) {
36543206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
36553206587aSBjoern A. Zeeb 			return;
36563206587aSBjoern A. Zeeb 		}
36573206587aSBjoern A. Zeeb 
36586b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
36596b4cac81SBjoern A. Zeeb 		if (error != 0) {
3660d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3661d9945d78SBjoern A. Zeeb 
36623206587aSBjoern A. Zeeb 			/*
36633206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
36643206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
36653206587aSBjoern A. Zeeb 			 * and only there.
36663206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
36673206587aSBjoern A. Zeeb 			 * behave differently:
36683206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
36693206587aSBjoern A. Zeeb 			 *        and can then still return an error.
36703206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
36713206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
36723206587aSBjoern A. Zeeb 			 * ...
36733206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
36743206587aSBjoern A. Zeeb 			 * and balance between both code paths.
36753206587aSBjoern A. Zeeb 			 */
36763206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
36773206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
36783206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
36796b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
36803206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
36813206587aSBjoern A. Zeeb 			}
36823206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3683d3ef7fb4SBjoern A. Zeeb 
3684d3ef7fb4SBjoern A. Zeeb 			/*
3685d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3686d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3687d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3688d3ef7fb4SBjoern A. Zeeb 			 */
3689196cfd0bSBjoern A. Zeeb 			if (error == 1) {
36908ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3691a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
36928ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
36933206587aSBjoern A. Zeeb 				/*
36943206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
36953206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
36963206587aSBjoern A. Zeeb 				 * currently not re-enable it?
36973206587aSBjoern A. Zeeb 				 */
36983206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3699196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3700196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3701196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3702196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3703196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3704196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3705196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3706196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3707d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3708196cfd0bSBjoern A. Zeeb 			}
3709d3ef7fb4SBjoern A. Zeeb 
3710d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3711d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
37126b4cac81SBjoern A. Zeeb 		}
37136b4cac81SBjoern A. Zeeb 	}
37146b4cac81SBjoern A. Zeeb }
37156b4cac81SBjoern A. Zeeb 
37166b4cac81SBjoern A. Zeeb static void
37176b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
37186b4cac81SBjoern A. Zeeb {
37196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37208ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
37216b4cac81SBjoern A. Zeeb 
37226b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3724a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
37258ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37266b4cac81SBjoern A. Zeeb 		return;
37276b4cac81SBjoern A. Zeeb 	}
37288ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
37298ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37306b4cac81SBjoern A. Zeeb 
37318ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3732a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3733a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
37346b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
37356b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
37366b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
37376b4cac81SBjoern A. Zeeb 
3738a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3739a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
37406b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
37416b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
37426b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3743a486fbbdSBjoern A. Zeeb 
37446b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
37456b4cac81SBjoern A. Zeeb 
37466b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3747086be6a8SBjoern A. Zeeb 
3748086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3749086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
37506b4cac81SBjoern A. Zeeb 	}
37516b4cac81SBjoern A. Zeeb }
37526b4cac81SBjoern A. Zeeb 
37536b4cac81SBjoern A. Zeeb static void
3754a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3755a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3756a486fbbdSBjoern A. Zeeb {
3757a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
37588ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3759a486fbbdSBjoern A. Zeeb 
3760a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
37618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
37628ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
37638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37648ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3765a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3766a486fbbdSBjoern A. Zeeb }
3767a486fbbdSBjoern A. Zeeb 
3768a486fbbdSBjoern A. Zeeb static void
3769a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3770a486fbbdSBjoern A. Zeeb {
3771a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
37728ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3773a486fbbdSBjoern A. Zeeb 
3774a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
37758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
37768ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
37778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37788ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3779a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3780a486fbbdSBjoern A. Zeeb }
3781a486fbbdSBjoern A. Zeeb 
3782a486fbbdSBjoern A. Zeeb static void
37836b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
37846b4cac81SBjoern A. Zeeb {
37856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37866b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3787b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3788b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
37896b4cac81SBjoern A. Zeeb 	int error;
37908ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
37916b4cac81SBjoern A. Zeeb 
37926b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3793b2cf3c21SBjoern A. Zeeb 
3794b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3795b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
37966b4cac81SBjoern A. Zeeb 		return;
37976b4cac81SBjoern A. Zeeb 
3798a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
37998ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
38008ac540d3SBjoern A. Zeeb 	hw_scan_running =
38018ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
38028ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
38038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38048ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3805a486fbbdSBjoern A. Zeeb 		return;
3806a486fbbdSBjoern A. Zeeb 
3807b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3808b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
38096b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
38106b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
38116b4cac81SBjoern A. Zeeb 		return;
38126b4cac81SBjoern A. Zeeb 	}
38136b4cac81SBjoern A. Zeeb 
38146b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
38156b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
38166b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
38176b4cac81SBjoern A. Zeeb 		    c, chan);
38186b4cac81SBjoern A. Zeeb 		return;
38196b4cac81SBjoern A. Zeeb 	}
38206b4cac81SBjoern A. Zeeb 
38216b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
38226b4cac81SBjoern A. Zeeb 	IMPROVE();
38236b4cac81SBjoern A. Zeeb 
38246b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38254a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
38269fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38279fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
38289fb91463SBjoern A. Zeeb #endif
38294a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
38306b4cac81SBjoern A. Zeeb 
38316b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
38326b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
38336b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
38346b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
38356b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
38366b4cac81SBjoern A. Zeeb 		IMPROVE();
38376b4cac81SBjoern A. Zeeb 	} else {
38386b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
38396b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
38406b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
38416b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
38426b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
38436b4cac81SBjoern A. Zeeb 	}
38446b4cac81SBjoern A. Zeeb 
38456b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
38466b4cac81SBjoern A. Zeeb 	IMPROVE();
38476b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
38486b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
38496b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
38506b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
38516b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
38526b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
38536b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
38546b4cac81SBjoern A. Zeeb 			    error);
38556b4cac81SBjoern A. Zeeb 	}
38566b4cac81SBjoern A. Zeeb }
38576b4cac81SBjoern A. Zeeb 
38586b4cac81SBjoern A. Zeeb static struct ieee80211_node *
38596b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
38606b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
38616b4cac81SBjoern A. Zeeb {
38626b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38644f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
38656b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38666b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
38676b4cac81SBjoern A. Zeeb 
38686b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
38696b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38706b4cac81SBjoern A. Zeeb 
38716b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
38724f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
38734f61ef8bSBjoern A. Zeeb 		return (NULL);
38744f61ef8bSBjoern A. Zeeb 
38756b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
38766b4cac81SBjoern A. Zeeb 	if (ni == NULL)
38776b4cac81SBjoern A. Zeeb 		return (NULL);
38786b4cac81SBjoern A. Zeeb 
38796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38804f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
38816b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
38826b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
38836b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
38846b4cac81SBjoern A. Zeeb 		return (NULL);
38856b4cac81SBjoern A. Zeeb 	}
38866b4cac81SBjoern A. Zeeb 
38876b4cac81SBjoern A. Zeeb 	return (ni);
38886b4cac81SBjoern A. Zeeb }
38896b4cac81SBjoern A. Zeeb 
38906b4cac81SBjoern A. Zeeb static int
38916b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
38926b4cac81SBjoern A. Zeeb {
38936b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38946b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38956b4cac81SBjoern A. Zeeb 	int error;
38966b4cac81SBjoern A. Zeeb 
38976b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
38986b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38996b4cac81SBjoern A. Zeeb 
39006b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
39016b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
39026b4cac81SBjoern A. Zeeb 		if (error != 0)
39036b4cac81SBjoern A. Zeeb 			return (error);
39046b4cac81SBjoern A. Zeeb 	}
39056b4cac81SBjoern A. Zeeb 
39066b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
39076b4cac81SBjoern A. Zeeb 	IMPROVE();
39086b4cac81SBjoern A. Zeeb 
39096b4cac81SBjoern A. Zeeb 	return (0);
39106b4cac81SBjoern A. Zeeb }
39116b4cac81SBjoern A. Zeeb 
39126b4cac81SBjoern A. Zeeb static void
39136b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
39146b4cac81SBjoern A. Zeeb {
39156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
39166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39176b4cac81SBjoern A. Zeeb 
39186b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
39196b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39206b4cac81SBjoern A. Zeeb 
39216b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
39226b4cac81SBjoern A. Zeeb 	IMPROVE();
39236b4cac81SBjoern A. Zeeb 
39246b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
39256b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
39266b4cac81SBjoern A. Zeeb }
39276b4cac81SBjoern A. Zeeb 
39286b4cac81SBjoern A. Zeeb static void
39296b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
39306b4cac81SBjoern A. Zeeb {
39316b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
39326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39346b4cac81SBjoern A. Zeeb 
39356b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
39366b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39376b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
39386b4cac81SBjoern A. Zeeb 
39390936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
39406b4cac81SBjoern A. Zeeb 
39410936c648SBjoern A. Zeeb 	/*
39420936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
39430936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
39440936c648SBjoern A. Zeeb 	 */
39450936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
39466b4cac81SBjoern A. Zeeb 
39476b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
39486b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
39496b4cac81SBjoern A. Zeeb }
39506b4cac81SBjoern A. Zeeb 
39516b4cac81SBjoern A. Zeeb static int
39526b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
39536b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
39546b4cac81SBjoern A. Zeeb {
39556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39566b4cac81SBjoern A. Zeeb 
39576b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3958fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
39592372f8ccSBjoern A. Zeeb #if 0
396088665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
39612372f8ccSBjoern A. Zeeb #else
39622372f8ccSBjoern A. Zeeb 	/*
39632372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
39642372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
39652372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
39662372f8ccSBjoern A. Zeeb 	 */
39672372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
39682372f8ccSBjoern A. Zeeb #endif
3969fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3970fa4e4257SBjoern A. Zeeb 		/*
3971fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3972fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
3973fa4e4257SBjoern A. Zeeb 		 */
39740936c648SBjoern A. Zeeb 		m_free(m);
39750936c648SBjoern A. Zeeb 		return (ENETDOWN);
39760936c648SBjoern A. Zeeb 	}
39776b4cac81SBjoern A. Zeeb 
39786b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
39796b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
3980fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3981fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
39826b4cac81SBjoern A. Zeeb 
39839d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39849d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
39856b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
39866b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
39876b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
39889d9ba2b7SBjoern A. Zeeb #endif
39896b4cac81SBjoern A. Zeeb 
39906b4cac81SBjoern A. Zeeb 	return (0);
39916b4cac81SBjoern A. Zeeb }
39926b4cac81SBjoern A. Zeeb 
39936b4cac81SBjoern A. Zeeb static void
39946b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
39956b4cac81SBjoern A. Zeeb {
39966b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3997b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
39986b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3999b35f6cd0SBjoern A. Zeeb #endif
40006b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
40016b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
40026b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
40036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40046b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40056b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
40066b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
40076b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
40086b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
40096b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
40106b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4011e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4012ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
40136b4cac81SBjoern A. Zeeb 	void *buf;
4014e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
40156b4cac81SBjoern A. Zeeb 
40166b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
40176b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
40189d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
40196b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
40206b4cac81SBjoern A. Zeeb #endif
40216b4cac81SBjoern A. Zeeb 
40226b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4023b35f6cd0SBjoern A. Zeeb 	k = NULL;
4024b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
40256b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
40266b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
40276b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
40286b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
40296b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
40306b4cac81SBjoern A. Zeeb 		if (k == NULL) {
40316b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
40326b4cac81SBjoern A. Zeeb 			m_freem(m);
40336b4cac81SBjoern A. Zeeb 			return;
40346b4cac81SBjoern A. Zeeb 		}
40356b4cac81SBjoern A. Zeeb 	}
40366b4cac81SBjoern A. Zeeb #endif
40376b4cac81SBjoern A. Zeeb 
40386b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
40396b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40406b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40416b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
40426b4cac81SBjoern A. Zeeb 
40436b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
40446b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
40456b4cac81SBjoern A. Zeeb 
40466b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
40476b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
40486b4cac81SBjoern A. Zeeb 		if (k != NULL)
40496b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
40506b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
40516b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
40526b4cac81SBjoern A. Zeeb 		IMPROVE();
40536b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
40546b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
40556b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
40566b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
40576b4cac81SBjoern A. Zeeb 		}
40586b4cac81SBjoern A. Zeeb 
40596b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
40606b4cac81SBjoern A. Zeeb 	}
40616b4cac81SBjoern A. Zeeb 
40626b4cac81SBjoern A. Zeeb 	/*
40636b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
40646b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
40653d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
40663d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
40676b4cac81SBjoern A. Zeeb 	 */
40686b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
40696b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4070bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4071bcf1d8eeSBjoern A. Zeeb 
4072bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4073bcf1d8eeSBjoern A. Zeeb 			int tid;
4074bcf1d8eeSBjoern A. Zeeb 
4075bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4076bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4077bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4078bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4079bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4080bcf1d8eeSBjoern A. Zeeb 					continue;
4081bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4082bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4083bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq-> stopped, skb_queue_len(&ltxq->skbq));
4084bcf1d8eeSBjoern A. Zeeb 			}
4085bcf1d8eeSBjoern A. Zeeb 		}
40866b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
40876b4cac81SBjoern A. Zeeb 		m_freem(m);
40886b4cac81SBjoern A. Zeeb 		return;
40896b4cac81SBjoern A. Zeeb 	}
40906b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
40916b4cac81SBjoern A. Zeeb 
40926b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
40936b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
40946b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
40956b4cac81SBjoern A. Zeeb 	skb->m = m;
40966b4cac81SBjoern A. Zeeb #if 0
40976b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
40986b4cac81SBjoern A. Zeeb #else
40996b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
41006b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
41016b4cac81SBjoern A. Zeeb #endif
41026b4cac81SBjoern A. Zeeb 	/* Save the ni. */
41036b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
41046b4cac81SBjoern A. Zeeb 
41056b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
41066b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
41076b4cac81SBjoern A. Zeeb 
4108e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4109e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4110e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
41111665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
41121665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
41131665ef97SBjoern A. Zeeb 			skb->priority = 7;
41141665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
41151665ef97SBjoern A. Zeeb 		} else {
41161665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
41171665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4118e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4119e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
41201665ef97SBjoern A. Zeeb 		}
4121e3a0b120SBjoern A. Zeeb 	} else {
4122e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4123fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4124e3a0b120SBjoern A. Zeeb 	}
41256b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
41266b4cac81SBjoern A. Zeeb 
41276b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
41286b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
41296b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
41306b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
41316b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
41326b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4133e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
41346b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
41356b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
41366b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
41376b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
41389fb91463SBjoern A. Zeeb #ifdef __notyet__
41399fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
41409fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
41419fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
41429fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
41439fb91463SBjoern A. Zeeb #endif
41449fb91463SBjoern A. Zeeb #endif
41456b4cac81SBjoern A. Zeeb 
41466b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4147b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
41486b4cac81SBjoern A. Zeeb 	info->control.hw_key = lsta->kc;
41496b4cac81SBjoern A. Zeeb #endif
41506b4cac81SBjoern A. Zeeb 
41516b4cac81SBjoern A. Zeeb 	IMPROVE();
41526b4cac81SBjoern A. Zeeb 
4153e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4154e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4155e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4156e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4157e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4158d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4159e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4160e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4161e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4162e3a0b120SBjoern A. Zeeb 	}
4163e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4164d0d29110SBjoern A. Zeeb 		goto ops_tx;
4165e3a0b120SBjoern A. Zeeb 
4166d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4167d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4168d0d29110SBjoern A. Zeeb 
4169eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
41706b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
41719d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
41729d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4173d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4174d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4175d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4176fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4177d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4178d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4179d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4180d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
41819d9ba2b7SBjoern A. Zeeb #endif
4182eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
418345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4184d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
418545bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
41866b4cac81SBjoern A. Zeeb 	return;
41876b4cac81SBjoern A. Zeeb 
41886b4cac81SBjoern A. Zeeb ops_tx:
41899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
41909d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4191d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4192d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
41936b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
41946b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
41959d9ba2b7SBjoern A. Zeeb #endif
41966b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
41976b4cac81SBjoern A. Zeeb 	control.sta = sta;
419845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
41996b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
420045bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
42016b4cac81SBjoern A. Zeeb }
42026b4cac81SBjoern A. Zeeb 
42036b4cac81SBjoern A. Zeeb static void
42046b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
42056b4cac81SBjoern A. Zeeb {
42066b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42076b4cac81SBjoern A. Zeeb 	struct mbufq mq;
42086b4cac81SBjoern A. Zeeb 	struct mbuf *m;
420988665349SBjoern A. Zeeb 	bool shall_tx;
42106b4cac81SBjoern A. Zeeb 
42116b4cac81SBjoern A. Zeeb 	lsta = ctx;
42126b4cac81SBjoern A. Zeeb 
42139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
42149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
42156b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
42169d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
42176b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
42189d9ba2b7SBjoern A. Zeeb #endif
42196b4cac81SBjoern A. Zeeb 
42206b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
42216b4cac81SBjoern A. Zeeb 
4222fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4223fa4e4257SBjoern A. Zeeb 	/*
4224fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
422588665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
422688665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
422788665349SBjoern A. Zeeb 	 * point to try to TX.
422888665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
422988665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4230fa4e4257SBjoern A. Zeeb 	 */
42312372f8ccSBjoern A. Zeeb #if 0
423288665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
42332372f8ccSBjoern A. Zeeb #else
42342372f8ccSBjoern A. Zeeb 	/*
42352372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
42362372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
42372372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
42382372f8ccSBjoern A. Zeeb 	 */
42392372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
42402372f8ccSBjoern A. Zeeb #endif
424188665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
42426b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
424388665349SBjoern A. Zeeb 	/*
424488665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
424588665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
424688665349SBjoern A. Zeeb 	 */
4247fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
42486b4cac81SBjoern A. Zeeb 
42496b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
42506b4cac81SBjoern A. Zeeb 	while (m != NULL) {
42516b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
42526b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
42536b4cac81SBjoern A. Zeeb 	}
42546b4cac81SBjoern A. Zeeb }
42556b4cac81SBjoern A. Zeeb 
42566b4cac81SBjoern A. Zeeb static int
42576b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
42586b4cac81SBjoern A. Zeeb {
42596b4cac81SBjoern A. Zeeb 
42606b4cac81SBjoern A. Zeeb 	/* XXX TODO */
42616b4cac81SBjoern A. Zeeb 	IMPROVE();
42626b4cac81SBjoern A. Zeeb 
42636b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
42646b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
42656b4cac81SBjoern A. Zeeb 
42666b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
42676b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
42686b4cac81SBjoern A. Zeeb }
42696b4cac81SBjoern A. Zeeb 
42709fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
42719fb91463SBjoern A. Zeeb static int
42729fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
42739fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
42749fb91463SBjoern A. Zeeb {
42759fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
42769fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42779fb91463SBjoern A. Zeeb 
42789fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
42799fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
42809fb91463SBjoern A. Zeeb 
4281310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
42829fb91463SBjoern A. Zeeb 
42839fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
42849fb91463SBjoern A. Zeeb }
42859fb91463SBjoern A. Zeeb 
42869fb91463SBjoern A. Zeeb static int
42879fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
42889fb91463SBjoern A. Zeeb {
42899fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
42909fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42919fb91463SBjoern A. Zeeb 
42929fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
42939fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
42949fb91463SBjoern A. Zeeb 
4295310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
42969fb91463SBjoern A. Zeeb 
42979fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
42989fb91463SBjoern A. Zeeb }
42999fb91463SBjoern A. Zeeb 
43009fb91463SBjoern A. Zeeb 
43019fb91463SBjoern A. Zeeb static int
43029fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
43039fb91463SBjoern A. Zeeb {
43049fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43059fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43069fb91463SBjoern A. Zeeb 
43079fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
43089fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
43099fb91463SBjoern A. Zeeb 
4310310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
43119fb91463SBjoern A. Zeeb 
43129fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
43139fb91463SBjoern A. Zeeb }
43149fb91463SBjoern A. Zeeb 
4315310743c4SBjoern A. Zeeb /*
4316310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4317310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4318310743c4SBjoern A. Zeeb  *
4319310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4320310743c4SBjoern A. Zeeb  */
43219fb91463SBjoern A. Zeeb static int
43229fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
43239fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
43249fb91463SBjoern A. Zeeb {
43259fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43269fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4327310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4328310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4329310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4330310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4331310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4332310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4333310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4334310743c4SBjoern A. Zeeb 	int error;
43359fb91463SBjoern A. Zeeb 
43369fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
43379fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4338310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4339310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4340310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4341310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4342310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4343310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
43449fb91463SBjoern A. Zeeb 
4345310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4346310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4347310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4348310743c4SBjoern A. Zeeb 		return (0);
4349310743c4SBjoern A. Zeeb 	}
4350310743c4SBjoern A. Zeeb 
4351310743c4SBjoern A. Zeeb 	params.sta = sta;
4352310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4353310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4354310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4355310743c4SBjoern A. Zeeb 	params.timeout = 0;
4356310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4357310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4358310743c4SBjoern A. Zeeb 	params.amsdu = false;
4359310743c4SBjoern A. Zeeb 
4360310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4361310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4362310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4363310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4364310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4365310743c4SBjoern A. Zeeb 	if (error != 0) {
4366310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4367310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4368310743c4SBjoern A. Zeeb 		return (0);
4369310743c4SBjoern A. Zeeb 	}
43709fb91463SBjoern A. Zeeb 
43719fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
43729fb91463SBjoern A. Zeeb }
43739fb91463SBjoern A. Zeeb 
4374310743c4SBjoern A. Zeeb /*
4375310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4376310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4377310743c4SBjoern A. Zeeb  *
4378310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4379310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4380310743c4SBjoern A. Zeeb  */
43819fb91463SBjoern A. Zeeb static int
43829fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
43839fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
43849fb91463SBjoern A. Zeeb {
43859fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43869fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4387310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4388310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4389310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4390310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4391310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4392310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4393310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4394310743c4SBjoern A. Zeeb 	int error;
43959fb91463SBjoern A. Zeeb 
43969fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
43979fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4398310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4399310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4400310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4401310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4402310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4403310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
44049fb91463SBjoern A. Zeeb 
4405310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4406310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4407310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4408310743c4SBjoern A. Zeeb 		return (0);
4409310743c4SBjoern A. Zeeb 	}
4410310743c4SBjoern A. Zeeb 
4411310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4412310743c4SBjoern A. Zeeb 		params.sta = sta;
4413310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4414310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4415310743c4SBjoern A. Zeeb 		params.timeout = 0;
4416310743c4SBjoern A. Zeeb 		params.ssn = 0;
4417310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4418310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4419310743c4SBjoern A. Zeeb 			params.amsdu = true;
4420310743c4SBjoern A. Zeeb 		else
4421310743c4SBjoern A. Zeeb 			params.amsdu = false;
4422310743c4SBjoern A. Zeeb 	} else {
4423310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4424310743c4SBjoern A. Zeeb 		params.sta = sta;
4425310743c4SBjoern A. Zeeb 		switch (status) {
4426310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4427310743c4SBjoern A. Zeeb 		default:
4428310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4429310743c4SBjoern A. Zeeb 			break;
4430310743c4SBjoern A. Zeeb 		}
4431310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4432310743c4SBjoern A. Zeeb 		params.timeout = 0;
4433310743c4SBjoern A. Zeeb 		params.ssn = 0;
4434310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4435310743c4SBjoern A. Zeeb 		params.amsdu = false;
4436310743c4SBjoern A. Zeeb 	}
4437310743c4SBjoern A. Zeeb 
4438310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4439310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4440310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4441310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4442310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4443310743c4SBjoern A. Zeeb 	if (error != 0) {
4444310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4445310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4446310743c4SBjoern A. Zeeb 		return (0);
4447310743c4SBjoern A. Zeeb 	}
4448310743c4SBjoern A. Zeeb 
4449310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
44509fb91463SBjoern A. Zeeb 
44519fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
44529fb91463SBjoern A. Zeeb }
44539fb91463SBjoern A. Zeeb 
4454310743c4SBjoern A. Zeeb /*
4455310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4456310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4457310743c4SBjoern A. Zeeb  */
44589fb91463SBjoern A. Zeeb static void
44599fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
44609fb91463SBjoern A. Zeeb {
44619fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
44629fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4463310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4464310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4465310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4466310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4467310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4468310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4469310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4470310743c4SBjoern A. Zeeb 	int error;
44719fb91463SBjoern A. Zeeb 
44729fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
44739fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4474310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4475310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4476310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4477310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4478310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4479310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
44809fb91463SBjoern A. Zeeb 
4481310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4482310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4483310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4484310743c4SBjoern A. Zeeb 		goto n80211;
4485310743c4SBjoern A. Zeeb 	}
44869fb91463SBjoern A. Zeeb 
4487310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4488310743c4SBjoern A. Zeeb 	params.sta = sta;
4489310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4490310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4491310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4492310743c4SBjoern A. Zeeb 	params.timeout = 0;
4493310743c4SBjoern A. Zeeb 	params.ssn = 0;
4494310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4495310743c4SBjoern A. Zeeb 	params.amsdu = false;
4496310743c4SBjoern A. Zeeb 
4497310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4498310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4499310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4500310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4501310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4502310743c4SBjoern A. Zeeb 	if (error != 0) {
4503310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4504310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4505310743c4SBjoern A. Zeeb 		goto n80211;
4506310743c4SBjoern A. Zeeb 	}
4507310743c4SBjoern A. Zeeb 
4508310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4509310743c4SBjoern A. Zeeb 
4510310743c4SBjoern A. Zeeb n80211:
45119fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
45129fb91463SBjoern A. Zeeb }
45139fb91463SBjoern A. Zeeb 
45149fb91463SBjoern A. Zeeb static void
45159fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
45169fb91463SBjoern A. Zeeb {
45179fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
45189fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45199fb91463SBjoern A. Zeeb 
45209fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
45219fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
45229fb91463SBjoern A. Zeeb 
45239fb91463SBjoern A. Zeeb 	IMPROVE_HT();
45249fb91463SBjoern A. Zeeb 
45259fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
45269fb91463SBjoern A. Zeeb }
45279fb91463SBjoern A. Zeeb 
45289fb91463SBjoern A. Zeeb static void
45299fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
45309fb91463SBjoern A. Zeeb     int status)
45319fb91463SBjoern A. Zeeb {
45329fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
45339fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45349fb91463SBjoern A. Zeeb 
45359fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
45369fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
45379fb91463SBjoern A. Zeeb 
45389fb91463SBjoern A. Zeeb 	IMPROVE_HT();
45399fb91463SBjoern A. Zeeb 
45409fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
45419fb91463SBjoern A. Zeeb }
45429fb91463SBjoern A. Zeeb 
45439fb91463SBjoern A. Zeeb static int
45449fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
45459fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
45469fb91463SBjoern A. Zeeb {
45479fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
45489fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45499fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
45509fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
45519fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45529fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
45539fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45549fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4555310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
45569fb91463SBjoern A. Zeeb 	int error;
45579fb91463SBjoern A. Zeeb 
45589fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
45599fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
45609fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45619fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
45629fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
45639fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
45649fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
45659fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
45669fb91463SBjoern A. Zeeb 
4567310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
4568310743c4SBjoern A. Zeeb 
4569310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4570310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
4571310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
4572310743c4SBjoern A. Zeeb 		return (-ENXIO);
4573310743c4SBjoern A. Zeeb 	}
4574310743c4SBjoern A. Zeeb 
45759fb91463SBjoern A. Zeeb 	params.sta = sta;
45769fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
45779fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
45789fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
45799fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
45809fb91463SBjoern A. Zeeb 	else
45819fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4582310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
4583310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
45849fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
45859fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
45869fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
45879fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4588310743c4SBjoern A. Zeeb 
4589310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
4590310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
4591310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
4592310743c4SBjoern A. Zeeb 		params.amsdu = true;
4593310743c4SBjoern A. Zeeb 	else
45949fb91463SBjoern A. Zeeb 		params.amsdu = false;
45959fb91463SBjoern A. Zeeb 
4596310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
45979fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4598310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
45999fb91463SBjoern A. Zeeb 	if (error != 0) {
46009fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
46019fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
46029fb91463SBjoern A. Zeeb 		return (error);
46039fb91463SBjoern A. Zeeb 	}
4604310743c4SBjoern A. Zeeb 
4605310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
4606310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
4607310743c4SBjoern A. Zeeb 	}
4608310743c4SBjoern A. Zeeb 
46099fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
46109fb91463SBjoern A. Zeeb 
46119fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
46129fb91463SBjoern A. Zeeb 	return (error);
46139fb91463SBjoern A. Zeeb }
46149fb91463SBjoern A. Zeeb 
46159fb91463SBjoern A. Zeeb static void
46169fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
46179fb91463SBjoern A. Zeeb {
46189fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46199fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46209fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
46219fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
46229fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
46239fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
46249fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46259fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4626310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
46279fb91463SBjoern A. Zeeb 	int error;
46289fb91463SBjoern A. Zeeb 	uint8_t tid;
46299fb91463SBjoern A. Zeeb 
46309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46329fb91463SBjoern A. Zeeb 
46339fb91463SBjoern A. Zeeb 	/*
46349fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
46359fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
46369fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
46379fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
46389fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
46399fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
46409fb91463SBjoern A. Zeeb 	 * track some state itself.
46419fb91463SBjoern A. Zeeb 	 */
46429fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
46439fb91463SBjoern A. Zeeb 		goto net80211_only;
46449fb91463SBjoern A. Zeeb 
46459fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
46469fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
46479fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
46489fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
46499fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
46509fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
46519fb91463SBjoern A. Zeeb 
46529fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
46539fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
46549fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
46559fb91463SBjoern A. Zeeb 			break;
46569fb91463SBjoern A. Zeeb 	}
46579fb91463SBjoern A. Zeeb 
46589fb91463SBjoern A. Zeeb 	params.sta = sta;
46599fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
46609fb91463SBjoern A. Zeeb 	params.buf_size = 0;
46619fb91463SBjoern A. Zeeb 	params.timeout = 0;
46629fb91463SBjoern A. Zeeb 	params.ssn = 0;
46639fb91463SBjoern A. Zeeb 	params.tid = tid;
46649fb91463SBjoern A. Zeeb 	params.amsdu = false;
46659fb91463SBjoern A. Zeeb 
4666310743c4SBjoern A. Zeeb 	// IEEE80211_UNLOCK(ic);
4667310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
46689fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4669310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4670310743c4SBjoern A. Zeeb 	// IEEE80211_LOCK(ic);
46719fb91463SBjoern A. Zeeb 	if (error != 0)
46729fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
46739fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
46749fb91463SBjoern A. Zeeb 
46759fb91463SBjoern A. Zeeb net80211_only:
46769fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
46779fb91463SBjoern A. Zeeb }
46789fb91463SBjoern A. Zeeb #endif
46799fb91463SBjoern A. Zeeb 
46809fb91463SBjoern A. Zeeb static void
46819fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
46829fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
46839fb91463SBjoern A. Zeeb {
46849fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46859fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
46869fb91463SBjoern A. Zeeb 
46879fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
46889fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
46899fb91463SBjoern A. Zeeb 		return;
46909fb91463SBjoern A. Zeeb 
46919fb91463SBjoern A. Zeeb 	switch (band) {
46929fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
46939fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
46949fb91463SBjoern A. Zeeb 		break;
46959fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
46969fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
46979fb91463SBjoern A. Zeeb 		break;
46989fb91463SBjoern A. Zeeb 	default:
46999fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
47009fb91463SBjoern A. Zeeb 		return;
47019fb91463SBjoern A. Zeeb 	}
47029fb91463SBjoern A. Zeeb 
47039fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
47049fb91463SBjoern A. Zeeb 
47059fb91463SBjoern A. Zeeb 	/*
47069fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
47079fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
47089fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
47099fb91463SBjoern A. Zeeb 	 */
47109fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
47119fb91463SBjoern A. Zeeb 
47129fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
47139fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
47149fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
47159fb91463SBjoern A. Zeeb #ifdef __notyet__
47169fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
47179fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
47189fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
47199fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
47209fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
47219fb91463SBjoern A. Zeeb #endif
47229fb91463SBjoern A. Zeeb 
47239fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
47249fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
47259fb91463SBjoern A. Zeeb 
47269fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
47279fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
47289fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
47299fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
47309fb91463SBjoern A. Zeeb #endif
47319fb91463SBjoern A. Zeeb }
47329fb91463SBjoern A. Zeeb 
47336b4cac81SBjoern A. Zeeb static void
47346b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
47356b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
47366b4cac81SBjoern A. Zeeb {
47376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
47396b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
47406b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
47416b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
47426b4cac81SBjoern A. Zeeb 
47436b4cac81SBjoern A. Zeeb 	/* Channels */
47446b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
47456b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
47466b4cac81SBjoern A. Zeeb 
47476b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
47486b4cac81SBjoern A. Zeeb 	nchans = 0;
47496b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
47506b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
47516b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
47526b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
47536b4cac81SBjoern A. Zeeb 		chan_flags = 0;
47546b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
47556b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
47569fb91463SBjoern A. Zeeb 
47579fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
47586b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
47599fb91463SBjoern A. Zeeb 
47609fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
47619fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
47626b4cac81SBjoern A. Zeeb 
47636b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4764cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
47656b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
47666b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
47676b4cac81SBjoern A. Zeeb 
47686b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
47693f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
47703f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
47716b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
47726b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
47736b4cac81SBjoern A. Zeeb 				continue;
47746b4cac81SBjoern A. Zeeb 			}
47756b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
47766b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
47776b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
47786b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
47796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
47806b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
47816b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
47826b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
47836b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
47846b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
47856b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
47866b4cac81SBjoern A. Zeeb 
47876b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
47886b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
47896b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
47905856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4791cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4792cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
47933f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
47943f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
47956b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
47966b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
47976b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4798cee56e77SBjoern A. Zeeb 			if (error != 0)
47996b4cac81SBjoern A. Zeeb 				break;
48006b4cac81SBjoern A. Zeeb 		}
48016b4cac81SBjoern A. Zeeb 	}
48026b4cac81SBjoern A. Zeeb 
48036b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
48046b4cac81SBjoern A. Zeeb 	nchans = 0;
48056b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
48066b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
48076b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
48086b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
48096b4cac81SBjoern A. Zeeb 		chan_flags = 0;
48106b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
48119fb91463SBjoern A. Zeeb 
48129fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
48139fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
48149fb91463SBjoern A. Zeeb 
48159fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
48166b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
48176b4cac81SBjoern A. Zeeb 
48186b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4819562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
48206b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
4821ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
4822ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
48236b4cac81SBjoern A. Zeeb 
48246b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
48256b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
48266b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4827562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
48286b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
48296b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4830562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
48316b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
48326b4cac81SBjoern A. Zeeb 		}
48336b4cac81SBjoern A. Zeeb #endif
48346b4cac81SBjoern A. Zeeb 
48356b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4836cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
48376b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
48386b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
48396b4cac81SBjoern A. Zeeb 
48406b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
48413f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
48423f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
48436b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
48446b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
48456b4cac81SBjoern A. Zeeb 				continue;
48466b4cac81SBjoern A. Zeeb 			}
48476b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
48486b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
48496b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
48506b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
48516b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
48526b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
48536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
48546b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
48556b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
48566b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
48576b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
48586b4cac81SBjoern A. Zeeb 
48596b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
48606b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
48616b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
48625856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4863cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4864cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
48653f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
48663f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
48676b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
48686b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
48696b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4870cee56e77SBjoern A. Zeeb 			if (error != 0)
48716b4cac81SBjoern A. Zeeb 				break;
48726b4cac81SBjoern A. Zeeb 		}
48736b4cac81SBjoern A. Zeeb 	}
48746b4cac81SBjoern A. Zeeb }
48756b4cac81SBjoern A. Zeeb 
48766b4cac81SBjoern A. Zeeb static void *
48776b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
48786b4cac81SBjoern A. Zeeb {
48796b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48806b4cac81SBjoern A. Zeeb 
48816b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
48826b4cac81SBjoern A. Zeeb 
48836b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
48846b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
48856b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
48866b4cac81SBjoern A. Zeeb 
48876b4cac81SBjoern A. Zeeb 	return (ic);
48886b4cac81SBjoern A. Zeeb }
48896b4cac81SBjoern A. Zeeb 
48906b4cac81SBjoern A. Zeeb struct ieee80211_hw *
48916b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
48926b4cac81SBjoern A. Zeeb {
48936b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
48946b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48956b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4896a5ae63edSBjoern A. Zeeb 	int ac;
48976b4cac81SBjoern A. Zeeb 
48986b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
48996b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
49006b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
49016b4cac81SBjoern A. Zeeb 		return (NULL);
49026b4cac81SBjoern A. Zeeb 
49036b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
49046b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4905652e22d3SBjoern A. Zeeb 
49068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
49078ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4908eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
49098891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
49106b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4911a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4912a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4913a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4914a5ae63edSBjoern A. Zeeb 	}
49156b4cac81SBjoern A. Zeeb 
4916759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
4917759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4918759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4919759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4920759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
4921759a996dSBjoern A. Zeeb 
49226b4cac81SBjoern A. Zeeb 	/*
49236b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
49246b4cac81SBjoern A. Zeeb 	 * not initialized.
49256b4cac81SBjoern A. Zeeb 	 */
49266b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
49276b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4928086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
49296b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
49306b4cac81SBjoern A. Zeeb 
49316b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
49326b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
49336b4cac81SBjoern A. Zeeb 
49346b4cac81SBjoern A. Zeeb 	IMPROVE();
49356b4cac81SBjoern A. Zeeb 
49366b4cac81SBjoern A. Zeeb 	return (hw);
49376b4cac81SBjoern A. Zeeb }
49386b4cac81SBjoern A. Zeeb 
49396b4cac81SBjoern A. Zeeb void
49406b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
49416b4cac81SBjoern A. Zeeb {
49426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4943759a996dSBjoern A. Zeeb 	struct mbuf *m;
49446b4cac81SBjoern A. Zeeb 
49456b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
49466b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
49476b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
49486b4cac81SBjoern A. Zeeb 
4949759a996dSBjoern A. Zeeb 	/*
4950759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
4951759a996dSBjoern A. Zeeb 	 */
4952759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4953759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
4954759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4955759a996dSBjoern A. Zeeb 
4956759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4957759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4958759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4959759a996dSBjoern A. Zeeb 
4960759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4961759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
4962759a996dSBjoern A. Zeeb 	while (m != NULL) {
4963759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
4964759a996dSBjoern A. Zeeb 
4965759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4966759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
4967759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
4968759a996dSBjoern A. Zeeb 
4969759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4970759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
4971759a996dSBjoern A. Zeeb 		}
4972759a996dSBjoern A. Zeeb 		m_freem(m);
4973759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
4974759a996dSBjoern A. Zeeb 	}
4975759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4976759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4977759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4978759a996dSBjoern A. Zeeb 
49796b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4980eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
49818ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4982eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4983eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
49846b4cac81SBjoern A. Zeeb 	IMPROVE();
49856b4cac81SBjoern A. Zeeb }
49866b4cac81SBjoern A. Zeeb 
49876b4cac81SBjoern A. Zeeb void
49886b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
49896b4cac81SBjoern A. Zeeb {
49906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49916b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49926b4cac81SBjoern A. Zeeb 
49936b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
49946b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
49956b4cac81SBjoern A. Zeeb 
49966b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
49976b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
49986b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
49996b4cac81SBjoern A. Zeeb 
50006b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
50016b4cac81SBjoern A. Zeeb }
50026b4cac81SBjoern A. Zeeb 
50036b4cac81SBjoern A. Zeeb struct ieee80211_hw *
50046b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
50056b4cac81SBjoern A. Zeeb {
50066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50076b4cac81SBjoern A. Zeeb 
50086b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
50096b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
50106b4cac81SBjoern A. Zeeb }
50116b4cac81SBjoern A. Zeeb 
50126b4cac81SBjoern A. Zeeb static void
50136b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
50146b4cac81SBjoern A. Zeeb {
50156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
50166b4cac81SBjoern A. Zeeb 
50176b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
50186b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
50196b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
50206b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
50216b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
50226b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
50236b4cac81SBjoern A. Zeeb }
50246b4cac81SBjoern A. Zeeb 
50252e183d99SBjoern A. Zeeb int
50266b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
50276b4cac81SBjoern A. Zeeb {
50286b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
50296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5030c5b96b3eSBjoern A. Zeeb 	int band, i;
50316b4cac81SBjoern A. Zeeb 
50326b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
50336b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
50346b4cac81SBjoern A. Zeeb 
5035652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5036652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5037652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5038652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5039652e22d3SBjoern A. Zeeb 
50406b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
50416b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
50426b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
50436b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
50446b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
50456b4cac81SBjoern A. Zeeb 		/* We take the first one. */
50466b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
50476b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
50486b4cac81SBjoern A. Zeeb 	} else {
50496b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
50506b4cac81SBjoern A. Zeeb 	}
50516b4cac81SBjoern A. Zeeb 
50523d09d310SBjoern A. Zeeb #ifdef __not_yet__
50533d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
50546b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
50553d09d310SBjoern A. Zeeb #endif
50566b4cac81SBjoern A. Zeeb 
50576b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
50586b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
50596b4cac81SBjoern A. Zeeb 
50606b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
50616b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
50626b4cac81SBjoern A. Zeeb 	ic->ic_caps =
50636b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
50646b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
50656b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
50664a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
50676b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
50684a67f1dfSBjoern A. Zeeb #endif
50696b4cac81SBjoern A. Zeeb #if 0
50706b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
50716b4cac81SBjoern A. Zeeb #endif
50726b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
50736b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
50746b4cac81SBjoern A. Zeeb 	    ;
50756b4cac81SBjoern A. Zeeb #if 0
50766b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
50776b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
50786b4cac81SBjoern A. Zeeb #endif
5079cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5080cc4e78d5SBjoern A. Zeeb 		/*
5081cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5082cc4e78d5SBjoern A. Zeeb 		 *
5083cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5084cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5085cc4e78d5SBjoern A. Zeeb 		 * the flag.
5086cc4e78d5SBjoern A. Zeeb 		 */
50876b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5088a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
50896b4cac81SBjoern A. Zeeb 	}
50906b4cac81SBjoern A. Zeeb 
5091527687a9SBjoern A. Zeeb 	/*
5092527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5093527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5094527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5095527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5096527687a9SBjoern A. Zeeb 	 */
5097527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5098527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5099527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5100527687a9SBjoern A. Zeeb 
5101527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5102527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5103527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5104527687a9SBjoern A. Zeeb 		}
5105527687a9SBjoern A. Zeeb 	}
5106527687a9SBjoern A. Zeeb 
51076b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5108b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
51096b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
51106b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
51116b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
51126b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
51136b4cac81SBjoern A. Zeeb 	}
51146b4cac81SBjoern A. Zeeb #endif
51156b4cac81SBjoern A. Zeeb 
51166b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
51176b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
51186b4cac81SBjoern A. Zeeb 
51196b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
51206b4cac81SBjoern A. Zeeb 
51216b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
51226b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
51236b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
51246b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
51256b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
51266b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
51276b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
51286b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
51296b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
51306b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
51316b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
51326b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
51336b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
51346b4cac81SBjoern A. Zeeb 
5135a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5136a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5137a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5138a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5139a486fbbdSBjoern A. Zeeb 
51406b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
51416b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
51426b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
51436b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
51446b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
51456b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
51466b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
51476b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
51486b4cac81SBjoern A. Zeeb 
51499fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
515086bc7259SBjoern A. Zeeb 	/*
515186bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
515286bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
515386bc7259SBjoern A. Zeeb 	 */
515486bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
51559fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
51569fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
51579fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
51589fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
51599fb91463SBjoern A. Zeeb 
51609fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
51619fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
51629fb91463SBjoern A. Zeeb 
51639fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
51649fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
51659fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
51669fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
51679fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
51689fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
51699fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
51709fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
51719fb91463SBjoern A. Zeeb 
51729fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
51739fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
51749fb91463SBjoern A. Zeeb 
51759fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
51769fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
51779fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
51789fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
517986bc7259SBjoern A. Zeeb 	}
51809fb91463SBjoern A. Zeeb #endif
51819fb91463SBjoern A. Zeeb 
51826b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
51836b4cac81SBjoern A. Zeeb 
5184c5b96b3eSBjoern A. Zeeb 	/*
5185c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5186c5b96b3eSBjoern A. Zeeb 	 * expect one.
5187d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5188d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5189c5b96b3eSBjoern A. Zeeb 	 */
5190d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5191f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5192c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5193c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5194c5b96b3eSBjoern A. Zeeb 
5195c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5196c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5197c5b96b3eSBjoern A. Zeeb 			continue;
5198c5b96b3eSBjoern A. Zeeb 
5199d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5200d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5201d9945d78SBjoern A. Zeeb 
5202f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5203f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5204f454a4a1SBjoern A. Zeeb 			continue;
5205f454a4a1SBjoern A. Zeeb 
5206c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5207c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5208c5b96b3eSBjoern A. Zeeb 
5209c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5210c5b96b3eSBjoern A. Zeeb 				continue;
5211c5b96b3eSBjoern A. Zeeb 
52124a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
52139fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
52149fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
52159fb91463SBjoern A. Zeeb #endif
5216c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5217c5b96b3eSBjoern A. Zeeb 			break;
5218c5b96b3eSBjoern A. Zeeb 		}
5219c5b96b3eSBjoern A. Zeeb 	}
5220c5b96b3eSBjoern A. Zeeb 
5221d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5222d9945d78SBjoern A. Zeeb 
5223d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5224d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5225d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5226d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5227d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5228d9945d78SBjoern A. Zeeb 	}
5229d9945d78SBjoern A. Zeeb 
5230d9945d78SBjoern A. Zeeb 	/*
5231d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5232d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5233d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5234d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5235d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5236d9945d78SBjoern A. Zeeb 	 */
5237d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5238d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5239d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
524078ca45dfSBjoern A. Zeeb 
524178ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5242d9945d78SBjoern A. Zeeb 		/*
524378ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
524478ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
524578ca45dfSBjoern A. Zeeb 		 * space in.
5246d9945d78SBjoern A. Zeeb 		 */
5247d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
524878ca45dfSBjoern A. Zeeb 	}
5249d9945d78SBjoern A. Zeeb 
52509fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
52519fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
52529fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
52539fb91463SBjoern A. Zeeb #endif
52549fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
52559fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
52569fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
52579fb91463SBjoern A. Zeeb #endif
52589fb91463SBjoern A. Zeeb 
5259d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
526078ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
526178ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
526278ca45dfSBjoern A. Zeeb 			goto err;
5263d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
526478ca45dfSBjoern A. Zeeb 	}
5265d9945d78SBjoern A. Zeeb 
52666b4cac81SBjoern A. Zeeb 	if (bootverbose)
52676b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
52682e183d99SBjoern A. Zeeb 
52692e183d99SBjoern A. Zeeb 	return (0);
527078ca45dfSBjoern A. Zeeb err:
527178ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
527278ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
52736b4cac81SBjoern A. Zeeb }
52746b4cac81SBjoern A. Zeeb 
52756b4cac81SBjoern A. Zeeb void
52766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
52776b4cac81SBjoern A. Zeeb {
52786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52796b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
52806b4cac81SBjoern A. Zeeb 
52816b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
52826b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
52836b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
52846b4cac81SBjoern A. Zeeb }
52856b4cac81SBjoern A. Zeeb 
52866b4cac81SBjoern A. Zeeb void
52876b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
52886b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
52896b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
52906b4cac81SBjoern A. Zeeb     void *arg)
52916b4cac81SBjoern A. Zeeb {
52926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52936b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
52946b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
529561a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
52966b4cac81SBjoern A. Zeeb 
52976b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
52986b4cac81SBjoern A. Zeeb 
52996b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
53006b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
530161a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5302adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
53036b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
53046b4cac81SBjoern A. Zeeb 		    __func__, flags);
53056b4cac81SBjoern A. Zeeb 	}
53066b4cac81SBjoern A. Zeeb 
5307adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
53086b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
530961a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
53106b4cac81SBjoern A. Zeeb 
53116b4cac81SBjoern A. Zeeb 	if (atomic)
53128891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
53136b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
53146b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
53156b4cac81SBjoern A. Zeeb 
53166b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
53176b4cac81SBjoern A. Zeeb 
53186b4cac81SBjoern A. Zeeb 		/*
53196b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
53206b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
53216b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
53226b4cac81SBjoern A. Zeeb 		 * driver does not know about.
53236b4cac81SBjoern A. Zeeb 		 */
53246b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
53256b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
53266b4cac81SBjoern A. Zeeb 			continue;
53276b4cac81SBjoern A. Zeeb 
53286b4cac81SBjoern A. Zeeb 		/*
532961a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
533061a68e50SBjoern A. Zeeb 		 * if we haven't yet.
533161a68e50SBjoern A. Zeeb 		 */
533261a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
533361a68e50SBjoern A. Zeeb 			continue;
533461a68e50SBjoern A. Zeeb 
533561a68e50SBjoern A. Zeeb 		/*
53366b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
53376b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
53386b4cac81SBjoern A. Zeeb 		 */
53396b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
53406b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
53416b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
53426b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
53436b4cac81SBjoern A. Zeeb 	}
53446b4cac81SBjoern A. Zeeb 	if (atomic)
53458891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
53466b4cac81SBjoern A. Zeeb }
53476b4cac81SBjoern A. Zeeb 
53486b4cac81SBjoern A. Zeeb void
53496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
53506b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
53516b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
53526b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
53536b4cac81SBjoern A. Zeeb     void *arg)
53546b4cac81SBjoern A. Zeeb {
53556b4cac81SBjoern A. Zeeb 
53566b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
53576b4cac81SBjoern A. Zeeb }
53586b4cac81SBjoern A. Zeeb 
53596b4cac81SBjoern A. Zeeb void
53606b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
53616b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
53626b4cac81SBjoern A. Zeeb 	void *),
53636b4cac81SBjoern A. Zeeb     void *arg)
53646b4cac81SBjoern A. Zeeb {
5365c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5366c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5367c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5368c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
53696b4cac81SBjoern A. Zeeb 
5370c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5371c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5372c5e25798SBjoern A. Zeeb 
5373c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5374c5e25798SBjoern A. Zeeb 
5375c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5376c5e25798SBjoern A. Zeeb 
5377c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5378c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5379c5e25798SBjoern A. Zeeb 
5380c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5381c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5382c5e25798SBjoern A. Zeeb 			continue;
5383c5e25798SBjoern A. Zeeb 
5384c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5385c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5386c5e25798SBjoern A. Zeeb 			continue;
5387c5e25798SBjoern A. Zeeb 
5388d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5389c5e25798SBjoern A. Zeeb 	}
5390c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
53916b4cac81SBjoern A. Zeeb }
53926b4cac81SBjoern A. Zeeb 
53936b4cac81SBjoern A. Zeeb void
53946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
53956b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
53966b4cac81SBjoern A. Zeeb {
53976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53986b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53996b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
54006b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
54016b4cac81SBjoern A. Zeeb 
54026b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
54036b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
54046b4cac81SBjoern A. Zeeb 
54056b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54066b4cac81SBjoern A. Zeeb 
54078891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
54086b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
54096b4cac81SBjoern A. Zeeb 
54106b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
54116b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
54126b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
54136b4cac81SBjoern A. Zeeb 				continue;
54146b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
54156b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
54166b4cac81SBjoern A. Zeeb 		}
54176b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
54186b4cac81SBjoern A. Zeeb 	}
54198891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
54206b4cac81SBjoern A. Zeeb }
54216b4cac81SBjoern A. Zeeb 
5422673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5423673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5424673d62fcSBjoern A. Zeeb {
5425673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5426673d62fcSBjoern A. Zeeb 
5427673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5428673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5429673d62fcSBjoern A. Zeeb 	return (regd);
5430673d62fcSBjoern A. Zeeb }
5431673d62fcSBjoern A. Zeeb 
54326b4cac81SBjoern A. Zeeb int
54336b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
54346b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
54356b4cac81SBjoern A. Zeeb {
54366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54376b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54386b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
54396b4cac81SBjoern A. Zeeb 
54406b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54416b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54426b4cac81SBjoern A. Zeeb 
54436b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
54446b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
54456b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
54466b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
54476b4cac81SBjoern A. Zeeb 	}
54486b4cac81SBjoern A. Zeeb 
54496b4cac81SBjoern A. Zeeb 	TODO();
54506b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
54516b4cac81SBjoern A. Zeeb 
54526b4cac81SBjoern A. Zeeb 	return (0);
54536b4cac81SBjoern A. Zeeb }
54546b4cac81SBjoern A. Zeeb 
54556b4cac81SBjoern A. Zeeb void
54566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
54576b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
54586b4cac81SBjoern A. Zeeb {
54596b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54606b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54616b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
54626b4cac81SBjoern A. Zeeb 
54636b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
54646b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54656b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
54666b4cac81SBjoern A. Zeeb 
54676b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
54686b4cac81SBjoern A. Zeeb 
54698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
54706b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
54716b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5472a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
54736b4cac81SBjoern A. Zeeb 	wakeup(lhw);
54748ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
54756b4cac81SBjoern A. Zeeb 
54766b4cac81SBjoern A. Zeeb 	return;
54776b4cac81SBjoern A. Zeeb }
54786b4cac81SBjoern A. Zeeb 
5479759a996dSBjoern A. Zeeb static void
5480759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5481759a996dSBjoern A. Zeeb {
5482759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5483759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5484759a996dSBjoern A. Zeeb 	int ok;
5485759a996dSBjoern A. Zeeb 
5486759a996dSBjoern A. Zeeb 	ni = NULL;
5487759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5488759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5489759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5490759a996dSBjoern A. Zeeb 
5491759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5492759a996dSBjoern A. Zeeb 		ni = rxni->ni;
5493759a996dSBjoern A. Zeeb 	}
5494759a996dSBjoern A. Zeeb 
5495759a996dSBjoern A. Zeeb 	if (ni != NULL) {
5496759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
5497759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
5498759a996dSBjoern A. Zeeb 		if (ok < 0)
5499759a996dSBjoern A. Zeeb 			m_freem(m);
5500759a996dSBjoern A. Zeeb 	} else {
5501759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5502759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
5503759a996dSBjoern A. Zeeb 	}
5504759a996dSBjoern A. Zeeb 
5505759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5506759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5507bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5508759a996dSBjoern A. Zeeb #endif
5509759a996dSBjoern A. Zeeb }
5510759a996dSBjoern A. Zeeb 
5511759a996dSBjoern A. Zeeb static void
5512759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5513759a996dSBjoern A. Zeeb {
5514759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5515759a996dSBjoern A. Zeeb 	struct mbufq mq;
5516759a996dSBjoern A. Zeeb 	struct mbuf *m;
5517759a996dSBjoern A. Zeeb 
5518759a996dSBjoern A. Zeeb 	lhw = ctx;
5519759a996dSBjoern A. Zeeb 
5520759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5521759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5522bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5523bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5524759a996dSBjoern A. Zeeb #endif
5525759a996dSBjoern A. Zeeb 
5526759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
5527759a996dSBjoern A. Zeeb 
5528759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5529759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
5530759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5531759a996dSBjoern A. Zeeb 
5532759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
5533759a996dSBjoern A. Zeeb 	while (m != NULL) {
5534759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5535759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
5536759a996dSBjoern A. Zeeb 	}
5537759a996dSBjoern A. Zeeb }
5538759a996dSBjoern A. Zeeb 
553949010ba7SBjoern A. Zeeb static void
554049010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
554149010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
554249010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
554349010ba7SBjoern A. Zeeb     uint8_t *rssip)
554449010ba7SBjoern A. Zeeb {
554549010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
554649010ba7SBjoern A. Zeeb 	int i;
554749010ba7SBjoern A. Zeeb 	uint8_t rssi;
554849010ba7SBjoern A. Zeeb 
554949010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
555049010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
555149010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
555249010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
555349010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
555449010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
555549010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
555649010ba7SBjoern A. Zeeb 	else
555749010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
555849010ba7SBjoern A. Zeeb 	/*
555949010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
556049010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
556149010ba7SBjoern A. Zeeb 	 */
556249010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
556349010ba7SBjoern A. Zeeb 	if (rssip != NULL)
556449010ba7SBjoern A. Zeeb 		*rssip = rssi;
556549010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
556649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
556749010ba7SBjoern A. Zeeb 	rx_stats->c_band =
556849010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
556949010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
557049010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
557149010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
557249010ba7SBjoern A. Zeeb 
557349010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
557449010ba7SBjoern A. Zeeb 
557549010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
557649010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
557749010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
557849010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
557949010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
558049010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
558149010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
558249010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
558349010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
558449010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
558549010ba7SBjoern A. Zeeb 	}
558649010ba7SBjoern A. Zeeb 
558749010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
558849010ba7SBjoern A. Zeeb 		int cc;
558949010ba7SBjoern A. Zeeb 		int8_t crssi;
559049010ba7SBjoern A. Zeeb 
559149010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
559249010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
559349010ba7SBjoern A. Zeeb 
559449010ba7SBjoern A. Zeeb 		cc = 0;
559549010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
559649010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
559749010ba7SBjoern A. Zeeb 				continue;
559849010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
559949010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
560049010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
560149010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
560249010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
560349010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
560449010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
560549010ba7SBjoern A. Zeeb 			cc++;
560649010ba7SBjoern A. Zeeb 		}
560749010ba7SBjoern A. Zeeb 		if (cc > 0)
560849010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
560949010ba7SBjoern A. Zeeb 	}
561049010ba7SBjoern A. Zeeb 
561149010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
561249010ba7SBjoern A. Zeeb 
561349010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
561449010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
561549010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
561649010ba7SBjoern A. Zeeb 		if (supband != NULL)
561749010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
561849010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
561949010ba7SBjoern A. Zeeb 		break;
562049010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
562149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
562249010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
562349010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
562449010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
562549010ba7SBjoern A. Zeeb 		break;
562649010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
562749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
562849010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
562949010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
563049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
563149010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
563249010ba7SBjoern A. Zeeb 		break;
563349010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
563449010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
563549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
563649010ba7SBjoern A. Zeeb 		break;
563749010ba7SBjoern A. Zeeb 	}
563849010ba7SBjoern A. Zeeb 
563949010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
564049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
564149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
564249010ba7SBjoern A. Zeeb 		break;
564349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
564449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
564549010ba7SBjoern A. Zeeb 		break;
564649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
564749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
564849010ba7SBjoern A. Zeeb 		break;
564949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
565049010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
565149010ba7SBjoern A. Zeeb 		break;
565249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
565349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
565449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
565549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
565649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
565749010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
565849010ba7SBjoern A. Zeeb 		break;
565949010ba7SBjoern A. Zeeb 	}
566049010ba7SBjoern A. Zeeb 
566149010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
566249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
566349010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
566449010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
566549010ba7SBjoern A. Zeeb 
566649010ba7SBjoern A. Zeeb 	/*
566749010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
566849010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
566949010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
567049010ba7SBjoern A. Zeeb 	 */
567149010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
567249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
567349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
567449010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
567549010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
567649010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
567749010ba7SBjoern A. Zeeb 	}
567849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
567949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
568049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
568149010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
568249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
568349010ba7SBjoern A. Zeeb 	}
568449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
568549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
568649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
568749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
568849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
568949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
569049010ba7SBjoern A. Zeeb #endif
569149010ba7SBjoern A. Zeeb }
569249010ba7SBjoern A. Zeeb 
5693e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
56946b4cac81SBjoern A. Zeeb void
56956b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
5696e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
5697e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
56986b4cac81SBjoern A. Zeeb {
56996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57006b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57016b4cac81SBjoern A. Zeeb 	struct mbuf *m;
57026b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
57036b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
57046b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
57056b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
57066b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
57076b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
57086b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
57099d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
571049010ba7SBjoern A. Zeeb 	uint8_t rssi;
5711c0cadd99SBjoern A. Zeeb 	bool is_beacon;
57126b4cac81SBjoern A. Zeeb 
57136b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
57146b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
57156b4cac81SBjoern A. Zeeb 		IMPROVE();
57166b4cac81SBjoern A. Zeeb 		goto err;
57176b4cac81SBjoern A. Zeeb 	}
57186b4cac81SBjoern A. Zeeb 
57196b4cac81SBjoern A. Zeeb 	/*
57206b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
57216b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
57226b4cac81SBjoern A. Zeeb 	 */
57236b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
57246b4cac81SBjoern A. Zeeb 	if (m == NULL)
57256b4cac81SBjoern A. Zeeb 		goto err;
57266b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
57276b4cac81SBjoern A. Zeeb 
57286b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
57296b4cac81SBjoern A. Zeeb 	offset = m->m_len;
57306b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
57316b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
57326b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
57336b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
57346b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
57356b4cac81SBjoern A. Zeeb 	}
57366b4cac81SBjoern A. Zeeb 
57376b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
57386b4cac81SBjoern A. Zeeb 
57396b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
5740c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
5741c0cadd99SBjoern A. Zeeb 
5742c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
57439d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
57446b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
57456b4cac81SBjoern A. Zeeb 
57469d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
57476b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5748c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
57496b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
57506b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
57516b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
5752c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
57536b4cac81SBjoern A. Zeeb 
57549d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
57556b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
57566b4cac81SBjoern A. Zeeb 
57576b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
57589d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5759f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
576060970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
57616b4cac81SBjoern A. Zeeb 			__func__,
5762c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
5763c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
57646b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
5765f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
57666b4cac81SBjoern A. Zeeb 			rx_status->freq,
57676b4cac81SBjoern A. Zeeb 			rx_status->bw,
57686b4cac81SBjoern A. Zeeb 			rx_status->encoding,
57696b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
57706b4cac81SBjoern A. Zeeb 			rx_status->band,
57716b4cac81SBjoern A. Zeeb 			rx_status->chains,
57726b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
57736b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
57746b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
577560970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
57766b4cac81SBjoern A. Zeeb 			rx_status->signal,
57776b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
57786b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
57796b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
57806b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
57816b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
57826b4cac81SBjoern A. Zeeb 			rx_status->nss,
57836b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
57846b4cac81SBjoern A. Zeeb no_trace_beacons:
57856b4cac81SBjoern A. Zeeb #endif
57866b4cac81SBjoern A. Zeeb 
578749010ba7SBjoern A. Zeeb 	rssi = 0;
578849010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
57896b4cac81SBjoern A. Zeeb 
57906b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57916b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57926b4cac81SBjoern A. Zeeb 
57936b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
57946b4cac81SBjoern A. Zeeb 	if (ok == 0) {
5795dbc06dd9SBjoern A. Zeeb 		m_freem(m);
57966b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
57976b4cac81SBjoern A. Zeeb 		goto err;
57986b4cac81SBjoern A. Zeeb 	}
57996b4cac81SBjoern A. Zeeb 
580058246901SBjoern A. Zeeb 	lsta = NULL;
58016b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
58026b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
58036b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
58046b4cac81SBjoern A. Zeeb 	} else {
5805c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
5806c8dafefaSBjoern A. Zeeb 
58076b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
58086b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
58096b4cac81SBjoern A. Zeeb 		if (ni != NULL)
58106b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
58116b4cac81SBjoern A. Zeeb 	}
58126b4cac81SBjoern A. Zeeb 
58136b4cac81SBjoern A. Zeeb 	if (ni != NULL)
58146b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
58156b4cac81SBjoern A. Zeeb 	else
58166b4cac81SBjoern A. Zeeb 		/*
58176b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
58186b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
58196b4cac81SBjoern A. Zeeb 		 */
58206b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
58216b4cac81SBjoern A. Zeeb 
58229d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
58239d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5824bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5825c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5826c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
58279d9ba2b7SBjoern A. Zeeb #endif
58286b4cac81SBjoern A. Zeeb 
5829c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
5830c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
5831c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5832c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
5833c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
5834c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
5835c8dafefaSBjoern A. Zeeb 
5836c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
5837c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5838c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
5839c8dafefaSBjoern A. Zeeb 
5840c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
5841c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5842c8dafefaSBjoern A. Zeeb 
5843c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
5844c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
5845c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5846c8dafefaSBjoern A. Zeeb 		/*
5847c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
5848c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5849c8dafefaSBjoern A. Zeeb 		 */
5850c8dafefaSBjoern A. Zeeb skip_device_ts:
58519fb91463SBjoern A. Zeeb 		;
58529fb91463SBjoern A. Zeeb 	}
5853c8dafefaSBjoern A. Zeeb 
58546b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
58556b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
58566b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
58576b4cac81SBjoern A. Zeeb 
58586b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
58596b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
58606b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
58616b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
58626b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
58636b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
58646b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
58656b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
58666b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
58676b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
58686b4cac81SBjoern A. Zeeb #endif
58696b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
58706b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
58716b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
58726b4cac81SBjoern A. Zeeb 		IMPROVE();
58736b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
58746b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
58756b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
58766b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5877170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
58786b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
58796b4cac81SBjoern A. Zeeb 	}
58806b4cac81SBjoern A. Zeeb 
58816b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
58826b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
58836b4cac81SBjoern A. Zeeb 
5884e30e05d3SBjoern A. Zeeb #if 0
5885e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
5886e30e05d3SBjoern A. Zeeb 		/*
5887e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
5888e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5889e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
5890e30e05d3SBjoern A. Zeeb 		*/
5891e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5892e30e05d3SBjoern A. Zeeb 	}
5893e30e05d3SBjoern A. Zeeb #endif
5894e30e05d3SBjoern A. Zeeb 
5895759a996dSBjoern A. Zeeb 	/*
5896759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
5897759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
5898759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
5899759a996dSBjoern A. Zeeb 	 */
59006b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
5901759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5902759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5903759a996dSBjoern A. Zeeb 
5904759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5905759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5906759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5907759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5908759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
5909759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
5910759a996dSBjoern A. Zeeb 		}
59116b4cac81SBjoern A. Zeeb 	}
59126b4cac81SBjoern A. Zeeb 
5913759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5914759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
5915759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5916759a996dSBjoern A. Zeeb 		m_freem(m);
5917759a996dSBjoern A. Zeeb 		goto err;
5918759a996dSBjoern A. Zeeb 	}
5919759a996dSBjoern A. Zeeb 
5920759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
5921759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5922759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
59236b4cac81SBjoern A. Zeeb 
59246b4cac81SBjoern A. Zeeb 	IMPROVE();
59256b4cac81SBjoern A. Zeeb 
59266b4cac81SBjoern A. Zeeb err:
59276b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
59286b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
59296b4cac81SBjoern A. Zeeb }
59306b4cac81SBjoern A. Zeeb 
59316b4cac81SBjoern A. Zeeb uint8_t
5932ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
59336b4cac81SBjoern A. Zeeb {
59346b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
5935ec190d91SBjoern A. Zeeb 	uint8_t tid;
5936ec190d91SBjoern A. Zeeb 
5937ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
5938ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5939ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5940ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
59416b4cac81SBjoern A. Zeeb 
59426b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
5943ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
5944ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5945ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5946ec190d91SBjoern A. Zeeb 
5947ec190d91SBjoern A. Zeeb 	return (tid);
59486b4cac81SBjoern A. Zeeb }
59496b4cac81SBjoern A. Zeeb 
5950ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
5951ac1d519cSBjoern A. Zeeb 
5952ac1d519cSBjoern A. Zeeb static void
5953ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
5954ac1d519cSBjoern A. Zeeb {
5955ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5956ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
5957ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
5958ac1d519cSBjoern A. Zeeb 
5959ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
5960ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5961ac1d519cSBjoern A. Zeeb 
5962ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
5963ac1d519cSBjoern A. Zeeb 
5964ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5965ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
5966ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
5967ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
5968ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5969ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
5970ac1d519cSBjoern A. Zeeb 		return;
5971ac1d519cSBjoern A. Zeeb 	}
5972ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
5973ac1d519cSBjoern A. Zeeb 
5974ac1d519cSBjoern A. Zeeb 	/* More work to do? */
5975ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
5976ac1d519cSBjoern A. Zeeb 		schedule_work(work);
5977ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5978ac1d519cSBjoern A. Zeeb 
5979ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
5980ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
5981ac1d519cSBjoern A. Zeeb 
5982ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
5983ac1d519cSBjoern A. Zeeb }
5984ac1d519cSBjoern A. Zeeb 
5985ac1d519cSBjoern A. Zeeb void
5986ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
5987ac1d519cSBjoern A. Zeeb {
5988ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5989ac1d519cSBjoern A. Zeeb 
5990ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5991ac1d519cSBjoern A. Zeeb 
5992ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5993ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
5994ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
5995ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
5996ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5997ac1d519cSBjoern A. Zeeb 
5998ac1d519cSBjoern A. Zeeb 	/*
5999ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6000ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6001ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6002ac1d519cSBjoern A. Zeeb 	 */
6003ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6004ac1d519cSBjoern A. Zeeb }
6005ac1d519cSBjoern A. Zeeb 
6006ac1d519cSBjoern A. Zeeb void
6007ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6008ac1d519cSBjoern A. Zeeb {
6009ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6010ac1d519cSBjoern A. Zeeb 
6011ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6012ac1d519cSBjoern A. Zeeb 
6013ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6014ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6015ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6016ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6017ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6018ac1d519cSBjoern A. Zeeb }
6019ac1d519cSBjoern A. Zeeb 
6020ac1d519cSBjoern A. Zeeb void
6021ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6022ac1d519cSBjoern A. Zeeb {
6023ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6024ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6025ac1d519cSBjoern A. Zeeb 
6026ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6027ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6028ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6029ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6030ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6031ac1d519cSBjoern A. Zeeb 		return;
6032ac1d519cSBjoern A. Zeeb 	}
6033ac1d519cSBjoern A. Zeeb 
6034ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6035ac1d519cSBjoern A. Zeeb 
6036ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6037ac1d519cSBjoern A. Zeeb 		    entry);
6038ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6039ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6040ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6041ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6042ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6043ac1d519cSBjoern A. Zeeb 			break;
6044ac1d519cSBjoern A. Zeeb 	}
6045ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6046ac1d519cSBjoern A. Zeeb }
6047ac1d519cSBjoern A. Zeeb 
6048ac1d519cSBjoern A. Zeeb void
6049ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6050ac1d519cSBjoern A. Zeeb {
6051ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6052ac1d519cSBjoern A. Zeeb 
6053ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6054ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6055ac1d519cSBjoern A. Zeeb }
6056ac1d519cSBjoern A. Zeeb 
6057ac1d519cSBjoern A. Zeeb void
6058ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6059ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6060ac1d519cSBjoern A. Zeeb {
6061ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6062ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6063ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6064ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6065ac1d519cSBjoern A. Zeeb 	} else {
6066ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6067ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6068ac1d519cSBjoern A. Zeeb 	}
6069ac1d519cSBjoern A. Zeeb }
6070ac1d519cSBjoern A. Zeeb 
6071ac1d519cSBjoern A. Zeeb void
6072ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6073ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6074ac1d519cSBjoern A. Zeeb {
6075ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6076ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6077ac1d519cSBjoern A. Zeeb }
6078ac1d519cSBjoern A. Zeeb 
6079ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6080ac1d519cSBjoern A. Zeeb 
60816b4cac81SBjoern A. Zeeb struct wiphy *
60826b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
60836b4cac81SBjoern A. Zeeb {
60846b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6085ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
60866b4cac81SBjoern A. Zeeb 
60876b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
60886b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
60896b4cac81SBjoern A. Zeeb 		return (NULL);
60906b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
60916b4cac81SBjoern A. Zeeb 
6092ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6093ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6094ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6095ac1d519cSBjoern A. Zeeb 
6096ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6097ac1d519cSBjoern A. Zeeb 
6098ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6099ac1d519cSBjoern A. Zeeb 	TODO();
6100ac1d519cSBjoern A. Zeeb 
6101ac1d519cSBjoern A. Zeeb 	return (wiphy);
61026b4cac81SBjoern A. Zeeb }
61036b4cac81SBjoern A. Zeeb 
61046b4cac81SBjoern A. Zeeb void
61056b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
61066b4cac81SBjoern A. Zeeb {
61076b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
61086b4cac81SBjoern A. Zeeb 
61096b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
61106b4cac81SBjoern A. Zeeb 		return;
61116b4cac81SBjoern A. Zeeb 
6112ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6113ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6114ac1d519cSBjoern A. Zeeb 
61156b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6116ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6117ac1d519cSBjoern A. Zeeb 
61186b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
61196b4cac81SBjoern A. Zeeb }
61206b4cac81SBjoern A. Zeeb 
6121a7c19b8aSBjoern A. Zeeb static uint32_t
6122a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6123a7c19b8aSBjoern A. Zeeb {
6124a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6125a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6126a7c19b8aSBjoern A. Zeeb }
6127a7c19b8aSBjoern A. Zeeb 
6128a7c19b8aSBjoern A. Zeeb static uint32_t
6129a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6130a7c19b8aSBjoern A. Zeeb {
6131a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6132a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6133a7c19b8aSBjoern A. Zeeb }
6134a7c19b8aSBjoern A. Zeeb 
6135a7c19b8aSBjoern A. Zeeb uint32_t
6136a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6137a7c19b8aSBjoern A. Zeeb {
6138a7c19b8aSBjoern A. Zeeb 
6139a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6140a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6141a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6142a7c19b8aSBjoern A. Zeeb 
6143a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6144a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6145a7c19b8aSBjoern A. Zeeb 
6146a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6147a7c19b8aSBjoern A. Zeeb 
6148a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6149a7c19b8aSBjoern A. Zeeb }
6150a7c19b8aSBjoern A. Zeeb 
61516b4cac81SBjoern A. Zeeb uint32_t
61526b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
61536b4cac81SBjoern A. Zeeb     enum nl80211_band band)
61546b4cac81SBjoern A. Zeeb {
61556b4cac81SBjoern A. Zeeb 
61566b4cac81SBjoern A. Zeeb 	switch (band) {
61576b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
61586b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
61596b4cac81SBjoern A. Zeeb 		break;
61606b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
61616b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
61626b4cac81SBjoern A. Zeeb 		break;
61636b4cac81SBjoern A. Zeeb 	default:
61646b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
61656b4cac81SBjoern A. Zeeb 		break;
61666b4cac81SBjoern A. Zeeb 	}
61676b4cac81SBjoern A. Zeeb 
61686b4cac81SBjoern A. Zeeb 	return (0);
61696b4cac81SBjoern A. Zeeb }
61706b4cac81SBjoern A. Zeeb 
61716b4cac81SBjoern A. Zeeb uint32_t
61726b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
61736b4cac81SBjoern A. Zeeb {
61746b4cac81SBjoern A. Zeeb 
61756b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
61766b4cac81SBjoern A. Zeeb }
61776b4cac81SBjoern A. Zeeb 
6178ac867c20SBjoern A. Zeeb #if 0
61796b4cac81SBjoern A. Zeeb static struct lkpi_sta *
61806b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
61816b4cac81SBjoern A. Zeeb {
61826b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
61836b4cac81SBjoern A. Zeeb 
61846b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
61856b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
61866b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
61876b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
61886b4cac81SBjoern A. Zeeb 			return (lsta);
61896b4cac81SBjoern A. Zeeb 		}
61906b4cac81SBjoern A. Zeeb 	}
61916b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
61926b4cac81SBjoern A. Zeeb 
61936b4cac81SBjoern A. Zeeb 	return (NULL);
61946b4cac81SBjoern A. Zeeb }
6195ac867c20SBjoern A. Zeeb #endif
61966b4cac81SBjoern A. Zeeb 
61976b4cac81SBjoern A. Zeeb struct ieee80211_sta *
61986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
61996b4cac81SBjoern A. Zeeb {
62006b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
62026b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
62036b4cac81SBjoern A. Zeeb 
62046b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
62056b4cac81SBjoern A. Zeeb 
62066b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
62076b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
62086b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
62096b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
62106b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
62116b4cac81SBjoern A. Zeeb 			return (sta);
62126b4cac81SBjoern A. Zeeb 		}
62136b4cac81SBjoern A. Zeeb 	}
62146b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
62156b4cac81SBjoern A. Zeeb 	return (NULL);
62166b4cac81SBjoern A. Zeeb }
62176b4cac81SBjoern A. Zeeb 
62186b4cac81SBjoern A. Zeeb struct ieee80211_sta *
62192e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
62202e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
62216b4cac81SBjoern A. Zeeb {
62226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62236b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62246b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
62256b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
62266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
62276b4cac81SBjoern A. Zeeb 
62286b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
62296b4cac81SBjoern A. Zeeb 	sta = NULL;
62306b4cac81SBjoern A. Zeeb 
62318891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
62326b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
62336b4cac81SBjoern A. Zeeb 
62346b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
62356b4cac81SBjoern A. Zeeb 
62366b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
62376b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
62386b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
62396b4cac81SBjoern A. Zeeb 			continue;
62406b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
62416b4cac81SBjoern A. Zeeb 		if (sta != NULL)
62426b4cac81SBjoern A. Zeeb 			break;
62436b4cac81SBjoern A. Zeeb 	}
62448891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
62456b4cac81SBjoern A. Zeeb 
62466b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
62476b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
62486b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
62496b4cac81SBjoern A. Zeeb 			return (NULL);
62506b4cac81SBjoern A. Zeeb 	}
62516b4cac81SBjoern A. Zeeb 
62526b4cac81SBjoern A. Zeeb 	return (sta);
62536b4cac81SBjoern A. Zeeb }
62546b4cac81SBjoern A. Zeeb 
62556b4cac81SBjoern A. Zeeb struct sk_buff *
62566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
62576b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
62586b4cac81SBjoern A. Zeeb {
62596b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
62600cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62616b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
62626b4cac81SBjoern A. Zeeb 
62630cbcfa19SBjoern A. Zeeb 	skb = NULL;
62646b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
62656b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
62666b4cac81SBjoern A. Zeeb 
62670cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
62680cbcfa19SBjoern A. Zeeb 		goto stopped;
62690cbcfa19SBjoern A. Zeeb 
62700cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
62710cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
62720cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
62730cbcfa19SBjoern A. Zeeb 		goto stopped;
62740cbcfa19SBjoern A. Zeeb 	}
62750cbcfa19SBjoern A. Zeeb 
6276eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6277eac3646fSBjoern A. Zeeb 
6278eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
62796b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6280eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
62816b4cac81SBjoern A. Zeeb 
62820cbcfa19SBjoern A. Zeeb stopped:
62836b4cac81SBjoern A. Zeeb 	return (skb);
62846b4cac81SBjoern A. Zeeb }
62856b4cac81SBjoern A. Zeeb 
62866b4cac81SBjoern A. Zeeb void
62876b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
628886220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
62896b4cac81SBjoern A. Zeeb {
62906b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
62916b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
629286220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
62936b4cac81SBjoern A. Zeeb 
62946b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
62956b4cac81SBjoern A. Zeeb 
62966b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6297eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
62986b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
62996b4cac81SBjoern A. Zeeb 		fc++;
63006b4cac81SBjoern A. Zeeb 		bc += skb->len;
63016b4cac81SBjoern A. Zeeb 	}
6302eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
63036b4cac81SBjoern A. Zeeb 	if (frame_cnt)
63046b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
63056b4cac81SBjoern A. Zeeb 	if (byte_cnt)
63066b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
63076b4cac81SBjoern A. Zeeb 
63086b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
63096b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
63106b4cac81SBjoern A. Zeeb 	IMPROVE();
63116b4cac81SBjoern A. Zeeb }
63126b4cac81SBjoern A. Zeeb 
63136b4cac81SBjoern A. Zeeb /*
63146b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
63156b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
63166b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
63176b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
63186b4cac81SBjoern A. Zeeb  */
6319a8397571SBjoern A. Zeeb static void
6320a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
63216b4cac81SBjoern A. Zeeb     int status)
63226b4cac81SBjoern A. Zeeb {
63236b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
63246b4cac81SBjoern A. Zeeb 	struct mbuf *m;
63256b4cac81SBjoern A. Zeeb 
63266b4cac81SBjoern A. Zeeb 	m = skb->m;
63276b4cac81SBjoern A. Zeeb 	skb->m = NULL;
63286b4cac81SBjoern A. Zeeb 
63296b4cac81SBjoern A. Zeeb 	if (m != NULL) {
63306b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
63316b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
63326b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
63336b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
63346b4cac81SBjoern A. Zeeb 	}
6335a8397571SBjoern A. Zeeb }
63366b4cac81SBjoern A. Zeeb 
6337a8397571SBjoern A. Zeeb void
6338a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6339a8397571SBjoern A. Zeeb     int status)
6340a8397571SBjoern A. Zeeb {
6341a8397571SBjoern A. Zeeb 
6342a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
63436b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
63446b4cac81SBjoern A. Zeeb }
63456b4cac81SBjoern A. Zeeb 
6346383b3e8fSBjoern A. Zeeb void
6347a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6348a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6349383b3e8fSBjoern A. Zeeb {
6350a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6351383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6352383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6353383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6354383b3e8fSBjoern A. Zeeb 	int status;
6355383b3e8fSBjoern A. Zeeb 
6356a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6357383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6358383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6359383b3e8fSBjoern A. Zeeb 
6360383b3e8fSBjoern A. Zeeb 		m = skb->m;
6361383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6362383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6363383b3e8fSBjoern A. Zeeb 	} else {
6364383b3e8fSBjoern A. Zeeb 		ni = NULL;
6365383b3e8fSBjoern A. Zeeb 	}
6366383b3e8fSBjoern A. Zeeb 
6367a8397571SBjoern A. Zeeb 	info = txstat->info;
6368383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6369383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6370383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6371383b3e8fSBjoern A. Zeeb 	} else {
6372383b3e8fSBjoern A. Zeeb 		status = 1;
6373383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6374383b3e8fSBjoern A. Zeeb 	}
6375383b3e8fSBjoern A. Zeeb 
6376383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6377e2c5ab09SJohn Baldwin 		int ridx __unused;
6378383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6379383b3e8fSBjoern A. Zeeb 		int old_rate;
6380383b3e8fSBjoern A. Zeeb 
6381383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6382383b3e8fSBjoern A. Zeeb #endif
6383383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6384383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6385383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6386383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6387383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6388383b3e8fSBjoern A. Zeeb 		}
6389383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6390a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6391383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6392383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6393383b3e8fSBjoern A. Zeeb #endif
6394adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6395383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6396383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6397383b3e8fSBjoern A. Zeeb 		}
6398383b3e8fSBjoern A. Zeeb 
6399383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6400383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6401383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6402383b3e8fSBjoern A. Zeeb 
6403383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6404383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6405383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6406383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6407383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6408383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6409383b3e8fSBjoern A. Zeeb 		}
6410383b3e8fSBjoern A. Zeeb #endif
6411383b3e8fSBjoern A. Zeeb 	}
6412383b3e8fSBjoern A. Zeeb 
6413383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6414383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6415383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6416383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6417383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6418383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6419adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6420383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6421383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6422383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6423383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6424383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6425383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6426383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6427383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6428383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6429383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6430383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6431383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6432383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6433adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6434383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6435383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6436383b3e8fSBjoern A. Zeeb #endif
6437383b3e8fSBjoern A. Zeeb 
6438a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6439a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6440a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6441a8397571SBjoern A. Zeeb 	} else {
6442383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6443383b3e8fSBjoern A. Zeeb 	}
6444a8397571SBjoern A. Zeeb }
6445a8397571SBjoern A. Zeeb 
6446a8397571SBjoern A. Zeeb void
6447a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6448a8397571SBjoern A. Zeeb {
6449a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6450a8397571SBjoern A. Zeeb 
6451a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6452a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6453a8397571SBjoern A. Zeeb 	status.skb = skb;
6454a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6455a8397571SBjoern A. Zeeb 
6456a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6457a8397571SBjoern A. Zeeb }
6458383b3e8fSBjoern A. Zeeb 
64596b4cac81SBjoern A. Zeeb /*
64606b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
64616b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
64626b4cac81SBjoern A. Zeeb  * mbufs this should go away.
64636b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
64646b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
64656b4cac81SBjoern A. Zeeb  */
64666b4cac81SBjoern A. Zeeb static void
64676b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
64686b4cac81SBjoern A. Zeeb {
64696b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
64706b4cac81SBjoern A. Zeeb 	struct mbuf *m;
64716b4cac81SBjoern A. Zeeb 
64726b4cac81SBjoern A. Zeeb 	if (p == NULL)
64736b4cac81SBjoern A. Zeeb 		return;
64746b4cac81SBjoern A. Zeeb 
64756b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
64766b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
64776b4cac81SBjoern A. Zeeb 
64786b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
64796b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
64806b4cac81SBjoern A. Zeeb 	if (ni != NULL)
64816b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
64826b4cac81SBjoern A. Zeeb 	m_freem(m);
64836b4cac81SBjoern A. Zeeb }
64846b4cac81SBjoern A. Zeeb 
64856b4cac81SBjoern A. Zeeb void
64866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
64876b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
64886b4cac81SBjoern A. Zeeb {
64896b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64906b4cac81SBjoern A. Zeeb 
64916b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
64926b4cac81SBjoern A. Zeeb 	IMPROVE();
64936b4cac81SBjoern A. Zeeb 
64946b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
64956b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
64966b4cac81SBjoern A. Zeeb }
64976b4cac81SBjoern A. Zeeb 
64986b4cac81SBjoern A. Zeeb void
64996b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
65006b4cac81SBjoern A. Zeeb     struct work_struct *w)
65016b4cac81SBjoern A. Zeeb {
65026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
65036b4cac81SBjoern A. Zeeb 
65046b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
65056b4cac81SBjoern A. Zeeb 	IMPROVE();
65066b4cac81SBjoern A. Zeeb 
65076b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
65086b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
65096b4cac81SBjoern A. Zeeb }
65106b4cac81SBjoern A. Zeeb 
65116b4cac81SBjoern A. Zeeb struct sk_buff *
6512ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
6513ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
6514ade774b1SBjoern A. Zeeb {
6515ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
6516ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
6517ade774b1SBjoern A. Zeeb 	uint8_t *p;
6518ade774b1SBjoern A. Zeeb 	size_t len;
6519ade774b1SBjoern A. Zeeb 
6520ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
6521ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
6522ade774b1SBjoern A. Zeeb 
6523ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
6524ade774b1SBjoern A. Zeeb 	if (skb == NULL)
6525ade774b1SBjoern A. Zeeb 		return (NULL);
6526ade774b1SBjoern A. Zeeb 
6527ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
6528ade774b1SBjoern A. Zeeb 
6529ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
6530ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
6531ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
6532ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
6533ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
6534ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
6535ade774b1SBjoern A. Zeeb 
6536ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
6537ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
6538ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
6539ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
6540ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
6541ade774b1SBjoern A. Zeeb 
6542ade774b1SBjoern A. Zeeb 	return (skb);
6543ade774b1SBjoern A. Zeeb }
6544ade774b1SBjoern A. Zeeb 
6545ade774b1SBjoern A. Zeeb struct sk_buff *
65466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
65476b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
65486b4cac81SBjoern A. Zeeb {
65496b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
65506b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
65516b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
65526b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
65536b4cac81SBjoern A. Zeeb 	uint16_t v;
65546b4cac81SBjoern A. Zeeb 
65556b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
65566b4cac81SBjoern A. Zeeb 	if (skb == NULL)
65576b4cac81SBjoern A. Zeeb 		return (NULL);
65586b4cac81SBjoern A. Zeeb 
65596b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
65606b4cac81SBjoern A. Zeeb 
65616b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
65626b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
65636b4cac81SBjoern A. Zeeb 
65646b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
65656b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
65666b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
6567616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
65686b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
65696b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
65706b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
65716b4cac81SBjoern A. Zeeb 
65726b4cac81SBjoern A. Zeeb 	return (skb);
65736b4cac81SBjoern A. Zeeb }
65746b4cac81SBjoern A. Zeeb 
65756b4cac81SBjoern A. Zeeb struct sk_buff *
65766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
6577adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
65786b4cac81SBjoern A. Zeeb {
65796b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
65806b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
65816b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
65826b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
65836b4cac81SBjoern A. Zeeb 
6584adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
6585adff403fSBjoern A. Zeeb 
65866b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
65876b4cac81SBjoern A. Zeeb 	if (skb == NULL)
65886b4cac81SBjoern A. Zeeb 		return (NULL);
65896b4cac81SBjoern A. Zeeb 
65906b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
65916b4cac81SBjoern A. Zeeb 
65926b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
65936b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
65946b4cac81SBjoern A. Zeeb 
65956b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
65966b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
65976b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
65986b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
65996b4cac81SBjoern A. Zeeb 
66006b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
66016b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
66026b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
66036b4cac81SBjoern A. Zeeb 
66046b4cac81SBjoern A. Zeeb 	return (skb);
66056b4cac81SBjoern A. Zeeb }
66066b4cac81SBjoern A. Zeeb 
66076b4cac81SBjoern A. Zeeb struct wireless_dev *
66086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
66096b4cac81SBjoern A. Zeeb {
66106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66116b4cac81SBjoern A. Zeeb 
66126b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
66136b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
66146b4cac81SBjoern A. Zeeb }
66156b4cac81SBjoern A. Zeeb 
66166b4cac81SBjoern A. Zeeb void
66176b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
66186b4cac81SBjoern A. Zeeb {
66196b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66206b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
66216b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
66226b4cac81SBjoern A. Zeeb 	int arg;
66236b4cac81SBjoern A. Zeeb 
66246b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
66256b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
66266b4cac81SBjoern A. Zeeb 
66276b4cac81SBjoern A. Zeeb 	/*
6628f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
66296b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
66306b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
66316b4cac81SBjoern A. Zeeb 	 */
6632f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
6633bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
66346b4cac81SBjoern A. Zeeb 
6635018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6636018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
66376b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
66386b4cac81SBjoern A. Zeeb }
66396b4cac81SBjoern A. Zeeb 
6640bb81db90SBjoern A. Zeeb void
6641bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
6642bb81db90SBjoern A. Zeeb {
6643bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6644bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
6645bb81db90SBjoern A. Zeeb 
6646bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
6647bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
6648bb81db90SBjoern A. Zeeb 
6649bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6650bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
66513540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
6652bb81db90SBjoern A. Zeeb }
6653bb81db90SBjoern A. Zeeb 
66545edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
66555edde07cSBjoern A. Zeeb 
66565a9a0d78SBjoern A. Zeeb void
66575a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
66585a9a0d78SBjoern A. Zeeb {
66595a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
66605a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66615a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
66625a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
66635a9a0d78SBjoern A. Zeeb 
66645a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
66655a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
66665a9a0d78SBjoern A. Zeeb 
66675a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
66685a9a0d78SBjoern A. Zeeb 
66695a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
66705a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
66715a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
66725a9a0d78SBjoern A. Zeeb 	else
66735a9a0d78SBjoern A. Zeeb 		ac_count = 1;
66745a9a0d78SBjoern A. Zeeb 
66755a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
66765a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
66775a9a0d78SBjoern A. Zeeb 
66785a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
66795a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
66805a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
66815a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
66820d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
66835a9a0d78SBjoern A. Zeeb 				/*
66845a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
66855a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
66865a9a0d78SBjoern A. Zeeb 				 */
66870d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
66880d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
66895a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
66905a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
66915a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
66925a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
66930d2cb6a6SBjoern A. Zeeb #endif
66945a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
66955a9a0d78SBjoern A. Zeeb 			}
66965a9a0d78SBjoern A. Zeeb 		}
66975a9a0d78SBjoern A. Zeeb 	}
66985a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
66995a9a0d78SBjoern A. Zeeb }
67005a9a0d78SBjoern A. Zeeb 
67015a9a0d78SBjoern A. Zeeb void
67025a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
67035a9a0d78SBjoern A. Zeeb {
67045a9a0d78SBjoern A. Zeeb 	int i;
67055a9a0d78SBjoern A. Zeeb 
67065a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
67075a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
67085a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
67095a9a0d78SBjoern A. Zeeb }
67105a9a0d78SBjoern A. Zeeb 
67115a9a0d78SBjoern A. Zeeb 
67125a9a0d78SBjoern A. Zeeb static void
67135a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
67145a9a0d78SBjoern A. Zeeb {
67155a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67165a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67175a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67185a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
67195a9a0d78SBjoern A. Zeeb 
67205a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
67215a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
67225a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
67235a9a0d78SBjoern A. Zeeb 	else
67245a9a0d78SBjoern A. Zeeb 		ac_count = 1;
67255a9a0d78SBjoern A. Zeeb 
67265a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
67275a9a0d78SBjoern A. Zeeb 
67285a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
67295a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
67305a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
67315a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
67325a9a0d78SBjoern A. Zeeb 
67335a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
67345a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
67355a9a0d78SBjoern A. Zeeb 
67365a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
67375a9a0d78SBjoern A. Zeeb 
67385a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
67395a9a0d78SBjoern A. Zeeb 
67400d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
67415a9a0d78SBjoern A. Zeeb 				/*
67425a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
67435a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
67445a9a0d78SBjoern A. Zeeb 				 */
67450d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
67460d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
67475a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
67485a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
67495a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
67505a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
67510d2cb6a6SBjoern A. Zeeb #endif
67525a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
67535a9a0d78SBjoern A. Zeeb 
67545a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
67555a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
67565a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
67575a9a0d78SBjoern A. Zeeb 
67585a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
67595a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
67605a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
67615a9a0d78SBjoern A. Zeeb 
67625a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
67635a9a0d78SBjoern A. Zeeb 							continue;
67645a9a0d78SBjoern A. Zeeb 
67655a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
67665a9a0d78SBjoern A. Zeeb 							continue;
67675a9a0d78SBjoern A. Zeeb 
67685a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
67695a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
67705a9a0d78SBjoern A. Zeeb 							continue;
67715a9a0d78SBjoern A. Zeeb 
67725a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
67735a9a0d78SBjoern A. Zeeb 
67745a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
67755a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
67765a9a0d78SBjoern A. Zeeb 					}
67775a9a0d78SBjoern A. Zeeb 				}
67785a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
67795a9a0d78SBjoern A. Zeeb 			}
67805a9a0d78SBjoern A. Zeeb 		}
67815a9a0d78SBjoern A. Zeeb 	}
67825a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
67835a9a0d78SBjoern A. Zeeb }
67845a9a0d78SBjoern A. Zeeb 
67855a9a0d78SBjoern A. Zeeb void
67865a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
67875a9a0d78SBjoern A. Zeeb {
67885a9a0d78SBjoern A. Zeeb 	int i;
67895a9a0d78SBjoern A. Zeeb 
67905a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
67915a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
67925a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
67935a9a0d78SBjoern A. Zeeb }
67945a9a0d78SBjoern A. Zeeb 
67955a9a0d78SBjoern A. Zeeb void
67965a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
67975a9a0d78SBjoern A. Zeeb {
67985a9a0d78SBjoern A. Zeeb 
67995a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
68005a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
68015a9a0d78SBjoern A. Zeeb 
68025a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
68035a9a0d78SBjoern A. Zeeb }
68045a9a0d78SBjoern A. Zeeb 
68055a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
68065a9a0d78SBjoern A. Zeeb void
68075a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
68085a9a0d78SBjoern A. Zeeb {
68095a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
68105a9a0d78SBjoern A. Zeeb 
68115a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
68125a9a0d78SBjoern A. Zeeb 
68135a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
68145a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
68155a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
68165a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
68175a9a0d78SBjoern A. Zeeb }
68185a9a0d78SBjoern A. Zeeb 
68195a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
68205a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
68215a9a0d78SBjoern A. Zeeb {
68225a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
68235a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
68245a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68255a9a0d78SBjoern A. Zeeb 
68265a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
68275a9a0d78SBjoern A. Zeeb 	txq = NULL;
68285a9a0d78SBjoern A. Zeeb 
68295a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
68305a9a0d78SBjoern A. Zeeb 
68315a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
68325a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
68335a9a0d78SBjoern A. Zeeb 		goto out;
68345a9a0d78SBjoern A. Zeeb 
68355a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
68365a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
68375a9a0d78SBjoern A. Zeeb 		goto out;
68385a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
68395a9a0d78SBjoern A. Zeeb 		goto out;
68405a9a0d78SBjoern A. Zeeb 
68415a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
68425a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
68435a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
68445a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
68455a9a0d78SBjoern A. Zeeb 
68465a9a0d78SBjoern A. Zeeb out:
68475a9a0d78SBjoern A. Zeeb 	return (txq);
68485a9a0d78SBjoern A. Zeeb }
68495a9a0d78SBjoern A. Zeeb 
68505a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
68515a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
68525a9a0d78SBjoern A. Zeeb {
68535a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
68545a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
6855eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
68565a9a0d78SBjoern A. Zeeb 
68575a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68585a9a0d78SBjoern A. Zeeb 
68595a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
68605a9a0d78SBjoern A. Zeeb 
68615a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
6862eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
6863eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
6864eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
6865eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
68665a9a0d78SBjoern A. Zeeb 		goto out;
68675a9a0d78SBjoern A. Zeeb 
686841b746e0SAustin Shafer 	/*
686941b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
687041b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
687141b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
687241b746e0SAustin Shafer 	 */
687341b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
68745a9a0d78SBjoern A. Zeeb 		goto out;
68755a9a0d78SBjoern A. Zeeb 
68765a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
68775a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
68785a9a0d78SBjoern A. Zeeb out:
68795a9a0d78SBjoern A. Zeeb 	return;
68805a9a0d78SBjoern A. Zeeb }
68815a9a0d78SBjoern A. Zeeb 
6882eac3646fSBjoern A. Zeeb void
6883eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
6884eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
6885eac3646fSBjoern A. Zeeb {
6886eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6887eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
6888eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
6889eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
6890eac3646fSBjoern A. Zeeb 
6891eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6892eac3646fSBjoern A. Zeeb 
6893eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
6894eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
6895eac3646fSBjoern A. Zeeb 	do {
6896eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
6897eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
6898eac3646fSBjoern A. Zeeb 			break;
6899eac3646fSBjoern A. Zeeb 
6900eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
6901eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
6902eac3646fSBjoern A. Zeeb 		do {
6903eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6904eac3646fSBjoern A. Zeeb 			if (skb == NULL)
6905eac3646fSBjoern A. Zeeb 				break;
6906eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
6907eac3646fSBjoern A. Zeeb 		} while(1);
6908eac3646fSBjoern A. Zeeb 
6909eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
6910eac3646fSBjoern A. Zeeb 	} while (1);
6911eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
6912eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6913eac3646fSBjoern A. Zeeb }
6914eac3646fSBjoern A. Zeeb 
69155a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
69165a9a0d78SBjoern A. Zeeb 
69175edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
69185edde07cSBjoern A. Zeeb 	u_int refcnt;
69195edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
69205edde07cSBjoern A. Zeeb };
69215edde07cSBjoern A. Zeeb 
69225edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
69235edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
69245edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
69255edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
69265edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
69275edde07cSBjoern A. Zeeb 	size_t ssid_len;
69285edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
69295edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
69305edde07cSBjoern A. Zeeb 
69315edde07cSBjoern A. Zeeb 	/*
69325edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
69335edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
69345edde07cSBjoern A. Zeeb 	 */
69355edde07cSBjoern A. Zeeb 	bool match;
69365edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
69375edde07cSBjoern A. Zeeb };
69385edde07cSBjoern A. Zeeb 
69395edde07cSBjoern A. Zeeb static void
69405edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
69415edde07cSBjoern A. Zeeb {
69425edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
69435edde07cSBjoern A. Zeeb 	size_t ielen;
69445edde07cSBjoern A. Zeeb 
69455edde07cSBjoern A. Zeeb 	lookup = arg;
69465edde07cSBjoern A. Zeeb 
69475edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
69485edde07cSBjoern A. Zeeb 	if (lookup->match)
69495edde07cSBjoern A. Zeeb 		return;
69505edde07cSBjoern A. Zeeb 
69515edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
69525edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
69535edde07cSBjoern A. Zeeb 		return;
69545edde07cSBjoern A. Zeeb 
69555edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
69565edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
69575edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
69585edde07cSBjoern A. Zeeb 		return;
69595edde07cSBjoern A. Zeeb 	}
69605edde07cSBjoern A. Zeeb 
69615edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
69625edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
69635edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
69645edde07cSBjoern A. Zeeb 		return;
69655edde07cSBjoern A. Zeeb 	}
69665edde07cSBjoern A. Zeeb 
69675edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
69685edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
69695edde07cSBjoern A. Zeeb 
69705edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
69715edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
69725edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
69735edde07cSBjoern A. Zeeb 			return;
69745edde07cSBjoern A. Zeeb 	}
69755edde07cSBjoern A. Zeeb 
69765edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
69775edde07cSBjoern A. Zeeb 		return;
69785edde07cSBjoern A. Zeeb 
69795edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
69805edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
69815edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
69825edde07cSBjoern A. Zeeb 			return;
69835edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
69845edde07cSBjoern A. Zeeb 			return;
69855edde07cSBjoern A. Zeeb 	}
69865edde07cSBjoern A. Zeeb 
69875edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
69885edde07cSBjoern A. Zeeb 
69895edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
69905edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
69915edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
69925edde07cSBjoern A. Zeeb 		return;
69935edde07cSBjoern A. Zeeb 
69945edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
69955edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
69965edde07cSBjoern A. Zeeb 	if (ielen)
69975edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
69985edde07cSBjoern A. Zeeb 
69995edde07cSBjoern A. Zeeb 	lookup->match = true;
70005edde07cSBjoern A. Zeeb }
70015edde07cSBjoern A. Zeeb 
70025edde07cSBjoern A. Zeeb struct cfg80211_bss *
70035edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
70045edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
70055edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
70065edde07cSBjoern A. Zeeb {
70075edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
70085edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
70095edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
70105edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
70115edde07cSBjoern A. Zeeb 
70125edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
70135edde07cSBjoern A. Zeeb 
70145edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
70155edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
70165edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
70175edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
70185edde07cSBjoern A. Zeeb 		return (NULL);
70195edde07cSBjoern A. Zeeb 	}
70205edde07cSBjoern A. Zeeb 
70215edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
70225edde07cSBjoern A. Zeeb 	lookup.chan = chan;
70235edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
70245edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
70255edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
70265edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
70275edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
70285edde07cSBjoern A. Zeeb 	lookup.match = false;
70295edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
70305edde07cSBjoern A. Zeeb 
70315edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
70325edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
70335edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
70345edde07cSBjoern A. Zeeb 	if (!lookup.match) {
70355edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
70365edde07cSBjoern A. Zeeb 		return (NULL);
70375edde07cSBjoern A. Zeeb 	}
70385edde07cSBjoern A. Zeeb 
70395edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
70405edde07cSBjoern A. Zeeb 	return (&lbss->bss);
70415edde07cSBjoern A. Zeeb }
70425edde07cSBjoern A. Zeeb 
70435edde07cSBjoern A. Zeeb void
70445edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
70455edde07cSBjoern A. Zeeb {
70465edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
70475edde07cSBjoern A. Zeeb 
70485edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
70495edde07cSBjoern A. Zeeb 
70505edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
70515edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
70525edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
70535edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
70545edde07cSBjoern A. Zeeb 	}
70555edde07cSBjoern A. Zeeb }
70565edde07cSBjoern A. Zeeb 
70575edde07cSBjoern A. Zeeb void
70585edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
70595edde07cSBjoern A. Zeeb {
70605edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
70615edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
70625edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
70635edde07cSBjoern A. Zeeb 
70645edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
70655edde07cSBjoern A. Zeeb 	ic = lhw->ic;
70665edde07cSBjoern A. Zeeb 
70675edde07cSBjoern A. Zeeb 	/*
70685edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
70695edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
70705edde07cSBjoern A. Zeeb 	 */
70715edde07cSBjoern A. Zeeb 	if (ic == NULL ||
70725edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
70735edde07cSBjoern A. Zeeb 		return;
70745edde07cSBjoern A. Zeeb 
70755edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
70765edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
70775edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
70785edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
70795edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
70805edde07cSBjoern A. Zeeb }
70815edde07cSBjoern A. Zeeb 
70825edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
70835edde07cSBjoern A. Zeeb 
7084ac1d519cSBjoern A. Zeeb /*
7085ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7086ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7087ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7088ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7089ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7090ac1d519cSBjoern A. Zeeb  */
7091ac1d519cSBjoern A. Zeeb 
7092ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7093ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7094ac1d519cSBjoern A. Zeeb {
7095ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7096ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7097ac1d519cSBjoern A. Zeeb 	int error;
7098ac1d519cSBjoern A. Zeeb 
7099ac1d519cSBjoern A. Zeeb 	changed = 0;
7100ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7101ac1d519cSBjoern A. Zeeb 		cd = NULL;
7102ac1d519cSBjoern A. Zeeb 	else
7103ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7104ac1d519cSBjoern A. Zeeb 
7105ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7106ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7107ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7108ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7109ac1d519cSBjoern A. Zeeb 	}
7110ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7111ac1d519cSBjoern A. Zeeb 
7112ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7113ac1d519cSBjoern A. Zeeb 		return (0);
7114ac1d519cSBjoern A. Zeeb 
7115ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7116ac1d519cSBjoern A. Zeeb 	return (error);
7117ac1d519cSBjoern A. Zeeb }
7118ac1d519cSBjoern A. Zeeb 
7119ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7120ac1d519cSBjoern A. Zeeb 
71216b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
71226b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
71236b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7124