xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision cb2887746f8b9dd4ad6b1e757cdc053a08b25a2e)
1 /*-
2  * Copyright (c) 2020-2025 The FreeBSD Foundation
3  * Copyright (c) 2020-2025 Bjoern A. Zeeb
4  *
5  * This software was developed by Björn Zeeb under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Public functions are called linuxkpi_*().
32  * Internal (static) functions are called lkpi_*().
33  *
34  * The internal structures holding metadata over public structures are also
35  * called lkpi_xxx (usually with a member at the end called xxx).
36  * Note: we do not replicate the structure names but the general variable names
37  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
38  * There are macros to access one from the other.
39  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
40  */
41 
42 /*
43  * TODO:
44  * - lots :)
45  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/sbuf.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/queue.h>
59 #include <sys/taskqueue.h>
60 #include <sys/libkern.h>
61 
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/if_media.h>
65 #include <net/ethernet.h>
66 
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_proto.h>
69 #include <net80211/ieee80211_ratectl.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_vht.h>
72 
73 #define	LINUXKPI_NET80211
74 #include <net/mac80211.h>
75 
76 #include <linux/workqueue.h>
77 #include <linux/rculist.h>
78 #include "linux_80211.h"
79 
80 #define	LKPI_80211_WME
81 #define	LKPI_80211_HW_CRYPTO
82 #define	LKPI_80211_HT
83 #define	LKPI_80211_VHT
84 
85 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
86 #define	LKPI_80211_HT
87 #endif
88 #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
89 #define	LKPI_80211_HW_CRYPTO
90 #endif
91 
92 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
93 
94 /* XXX-BZ really want this and others in queue.h */
95 #define	TAILQ_ELEM_INIT(elm, field) do {				\
96 	(elm)->field.tqe_next = NULL;					\
97 	(elm)->field.tqe_prev = NULL;					\
98 } while (0)
99 
100 /* -------------------------------------------------------------------------- */
101 
102 SYSCTL_DECL(_compat_linuxkpi);
103 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
104     "LinuxKPI 802.11 compatibility layer");
105 
106 #if defined(LKPI_80211_HW_CRYPTO)
107 static bool lkpi_hwcrypto = false;
108 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
109     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
110 
111 static bool lkpi_hwcrypto_tkip = false;
112 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, tkip, CTLFLAG_RDTUN,
113     &lkpi_hwcrypto_tkip, 0, "Enable LinuxKPI 802.11 TKIP crypto offload");
114 #endif
115 
116 /* Keep public for as long as header files are using it too. */
117 int linuxkpi_debug_80211;
118 
119 #ifdef LINUXKPI_DEBUG_80211
120 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
121     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
122 
123 #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
124     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
125 #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
126     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
127 #else
128 #define	UNIMPLEMENTED		do { } while (0)
129 #define	TRACEOK()		do { } while (0)
130 #endif
131 
132 /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
133 #ifndef PREP_TX_INFO_DURATION
134 #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
135 #endif
136 
137 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
138 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
139 
140 /* IEEE 802.11-05/0257r1 */
141 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
142 
143 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
144 static const uint8_t ieee80211e_up_to_ac[] = {
145 	IEEE80211_AC_BE,
146 	IEEE80211_AC_BK,
147 	IEEE80211_AC_BK,
148 	IEEE80211_AC_BE,
149 	IEEE80211_AC_VI,
150 	IEEE80211_AC_VI,
151 	IEEE80211_AC_VO,
152 	IEEE80211_AC_VO,
153 #if 0
154 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
155 #endif
156 };
157 
158 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
159 	/*
160 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
161 	 * mac80211 and to the driver or net80211.
162 	 * Can we pass some on 1:1? Need to compare the (*f)().
163 	 */
164 };
165 
166 #if 0
167 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
168     struct ieee80211_node *);
169 #endif
170 static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
171 static void lkpi_80211_txq_task(void *, int);
172 static void lkpi_80211_lhw_rxq_task(void *, int);
173 static void lkpi_ieee80211_free_skb_mbuf(void *);
174 #ifdef LKPI_80211_WME
175 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
176 #endif
177 static void lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *);
178 
179 static const char *
180 lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
181 {
182 
183 	switch (bw) {
184 
185         case RATE_INFO_BW_20:
186 		return ("20");
187 		break;
188         case RATE_INFO_BW_5:
189 		return ("5");
190 		break;
191         case RATE_INFO_BW_10:
192 		return ("10");
193 		break;
194         case RATE_INFO_BW_40:
195 		return ("40");
196 		break;
197         case RATE_INFO_BW_80:
198 		return ("80");
199 		break;
200         case RATE_INFO_BW_160:
201 		return ("160");
202 		break;
203         case RATE_INFO_BW_HE_RU:
204 		IMPROVE("nl80211_he_ru_alloc");
205 		return ("HE_RU");
206 		break;
207         case RATE_INFO_BW_320:
208 		return ("320");
209 		break;
210         case RATE_INFO_BW_EHT_RU:
211 		IMPROVE("nl80211_eht_ru_alloc");
212 		return ("EHT_RU");
213 		break;
214 	default:
215 		return ("?");
216 		break;
217 	}
218 }
219 
220 static void
221 lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
222     const uint64_t flags)
223 {
224 	int bit, i;
225 
226 	sbuf_printf(s, "%s %#010jx", prefix, flags);
227 
228 	i = 0;
229 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
230 
231 		if ((flags & BIT_ULL(bit)) == 0)
232 			continue;
233 
234 #define	EXPAND_CASE(_flag)						\
235 	case NL80211_STA_INFO_ ## _flag:				\
236 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
237 		i++;							\
238 		break;
239 
240 		switch (bit) {
241 		EXPAND_CASE(BEACON_RX)
242 		EXPAND_CASE(BEACON_SIGNAL_AVG)
243 		EXPAND_CASE(BSS_PARAM)
244 		EXPAND_CASE(CHAIN_SIGNAL)
245 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
246 		EXPAND_CASE(CONNECTED_TIME)
247 		EXPAND_CASE(INACTIVE_TIME)
248 		EXPAND_CASE(SIGNAL)
249 		EXPAND_CASE(SIGNAL_AVG)
250 		EXPAND_CASE(STA_FLAGS)
251 		EXPAND_CASE(RX_BITRATE)
252 		EXPAND_CASE(RX_PACKETS)
253 		EXPAND_CASE(RX_BYTES)
254 		EXPAND_CASE(RX_DROP_MISC)
255 		EXPAND_CASE(TX_BITRATE)
256 		EXPAND_CASE(TX_PACKETS)
257 		EXPAND_CASE(TX_BYTES)
258 		EXPAND_CASE(TX_BYTES64)
259 		EXPAND_CASE(RX_BYTES64)
260 		EXPAND_CASE(TX_FAILED)
261 		EXPAND_CASE(TX_RETRIES)
262 		EXPAND_CASE(RX_DURATION)
263 		EXPAND_CASE(TX_DURATION)
264 		EXPAND_CASE(ACK_SIGNAL)
265 		EXPAND_CASE(ACK_SIGNAL_AVG)
266 		default:
267 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
268 			break;
269 		}
270 	}
271 #undef	EXPAND_CASE
272 	if (i > 0)
273 		sbuf_printf(s, ">");
274 	sbuf_printf(s, "\n");
275 }
276 
277 static void
278 lkpi_80211_dump_lvif_stas(struct lkpi_vif *lvif, struct sbuf *s)
279 {
280 	struct lkpi_hw *lhw;
281 	struct ieee80211_hw *hw;
282 	struct ieee80211vap *vap;
283 	struct ieee80211_vif *vif;
284 	struct lkpi_sta *lsta;
285 	struct ieee80211_sta *sta;
286 	struct station_info sinfo;
287 	int error;
288 
289 	vif = LVIF_TO_VIF(lvif);
290 	vap = LVIF_TO_VAP(lvif);
291 	lhw = vap->iv_ic->ic_softc;
292 	hw = LHW_TO_HW(lhw);
293 
294 	wiphy_lock(hw->wiphy);
295 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
296 		sta = LSTA_TO_STA(lsta);
297 
298 		sbuf_putc(s, '\n');
299 		sbuf_printf(s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
300 
301 		memset(&sinfo, 0, sizeof(sinfo));
302 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
303 		if (error == EEXIST)	/* Not added to driver. */
304 			continue;
305 		if (error == ENOTSUPP) {
306 			sbuf_printf(s, " sta_statistics not supported\n");
307 			continue;
308 		}
309 		if (error != 0) {
310 			sbuf_printf(s, " sta_statistics failed: %d\n", error);
311 			continue;
312 		}
313 
314 		/* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
315 		if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
316 		    (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
317 			memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
318 			sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
319 		}
320 		/* If no CHAIN_SIGNAL is reported,  try to fill it in from the lsta sinfo. */
321 		if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) == 0 &&
322 		    (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) != 0) {
323 			sinfo.chains = lsta->sinfo.chains;
324 			memcpy(sinfo.chain_signal, lsta->sinfo.chain_signal,
325 			    sizeof(sinfo.chain_signal));
326 			sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
327 		}
328 
329 		lkpi_nl80211_sta_info_to_str(s, " nl80211_sta_info (valid fields)", sinfo.filled);
330 		sbuf_printf(s, " connected_time %u inactive_time %u\n",
331 		    sinfo.connected_time, sinfo.inactive_time);
332 		sbuf_printf(s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
333 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
334 		sbuf_printf(s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
335 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
336 
337 		sbuf_printf(s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
338 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
339 		sbuf_printf(s, " tx_duration %ju tx_retries %u\n",
340 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
341 
342 		sbuf_printf(s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
343 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
344 		sbuf_printf(s, " generation %d assoc_req_ies_len %zu chains %#04x\n",
345 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
346 
347 		for (int i = 0; i < nitems(sinfo.chain_signal) && i < IEEE80211_MAX_CHAINS; i++) {
348 			if (!(sinfo.chains & BIT(i)))
349 				continue;
350 			sbuf_printf(s, "  chain[%d] signal %d signal_avg %d\n",
351 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
352 		}
353 
354 		/* assoc_req_ies, bss_param, sta_flags */
355 
356 		sbuf_printf(s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
357 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
358 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
359 		    sinfo.rxrate.legacy * 100,
360 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
361 		sbuf_printf(s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
362 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
363 		    sinfo.rxrate.eht_gi);
364 		sbuf_printf(s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
365 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
366 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
367 		    sinfo.txrate.legacy * 100,
368 		    sinfo.txrate.mcs, sinfo.txrate.nss);
369 		sbuf_printf(s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
370 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
371 		    sinfo.txrate.eht_gi);
372 	}
373 	wiphy_unlock(hw->wiphy);
374 }
375 
376 static int
377 lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
378 {
379 	struct lkpi_vif *lvif;
380 	struct sbuf s;
381 
382 	if (req->newptr)
383 		return (EPERM);
384 
385 	lvif = (struct lkpi_vif *)arg1;
386 
387 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
388 
389 	lkpi_80211_dump_lvif_stas(lvif, &s);
390 
391 	sbuf_finish(&s);
392 	sbuf_delete(&s);
393 
394 	return (0);
395 }
396 
397 static enum ieee80211_sta_rx_bw
398 lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
399 {
400 	switch (cw) {
401 	case NL80211_CHAN_WIDTH_320:
402 		return (IEEE80211_STA_RX_BW_320);
403 	case NL80211_CHAN_WIDTH_160:
404 	case NL80211_CHAN_WIDTH_80P80:
405 		return (IEEE80211_STA_RX_BW_160);
406 	case NL80211_CHAN_WIDTH_80:
407 		return (IEEE80211_STA_RX_BW_80);
408 	case NL80211_CHAN_WIDTH_40:
409 		return (IEEE80211_STA_RX_BW_40);
410 	case NL80211_CHAN_WIDTH_20:
411 	case NL80211_CHAN_WIDTH_20_NOHT:
412 		return (IEEE80211_STA_RX_BW_20);
413 	case NL80211_CHAN_WIDTH_5:
414 	case NL80211_CHAN_WIDTH_10:
415 		/* Unsupported input. */
416 		return (IEEE80211_STA_RX_BW_20);
417 	}
418 }
419 
420 static enum nl80211_chan_width
421 lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bw rx_bw)
422 {
423 	switch (rx_bw) {
424 	case IEEE80211_STA_RX_BW_20:
425 		return (NL80211_CHAN_WIDTH_20);	/* _NOHT */
426 	case IEEE80211_STA_RX_BW_40:
427 		return (NL80211_CHAN_WIDTH_40);
428 	case IEEE80211_STA_RX_BW_80:
429 		return (NL80211_CHAN_WIDTH_80);
430 	case IEEE80211_STA_RX_BW_160:
431 		return (NL80211_CHAN_WIDTH_160); /* 80P80 */
432 	case IEEE80211_STA_RX_BW_320:
433 		return (NL80211_CHAN_WIDTH_320);
434 	}
435 }
436 
437 static void
438 lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
439     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
440 {
441 	struct ieee80211_chanctx_conf *chanctx_conf;
442 	enum ieee80211_sta_rx_bw old_bw;
443 	uint32_t changed;
444 
445 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
446 	    lockdep_is_held(&hw->wiphy->mtx));
447 	if (chanctx_conf == NULL)
448 		return;
449 
450 	old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
451 	if (old_bw == sta->deflink.bandwidth)
452 		return;
453 
454 	chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
455 	if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
456 	    !sta->deflink.ht_cap.ht_supported)
457 		chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
458 
459 	chanctx_conf->min_def = chanctx_conf->def;
460 
461 	vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
462 
463 	changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
464 	changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
465 	lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
466 }
467 
468 #if defined(LKPI_80211_HT)
469 static void
470 lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
471     struct ieee80211_node *ni)
472 {
473 	struct ieee80211vap *vap;
474 	uint8_t *ie;
475 	struct ieee80211_ht_cap *htcap;
476 	int i, rx_nss;
477 
478 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
479 		sta->deflink.ht_cap.ht_supported = false;
480 		return;
481 	}
482 
483 	sta->deflink.ht_cap.ht_supported = true;
484 
485 	/* htcap->ampdu_params_info */
486 	vap = ni->ni_vap;
487 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
488 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
489 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
490 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
491 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
492 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
493 
494 	ie = ni->ni_ies.htcap_ie;
495 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
496 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
497 		ie += 4;
498 	ie += 2;
499 	htcap = (struct ieee80211_ht_cap *)ie;
500 	sta->deflink.ht_cap.cap = htcap->cap_info;
501 	sta->deflink.ht_cap.mcs = htcap->mcs;
502 
503 	/*
504 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
505 	 * optional MCS for Nss=1..4.  We need to check the first four
506 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
507 	 * MCS33.. is UEQM.
508 	 */
509 	rx_nss = 0;
510 	for (i = 0; i < 4; i++) {
511 		if (htcap->mcs.rx_mask[i] != 0)
512 			rx_nss++;
513 	}
514 	if (rx_nss > 0) {
515 		sta->deflink.rx_nss = rx_nss;
516 	} else {
517 		sta->deflink.ht_cap.ht_supported = false;
518 		return;
519 	}
520 
521 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
522 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
523 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
524 	else
525 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
526 
527 	IMPROVE("sta->wme");
528 
529 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
530 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
531 	else
532 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
533 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
534 #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
535 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
536 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
537 	}
538 #endif
539 }
540 #endif
541 
542 #if defined(LKPI_80211_VHT)
543 static void
544 lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
545     struct ieee80211_node *ni)
546 {
547 	enum ieee80211_sta_rx_bw bw;
548 	uint32_t width;
549 	int rx_nss;
550 	uint16_t rx_mcs_map;
551 	uint8_t mcs;
552 
553 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
554 	    !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
555 		sta->deflink.vht_cap.vht_supported = false;
556 		return;
557 	}
558 
559 	sta->deflink.vht_cap.vht_supported = true;
560 
561 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
562 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
563 
564 	/*
565 	 * If VHT20/40 are selected do not update the bandwidth
566 	 * from HT but stya on VHT.
567 	 */
568 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
569 		goto skip_bw;
570 
571 	bw = sta->deflink.bandwidth;
572 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
573 	switch (width) {
574 	/* Deprecated. */
575 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
576 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
577 		bw = IEEE80211_STA_RX_BW_160;
578 		break;
579 	default:
580 		/* Check if we do support 160Mhz somehow after all. */
581 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
582 			bw = IEEE80211_STA_RX_BW_160;
583 		else
584 			bw = IEEE80211_STA_RX_BW_80;
585 	}
586 	/*
587 	 * While we can set what is possibly supported we also need to be
588 	 * on a channel which supports that bandwidth; e.g., we can support
589 	 * VHT160 but the AP only does VHT80.
590 	 * Further ni_chan will also have filtered out what we disabled
591 	 * by configuration.
592 	 * Once net80211 channel selection is fixed for 802.11-2020 and
593 	 * VHT160 we can possibly spare ourselves the above.
594 	 */
595 	if (bw == IEEE80211_STA_RX_BW_160 &&
596 	    !IEEE80211_IS_CHAN_VHT160(ni->ni_chan) &&
597 	    !IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
598 		bw = IEEE80211_STA_RX_BW_80;
599 	if (bw == IEEE80211_STA_RX_BW_80 &&
600 	    !IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
601 		bw = sta->deflink.bandwidth;
602 	sta->deflink.bandwidth = bw;
603 skip_bw:
604 
605 	rx_nss = 0;
606 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
607 	for (int i = 7; i >= 0; i--) {
608 		mcs = rx_mcs_map >> (2 * i);
609 		mcs &= 0x3;
610 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
611 			rx_nss = i + 1;
612 			break;
613 		}
614 	}
615 	if (rx_nss > 0)
616 		sta->deflink.rx_nss = rx_nss;
617 
618 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
619 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
620 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
621 		break;
622 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
623 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
624 		break;
625 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
626 	default:
627 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
628 		break;
629 	}
630 }
631 #endif
632 
633 static void
634 lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
635     struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
636 {
637 
638 #if defined(LKPI_80211_HT)
639 	lkpi_sta_sync_ht_from_ni(vif, sta, ni);
640 #endif
641 #if defined(LKPI_80211_VHT)
642 	lkpi_sta_sync_vht_from_ni(vif, sta, ni);
643 #endif
644 
645 	/*
646 	 * Ensure rx_nss is at least 1 as otherwise drivers run into
647 	 * unexpected problems.
648 	 */
649 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
650 
651 	/*
652 	 * We are also called from node allocation which net80211
653 	 * can do even on `ifconfig down`; in that case the chanctx
654 	 * may still be valid and we get a discrepancy between
655 	 * sta and chanctx.  Thus do not try to update the chanctx
656 	 * when called from lkpi_lsta_alloc().
657 	 */
658 	if (updchnctx)
659 		lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
660 }
661 
662 static uint8_t
663 lkpi_get_max_rx_chains(struct ieee80211_node *ni)
664 {
665 	uint8_t chains;
666 #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
667 	struct lkpi_sta *lsta;
668 	struct ieee80211_sta *sta;
669 
670 	lsta = ni->ni_drv_data;
671 	sta = LSTA_TO_STA(lsta);
672 #endif
673 
674 	chains = 1;
675 #if defined(LKPI_80211_HT)
676 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
677 	if (sta->deflink.ht_cap.ht_supported)
678 		chains = MAX(chains, sta->deflink.rx_nss);
679 #endif
680 
681 #if defined(LKPI_80211_VHT)
682 	if (sta->deflink.vht_cap.vht_supported)
683 		chains = MAX(chains, sta->deflink.rx_nss);
684 #endif
685 
686 	return (chains);
687 }
688 
689 static void
690 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
691     const char *_f, int _l)
692 {
693 
694 #ifdef LINUXKPI_DEBUG_80211
695 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
696 		return;
697 	if (lsta == NULL)
698 		return;
699 
700 	printf("%s:%d lsta %p ni %p sta %p\n",
701 	    _f, _l, lsta, ni, &lsta->sta);
702 	if (ni != NULL)
703 		ieee80211_dump_node(NULL, ni);
704 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
705 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
706 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
707 #endif
708 }
709 
710 static void
711 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
712 {
713 
714 	lockdep_assert_wiphy(lsta->hw->wiphy);
715 
716 	KASSERT(!list_empty(&lsta->lsta_list),
717 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
718 	list_del_init(&lsta->lsta_list);
719 }
720 
721 static struct lkpi_sta *
722 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
723     struct ieee80211_hw *hw, struct ieee80211_node *ni)
724 {
725 	struct lkpi_sta *lsta;
726 	struct lkpi_vif *lvif;
727 	struct ieee80211_vif *vif;
728 	struct ieee80211_sta *sta;
729 	int band, i, tid;
730 
731 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
732 	    M_NOWAIT | M_ZERO);
733 	if (lsta == NULL)
734 		return (NULL);
735 
736 	lsta->hw = hw;
737 	lsta->added_to_drv = false;
738 	lsta->state = IEEE80211_STA_NOTEXIST;
739 	/*
740 	 * Link the ni to the lsta here without taking a reference.
741 	 * For one we would have to take the reference in node_init()
742 	 * as ieee80211_alloc_node() will initialise the refcount after us.
743 	 * For the other a ni and an lsta are 1:1 mapped and always together
744 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
745 	 * using the ni references for the lsta as well despite it being
746 	 * two separate allocations.
747 	 */
748 	lsta->ni = ni;
749 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
750 	ni->ni_drv_data = lsta;
751 
752 	lvif = VAP_TO_LVIF(vap);
753 	vif = LVIF_TO_VIF(lvif);
754 	sta = LSTA_TO_STA(lsta);
755 
756 	IEEE80211_ADDR_COPY(sta->addr, mac);
757 
758 	/* TXQ */
759 	for (tid = 0; tid < nitems(sta->txq); tid++) {
760 		struct lkpi_txq *ltxq;
761 
762 		/* We are not limiting ourselves to hw.queues here. */
763 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
764 		    M_LKPI80211, M_NOWAIT | M_ZERO);
765 		if (ltxq == NULL)
766 			goto cleanup;
767 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
768 		if (tid == IEEE80211_NUM_TIDS) {
769 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
770 				free(ltxq, M_LKPI80211);
771 				continue;
772 			}
773 			IMPROVE("AP/if we support non-STA here too");
774 			ltxq->txq.ac = IEEE80211_AC_VO;
775 		} else {
776 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
777 		}
778 		ltxq->seen_dequeue = false;
779 		ltxq->stopped = false;
780 		ltxq->txq.vif = vif;
781 		ltxq->txq.tid = tid;
782 		ltxq->txq.sta = sta;
783 		TAILQ_ELEM_INIT(ltxq, txq_entry);
784 		skb_queue_head_init(&ltxq->skbq);
785 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
786 		sta->txq[tid] = &ltxq->txq;
787 	}
788 
789 	/* Deflink information. */
790 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
791 		struct ieee80211_supported_band *supband;
792 
793 		supband = hw->wiphy->bands[band];
794 		if (supband == NULL)
795 			continue;
796 
797 		for (i = 0; i < supband->n_bitrates; i++) {
798 			switch (band) {
799 			case NL80211_BAND_2GHZ:
800 				switch (supband->bitrates[i].bitrate) {
801 				case 240:	/* 11g only */
802 				case 120:	/* 11g only */
803 				case 110:
804 				case 60:	/* 11g only */
805 				case 55:
806 				case 20:
807 				case 10:
808 					sta->deflink.supp_rates[band] |= BIT(i);
809 					break;
810 				}
811 				break;
812 			case NL80211_BAND_5GHZ:
813 				switch (supband->bitrates[i].bitrate) {
814 				case 240:
815 				case 120:
816 				case 60:
817 					sta->deflink.supp_rates[band] |= BIT(i);
818 					break;
819 				}
820 				break;
821 			}
822 		}
823 	}
824 
825 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
826 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
827 	sta->deflink.rx_nss = 1;
828 
829 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
830 
831 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
832 
833 	/* Link configuration. */
834 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
835 	sta->link[0] = &sta->deflink;
836 	for (i = 1; i < nitems(sta->link); i++) {
837 		IMPROVE("more links; only link[0] = deflink currently.");
838 	}
839 	IMPROVE("11be");
840 	sta->mlo = false;
841 
842 	/* Deferred TX path. */
843 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
844 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
845 	mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
846 	lsta->txq_ready = true;
847 
848 	return (lsta);
849 
850 cleanup:
851 	for (; tid >= 0; tid--) {
852 		struct lkpi_txq *ltxq;
853 
854 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
855 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
856 		free(sta->txq[tid], M_LKPI80211);
857 	}
858 	free(lsta, M_LKPI80211);
859 	return (NULL);
860 }
861 
862 static void
863 lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
864 {
865 	struct mbuf *m;
866 
867 	if (lsta->added_to_drv)
868 		panic("%s: Trying to free an lsta still known to firmware: "
869 		    "lsta %p ni %p added_to_drv %d\n",
870 		    __func__, lsta, ni, lsta->added_to_drv);
871 
872 	/* XXX-BZ free resources, ... */
873 	IMPROVE();
874 
875 	/* Drain sta->txq[] */
876 
877 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
878 	lsta->txq_ready = false;
879 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
880 
881 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
882 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
883 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
884 
885 	/* Flush mbufq (make sure to release ni refs!). */
886 	m = mbufq_dequeue(&lsta->txq);
887 	while (m != NULL) {
888 		struct ieee80211_node *nim;
889 
890 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
891 		if (nim != NULL)
892 			ieee80211_free_node(nim);
893 		m_freem(m);
894 		m = mbufq_dequeue(&lsta->txq);
895 	}
896 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
897 	    __func__, lsta, mbufq_len(&lsta->txq)));
898 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
899 
900 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
901 
902 	IMPROVE("Make sure everything is cleaned up.");
903 
904 	/* Free lsta. */
905 	lsta->ni = NULL;
906 	ni->ni_drv_data = NULL;
907 	free(lsta, M_LKPI80211);
908 }
909 
910 
911 static enum nl80211_band
912 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
913 {
914 
915 	if (IEEE80211_IS_CHAN_2GHZ(c))
916 		return (NL80211_BAND_2GHZ);
917 	else if (IEEE80211_IS_CHAN_5GHZ(c))
918 		return (NL80211_BAND_5GHZ);
919 #ifdef __notyet__
920 	else if ()
921 		return (NL80211_BAND_6GHZ);
922 	else if ()
923 		return (NL80211_BAND_60GHZ);
924 	else if (IEEE80211_IS_CHAN_GSM(c))
925 		return (NL80211_BAND_XXX);
926 #endif
927 	else
928 		panic("%s: unsupported band. c %p flags %#x\n",
929 		    __func__, c, c->ic_flags);
930 }
931 
932 static uint32_t
933 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
934 {
935 
936 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
937 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
938 	switch (band) {
939 	case NL80211_BAND_2GHZ:
940 		return (IEEE80211_CHAN_2GHZ);
941 		break;
942 	case NL80211_BAND_5GHZ:
943 		return (IEEE80211_CHAN_5GHZ);
944 		break;
945 	case NL80211_BAND_60GHZ:
946 		break;
947 	case NL80211_BAND_6GHZ:
948 		break;
949 	default:
950 		panic("%s: unsupported band %u\n", __func__, band);
951 		break;
952 	}
953 
954 	IMPROVE();
955 	return (0x00);
956 }
957 
958 #if 0
959 static enum ieee80211_ac_numbers
960 lkpi_ac_net_to_l80211(int ac)
961 {
962 
963 	switch (ac) {
964 	case WME_AC_VO:
965 		return (IEEE80211_AC_VO);
966 	case WME_AC_VI:
967 		return (IEEE80211_AC_VI);
968 	case WME_AC_BE:
969 		return (IEEE80211_AC_BE);
970 	case WME_AC_BK:
971 		return (IEEE80211_AC_BK);
972 	default:
973 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
974 		return (IEEE80211_AC_BE);
975 	}
976 }
977 #endif
978 
979 static enum nl80211_iftype
980 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
981 {
982 
983 	switch (opmode) {
984 	case IEEE80211_M_IBSS:
985 		return (NL80211_IFTYPE_ADHOC);
986 		break;
987 	case IEEE80211_M_STA:
988 		return (NL80211_IFTYPE_STATION);
989 		break;
990 	case IEEE80211_M_WDS:
991 		return (NL80211_IFTYPE_WDS);
992 		break;
993 	case IEEE80211_M_HOSTAP:
994 		return (NL80211_IFTYPE_AP);
995 		break;
996 	case IEEE80211_M_MONITOR:
997 		return (NL80211_IFTYPE_MONITOR);
998 		break;
999 	case IEEE80211_M_MBSS:
1000 		return (NL80211_IFTYPE_MESH_POINT);
1001 		break;
1002 	case IEEE80211_M_AHDEMO:
1003 		/* FALLTHROUGH */
1004 	default:
1005 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
1006 		/* FALLTHROUGH */
1007 	}
1008 	return (NL80211_IFTYPE_UNSPECIFIED);
1009 }
1010 
1011 #ifdef LKPI_80211_HW_CRYPTO
1012 static const char *
1013 lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
1014 {
1015 	switch (wlan_cipher_suite) {
1016 	case WLAN_CIPHER_SUITE_WEP40:
1017 		return ("WEP40");
1018 	case WLAN_CIPHER_SUITE_WEP104:
1019 		return ("WEP104");
1020 	case WLAN_CIPHER_SUITE_TKIP:
1021 		return ("TKIP");
1022 	case WLAN_CIPHER_SUITE_CCMP:
1023 		return ("CCMP");
1024 	case WLAN_CIPHER_SUITE_CCMP_256:
1025 		return ("CCMP_256");
1026 	case WLAN_CIPHER_SUITE_GCMP:
1027 		return ("GCMP");
1028 	case WLAN_CIPHER_SUITE_GCMP_256:
1029 		return ("GCMP_256");
1030 	case WLAN_CIPHER_SUITE_AES_CMAC:
1031 		return ("AES_CMAC");
1032 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1033 		return ("BIP_CMAC_256");
1034 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1035 		return ("BIP_GMAC_128");
1036 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1037 		return ("BIP_GMAC_256");
1038 	default:
1039 		return ("??");
1040 	}
1041 }
1042 
1043 static uint32_t
1044 lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
1045     uint32_t wlan_cipher_suite)
1046 {
1047 	switch (wlan_cipher_suite) {
1048 	case WLAN_CIPHER_SUITE_WEP40:
1049 		return (IEEE80211_CRYPTO_WEP);
1050 	case WLAN_CIPHER_SUITE_WEP104:
1051 		return (IEEE80211_CRYPTO_WEP);
1052 	case WLAN_CIPHER_SUITE_TKIP:
1053 		return (IEEE80211_CRYPTO_TKIP);
1054 	case WLAN_CIPHER_SUITE_CCMP:
1055 		return (IEEE80211_CRYPTO_AES_CCM);
1056 	case WLAN_CIPHER_SUITE_CCMP_256:
1057 		return (IEEE80211_CRYPTO_AES_CCM_256);
1058 	case WLAN_CIPHER_SUITE_GCMP:
1059 		return (IEEE80211_CRYPTO_AES_GCM_128);
1060 	case WLAN_CIPHER_SUITE_GCMP_256:
1061 		return (IEEE80211_CRYPTO_AES_GCM_256);
1062 	case WLAN_CIPHER_SUITE_AES_CMAC:
1063 		return (IEEE80211_CRYPTO_BIP_CMAC_128);
1064 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1065 		return (IEEE80211_CRYPTO_BIP_CMAC_256);
1066 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1067 		return (IEEE80211_CRYPTO_BIP_GMAC_128);
1068 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1069 		return (IEEE80211_CRYPTO_BIP_GMAC_256);
1070 	default:
1071 		ic_printf(ic, "%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
1072 		    __func__,
1073 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
1074 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
1075 		return (0);
1076 	}
1077 }
1078 
1079 static uint32_t
1080 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
1081 {
1082 
1083 	switch (cipher) {
1084 	case IEEE80211_CIPHER_WEP:
1085 		if (keylen == (40/NBBY))
1086 			return (WLAN_CIPHER_SUITE_WEP40);
1087 		else if (keylen == (104/NBBY))
1088 			return (WLAN_CIPHER_SUITE_WEP104);
1089 		else {
1090 			printf("%s: WEP with unsupported keylen %d\n",
1091 			    __func__, keylen * NBBY);
1092 			return (0);
1093 		}
1094 		break;
1095 	case IEEE80211_CIPHER_TKIP:
1096 		return (WLAN_CIPHER_SUITE_TKIP);
1097 	case IEEE80211_CIPHER_AES_CCM:
1098 		return (WLAN_CIPHER_SUITE_CCMP);
1099 	case IEEE80211_CIPHER_AES_CCM_256:
1100 		return (WLAN_CIPHER_SUITE_CCMP_256);
1101 	case IEEE80211_CIPHER_AES_GCM_128:
1102 		return (WLAN_CIPHER_SUITE_GCMP);
1103 	case IEEE80211_CIPHER_AES_GCM_256:
1104 		return (WLAN_CIPHER_SUITE_GCMP_256);
1105 	case IEEE80211_CIPHER_BIP_CMAC_128:
1106 		return (WLAN_CIPHER_SUITE_AES_CMAC);
1107 	case IEEE80211_CIPHER_BIP_CMAC_256:
1108 		return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1109 	case IEEE80211_CIPHER_BIP_GMAC_128:
1110 		return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1111 	case IEEE80211_CIPHER_BIP_GMAC_256:
1112 		return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1113 
1114 	case IEEE80211_CIPHER_AES_OCB:
1115 	case IEEE80211_CIPHER_TKIPMIC:
1116 		/*
1117 		 * TKIP w/ hw MIC support
1118 		 * (gone wrong; should really be a crypto flag in net80211).
1119 		 */
1120 	case IEEE80211_CIPHER_CKIP:
1121 	case IEEE80211_CIPHER_NONE:
1122 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
1123 		break;
1124 	default:
1125 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
1126 	};
1127 	return (0);
1128 }
1129 #endif
1130 
1131 #ifdef __notyet__
1132 static enum ieee80211_sta_state
1133 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
1134 {
1135 
1136 	/*
1137 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
1138 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
1139 	 */
1140 	switch (state) {
1141 	case IEEE80211_S_INIT:
1142 		return (IEEE80211_STA_NOTEXIST);
1143 	case IEEE80211_S_SCAN:
1144 		return (IEEE80211_STA_NONE);
1145 	case IEEE80211_S_AUTH:
1146 		return (IEEE80211_STA_AUTH);
1147 	case IEEE80211_S_ASSOC:
1148 		return (IEEE80211_STA_ASSOC);
1149 	case IEEE80211_S_RUN:
1150 		return (IEEE80211_STA_AUTHORIZED);
1151 	case IEEE80211_S_CAC:
1152 	case IEEE80211_S_CSA:
1153 	case IEEE80211_S_SLEEP:
1154 	default:
1155 		UNIMPLEMENTED;
1156 	};
1157 
1158 	return (IEEE80211_STA_NOTEXIST);
1159 }
1160 #endif
1161 
1162 static struct linuxkpi_ieee80211_channel *
1163 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
1164     struct ieee80211_channel *c)
1165 {
1166 	struct ieee80211_hw *hw;
1167 	struct linuxkpi_ieee80211_channel *channels;
1168 	enum nl80211_band band;
1169 	int i, nchans;
1170 
1171 	hw = LHW_TO_HW(lhw);
1172 	band = lkpi_net80211_chan_to_nl80211_band(c);
1173 	if (hw->wiphy->bands[band] == NULL)
1174 		return (NULL);
1175 
1176 	nchans = hw->wiphy->bands[band]->n_channels;
1177 	if (nchans <= 0)
1178 		return (NULL);
1179 
1180 	channels = hw->wiphy->bands[band]->channels;
1181 	for (i = 0; i < nchans; i++) {
1182 		if (channels[i].center_freq == c->ic_freq)
1183 			return (&channels[i]);
1184 	}
1185 
1186 	return (NULL);
1187 }
1188 
1189 #if 0
1190 static struct linuxkpi_ieee80211_channel *
1191 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
1192 {
1193 	struct linuxkpi_ieee80211_channel *chan;
1194 	struct ieee80211_channel *c;
1195 	struct lkpi_hw *lhw;
1196 
1197 	chan = NULL;
1198 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
1199 		c = ni->ni_chan;
1200 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1201 		c = ic->ic_bsschan;
1202 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
1203 		c = ic->ic_curchan;
1204 	else
1205 		c = NULL;
1206 
1207 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
1208 		lhw = ic->ic_softc;
1209 		chan = lkpi_find_lkpi80211_chan(lhw, c);
1210 	}
1211 
1212 	return (chan);
1213 }
1214 #endif
1215 
1216 struct linuxkpi_ieee80211_channel *
1217 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
1218 {
1219 	enum nl80211_band band;
1220 
1221 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1222 		struct ieee80211_supported_band *supband;
1223 		struct linuxkpi_ieee80211_channel *channels;
1224 		int i;
1225 
1226 		supband = wiphy->bands[band];
1227 		if (supband == NULL || supband->n_channels == 0)
1228 			continue;
1229 
1230 		channels = supband->channels;
1231 		for (i = 0; i < supband->n_channels; i++) {
1232 			if (channels[i].center_freq == freq)
1233 				return (&channels[i]);
1234 		}
1235 	}
1236 
1237 	return (NULL);
1238 }
1239 
1240 #ifdef LKPI_80211_HW_CRYPTO
1241 static int
1242 lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1243     struct lkpi_sta *lsta)
1244 {
1245 	int error;
1246 
1247 	if (!lkpi_hwcrypto)
1248 		return (0);
1249 
1250 	lockdep_assert_wiphy(hw->wiphy);
1251 	ieee80211_ref_node(lsta->ni);
1252 
1253 	error = 0;
1254 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
1255 		struct ieee80211_key_conf *kc;
1256 		int err;
1257 
1258 		if (lsta->kc[keyix] == NULL)
1259 			continue;
1260 		kc = lsta->kc[keyix];
1261 
1262 #ifdef LINUXKPI_DEBUG_80211
1263 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1264 			ic_printf(lsta->ni->ni_ic, "%d %lu %s: running set_key cmd %d(%s) for "
1265 			    "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1266 			    curthread->td_tid, jiffies, __func__,
1267 			    DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1268 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1269 #endif
1270 
1271 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
1272 		    LSTA_TO_STA(lsta), kc);
1273 		if (err != 0) {
1274 			ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1275 			    "sta %6D failed: %d\n", curthread->td_tid, jiffies, __func__,
1276 			    DISABLE_KEY, "DISABLE", lsta->sta.addr, ":", err);
1277 			error++;
1278 
1279 			/*
1280 			 * If we free the key here we will never be able to get it
1281 			 * removed from the driver/fw which will likely make us
1282 			 * crash (firmware).
1283 			 */
1284 			continue;
1285 		}
1286 #ifdef LINUXKPI_DEBUG_80211
1287 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1288 			ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1289 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
1290 			    curthread->td_tid, jiffies, __func__,
1291 			    DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1292 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1293 #endif
1294 
1295 		lsta->kc[keyix] = NULL;
1296 		free(kc, M_LKPI80211);
1297 	}
1298 	ieee80211_free_node(lsta->ni);
1299 	return (error);
1300 }
1301 
1302 /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
1303 /* See also lkpi_sta_del_keys() these days. */
1304 static int
1305 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1306 {
1307 	struct ieee80211com *ic;
1308 	struct lkpi_hw *lhw;
1309 	struct ieee80211_hw *hw;
1310 	struct lkpi_vif *lvif;
1311 	struct lkpi_sta *lsta;
1312 	struct ieee80211_vif *vif;
1313 	struct ieee80211_sta *sta;
1314 	struct ieee80211_node *ni;
1315 	struct ieee80211_key_conf *kc;
1316 	int error;
1317 
1318 	ic = vap->iv_ic;
1319 	lhw = ic->ic_softc;
1320 	hw = LHW_TO_HW(lhw);
1321 	lvif = VAP_TO_LVIF(vap);
1322 
1323 	/*
1324 	 * Make sure we do not make it here without going through
1325 	 * lkpi_iv_key_update_begin() first.
1326 	 */
1327 	lockdep_assert_wiphy(hw->wiphy);
1328 
1329 	if (IEEE80211_KEY_UNDEFINED(k)) {
1330 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1331 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1332 		return (0);
1333 	}
1334 
1335 	if (vap->iv_bss == NULL) {
1336 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1337 		    __func__, vap->iv_bss, vap);
1338 		return (0);
1339 	}
1340 
1341 	ni = ieee80211_ref_node(vap->iv_bss);
1342 	lsta = ni->ni_drv_data;
1343 	if (lsta == NULL) {
1344 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1345 		    __func__, ni, ni->ni_bssid, ":");
1346 		ieee80211_free_node(ni);
1347 		return (0);
1348 	}
1349 	sta = LSTA_TO_STA(lsta);
1350 
1351 	if (lsta->kc[k->wk_keyix] == NULL) {
1352 #ifdef LINUXKPI_DEBUG_80211
1353 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1354 			ic_printf(ic, "%d %lu %s: sta %6D and no key information, "
1355 			    "keyidx %u wk_macaddr %6D; returning success\n",
1356 			    curthread->td_tid, jiffies, __func__, sta->addr, ":",
1357 			    k->wk_keyix, k->wk_macaddr, ":");
1358 #endif
1359 		ieee80211_free_node(ni);
1360 		return (1);
1361 	}
1362 	kc = lsta->kc[k->wk_keyix];
1363 
1364 #ifdef LINUXKPI_DEBUG_80211
1365 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1366 		ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1367 		    "keyidx %u hw_key_idx %u flags %b\n",
1368 		    curthread->td_tid, jiffies, __func__,
1369 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1370 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1371 #endif
1372 
1373 	vif = LVIF_TO_VIF(lvif);
1374 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
1375 	if (error != 0) {
1376 		ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1377 		    curthread->td_tid, jiffies, __func__,
1378 		    DISABLE_KEY, "DISABLE", sta->addr, ":", error);
1379 		error = 0;
1380 		goto out;
1381 	}
1382 
1383 #ifdef LINUXKPI_DEBUG_80211
1384 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1385 		ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1386 		    "keyidx %u hw_key_idx %u flags %b\n",
1387 		    curthread->td_tid, jiffies, __func__,
1388 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1389 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1390 #endif
1391 	lsta->kc[k->wk_keyix] = NULL;
1392 	free(kc, M_LKPI80211);
1393 	error = 1;
1394 out:
1395 	ieee80211_free_node(ni);
1396 	return (error);
1397 }
1398 
1399 static int
1400 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1401 {
1402 	struct ieee80211com *ic;
1403 	struct lkpi_hw *lhw;
1404 	struct ieee80211_hw *hw;
1405 	struct lkpi_vif *lvif;
1406 	struct lkpi_sta *lsta;
1407 	struct ieee80211_vif *vif;
1408 	struct ieee80211_sta *sta;
1409 	struct ieee80211_node *ni;
1410 	struct ieee80211_key_conf *kc;
1411 	uint32_t lcipher;
1412 	uint16_t exp_flags;
1413 	uint8_t keylen;
1414 	int error;
1415 
1416 	ic = vap->iv_ic;
1417 	lhw = ic->ic_softc;
1418 	hw = LHW_TO_HW(lhw);
1419 
1420 	/*
1421 	 * Make sure we do not make it here without going through
1422 	 * lkpi_iv_key_update_begin() first.
1423 	 */
1424 	lockdep_assert_wiphy(hw->wiphy);
1425 
1426 	if (IEEE80211_KEY_UNDEFINED(k)) {
1427 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1428 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1429 		return (0);
1430 	}
1431 
1432 	if (vap->iv_bss == NULL) {
1433 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1434 		    __func__, vap->iv_bss, vap);
1435 		return (0);
1436 	}
1437 	ni = ieee80211_ref_node(vap->iv_bss);
1438 	lsta = ni->ni_drv_data;
1439 	if (lsta == NULL) {
1440 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1441 		    __func__, ni, ni->ni_bssid, ":");
1442 		ieee80211_free_node(ni);
1443 		return (0);
1444 	}
1445 	sta = LSTA_TO_STA(lsta);
1446 
1447 	keylen = k->wk_keylen;
1448 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
1449 	    k->wk_cipher->ic_cipher, k->wk_keylen);
1450 	switch (lcipher) {
1451 	case WLAN_CIPHER_SUITE_TKIP:
1452 		keylen += 2 * k->wk_cipher->ic_miclen;
1453 		break;
1454 	case WLAN_CIPHER_SUITE_CCMP:
1455 	case WLAN_CIPHER_SUITE_GCMP:
1456 		break;
1457 	default:
1458 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
1459 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
1460 		IMPROVE();
1461 		ieee80211_free_node(ni);
1462 		return (0);
1463 	}
1464 
1465 	if (lsta->kc[k->wk_keyix] != NULL) {
1466 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
1467 		ic_printf(ic, "%s: sta %6D found with key information\n",
1468 		    __func__, sta->addr, ":");
1469 		kc = lsta->kc[k->wk_keyix];
1470 		lsta->kc[k->wk_keyix] = NULL;
1471 		free(kc, M_LKPI80211);
1472 		kc = NULL;	/* safeguard */
1473 	}
1474 
1475 	kc = malloc(sizeof(*kc) + keylen, M_LKPI80211, M_WAITOK | M_ZERO);
1476 	kc->_k = k;		/* Save the pointer to net80211. */
1477 	kc->cipher = lcipher;
1478 	kc->keyidx = k->wk_keyix;
1479 #if 0
1480 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
1481 #endif
1482 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
1483 	kc->keylen = k->wk_keylen;
1484 	memcpy(kc->key, k->wk_key, k->wk_keylen);
1485 
1486 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1487 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
1488 	if (k->wk_flags & IEEE80211_KEY_GROUP)
1489 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
1490 
1491 	kc->iv_len = k->wk_cipher->ic_header;
1492 	kc->icv_len = k->wk_cipher->ic_trailer;
1493 
1494 	switch (kc->cipher) {
1495 	case WLAN_CIPHER_SUITE_TKIP:
1496 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, k->wk_txmic, k->wk_cipher->ic_miclen);
1497 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, k->wk_rxmic, k->wk_cipher->ic_miclen);
1498 		break;
1499 	case WLAN_CIPHER_SUITE_CCMP:
1500 	case WLAN_CIPHER_SUITE_GCMP:
1501 		break;
1502 	default:
1503 		/* currently UNREACH */
1504 		IMPROVE();
1505 		break;
1506 	};
1507 	lsta->kc[k->wk_keyix] = kc;
1508 
1509 #ifdef LINUXKPI_DEBUG_80211
1510 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1511 		ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1512 		    "kc %p keyidx %u hw_key_idx %u keylen %u flags %b\n",
1513 		    curthread->td_tid, jiffies, __func__,
1514 		    SET_KEY, "SET", sta->addr, ":", kc, kc->keyidx, kc->hw_key_idx,
1515 		    kc->keylen, kc->flags, IEEE80211_KEY_FLAG_BITS);
1516 #endif
1517 
1518 	lvif = VAP_TO_LVIF(vap);
1519 	vif = LVIF_TO_VIF(lvif);
1520 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
1521 	if (error != 0) {
1522 		ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1523 		    curthread->td_tid, jiffies, __func__,
1524 		    SET_KEY, "SET", sta->addr, ":", error);
1525 		lsta->kc[k->wk_keyix] = NULL;
1526 		free(kc, M_LKPI80211);
1527 		ieee80211_free_node(ni);
1528 		return (0);
1529 	}
1530 
1531 #ifdef LINUXKPI_DEBUG_80211
1532 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1533 		ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1534 		    "kc %p keyidx %u hw_key_idx %u flags %b\n",
1535 		    curthread->td_tid, jiffies, __func__,
1536 		    SET_KEY, "SET", sta->addr, ":",
1537 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1538 #endif
1539 
1540 	exp_flags = 0;
1541 	switch (kc->cipher) {
1542 	case WLAN_CIPHER_SUITE_TKIP:
1543 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1544 			IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1545 			IEEE80211_KEY_FLAG_GENERATE_MMIC |
1546 			IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
1547 #define	TKIP_INVAL_COMBINATION						\
1548      (IEEE80211_KEY_FLAG_PUT_MIC_SPACE|IEEE80211_KEY_FLAG_GENERATE_MMIC)
1549 		if ((kc->flags & TKIP_INVAL_COMBINATION) == TKIP_INVAL_COMBINATION) {
1550 			ic_printf(ic, "%s: SET_KEY for %s returned invalid "
1551 			    "combination %b\n", __func__,
1552 			    lkpi_cipher_suite_to_name(kc->cipher),
1553 			    kc->flags, IEEE80211_KEY_FLAG_BITS);
1554 		}
1555 #undef	TKIP_INVAL_COMBINATION
1556 #ifdef __notyet__
1557 		/* Do flags surgery; special see linuxkpi_ieee80211_ifattach(). */
1558 		if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) != 0) {
1559 			k->wk_flags &= ~(IEEE80211_KEY_NOMICMGT|IEEE80211_KEY_NOMIC);
1560 			k->wk_flags |= IEEE80211_KEY_SWMIC;
1561 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC
1562 		}
1563 #endif
1564 		break;
1565 	case WLAN_CIPHER_SUITE_CCMP:
1566 	case WLAN_CIPHER_SUITE_GCMP:
1567 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1568 		    IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1569 		    IEEE80211_KEY_FLAG_GENERATE_IV |
1570 		    IEEE80211_KEY_FLAG_GENERATE_IV_MGMT |	/* Only needs IV geeration for MGMT frames. */
1571 		    IEEE80211_KEY_FLAG_SW_MGMT_TX);		/* MFP in software */
1572 		break;
1573 	}
1574 	if ((kc->flags & ~exp_flags) != 0)
1575 		ic_printf(ic, "%s: SET_KEY for %s returned unexpected key flags: "
1576 		    " %#06x & ~%#06x = %b\n", __func__,
1577 		    lkpi_cipher_suite_to_name(kc->cipher), kc->flags, exp_flags,
1578 		    (kc->flags & ~exp_flags), IEEE80211_KEY_FLAG_BITS);
1579 
1580 #ifdef __notyet__
1581 	/* Do flags surgery. */
1582 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) == 0)
1583 		k->wk_flags |= IEEE80211_KEY_NOIVMGT;
1584 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
1585 		k->wk_flags |= IEEE80211_KEY_NOIV;
1586 #endif
1587 
1588 	ieee80211_free_node(ni);
1589 	return (1);
1590 }
1591 
1592 static void
1593 lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1594 {
1595 	struct ieee80211_node_table *nt;
1596 	struct ieee80211com *ic;
1597 	struct lkpi_hw *lhw;
1598 	struct ieee80211_hw *hw;
1599 	struct lkpi_vif *lvif;
1600 	struct ieee80211_node *ni;
1601 	bool icislocked, ntislocked;
1602 
1603 	ic = vap->iv_ic;
1604 	lhw = ic->ic_softc;
1605 	hw = LHW_TO_HW(lhw);
1606 	lvif = VAP_TO_LVIF(vap);
1607 	nt = &ic->ic_sta;
1608 
1609 	icislocked = IEEE80211_IS_LOCKED(ic);
1610 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1611 
1612 #ifdef LINUXKPI_DEBUG_80211
1613 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1614 		ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
1615 		    "lvif ic_unlocked %d nt_unlocked %d\n",
1616 		    curthread->td_tid, jiffies, __func__, vap,
1617 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1618 		    lvif->ic_unlocked, lvif->nt_unlocked);
1619 #endif
1620 
1621 	/*
1622 	 * This is inconsistent net80211 locking to be fixed one day.
1623 	 */
1624 	/* Try to make sure the node does not go away while possibly unlocked. */
1625 	ni = NULL;
1626 	if (icislocked || ntislocked) {
1627 		if (vap->iv_bss != NULL)
1628 			ni = ieee80211_ref_node(vap->iv_bss);
1629 	}
1630 
1631 	if (icislocked)
1632 		IEEE80211_UNLOCK(ic);
1633 	if (ntislocked)
1634 		IEEE80211_NODE_UNLOCK(nt);
1635 
1636 	wiphy_lock(hw->wiphy);
1637 
1638 	KASSERT(lvif->key_update_iv_bss == NULL, ("%s: key_update_iv_bss not NULL %p",
1639 	    __func__, lvif->key_update_iv_bss));
1640 	lvif->key_update_iv_bss = ni;
1641 
1642 	/*
1643 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1644 	 * must only be a single thread.
1645 	 * In case anything in the future disturbs the order the refcnt will
1646 	 * help us catching problems a lot easier.
1647 	 */
1648 	if (icislocked)
1649 		refcount_acquire(&lvif->ic_unlocked);
1650 	if (ntislocked)
1651 		refcount_acquire(&lvif->nt_unlocked);
1652 
1653 	/*
1654 	 * Stop the queues while doing key updates.
1655 	 */
1656 	ieee80211_stop_queues(hw);
1657 }
1658 
1659 static void
1660 lkpi_iv_key_update_end(struct ieee80211vap *vap)
1661 {
1662 	struct ieee80211_node_table *nt;
1663 	struct ieee80211com *ic;
1664 	struct lkpi_hw *lhw;
1665 	struct ieee80211_hw *hw;
1666 	struct lkpi_vif *lvif;
1667 	bool icislocked, ntislocked;
1668 
1669 	ic = vap->iv_ic;
1670 	lhw = ic->ic_softc;
1671 	hw = LHW_TO_HW(lhw);
1672 	lvif = VAP_TO_LVIF(vap);
1673 	nt = &ic->ic_sta;
1674 
1675 	/*
1676 	 * Re-enabled the queues after the key update.
1677 	 */
1678 	lkpi_ieee80211_wake_queues_locked(hw);
1679 
1680 	icislocked = IEEE80211_IS_LOCKED(ic);
1681 	MPASS(!icislocked);
1682 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1683 	MPASS(!ntislocked);
1684 
1685 #ifdef LINUXKPI_DEBUG_80211
1686 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1687 		ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
1688 		    "lvif ic_unlocked %d nt_unlocked %d\n",
1689 		    curthread->td_tid, jiffies, __func__, vap,
1690 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1691 		    lvif->ic_unlocked, lvif->nt_unlocked);
1692 #endif
1693 
1694 	/*
1695 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1696 	 * In case the refcnt gets out of sync locking in net80211 will
1697 	 * quickly barf as well (trying to unlock a lock not held).
1698 	 */
1699 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1700 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1701 
1702 	if (lvif->key_update_iv_bss != NULL) {
1703 		ieee80211_free_node(lvif->key_update_iv_bss);
1704 		lvif->key_update_iv_bss = NULL;
1705 	}
1706 
1707 	wiphy_unlock(hw->wiphy);
1708 
1709 	/*
1710 	 * This is inconsistent net80211 locking to be fixed one day.
1711 	 * ic before nt to avoid a LOR.
1712 	 */
1713 	if (icislocked)
1714 		IEEE80211_LOCK(ic);
1715 	if (ntislocked)
1716 		IEEE80211_NODE_LOCK(nt);
1717 }
1718 #endif
1719 
1720 static void
1721 lkpi_cleanup_mcast_list_locked(struct lkpi_hw *lhw)
1722 {
1723 	struct list_head *le, *next;
1724 	struct netdev_hw_addr *addr;
1725 
1726 	if (lhw->mc_list.count != 0) {
1727 		list_for_each_safe(le, next, &lhw->mc_list.addr_list) {
1728 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
1729 			list_del(le);
1730 			lhw->mc_list.count--;
1731 			free(addr, M_LKPI80211);
1732 		}
1733 	}
1734 	KASSERT(lhw->mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
1735 	    __func__, &lhw->mc_list, lhw->mc_list.count));
1736 }
1737 
1738 static u_int
1739 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
1740 {
1741 	struct netdev_hw_addr_list *mc_list;
1742 	struct netdev_hw_addr *addr;
1743 
1744 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
1745 	    __func__, arg, sdl, cnt));
1746 
1747 	mc_list = arg;
1748 	/* If it is on the list already skip it. */
1749 	netdev_hw_addr_list_for_each(addr, mc_list) {
1750 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
1751 			return (0);
1752 	}
1753 
1754 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
1755 	if (addr == NULL)
1756 		return (0);
1757 
1758 	INIT_LIST_HEAD(&addr->addr_list);
1759 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
1760 	/* XXX this should be a netdev function? */
1761 	list_add(&addr->addr_list, &mc_list->addr_list);
1762 	mc_list->count++;
1763 
1764 #ifdef LINUXKPI_DEBUG_80211
1765 	if (linuxkpi_debug_80211 & D80211_TRACE)
1766 		printf("%s:%d: mc_list count %d: added %6D\n",
1767 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
1768 #endif
1769 
1770 	return (1);
1771 }
1772 
1773 static void
1774 lkpi_update_mcast_filter(struct ieee80211com *ic)
1775 {
1776 	struct lkpi_hw *lhw;
1777 	struct ieee80211_hw *hw;
1778 	u64 mc;
1779 	unsigned int changed_flags, flags;
1780 	bool scanning;
1781 
1782 	lhw = ic->ic_softc;
1783 
1784 	if (lhw->ops->prepare_multicast == NULL ||
1785 	    lhw->ops->configure_filter == NULL)
1786 		return;
1787 
1788 	LKPI_80211_LHW_SCAN_LOCK(lhw);
1789 	scanning = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1790 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1791 
1792 	LKPI_80211_LHW_MC_LOCK(lhw);
1793 
1794 	flags = 0;
1795 	if (scanning)
1796 		flags |= FIF_BCN_PRBRESP_PROMISC;
1797 	if (lhw->mc_all_multi)
1798 		flags |= FIF_ALLMULTI;
1799 
1800 	hw = LHW_TO_HW(lhw);
1801 	mc = lkpi_80211_mo_prepare_multicast(hw, &lhw->mc_list);
1802 
1803 	changed_flags = (lhw->mc_flags ^ flags) & FIF_FLAGS_MASK;
1804 	lkpi_80211_mo_configure_filter(hw, changed_flags, &flags, mc);
1805 	lhw->mc_flags = flags;
1806 
1807 #ifdef LINUXKPI_DEBUG_80211
1808 	if (linuxkpi_debug_80211 & D80211_TRACE)
1809 		printf("%s: changed_flags %#06x count %d mc_flags %#010x\n",
1810 		    __func__, changed_flags, lhw->mc_list.count, lhw->mc_flags);
1811 #endif
1812 
1813 	LKPI_80211_LHW_MC_UNLOCK(lhw);
1814 }
1815 
1816 static enum ieee80211_bss_changed
1817 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1818     struct ieee80211vap *vap, const char *_f, int _l)
1819 {
1820 	enum ieee80211_bss_changed bss_changed;
1821 
1822 	bss_changed = 0;
1823 
1824 #ifdef LINUXKPI_DEBUG_80211
1825 	if (linuxkpi_debug_80211 & D80211_TRACE)
1826 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1827 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1828 		    "sync_device_ts %u bss_changed %#010jx\n",
1829 			__func__, __LINE__, _f, _l,
1830 			vif->cfg.assoc, vif->cfg.aid,
1831 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1832 			vif->bss_conf.sync_dtim_count,
1833 			(uintmax_t)vif->bss_conf.sync_tsf,
1834 			vif->bss_conf.sync_device_ts,
1835 			(uintmax_t)bss_changed);
1836 #endif
1837 
1838 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1839 		vif->bss_conf.beacon_int = ni->ni_intval;
1840 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1841 		if (vif->bss_conf.beacon_int < 16)
1842 			vif->bss_conf.beacon_int = 16;
1843 		bss_changed |= BSS_CHANGED_BEACON_INT;
1844 	}
1845 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1846 	    vap->iv_dtim_period > 0) {
1847 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1848 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1849 	}
1850 
1851 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1852 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1853 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1854 
1855 #ifdef LINUXKPI_DEBUG_80211
1856 	if (linuxkpi_debug_80211 & D80211_TRACE)
1857 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1858 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1859 		    "sync_device_ts %u bss_changed %#010jx\n",
1860 			__func__, __LINE__, _f, _l,
1861 			vif->cfg.assoc, vif->cfg.aid,
1862 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1863 			vif->bss_conf.sync_dtim_count,
1864 			(uintmax_t)vif->bss_conf.sync_tsf,
1865 			vif->bss_conf.sync_device_ts,
1866 			(uintmax_t)bss_changed);
1867 #endif
1868 
1869 	return (bss_changed);
1870 }
1871 
1872 static void
1873 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
1874 {
1875 	struct ieee80211_hw *hw;
1876 	int error;
1877 	bool cancel;
1878 
1879 	LKPI_80211_LHW_SCAN_LOCK(lhw);
1880 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1881 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1882 	if (!cancel)
1883 		return;
1884 
1885 	hw = LHW_TO_HW(lhw);
1886 
1887 	IEEE80211_UNLOCK(lhw->ic);
1888 	wiphy_lock(hw->wiphy);
1889 	/* Need to cancel the scan. */
1890 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1891 	wiphy_unlock(hw->wiphy);
1892 
1893 	/* Need to make sure we see ieee80211_scan_completed. */
1894 	LKPI_80211_LHW_SCAN_LOCK(lhw);
1895 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
1896 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
1897 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1898 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1899 
1900 	IEEE80211_LOCK(lhw->ic);
1901 
1902 	if (cancel)
1903 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
1904 		    __func__, error, lhw, vif);
1905 }
1906 
1907 static void
1908 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1909 {
1910 	struct lkpi_hw *lhw;
1911 	int error;
1912 	bool old;
1913 
1914 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1915 	if (old == new)
1916 		return;
1917 
1918 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1919 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1920 	if (error != 0 && error != EOPNOTSUPP) {
1921 		lhw = HW_TO_LHW(hw);
1922 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1923 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1924 	}
1925 }
1926 
1927 static enum ieee80211_bss_changed
1928 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1929     struct lkpi_hw *lhw)
1930 {
1931 	enum ieee80211_bss_changed changed;
1932 	struct lkpi_vif *lvif;
1933 
1934 	changed = 0;
1935 	sta->aid = 0;
1936 	if (vif->cfg.assoc) {
1937 
1938 		vif->cfg.assoc = false;
1939 		vif->cfg.aid = 0;
1940 		changed |= BSS_CHANGED_ASSOC;
1941 		IMPROVE();
1942 
1943 		lkpi_update_mcast_filter(lhw->ic);
1944 
1945 		/*
1946 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
1947 		 * assoc = false right away here will remove the sta from
1948 		 * firmware for iwlwifi.
1949 		 * We no longer do this but only return the BSS_CHNAGED value.
1950 		 * The caller is responsible for removing the sta gong to
1951 		 * IEEE80211_STA_NOTEXIST and then executing the
1952 		 * bss_info_changed() update.
1953 		 * See lkpi_sta_run_to_init() for more detailed comment.
1954 		 */
1955 
1956 		lvif = VIF_TO_LVIF(vif);
1957 		lvif->beacons = 0;
1958 	}
1959 
1960 	return (changed);
1961 }
1962 
1963 static void
1964 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
1965     bool dequeue_seen, bool no_emptyq)
1966 {
1967 	struct lkpi_txq *ltxq;
1968 	int tid;
1969 	bool ltxq_empty;
1970 
1971 	/* Wake up all queues to know they are allocated in the driver. */
1972 	for (tid = 0; tid < nitems(sta->txq); tid++) {
1973 
1974 		if (tid == IEEE80211_NUM_TIDS) {
1975 			IMPROVE("station specific?");
1976 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
1977 				continue;
1978 		} else if (tid >= hw->queues)
1979 			continue;
1980 
1981 		if (sta->txq[tid] == NULL)
1982 			continue;
1983 
1984 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
1985 		if (dequeue_seen && !ltxq->seen_dequeue)
1986 			continue;
1987 
1988 		LKPI_80211_LTXQ_LOCK(ltxq);
1989 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1990 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1991 		if (no_emptyq && ltxq_empty)
1992 			continue;
1993 
1994 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
1995 	}
1996 }
1997 
1998 /*
1999  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
2000  * packet.  The problem is that the state machine functions tend to hold the
2001  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
2002  * We call this after dropping the ic lock and before acquiring the LHW lock.
2003  * we make sure no further packets are queued and if they are queued the task
2004  * will finish or be cancelled.  At the end if a packet is left we manually
2005  * send it.  scan_to_auth() would re-enable sending if the lsta would be
2006  * re-used.
2007  */
2008 static void
2009 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
2010 {
2011 	struct ieee80211_hw *hw;
2012 	struct mbufq mq;
2013 	struct mbuf *m;
2014 	int len;
2015 
2016 	/* There is no lockdep_assert_not_held_wiphy(). */
2017 	hw = LHW_TO_HW(lhw);
2018 	lockdep_assert_not_held(&hw->wiphy->mtx);
2019 
2020 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
2021 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
2022 	lsta->txq_ready = false;
2023 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2024 
2025 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
2026 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
2027 
2028 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
2029 	len = mbufq_len(&lsta->txq);
2030 	if (len <= 0) {
2031 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2032 		return;
2033 	}
2034 
2035 	mbufq_init(&mq, IFQ_MAXLEN);
2036 	mbufq_concat(&mq, &lsta->txq);
2037 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2038 
2039 	m = mbufq_dequeue(&mq);
2040 	while (m != NULL) {
2041 		lkpi_80211_txq_tx_one(lsta, m);
2042 		m = mbufq_dequeue(&mq);
2043 	}
2044 }
2045 
2046 
2047 static void
2048 lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2049 {
2050 	struct ieee80211_chanctx_conf *chanctx_conf;
2051 	struct lkpi_chanctx *lchanctx;
2052 
2053 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2054 	    lockdep_is_held(&hw->wiphy->mtx));
2055 
2056 	if (chanctx_conf == NULL)
2057 		return;
2058 
2059 	/* Remove vif context. */
2060 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
2061 
2062 	lkpi_hw_conf_idle(hw, true);
2063 
2064 	/* Remove chan ctx. */
2065 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2066 
2067 	/* Cleanup. */
2068 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2069 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2070 	list_del(&lchanctx->entry);
2071 	free(lchanctx, M_LKPI80211);
2072 }
2073 
2074 
2075 /* -------------------------------------------------------------------------- */
2076 
2077 static int
2078 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2079 {
2080 
2081 	return (0);
2082 }
2083 
2084 /* lkpi_iv_newstate() handles the stop scan case generally. */
2085 #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
2086 
2087 static int
2088 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2089 {
2090 	struct linuxkpi_ieee80211_channel *chan;
2091 	struct lkpi_chanctx *lchanctx;
2092 	struct ieee80211_chanctx_conf *chanctx_conf;
2093 	struct lkpi_hw *lhw;
2094 	struct ieee80211_hw *hw;
2095 	struct lkpi_vif *lvif;
2096 	struct ieee80211_vif *vif;
2097 	struct ieee80211_node *ni;
2098 	struct lkpi_sta *lsta;
2099 	enum ieee80211_bss_changed bss_changed;
2100 	struct ieee80211_prep_tx_info prep_tx_info;
2101 	uint32_t changed;
2102 	int error;
2103 	bool synched;
2104 
2105 	/*
2106 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
2107 	 * For all later (STATE >= AUTH) functions we need to use the lvif
2108 	 * cache which will be tracked even through (*iv_update_bss)().
2109 	 */
2110 
2111 	if (vap->iv_bss == NULL) {
2112 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
2113 		return (EINVAL);
2114 	}
2115 	/*
2116 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
2117 	 * once we unlock here.  This is due to net80211 allowing state changes
2118 	 * and new join1() despite having an active node as well as due to
2119 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
2120 	 */
2121 	ni = ieee80211_ref_node(vap->iv_bss);
2122 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
2123 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
2124 		    "on vap %p\n", __func__, ni, vap);
2125 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
2126 		return (EINVAL);
2127 	}
2128 
2129 	lhw = vap->iv_ic->ic_softc;
2130 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
2131 	if (chan == NULL) {
2132 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
2133 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
2134 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
2135 		return (ESRCH);
2136 	}
2137 
2138 	hw = LHW_TO_HW(lhw);
2139 	lvif = VAP_TO_LVIF(vap);
2140 	vif = LVIF_TO_VIF(lvif);
2141 
2142 	LKPI_80211_LVIF_LOCK(lvif);
2143 	/* XXX-BZ KASSERT later? */
2144 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
2145 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2146 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2147 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2148 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2149 		    lvif->lvif_bss_synched);
2150 		LKPI_80211_LVIF_UNLOCK(lvif);
2151 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
2152 		return (EBUSY);
2153 	}
2154 	LKPI_80211_LVIF_UNLOCK(lvif);
2155 
2156 	IEEE80211_UNLOCK(vap->iv_ic);
2157 	wiphy_lock(hw->wiphy);
2158 
2159 	/* Add chanctx (or if exists, change it). */
2160 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2161 	    lockdep_is_held(&hw->wiphy->mtx));
2162 	if (chanctx_conf != NULL) {
2163 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2164 		IMPROVE("diff changes for changed, working on live copy, rcu");
2165 	} else {
2166 		/* Keep separate alloc as in Linux this is rcu managed? */
2167 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
2168 		    M_LKPI80211, M_WAITOK | M_ZERO);
2169 		chanctx_conf = &lchanctx->chanctx_conf;
2170 	}
2171 
2172 	chanctx_conf->rx_chains_static = 1;
2173 	chanctx_conf->rx_chains_dynamic = 1;
2174 	chanctx_conf->radar_enabled =
2175 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2176 	chanctx_conf->def.chan = chan;
2177 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
2178 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
2179 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
2180 	IMPROVE("Check vht_cap from band not just chan?");
2181 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
2182 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
2183 
2184 #ifdef LKPI_80211_HT
2185 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2186 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2187 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
2188 		else
2189 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
2190 	}
2191 #endif
2192 #ifdef LKPI_80211_VHT
2193 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
2194 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2195 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
2196 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2197 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
2198 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2199 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
2200 	}
2201 #endif
2202 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
2203 	/* Responder ... */
2204 #if 0
2205 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2206 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
2207 #ifdef LKPI_80211_HT
2208 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
2209 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
2210 #endif
2211 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
2212 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
2213 #else
2214 	chanctx_conf->min_def = chanctx_conf->def;
2215 #endif
2216 
2217 	/* Set bss info (bss_info_changed). */
2218 	bss_changed = 0;
2219 	vif->bss_conf.bssid = ni->ni_bssid;
2220 	bss_changed |= BSS_CHANGED_BSSID;
2221 	vif->bss_conf.txpower = ni->ni_txpower;
2222 	bss_changed |= BSS_CHANGED_TXPOWER;
2223 	vif->cfg.idle = false;
2224 	bss_changed |= BSS_CHANGED_IDLE;
2225 
2226 	/* vif->bss_conf.basic_rates ? Where exactly? */
2227 
2228 	lvif->beacons = 0;
2229 	/* Should almost assert it is this. */
2230 	vif->cfg.assoc = false;
2231 	vif->cfg.aid = 0;
2232 
2233 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2234 
2235 	error = 0;
2236 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
2237 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
2238 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
2239 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
2240 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2241 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
2242 	} else {
2243 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
2244 		if (error == 0 || error == EOPNOTSUPP) {
2245 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
2246 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
2247 			vif->bss_conf.chanreq.oper.center_freq1 =
2248 			    chanctx_conf->def.center_freq1;
2249 			vif->bss_conf.chanreq.oper.center_freq2 =
2250 			    chanctx_conf->def.center_freq2;
2251 		} else {
2252 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2253 			    "failed: %d\n", __func__, __LINE__, error);
2254 			goto out;
2255 		}
2256 
2257 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2258 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
2259 
2260 		/* Assign vif chanctx. */
2261 		if (error == 0)
2262 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2263 			    &vif->bss_conf, chanctx_conf);
2264 		if (error == EOPNOTSUPP)
2265 			error = 0;
2266 		if (error != 0) {
2267 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2268 			    "failed: %d\n", __func__, __LINE__, error);
2269 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2270 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2271 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2272 			list_del(&lchanctx->entry);
2273 			free(lchanctx, M_LKPI80211);
2274 			goto out;
2275 		}
2276 	}
2277 	IMPROVE("update radiotap chan fields too");
2278 
2279 	/* RATES */
2280 	IMPROVE("bss info: not all needs to come now and rates are missing");
2281 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2282 
2283 	/*
2284 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
2285 	 * ni always has lsta data attach despite net80211 node swapping
2286 	 * under the hoods.
2287 	 */
2288 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
2289 	    __func__, ni, ni->ni_drv_data));
2290 	lsta = ni->ni_drv_data;
2291 
2292 	/* Insert the [l]sta into the list of known stations. */
2293 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2294 
2295 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
2296 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2297 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
2298 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2299 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2300 	if (error != 0) {
2301 		IMPROVE("do we need to undo the chan ctx?");
2302 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2303 		    "failed: %d\n", __func__, __LINE__, error);
2304 		goto out;
2305 	}
2306 #if 0
2307 	lsta->added_to_drv = true;	/* mo manages. */
2308 #endif
2309 
2310 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2311 
2312 #if 0
2313 	/*
2314 	 * Wakeup all queues now that sta is there so we have as much time to
2315 	 * possibly prepare the queue in the driver to be ready for the 1st
2316 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
2317 	 * is no guarantee or way to check.
2318 	 * XXX-BZ and by now we know that this does not work on all drivers
2319 	 * for all queues.
2320 	 */
2321 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
2322 #endif
2323 
2324 	/* Start mgd_prepare_tx. */
2325 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2326 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
2327 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2328 	lsta->in_mgd = true;
2329 
2330 	/*
2331 	 * What is going to happen next:
2332 	 * - <twiddle> .. we should end up in "auth_to_assoc"
2333 	 * - event_callback
2334 	 * - update sta_state (NONE to AUTH)
2335 	 * - mgd_complete_tx
2336 	 * (ideally we'd do that on a callback for something else ...)
2337 	 */
2338 
2339 	wiphy_unlock(hw->wiphy);
2340 	IEEE80211_LOCK(vap->iv_ic);
2341 
2342 	LKPI_80211_LVIF_LOCK(lvif);
2343 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2344 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2345 	    lsta->ni != vap->iv_bss)
2346 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2347 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2348 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2349 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2350 		    lvif->lvif_bss_synched, ni, lsta);
2351 
2352 	/*
2353 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2354 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2355 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2356 	 * assume that ni == vap->iv_bss which it may or may not be.
2357 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2358 	 * function local ni already while ic was unlocked and would lead to
2359 	 * inconsistencies.  Go and see if we lost a race and do not update
2360 	 * lvif_bss_synched in that case.
2361 	 */
2362 	ieee80211_ref_node(lsta->ni);
2363 	lvif->lvif_bss = lsta;
2364 	if (lsta->ni == vap->iv_bss) {
2365 		lvif->lvif_bss_synched = synched = true;
2366 	} else {
2367 		/* Set to un-synched no matter what. */
2368 		lvif->lvif_bss_synched = synched = false;
2369 		/*
2370 		 * We do not error as someone has to take us down.
2371 		 * If we are followed by a 2nd, new net80211::join1() going to
2372 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2373 		 * will take the lvif->lvif_bss node down eventually.
2374 		 * What happens with the vap->iv_bss node will entirely be up
2375 		 * to net80211 as we never used the node beyond alloc()/free()
2376 		 * and we do not hold an extra reference for that anymore given
2377 		 * ni : lsta == 1:1.
2378 		 * Problem is if we do not error a MGMT/AUTH frame will be
2379 		 * sent from net80211::sta_newstate(); disable lsta queue below.
2380 		 */
2381 	}
2382 	LKPI_80211_LVIF_UNLOCK(lvif);
2383 	/*
2384 	 * Make sure in case the sta did not change and we re-added it,
2385 	 * that we can tx again but only if the vif/iv_bss are in sync.
2386 	 * Otherwise this should prevent the MGMT/AUTH frame from being
2387 	 * sent triggering a warning in iwlwifi.
2388 	 */
2389 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
2390 	lsta->txq_ready = synched;
2391 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2392 	goto out_relocked;
2393 
2394 out:
2395 	wiphy_unlock(hw->wiphy);
2396 	IEEE80211_LOCK(vap->iv_ic);
2397 out_relocked:
2398 	/*
2399 	 * Release the reference that kept the ni stable locally
2400 	 * during the work of this function.
2401 	 */
2402 	if (ni != NULL)
2403 		ieee80211_free_node(ni);
2404 	return (error);
2405 }
2406 
2407 static int
2408 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2409 {
2410 	struct lkpi_hw *lhw;
2411 	struct ieee80211_hw *hw;
2412 	struct lkpi_vif *lvif;
2413 	struct ieee80211_vif *vif;
2414 	struct ieee80211_node *ni;
2415 	struct lkpi_sta *lsta;
2416 	struct ieee80211_sta *sta;
2417 	struct ieee80211_prep_tx_info prep_tx_info;
2418 	enum ieee80211_bss_changed bss_changed;
2419 	int error;
2420 
2421 	lhw = vap->iv_ic->ic_softc;
2422 	hw = LHW_TO_HW(lhw);
2423 	lvif = VAP_TO_LVIF(vap);
2424 	vif = LVIF_TO_VIF(lvif);
2425 
2426 	LKPI_80211_LVIF_LOCK(lvif);
2427 #ifdef LINUXKPI_DEBUG_80211
2428 	/* XXX-BZ KASSERT later; state going down so no action. */
2429 	if (lvif->lvif_bss == NULL)
2430 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2431 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2432 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2433 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2434 		    lvif->lvif_bss_synched);
2435 #endif
2436 
2437 	lsta = lvif->lvif_bss;
2438 	LKPI_80211_LVIF_UNLOCK(lvif);
2439 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2440 	    "lvif %p vap %p\n", __func__,
2441 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2442 	ni = lsta->ni;			/* Reference held for lvif_bss. */
2443 	sta = LSTA_TO_STA(lsta);
2444 
2445 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2446 
2447 	IEEE80211_UNLOCK(vap->iv_ic);
2448 	wiphy_lock(hw->wiphy);
2449 
2450 	/* flush, drop. */
2451 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2452 
2453 	/* Wake tx queues to get packet(s) out. */
2454 	lkpi_wake_tx_queues(hw, sta, false, true);
2455 
2456 	/* flush, no drop */
2457 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2458 
2459 	/* End mgd_complete_tx. */
2460 	if (lsta->in_mgd) {
2461 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2462 		prep_tx_info.success = false;
2463 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2464 		lsta->in_mgd = false;
2465 	}
2466 
2467 	/* sync_rx_queues */
2468 	lkpi_80211_mo_sync_rx_queues(hw);
2469 
2470 	/* sta_pre_rcu_remove */
2471         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2472 
2473 	/* Take the station down. */
2474 
2475 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2476 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2477 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2478 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2479 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2480 	if (error != 0) {
2481 		IMPROVE("do we need to undo the chan ctx?");
2482 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2483 		    "failed: %d\n", __func__, __LINE__, error);
2484 		goto out;
2485 	}
2486 #if 0
2487 	lsta->added_to_drv = false;	/* mo manages. */
2488 #endif
2489 
2490 	bss_changed = 0;
2491 	vif->bss_conf.dtim_period = 0; /* go back to 0. */
2492 	bss_changed |= BSS_CHANGED_BEACON_INFO;
2493 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2494 
2495 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2496 
2497 	LKPI_80211_LVIF_LOCK(lvif);
2498 	/* Remove ni reference for this cache of lsta. */
2499 	lvif->lvif_bss = NULL;
2500 	lvif->lvif_bss_synched = false;
2501 	LKPI_80211_LVIF_UNLOCK(lvif);
2502 	lkpi_lsta_remove(lsta, lvif);
2503 	/*
2504 	 * The very last release the reference on the ni for the ni/lsta on
2505 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
2506 	 * and potentially freed.
2507 	 */
2508 	ieee80211_free_node(ni);
2509 
2510 	/* conf_tx */
2511 
2512 	lkpi_remove_chanctx(hw, vif);
2513 
2514 out:
2515 	wiphy_unlock(hw->wiphy);
2516 	IEEE80211_LOCK(vap->iv_ic);
2517 	return (error);
2518 }
2519 
2520 static int
2521 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2522 {
2523 	int error;
2524 
2525 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
2526 	if (error == 0)
2527 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
2528 	return (error);
2529 }
2530 
2531 static int
2532 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2533 {
2534 	struct lkpi_hw *lhw;
2535 	struct ieee80211_hw *hw;
2536 	struct lkpi_vif *lvif;
2537 	struct ieee80211_vif *vif;
2538 	struct lkpi_sta *lsta;
2539 	struct ieee80211_prep_tx_info prep_tx_info;
2540 	int error;
2541 
2542 	lhw = vap->iv_ic->ic_softc;
2543 	hw = LHW_TO_HW(lhw);
2544 	lvif = VAP_TO_LVIF(vap);
2545 	vif = LVIF_TO_VIF(lvif);
2546 
2547 	IEEE80211_UNLOCK(vap->iv_ic);
2548 	wiphy_lock(hw->wiphy);
2549 
2550 	LKPI_80211_LVIF_LOCK(lvif);
2551 	/* XXX-BZ KASSERT later? */
2552 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2553 #ifdef LINUXKPI_DEBUG_80211
2554 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2555 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2556 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2557 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2558 		    lvif->lvif_bss_synched);
2559 #endif
2560 		error = ENOTRECOVERABLE;
2561 		LKPI_80211_LVIF_UNLOCK(lvif);
2562 		goto out;
2563 	}
2564 	lsta = lvif->lvif_bss;
2565 	LKPI_80211_LVIF_UNLOCK(lvif);
2566 
2567 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
2568 
2569 	/* Finish auth. */
2570 	IMPROVE("event callback");
2571 
2572 	/* Update sta_state (NONE to AUTH). */
2573 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2574 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2575 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2576 	if (error != 0) {
2577 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2578 		    "failed: %d\n", __func__, __LINE__, error);
2579 		goto out;
2580 	}
2581 
2582 	/* End mgd_complete_tx. */
2583 	if (lsta->in_mgd) {
2584 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2585 		prep_tx_info.success = true;
2586 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2587 		lsta->in_mgd = false;
2588 	}
2589 
2590 	/* Now start assoc. */
2591 
2592 	/* Start mgd_prepare_tx. */
2593 	if (!lsta->in_mgd) {
2594 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2595 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2596 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2597 		lsta->in_mgd = true;
2598 	}
2599 
2600 	/* Wake tx queue to get packet out. */
2601 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
2602 
2603 	/*
2604 	 * <twiddle> .. we end up in "assoc_to_run"
2605 	 * - update sta_state (AUTH to ASSOC)
2606 	 * - conf_tx [all]
2607 	 * - bss_info_changed (assoc, aid, ssid, ..)
2608 	 * - change_chanctx (if needed)
2609 	 * - event_callback
2610 	 * - mgd_complete_tx
2611 	 */
2612 
2613 out:
2614 	wiphy_unlock(hw->wiphy);
2615 	IEEE80211_LOCK(vap->iv_ic);
2616 	return (error);
2617 }
2618 
2619 /* auth_to_auth, assoc_to_assoc. */
2620 static int
2621 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2622 {
2623 	struct lkpi_hw *lhw;
2624 	struct ieee80211_hw *hw;
2625 	struct lkpi_vif *lvif;
2626 	struct ieee80211_vif *vif;
2627 	struct lkpi_sta *lsta;
2628 	struct ieee80211_prep_tx_info prep_tx_info;
2629 	int error;
2630 
2631 	lhw = vap->iv_ic->ic_softc;
2632 	hw = LHW_TO_HW(lhw);
2633 	lvif = VAP_TO_LVIF(vap);
2634 	vif = LVIF_TO_VIF(lvif);
2635 
2636 	IEEE80211_UNLOCK(vap->iv_ic);
2637 	wiphy_lock(hw->wiphy);
2638 
2639 	LKPI_80211_LVIF_LOCK(lvif);
2640 	/* XXX-BZ KASSERT later? */
2641 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2642 #ifdef LINUXKPI_DEBUG_80211
2643 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2644 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2645 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2646 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2647 		    lvif->lvif_bss_synched);
2648 #endif
2649 		LKPI_80211_LVIF_UNLOCK(lvif);
2650 		error = ENOTRECOVERABLE;
2651 		goto out;
2652 	}
2653 	lsta = lvif->lvif_bss;
2654 	LKPI_80211_LVIF_UNLOCK(lvif);
2655 
2656 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
2657 	    lsta, lvif, vap));
2658 
2659 	IMPROVE("event callback?");
2660 
2661 	/* End mgd_complete_tx. */
2662 	if (lsta->in_mgd) {
2663 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2664 		prep_tx_info.success = false;
2665 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2666 		lsta->in_mgd = false;
2667 	}
2668 
2669 	/* Now start assoc. */
2670 
2671 	/* Start mgd_prepare_tx. */
2672 	if (!lsta->in_mgd) {
2673 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2674 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2675 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2676 		lsta->in_mgd = true;
2677 	}
2678 
2679 	error = 0;
2680 out:
2681 	wiphy_unlock(hw->wiphy);
2682 	IEEE80211_LOCK(vap->iv_ic);
2683 
2684 	return (error);
2685 }
2686 
2687 static int
2688 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2689 {
2690 	struct lkpi_hw *lhw;
2691 	struct ieee80211_hw *hw;
2692 	struct lkpi_vif *lvif;
2693 	struct ieee80211_vif *vif;
2694 	struct ieee80211_node *ni;
2695 	struct lkpi_sta *lsta;
2696 	struct ieee80211_sta *sta;
2697 	struct ieee80211_prep_tx_info prep_tx_info;
2698 	enum ieee80211_bss_changed bss_changed;
2699 	int error;
2700 
2701 	lhw = vap->iv_ic->ic_softc;
2702 	hw = LHW_TO_HW(lhw);
2703 	lvif = VAP_TO_LVIF(vap);
2704 	vif = LVIF_TO_VIF(lvif);
2705 
2706 	IEEE80211_UNLOCK(vap->iv_ic);
2707 	wiphy_lock(hw->wiphy);
2708 
2709 	LKPI_80211_LVIF_LOCK(lvif);
2710 #ifdef LINUXKPI_DEBUG_80211
2711 	/* XXX-BZ KASSERT later; state going down so no action. */
2712 	if (lvif->lvif_bss == NULL)
2713 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2714 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2715 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2716 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2717 		    lvif->lvif_bss_synched);
2718 #endif
2719 	lsta = lvif->lvif_bss;
2720 	LKPI_80211_LVIF_UNLOCK(lvif);
2721 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2722 	    "lvif %p vap %p\n", __func__,
2723 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2724 
2725 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2726 	sta = LSTA_TO_STA(lsta);
2727 
2728 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2729 
2730 	/* flush, drop. */
2731 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2732 
2733 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2734 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2735 	    !lsta->in_mgd) {
2736 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2737 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2738 		prep_tx_info.was_assoc = true;
2739 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2740 		lsta->in_mgd = true;
2741 	}
2742 
2743 	wiphy_unlock(hw->wiphy);
2744 	IEEE80211_LOCK(vap->iv_ic);
2745 
2746 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2747 	error = lvif->iv_newstate(vap, nstate, arg);
2748 	if (error != 0) {
2749 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2750 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2751 		goto outni;
2752 	}
2753 
2754 	IEEE80211_UNLOCK(vap->iv_ic);
2755 
2756 	/* Ensure the packets get out. */
2757 	lkpi_80211_flush_tx(lhw, lsta);
2758 
2759 	wiphy_lock(hw->wiphy);
2760 
2761 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2762 
2763 	/* Wake tx queues to get packet(s) out. */
2764 	lkpi_wake_tx_queues(hw, sta, false, true);
2765 
2766 	/* flush, no drop */
2767 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2768 
2769 	/* End mgd_complete_tx. */
2770 	if (lsta->in_mgd) {
2771 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2772 		prep_tx_info.success = false;
2773 		prep_tx_info.was_assoc = true;
2774 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2775 		lsta->in_mgd = false;
2776 	}
2777 
2778 	/* sync_rx_queues */
2779 	lkpi_80211_mo_sync_rx_queues(hw);
2780 
2781 	/* sta_pre_rcu_remove */
2782         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2783 
2784 	/* Take the station down. */
2785 
2786 	/* Update sta and change state (from AUTH) to NONE. */
2787 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2788 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2789 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2790 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2791 	if (error != 0) {
2792 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2793 		    "failed: %d\n", __func__, __LINE__, error);
2794 		goto out;
2795 	}
2796 
2797 	/* See comment in lkpi_sta_run_to_init(). */
2798 	bss_changed = 0;
2799 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
2800 
2801 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2802 
2803 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2804 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2805 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2806 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2807 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2808 	if (error != 0) {
2809 		IMPROVE("do we need to undo the chan ctx?");
2810 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2811 		    "failed: %d\n", __func__, __LINE__, error);
2812 		goto out;
2813 	}
2814 
2815 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2816 
2817 	IMPROVE("Any bss_info changes to announce?");
2818 	vif->bss_conf.qos = 0;
2819 	bss_changed |= BSS_CHANGED_QOS;
2820 	vif->cfg.ssid_len = 0;
2821 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2822 	bss_changed |= BSS_CHANGED_BSSID;
2823 	vif->bss_conf.dtim_period = 0; /* go back to 0. */
2824 	bss_changed |= BSS_CHANGED_BEACON_INFO;
2825 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2826 
2827 	LKPI_80211_LVIF_LOCK(lvif);
2828 	/* Remove ni reference for this cache of lsta. */
2829 	lvif->lvif_bss = NULL;
2830 	lvif->lvif_bss_synched = false;
2831 	LKPI_80211_LVIF_UNLOCK(lvif);
2832 	lkpi_lsta_remove(lsta, lvif);
2833 	/*
2834 	 * The very last release the reference on the ni for the ni/lsta on
2835 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
2836 	 * and potentially freed.
2837 	 */
2838 	ieee80211_free_node(ni);
2839 
2840 	/* conf_tx */
2841 
2842 	lkpi_remove_chanctx(hw, vif);
2843 
2844 	error = EALREADY;
2845 out:
2846 	wiphy_unlock(hw->wiphy);
2847 	IEEE80211_LOCK(vap->iv_ic);
2848 outni:
2849 	return (error);
2850 }
2851 
2852 static int
2853 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2854 {
2855 	int error;
2856 
2857 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2858 	if (error != 0 && error != EALREADY)
2859 		return (error);
2860 
2861 	/* At this point iv_bss is long a new node! */
2862 
2863 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2864 	return (error);
2865 }
2866 
2867 static int
2868 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2869 {
2870 	int error;
2871 
2872 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2873 	return (error);
2874 }
2875 
2876 static int
2877 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2878 {
2879 	int error;
2880 
2881 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2882 	return (error);
2883 }
2884 
2885 static int
2886 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2887 {
2888 	struct lkpi_hw *lhw;
2889 	struct ieee80211_hw *hw;
2890 	struct lkpi_vif *lvif;
2891 	struct ieee80211_vif *vif;
2892 	struct ieee80211_node *ni;
2893 	struct lkpi_sta *lsta;
2894 	struct ieee80211_sta *sta;
2895 	struct ieee80211_prep_tx_info prep_tx_info;
2896 	enum ieee80211_bss_changed bss_changed;
2897 	int error;
2898 
2899 	lhw = vap->iv_ic->ic_softc;
2900 	hw = LHW_TO_HW(lhw);
2901 	lvif = VAP_TO_LVIF(vap);
2902 	vif = LVIF_TO_VIF(lvif);
2903 
2904 	IEEE80211_UNLOCK(vap->iv_ic);
2905 	wiphy_lock(hw->wiphy);
2906 
2907 	LKPI_80211_LVIF_LOCK(lvif);
2908 	/* XXX-BZ KASSERT later? */
2909 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2910 #ifdef LINUXKPI_DEBUG_80211
2911 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2912 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2913 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2914 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2915 		    lvif->lvif_bss_synched);
2916 #endif
2917 		LKPI_80211_LVIF_UNLOCK(lvif);
2918 		error = ENOTRECOVERABLE;
2919 		goto out;
2920 	}
2921 	lsta = lvif->lvif_bss;
2922 	LKPI_80211_LVIF_UNLOCK(lvif);
2923 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2924 	    "lvif %p vap %p\n", __func__,
2925 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2926 
2927 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2928 
2929 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
2930 	    "and to lesser extend ieee80211_notify_node_join");
2931 
2932 	/* Finish assoc. */
2933 	/* Update sta_state (AUTH to ASSOC) and set aid. */
2934 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2935 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2936 	sta = LSTA_TO_STA(lsta);
2937 	sta->aid = IEEE80211_NODE_AID(ni);
2938 #ifdef LKPI_80211_WME
2939 	if (vap->iv_flags & IEEE80211_F_WME)
2940 		sta->wme = true;
2941 #endif
2942 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2943 	if (error != 0) {
2944 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2945 		    "failed: %d\n", __func__, __LINE__, error);
2946 		goto out;
2947 	}
2948 
2949 	IMPROVE("wme / conf_tx [all]");
2950 
2951 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2952 	bss_changed = 0;
2953 #ifdef LKPI_80211_WME
2954 	bss_changed |= lkpi_wme_update(lhw, vap, true);
2955 #endif
2956 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2957 		lvif->beacons = 0;
2958 		vif->cfg.assoc = true;
2959 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
2960 		bss_changed |= BSS_CHANGED_ASSOC;
2961 	}
2962 	/* We set SSID but this is not BSSID! */
2963 	vif->cfg.ssid_len = ni->ni_esslen;
2964 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
2965 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
2966 	    vif->bss_conf.use_short_preamble) {
2967 		vif->bss_conf.use_short_preamble ^= 1;
2968 		/* bss_changed |= BSS_CHANGED_??? */
2969 	}
2970 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
2971 	    vif->bss_conf.use_short_slot) {
2972 		vif->bss_conf.use_short_slot ^= 1;
2973 		/* bss_changed |= BSS_CHANGED_??? */
2974 	}
2975 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
2976 	    vif->bss_conf.qos) {
2977 		vif->bss_conf.qos ^= 1;
2978 		bss_changed |= BSS_CHANGED_QOS;
2979 	}
2980 
2981 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2982 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2983 
2984 	/* - change_chanctx (if needed)
2985 	 * - event_callback
2986 	 */
2987 
2988 	/* End mgd_complete_tx. */
2989 	if (lsta->in_mgd) {
2990 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2991 		prep_tx_info.success = true;
2992 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2993 		lsta->in_mgd = false;
2994 	}
2995 
2996 	lkpi_hw_conf_idle(hw, false);
2997 
2998 	/*
2999 	 * And then:
3000 	 * - (more packets)?
3001 	 * - set_key
3002 	 * - set_default_unicast_key
3003 	 * - set_key (?)
3004 	 * - ipv6_addr_change (?)
3005 	 */
3006 
3007 	if (!ieee80211_node_is_authorized(ni)) {
3008 		IMPROVE("net80211 does not consider node authorized");
3009 	}
3010 
3011 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
3012 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
3013 
3014 	/* Update thresholds. */
3015 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
3016 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
3017 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
3018 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
3019 
3020 	/* Update sta_state (ASSOC to AUTHORIZED). */
3021 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3022 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3023 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3024 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
3025 	if (error != 0) {
3026 		IMPROVE("undo some changes?");
3027 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
3028 		    "failed: %d\n", __func__, __LINE__, error);
3029 		goto out;
3030 	}
3031 
3032 	/* - drv_config (?)
3033 	 * - bss_info_changed
3034 	 * - set_rekey_data (?)
3035 	 *
3036 	 * And now we should be passing packets.
3037 	 */
3038 	IMPROVE("Need that bssid setting, and the keys");
3039 
3040 	bss_changed = 0;
3041 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3042 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3043 
3044 	/* Prepare_multicast && configure_filter. */
3045 	lkpi_update_mcast_filter(vap->iv_ic);
3046 
3047 out:
3048 	wiphy_unlock(hw->wiphy);
3049 	IEEE80211_LOCK(vap->iv_ic);
3050 	return (error);
3051 }
3052 
3053 static int
3054 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3055 {
3056 	int error;
3057 
3058 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
3059 	if (error == 0)
3060 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
3061 	return (error);
3062 }
3063 
3064 static int
3065 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3066 {
3067 	struct lkpi_hw *lhw;
3068 	struct ieee80211_hw *hw;
3069 	struct lkpi_vif *lvif;
3070 	struct ieee80211_vif *vif;
3071 	struct ieee80211_node *ni;
3072 	struct lkpi_sta *lsta;
3073 	struct ieee80211_sta *sta;
3074 	struct ieee80211_prep_tx_info prep_tx_info;
3075 #if 0
3076 	enum ieee80211_bss_changed bss_changed;
3077 #endif
3078 	int error;
3079 
3080 	lhw = vap->iv_ic->ic_softc;
3081 	hw = LHW_TO_HW(lhw);
3082 	lvif = VAP_TO_LVIF(vap);
3083 	vif = LVIF_TO_VIF(lvif);
3084 
3085 	LKPI_80211_LVIF_LOCK(lvif);
3086 #ifdef LINUXKPI_DEBUG_80211
3087 	/* XXX-BZ KASSERT later; state going down so no action. */
3088 	if (lvif->lvif_bss == NULL)
3089 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3090 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3091 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
3092 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3093 		    lvif->lvif_bss_synched);
3094 #endif
3095 	lsta = lvif->lvif_bss;
3096 	LKPI_80211_LVIF_UNLOCK(lvif);
3097 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3098 	    "lvif %p vap %p\n", __func__,
3099 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3100 
3101 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3102 	sta = LSTA_TO_STA(lsta);
3103 
3104 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3105 
3106 	IEEE80211_UNLOCK(vap->iv_ic);
3107 	wiphy_lock(hw->wiphy);
3108 
3109 	/* flush, drop. */
3110 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3111 
3112 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3113 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3114 	    !lsta->in_mgd) {
3115 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3116 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3117 		prep_tx_info.was_assoc = true;
3118 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3119 		lsta->in_mgd = true;
3120 	}
3121 
3122 	wiphy_unlock(hw->wiphy);
3123 	IEEE80211_LOCK(vap->iv_ic);
3124 
3125 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3126 	error = lvif->iv_newstate(vap, nstate, arg);
3127 	if (error != 0) {
3128 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3129 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3130 		goto outni;
3131 	}
3132 
3133 	IEEE80211_UNLOCK(vap->iv_ic);
3134 
3135 	/* Ensure the packets get out. */
3136 	lkpi_80211_flush_tx(lhw, lsta);
3137 
3138 	wiphy_lock(hw->wiphy);
3139 
3140 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3141 
3142 	/* Wake tx queues to get packet(s) out. */
3143 	lkpi_wake_tx_queues(hw, sta, false, true);
3144 
3145 	/* flush, no drop */
3146 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3147 
3148 	/* End mgd_complete_tx. */
3149 	if (lsta->in_mgd) {
3150 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3151 		prep_tx_info.success = false;
3152 		prep_tx_info.was_assoc = true;
3153 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3154 		lsta->in_mgd = false;
3155 	}
3156 
3157 #if 0
3158 	/* sync_rx_queues */
3159 	lkpi_80211_mo_sync_rx_queues(hw);
3160 
3161 	/* sta_pre_rcu_remove */
3162         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3163 #endif
3164 
3165 	/* Take the station down. */
3166 
3167 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3168 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3169 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3170 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3171 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3172 	if (error != 0) {
3173 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3174 		    "failed: %d\n", __func__, __LINE__, error);
3175 		goto out;
3176 	}
3177 
3178 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3179 
3180 #ifdef LKPI_80211_HW_CRYPTO
3181 	if (lkpi_hwcrypto) {
3182 		error = lkpi_sta_del_keys(hw, vif, lsta);
3183 		if (error != 0) {
3184 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3185 			    "failed: %d\n", __func__, __LINE__, error);
3186 			/*
3187 			 * Either drv/fw will crash or cleanup itself,
3188 			 * otherwise net80211 will delete the keys (at a
3189 			 * less appropriate time).
3190 			 */
3191 			/* goto out; */
3192 		}
3193 	}
3194 #endif
3195 
3196 	/* Update sta_state (ASSOC to AUTH). */
3197 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3198 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3199 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3200 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3201 	if (error != 0) {
3202 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3203 		    "failed: %d\n", __func__, __LINE__, error);
3204 		goto out;
3205 	}
3206 
3207 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3208 
3209 #if 0
3210 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
3211 	lkpi_disassoc(sta, vif, lhw);
3212 #endif
3213 
3214 	error = EALREADY;
3215 out:
3216 	wiphy_unlock(hw->wiphy);
3217 	IEEE80211_LOCK(vap->iv_ic);
3218 outni:
3219 	return (error);
3220 }
3221 
3222 static int
3223 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3224 {
3225 	struct lkpi_hw *lhw;
3226 	struct ieee80211_hw *hw;
3227 	struct lkpi_vif *lvif;
3228 	struct ieee80211_vif *vif;
3229 	struct ieee80211_node *ni;
3230 	struct lkpi_sta *lsta;
3231 	struct ieee80211_sta *sta;
3232 	struct ieee80211_prep_tx_info prep_tx_info;
3233 	enum ieee80211_bss_changed bss_changed;
3234 	int error;
3235 
3236 	lhw = vap->iv_ic->ic_softc;
3237 	hw = LHW_TO_HW(lhw);
3238 	lvif = VAP_TO_LVIF(vap);
3239 	vif = LVIF_TO_VIF(lvif);
3240 
3241 	IEEE80211_UNLOCK(vap->iv_ic);
3242 	wiphy_lock(hw->wiphy);
3243 
3244 	LKPI_80211_LVIF_LOCK(lvif);
3245 #ifdef LINUXKPI_DEBUG_80211
3246 	/* XXX-BZ KASSERT later; state going down so no action. */
3247 	if (lvif->lvif_bss == NULL)
3248 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3249 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3250 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
3251 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3252 		    lvif->lvif_bss_synched);
3253 #endif
3254 	lsta = lvif->lvif_bss;
3255 	LKPI_80211_LVIF_UNLOCK(lvif);
3256 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3257 	    "lvif %p vap %p\n", __func__,
3258 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3259 
3260 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3261 	sta = LSTA_TO_STA(lsta);
3262 
3263 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3264 
3265 	/* flush, drop. */
3266 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3267 
3268 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3269 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3270 	    !lsta->in_mgd) {
3271 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3272 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3273 		prep_tx_info.was_assoc = true;
3274 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3275 		lsta->in_mgd = true;
3276 	}
3277 
3278 	wiphy_unlock(hw->wiphy);
3279 	IEEE80211_LOCK(vap->iv_ic);
3280 
3281 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3282 	error = lvif->iv_newstate(vap, nstate, arg);
3283 	if (error != 0) {
3284 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3285 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3286 		goto outni;
3287 	}
3288 
3289 	IEEE80211_UNLOCK(vap->iv_ic);
3290 
3291 	/* Ensure the packets get out. */
3292 	lkpi_80211_flush_tx(lhw, lsta);
3293 
3294 	wiphy_lock(hw->wiphy);
3295 
3296 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3297 
3298 	/* Wake tx queues to get packet(s) out. */
3299 	lkpi_wake_tx_queues(hw, sta, false, true);
3300 
3301 	/* flush, no drop */
3302 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3303 
3304 	/* End mgd_complete_tx. */
3305 	if (lsta->in_mgd) {
3306 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3307 		prep_tx_info.success = false;
3308 		prep_tx_info.was_assoc = true;
3309 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3310 		lsta->in_mgd = false;
3311 	}
3312 
3313 	/* sync_rx_queues */
3314 	lkpi_80211_mo_sync_rx_queues(hw);
3315 
3316 	/* sta_pre_rcu_remove */
3317         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3318 
3319 	/* Take the station down. */
3320 
3321 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3322 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3323 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3324 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3325 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3326 	if (error != 0) {
3327 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3328 		    "failed: %d\n", __func__, __LINE__, error);
3329 		goto out;
3330 	}
3331 
3332 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3333 
3334 #ifdef LKPI_80211_HW_CRYPTO
3335 	if (lkpi_hwcrypto) {
3336 		error = lkpi_sta_del_keys(hw, vif, lsta);
3337 		if (error != 0) {
3338 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3339 			    "failed: %d\n", __func__, __LINE__, error);
3340 			/*
3341 			 * Either drv/fw will crash or cleanup itself,
3342 			 * otherwise net80211 will delete the keys (at a
3343 			 * less appropriate time).
3344 			 */
3345 			/* goto out; */
3346 		}
3347 	}
3348 #endif
3349 
3350 	/* Update sta_state (ASSOC to AUTH). */
3351 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3352 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3353 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3354 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3355 	if (error != 0) {
3356 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3357 		    "failed: %d\n", __func__, __LINE__, error);
3358 		goto out;
3359 	}
3360 
3361 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3362 
3363 	/* Update sta and change state (from AUTH) to NONE. */
3364 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3365 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3366 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3367 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3368 	if (error != 0) {
3369 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3370 		    "failed: %d\n", __func__, __LINE__, error);
3371 		goto out;
3372 	}
3373 
3374 	bss_changed = 0;
3375 	/*
3376 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
3377 	 *
3378 	 * One would expect this to happen when going off AUTHORIZED.
3379 	 * See comment there; removes the sta from fw if not careful
3380 	 * (bss_info_changed() change is executed right away).
3381 	 *
3382 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
3383 	 * as otherwise drivers (iwlwifi at least) will silently not remove
3384 	 * the sta from the firmware and when we will add a new one trigger
3385 	 * a fw assert.
3386 	 *
3387 	 * The order which works best so far avoiding early removal or silent
3388 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
3389 	 * the iwlwifi:mac80211.c case still to be tested):
3390 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
3391 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
3392 	 *    (removes the sta given assoc is false)
3393 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
3394 	 * 4) call unassign_vif_chanctx
3395 	 * 5) call lkpi_hw_conf_idle
3396 	 * 6) call remove_chanctx
3397 	 */
3398 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
3399 
3400 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3401 
3402 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3403 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3404 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3405 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3406 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3407 	if (error != 0) {
3408 		IMPROVE("do we need to undo the chan ctx?");
3409 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3410 		    "failed: %d\n", __func__, __LINE__, error);
3411 		goto out;
3412 	}
3413 
3414 	lkpi_lsta_remove(lsta, lvif);
3415 
3416 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3417 
3418 	IMPROVE("Any bss_info changes to announce?");
3419 	vif->bss_conf.qos = 0;
3420 	bss_changed |= BSS_CHANGED_QOS;
3421 	vif->cfg.ssid_len = 0;
3422 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3423 	bss_changed |= BSS_CHANGED_BSSID;
3424 	vif->bss_conf.use_short_preamble = false;
3425 	vif->bss_conf.qos = false;
3426 	/* XXX BSS_CHANGED_???? */
3427 	vif->bss_conf.dtim_period = 0; /* go back to 0. */
3428 	bss_changed |= BSS_CHANGED_BEACON_INFO;
3429 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3430 
3431 	LKPI_80211_LVIF_LOCK(lvif);
3432 	/* Remove ni reference for this cache of lsta. */
3433 	lvif->lvif_bss = NULL;
3434 	lvif->lvif_bss_synched = false;
3435 	LKPI_80211_LVIF_UNLOCK(lvif);
3436 	/*
3437 	 * The very last release the reference on the ni for the ni/lsta on
3438 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
3439 	 * and potentially freed.
3440 	 */
3441 	ieee80211_free_node(ni);
3442 
3443 	/* conf_tx */
3444 
3445 	lkpi_remove_chanctx(hw, vif);
3446 
3447 	error = EALREADY;
3448 out:
3449 	wiphy_unlock(hw->wiphy);
3450 	IEEE80211_LOCK(vap->iv_ic);
3451 outni:
3452 	return (error);
3453 }
3454 
3455 static int
3456 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3457 {
3458 
3459 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3460 }
3461 
3462 static int
3463 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3464 {
3465 	int error;
3466 
3467 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3468 	if (error != 0 && error != EALREADY)
3469 		return (error);
3470 
3471 	/* At this point iv_bss is long a new node! */
3472 
3473 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
3474 	return (error);
3475 }
3476 
3477 /* -------------------------------------------------------------------------- */
3478 
3479 /*
3480  * The matches the documented state changes in net80211::sta_newstate().
3481  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
3482  * there are "invalid" (so there is a room for failure here).
3483  */
3484 struct fsm_state {
3485 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
3486 	enum ieee80211_state ostate;
3487 	enum ieee80211_state nstate;
3488 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
3489 } sta_state_fsm[] = {
3490 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
3491 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
3492 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3493 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3494 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
3495 
3496 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3497 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3498 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
3499 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3500 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
3501 
3502 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3503 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3504 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3505 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3506 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
3507 
3508 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3509 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3510 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
3511 
3512 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
3513 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
3514 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
3515 
3516 	/* Dummy at the end without handler. */
3517 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
3518 };
3519 
3520 static int
3521 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3522 {
3523 	struct ieee80211com *ic;
3524 	struct lkpi_hw *lhw;
3525 	struct lkpi_vif *lvif;
3526 	struct ieee80211_vif *vif;
3527 	struct fsm_state *s;
3528 	enum ieee80211_state ostate;
3529 	int error;
3530 
3531 	ic = vap->iv_ic;
3532 	IEEE80211_LOCK_ASSERT(ic);
3533 	ostate = vap->iv_state;
3534 
3535 #ifdef LINUXKPI_DEBUG_80211
3536 	if (linuxkpi_debug_80211 & D80211_TRACE)
3537 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
3538 		    __func__, __LINE__, vap, nstate, arg);
3539 #endif
3540 
3541 	if (vap->iv_opmode == IEEE80211_M_STA) {
3542 
3543 		lhw = ic->ic_softc;
3544 		lvif = VAP_TO_LVIF(vap);
3545 		vif = LVIF_TO_VIF(lvif);
3546 
3547 		/* No need to replicate this in most state handlers. */
3548 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
3549 			lkpi_stop_hw_scan(lhw, vif);
3550 
3551 		s = sta_state_fsm;
3552 
3553 	} else {
3554 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
3555 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
3556 		return (ENOSYS);
3557 	}
3558 
3559 	error = 0;
3560 	for (; s->handler != NULL; s++) {
3561 		if (ostate == s->ostate && nstate == s->nstate) {
3562 #ifdef LINUXKPI_DEBUG_80211
3563 			if (linuxkpi_debug_80211 & D80211_TRACE)
3564 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3565 				    " %d (%s): arg %d.\n", __func__,
3566 				    ostate, ieee80211_state_name[ostate],
3567 				    nstate, ieee80211_state_name[nstate], arg);
3568 #endif
3569 			error = s->handler(vap, nstate, arg);
3570 			break;
3571 		}
3572 	}
3573 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3574 
3575 	if (s->handler == NULL) {
3576 		IMPROVE("turn this into a KASSERT\n");
3577 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
3578 		    "%d (%s) -> %d (%s)\n", __func__,
3579 		    ostate, ieee80211_state_name[ostate],
3580 		    nstate, ieee80211_state_name[nstate]);
3581 		return (ENOSYS);
3582 	}
3583 
3584 	if (error == EALREADY) {
3585 #ifdef LINUXKPI_DEBUG_80211
3586 		if (linuxkpi_debug_80211 & D80211_TRACE)
3587 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3588 			    "%d (%s): iv_newstate already handled: %d.\n",
3589 			    __func__, ostate, ieee80211_state_name[ostate],
3590 			    nstate, ieee80211_state_name[nstate], error);
3591 #endif
3592 		return (0);
3593 	}
3594 
3595 	if (error != 0) {
3596 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
3597 		    "%d (%s) -> %d (%s)\n", __func__, error,
3598 		    ostate, ieee80211_state_name[ostate],
3599 		    nstate, ieee80211_state_name[nstate]);
3600 		return (error);
3601 	}
3602 
3603 #ifdef LINUXKPI_DEBUG_80211
3604 	if (linuxkpi_debug_80211 & D80211_TRACE)
3605 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3606 		    "calling net80211 parent\n",
3607 		    __func__, __LINE__, vap, nstate, arg);
3608 #endif
3609 
3610 	return (lvif->iv_newstate(vap, nstate, arg));
3611 }
3612 
3613 /* -------------------------------------------------------------------------- */
3614 
3615 /*
3616  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3617  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3618  * new node without us knowing and thus our ni/lsta are out of sync.
3619  */
3620 static struct ieee80211_node *
3621 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3622 {
3623 	struct lkpi_vif *lvif;
3624 	struct ieee80211_node *rni;
3625 
3626 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3627 
3628 	lvif = VAP_TO_LVIF(vap);
3629 
3630 	LKPI_80211_LVIF_LOCK(lvif);
3631 	lvif->lvif_bss_synched = false;
3632 	LKPI_80211_LVIF_UNLOCK(lvif);
3633 
3634 	rni = lvif->iv_update_bss(vap, ni);
3635 	return (rni);
3636 }
3637 
3638 #ifdef LKPI_80211_WME
3639 static int
3640 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
3641 {
3642 	struct ieee80211com *ic;
3643 	struct ieee80211_hw *hw;
3644 	struct lkpi_vif *lvif;
3645 	struct ieee80211_vif *vif;
3646 	struct chanAccParams chp;
3647 	struct wmeParams wmeparr[WME_NUM_AC];
3648 	struct ieee80211_tx_queue_params txqp;
3649 	enum ieee80211_bss_changed changed;
3650 	int error;
3651 	uint16_t ac;
3652 
3653 	hw = LHW_TO_HW(lhw);
3654 	lockdep_assert_wiphy(hw->wiphy);
3655 
3656 	IMPROVE();
3657 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
3658 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
3659 
3660 	if (vap == NULL)
3661 		return (0);
3662 
3663 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
3664 		return (0);
3665 
3666 	if (lhw->ops->conf_tx == NULL)
3667 		return (0);
3668 
3669 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
3670 		lhw->update_wme = true;
3671 		return (0);
3672 	}
3673 	lhw->update_wme = false;
3674 
3675 	ic = lhw->ic;
3676 	ieee80211_wme_ic_getparams(ic, &chp);
3677 	IEEE80211_LOCK(ic);
3678 	for (ac = 0; ac < WME_NUM_AC; ac++)
3679 		wmeparr[ac] = chp.cap_wmeParams[ac];
3680 	IEEE80211_UNLOCK(ic);
3681 
3682 	lvif = VAP_TO_LVIF(vap);
3683 	vif = LVIF_TO_VIF(lvif);
3684 
3685 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
3686 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3687 		struct wmeParams *wmep;
3688 
3689 		wmep = &wmeparr[ac];
3690 		bzero(&txqp, sizeof(txqp));
3691 		txqp.cw_min = wmep->wmep_logcwmin;
3692 		txqp.cw_max = wmep->wmep_logcwmax;
3693 		txqp.txop = wmep->wmep_txopLimit;
3694 		txqp.aifs = wmep->wmep_aifsn;
3695 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3696 		if (error != 0)
3697 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3698 			    __func__, ac, error);
3699 	}
3700 	changed = BSS_CHANGED_QOS;
3701 	if (!planned)
3702 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
3703 
3704 	return (changed);
3705 }
3706 #endif
3707 
3708 static int
3709 lkpi_ic_wme_update(struct ieee80211com *ic)
3710 {
3711 #ifdef LKPI_80211_WME
3712 	struct ieee80211vap *vap;
3713 	struct lkpi_hw *lhw;
3714 	struct ieee80211_hw *hw;
3715 
3716 	IMPROVE("Use the per-VAP callback in net80211.");
3717 	vap = TAILQ_FIRST(&ic->ic_vaps);
3718 	if (vap == NULL)
3719 		return (0);
3720 
3721 	lhw = ic->ic_softc;
3722 	hw = LHW_TO_HW(lhw);
3723 
3724 	wiphy_lock(hw->wiphy);
3725 	lkpi_wme_update(lhw, vap, false);
3726 	wiphy_unlock(hw->wiphy);
3727 #endif
3728 	return (0);	/* unused */
3729 }
3730 
3731 static void
3732 lkpi_iv_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
3733     int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
3734 {
3735 	struct lkpi_hw *lhw;
3736 	struct ieee80211_hw *hw;
3737 	struct lkpi_vif *lvif;
3738 	struct ieee80211_vif *vif;
3739 	enum ieee80211_bss_changed bss_changed;
3740 
3741 	lvif = VAP_TO_LVIF(ni->ni_vap);
3742 
3743 	lvif->iv_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
3744 
3745 	switch (subtype) {
3746 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3747 		break;
3748 	case IEEE80211_FC0_SUBTYPE_BEACON:
3749 		lvif->beacons++;
3750 		break;
3751 	default:
3752 		return;
3753 	}
3754 
3755 	vif = LVIF_TO_VIF(lvif);
3756 	lhw = ni->ni_ic->ic_softc;
3757 	hw = LHW_TO_HW(lhw);
3758 
3759 	/*
3760 	 * If this direct call to mo_bss_info_changed will not work due to
3761 	 * locking, see if queue_work() is fast enough.
3762 	 */
3763 	bss_changed = lkpi_update_dtim_tsf(vif, ni, ni->ni_vap, __func__, __LINE__);
3764 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3765 }
3766 
3767 /*
3768  * Change link-layer address on the vif (if the vap is not started/"UP").
3769  * This can happen if a user changes 'ether' using ifconfig.
3770  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
3771  * we do use a per-[l]vif event handler to be sure we exist as we
3772  * cannot assume that from every vap derives a vif and we have a hard
3773  * time checking based on net80211 information.
3774  * Should this ever become a real problem we could add a callback function
3775  * to wlan_iflladdr() to be set optionally but that would be for a
3776  * single-consumer (or needs a list) -- was just too complicated for an
3777  * otherwise perfect mechanism FreeBSD already provides.
3778  */
3779 static void
3780 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
3781 {
3782 	struct epoch_tracker et;
3783 	struct ieee80211_vif *vif;
3784 
3785 	NET_EPOCH_ENTER(et);
3786 	/* NB: identify vap's by if_transmit; left as an extra check. */
3787 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3788 	    (if_getflags(ifp) & IFF_UP) != 0) {
3789 		NET_EPOCH_EXIT(et);
3790 		return;
3791 	}
3792 
3793 	vif = arg;
3794 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
3795 	NET_EPOCH_EXIT(et);
3796 }
3797 
3798 static struct ieee80211vap *
3799 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
3800     int unit, enum ieee80211_opmode opmode, int flags,
3801     const uint8_t bssid[IEEE80211_ADDR_LEN],
3802     const uint8_t mac[IEEE80211_ADDR_LEN])
3803 {
3804 	struct lkpi_hw *lhw;
3805 	struct ieee80211_hw *hw;
3806 	struct lkpi_vif *lvif;
3807 	struct ieee80211vap *vap;
3808 	struct ieee80211_vif *vif;
3809 	struct ieee80211_tx_queue_params txqp;
3810 	enum ieee80211_bss_changed changed;
3811 	struct sysctl_oid *node;
3812 	size_t len;
3813 	int error, i;
3814 	uint16_t ac;
3815 
3816 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
3817 		return (NULL);
3818 
3819 	lhw = ic->ic_softc;
3820 	hw = LHW_TO_HW(lhw);
3821 
3822 	len = sizeof(*lvif);
3823 	len += hw->vif_data_size;	/* vif->drv_priv */
3824 
3825 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
3826 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3827 	INIT_LIST_HEAD(&lvif->lsta_list);
3828 	lvif->lvif_bss = NULL;
3829 	refcount_init(&lvif->nt_unlocked, 0);
3830 	lvif->lvif_bss_synched = false;
3831 	vap = LVIF_TO_VAP(lvif);
3832 
3833 	vif = LVIF_TO_VIF(lvif);
3834 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
3835 	vif->p2p = false;
3836 	vif->probe_req_reg = false;
3837 	vif->type = lkpi_opmode_to_vif_type(opmode);
3838 	lvif->wdev.iftype = vif->type;
3839 	/* Need to fill in other fields as well. */
3840 	IMPROVE();
3841 
3842 	/* XXX-BZ hardcoded for now! */
3843 #if 1
3844 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3845 	vif->bss_conf.vif = vif;
3846 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
3847 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
3848 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
3849 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
3850 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
3851 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
3852 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
3853 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
3854 	vif->bss_conf.qos = false;
3855 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
3856 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3857 	vif->cfg.aid = 0;
3858 	vif->cfg.assoc = false;
3859 	vif->cfg.idle = true;
3860 	vif->cfg.ps = false;
3861 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3862 	/*
3863 	 * We need to initialize it to something as the bss_info_changed call
3864 	 * will try to copy from it in iwlwifi and NULL is a panic.
3865 	 * We will set the proper one in scan_to_auth() before being assoc.
3866 	 */
3867 	vif->bss_conf.bssid = ieee80211broadcastaddr;
3868 #endif
3869 #if 0
3870 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
3871 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
3872 	vif->bss_conf.beacon_int = ic->ic_bintval;
3873 	/* iwlwifi bug. */
3874 	if (vif->bss_conf.beacon_int < 16)
3875 		vif->bss_conf.beacon_int = 16;
3876 #endif
3877 
3878 	/* Link Config */
3879 	vif->link_conf[0] = &vif->bss_conf;
3880 	for (i = 0; i < nitems(vif->link_conf); i++) {
3881 		IMPROVE("more than 1 link one day");
3882 	}
3883 
3884 	/* Setup queue defaults; driver may override in (*add_interface). */
3885 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3886 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3887 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3888 		else if (hw->queues >= IEEE80211_NUM_ACS)
3889 			vif->hw_queue[i] = i;
3890 		else
3891 			vif->hw_queue[i] = 0;
3892 
3893 		/* Initialize the queue to running. Stopped? */
3894 		lvif->hw_queue_stopped[i] = false;
3895 	}
3896 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3897 
3898 	IMPROVE();
3899 
3900 	wiphy_lock(hw->wiphy);
3901 	error = lkpi_80211_mo_start(hw);
3902 	if (error != 0) {
3903 		wiphy_unlock(hw->wiphy);
3904 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
3905 		mtx_destroy(&lvif->mtx);
3906 		free(lvif, M_80211_VAP);
3907 		return (NULL);
3908 	}
3909 
3910 	error = lkpi_80211_mo_add_interface(hw, vif);
3911 	if (error != 0) {
3912 		IMPROVE();	/* XXX-BZ mo_stop()? */
3913 		wiphy_unlock(hw->wiphy);
3914 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
3915 		mtx_destroy(&lvif->mtx);
3916 		free(lvif, M_80211_VAP);
3917 		return (NULL);
3918 	}
3919 	wiphy_unlock(hw->wiphy);
3920 
3921 	LKPI_80211_LHW_LVIF_LOCK(lhw);
3922 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
3923 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3924 
3925 	/* Set bss_info. */
3926 	changed = 0;
3927 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
3928 
3929 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3930 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3931 	wiphy_lock(hw->wiphy);
3932 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3933 
3934 		bzero(&txqp, sizeof(txqp));
3935 		txqp.cw_min = 15;
3936 		txqp.cw_max = 1023;
3937 		txqp.txop = 0;
3938 		txqp.aifs = 2;
3939 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3940 		if (error != 0)
3941 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3942 			    __func__, ac, error);
3943 	}
3944 	wiphy_unlock(hw->wiphy);
3945 	changed = BSS_CHANGED_QOS;
3946 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
3947 
3948 	/* Force MC init. */
3949 	lkpi_update_mcast_filter(ic);
3950 
3951 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
3952 
3953 	/* Now we have a valid vap->iv_ifp.  Any checksum offloading goes below. */
3954 
3955 	IMPROVE();
3956 
3957 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
3958 	lvif->iv_newstate = vap->iv_newstate;
3959 	vap->iv_newstate = lkpi_iv_newstate;
3960 	lvif->iv_update_bss = vap->iv_update_bss;
3961 	vap->iv_update_bss = lkpi_iv_update_bss;
3962 	lvif->iv_recv_mgmt = vap->iv_recv_mgmt;
3963 	vap->iv_recv_mgmt = lkpi_iv_sta_recv_mgmt;
3964 
3965 #ifdef LKPI_80211_HW_CRYPTO
3966 	/* Key management. */
3967 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
3968 		vap->iv_key_set = lkpi_iv_key_set;
3969 		vap->iv_key_delete = lkpi_iv_key_delete;
3970 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3971 		vap->iv_key_update_end = lkpi_iv_key_update_end;
3972 	}
3973 #endif
3974 
3975 #ifdef LKPI_80211_HT
3976 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
3977 #endif
3978 
3979 	ieee80211_ratectl_init(vap);
3980 
3981 	/* Complete setup. */
3982 	ieee80211_vap_attach(vap, ieee80211_media_change,
3983 	    ieee80211_media_status, mac);
3984 
3985 #ifdef LKPI_80211_HT
3986 	/*
3987 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
3988 	 * to do so if they cannot do the crypto too.
3989 	 */
3990 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
3991 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
3992 #endif
3993 #if defined(LKPI_80211_HT)
3994 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3995 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3996 #endif
3997 
3998 	if (hw->max_listen_interval == 0)
3999 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
4000 	hw->conf.listen_interval = hw->max_listen_interval;
4001 	ic->ic_set_channel(ic);
4002 
4003 	/* XXX-BZ do we need to be able to update these? */
4004 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
4005 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
4006 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
4007 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
4008 	/* any others? */
4009 
4010 	/* Add per-VIF/VAP sysctls. */
4011 	sysctl_ctx_init(&lvif->sysctl_ctx);
4012 
4013 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
4014 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
4015 	    OID_AUTO, if_name(vap->iv_ifp),
4016 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
4017 
4018 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4019 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
4020 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
4021 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
4022 
4023 	IMPROVE();
4024 
4025 	return (vap);
4026 }
4027 
4028 void
4029 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
4030 {
4031 
4032 	wiphy_unregister(hw->wiphy);
4033 	linuxkpi_ieee80211_ifdetach(hw);
4034 
4035 	IMPROVE();
4036 }
4037 
4038 void
4039 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
4040 {
4041 
4042 	TODO();
4043 }
4044 
4045 static void
4046 lkpi_ic_vap_delete(struct ieee80211vap *vap)
4047 {
4048 	struct ieee80211com *ic;
4049 	struct lkpi_hw *lhw;
4050 	struct ieee80211_hw *hw;
4051 	struct lkpi_vif *lvif;
4052 	struct ieee80211_vif *vif;
4053 
4054 	lvif = VAP_TO_LVIF(vap);
4055 	vif = LVIF_TO_VIF(lvif);
4056 	ic = vap->iv_ic;
4057 	lhw = ic->ic_softc;
4058 	hw = LHW_TO_HW(lhw);
4059 
4060 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
4061 
4062 	/* Clear up per-VIF/VAP sysctls. */
4063 	sysctl_ctx_free(&lvif->sysctl_ctx);
4064 
4065 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4066 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
4067 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4068 
4069 	ieee80211_ratectl_deinit(vap);
4070 	ieee80211_vap_detach(vap);
4071 
4072 	IMPROVE("clear up other bits in this state");
4073 
4074 	lkpi_80211_mo_remove_interface(hw, vif);
4075 
4076 	/* Single VAP, so we can do this here. */
4077 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
4078 
4079 	mtx_destroy(&lvif->mtx);
4080 	free(lvif, M_80211_VAP);
4081 }
4082 
4083 static void
4084 lkpi_ic_update_mcast(struct ieee80211com *ic)
4085 {
4086 	struct ieee80211vap *vap;
4087 	struct lkpi_hw *lhw;
4088 
4089 	lhw = ic->ic_softc;
4090 	if (lhw->ops->prepare_multicast == NULL ||
4091 	    lhw->ops->configure_filter == NULL)
4092 		return;
4093 
4094 	LKPI_80211_LHW_MC_LOCK(lhw);
4095 	/* Cleanup anything on the current list. */
4096 	lkpi_cleanup_mcast_list_locked(lhw);
4097 
4098 	/* Build up the new list (or allmulti). */
4099 	if (ic->ic_allmulti == 0) {
4100 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
4101 			if_foreach_llmaddr(vap->iv_ifp,
4102 			    lkpi_ic_update_mcast_copy, &lhw->mc_list);
4103 		lhw->mc_all_multi = false;
4104 	} else {
4105 		lhw->mc_all_multi = true;
4106 	}
4107 	LKPI_80211_LHW_MC_UNLOCK(lhw);
4108 
4109 	lkpi_update_mcast_filter(ic);
4110 	TRACEOK();
4111 }
4112 
4113 static void
4114 lkpi_ic_update_promisc(struct ieee80211com *ic)
4115 {
4116 
4117 	UNIMPLEMENTED;
4118 }
4119 
4120 static void
4121 lkpi_ic_update_chw(struct ieee80211com *ic)
4122 {
4123 
4124 	UNIMPLEMENTED;
4125 }
4126 
4127 /* Start / stop device. */
4128 static void
4129 lkpi_ic_parent(struct ieee80211com *ic)
4130 {
4131 	struct lkpi_hw *lhw;
4132 	struct ieee80211_hw *hw;
4133 #ifdef HW_START_STOP
4134 	int error;
4135 #endif
4136 	bool start_all;
4137 
4138 	IMPROVE();
4139 
4140 	lhw = ic->ic_softc;
4141 	hw = LHW_TO_HW(lhw);
4142 	start_all = false;
4143 
4144 	/* IEEE80211_UNLOCK(ic); */
4145 	wiphy_lock(hw->wiphy);
4146 	if (ic->ic_nrunning > 0) {
4147 #ifdef HW_START_STOP
4148 		error = lkpi_80211_mo_start(hw);
4149 		if (error == 0)
4150 #endif
4151 			start_all = true;
4152 	} else {
4153 #ifdef HW_START_STOP
4154 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
4155 #endif
4156 	}
4157 	wiphy_unlock(hw->wiphy);
4158 	/* IEEE80211_LOCK(ic); */
4159 
4160 	if (start_all)
4161 		ieee80211_start_all(ic);
4162 }
4163 
4164 bool
4165 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
4166     size_t ie_ids_len)
4167 {
4168 	int i;
4169 
4170 	for (i = 0; i < ie_ids_len; i++) {
4171 		if (ie == *ie_ids)
4172 			return (true);
4173 	}
4174 
4175 	return (false);
4176 }
4177 
4178 /* Return true if skipped; false if error. */
4179 bool
4180 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
4181 {
4182 	size_t x;
4183 	uint8_t l;
4184 
4185 	x = *xp;
4186 
4187 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
4188 	    __func__, x, ies_len, ies));
4189 	l = ies[x + 1];
4190 	x += 2 + l;
4191 
4192 	if (x > ies_len)
4193 		return (false);
4194 
4195 	*xp = x;
4196 	return (true);
4197 }
4198 
4199 static uint8_t *
4200 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4201     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
4202 {
4203 	struct ieee80211_supported_band *supband;
4204 	struct linuxkpi_ieee80211_channel *channels;
4205 	struct ieee80211com *ic;
4206 	const struct ieee80211_channel *chan;
4207 	const struct ieee80211_rateset *rs;
4208 	uint8_t *pb;
4209 	int band, i;
4210 
4211 	ic = vap->iv_ic;
4212 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4213 		if ((band_mask & (1 << band)) == 0)
4214 			continue;
4215 
4216 		supband = hw->wiphy->bands[band];
4217 		/*
4218 		 * This should not happen;
4219 		 * band_mask is a bitmask of valid bands to scan on.
4220 		 */
4221 		if (supband == NULL || supband->n_channels == 0)
4222 			continue;
4223 
4224 		/* Find a first channel to get the mode and rates from. */
4225 		channels = supband->channels;
4226 		chan = NULL;
4227 		for (i = 0; i < supband->n_channels; i++) {
4228 			uint32_t flags;
4229 
4230 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4231 				continue;
4232 
4233 			flags = 0;
4234 			switch (band) {
4235 			case NL80211_BAND_2GHZ:
4236 				flags |= IEEE80211_CHAN_G;
4237 				break;
4238 			case NL80211_BAND_5GHZ:
4239 				flags |= IEEE80211_CHAN_A;
4240 				break;
4241 			default:
4242 				panic("%s:%d: unupported band %d\n",
4243 				    __func__, __LINE__, band);
4244 			}
4245 
4246 			chan = ieee80211_find_channel(ic,
4247 			    channels[i].center_freq, flags);
4248 			if (chan != NULL)
4249 				break;
4250 		}
4251 
4252 		/* This really should not happen. */
4253 		if (chan == NULL)
4254 			continue;
4255 
4256 		pb = p;
4257 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
4258 		p = ieee80211_add_rates(p, rs);
4259 		p = ieee80211_add_xrates(p, rs);
4260 
4261 #if defined(LKPI_80211_HT)
4262 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
4263 			struct ieee80211_channel *c;
4264 
4265 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4266 			    vap->iv_flags_ht);
4267 			p = ieee80211_add_htcap_ch(p, vap, c);
4268 		}
4269 #endif
4270 #if defined(LKPI_80211_VHT)
4271 		if (band == NL80211_BAND_5GHZ &&
4272 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
4273 			struct ieee80211_channel *c;
4274 
4275 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4276 			    vap->iv_flags_ht);
4277 			c = ieee80211_vht_adjust_channel(ic, c,
4278 			    vap->iv_vht_flags);
4279 			p = ieee80211_add_vhtcap_ch(p, vap, c);
4280 		}
4281 #endif
4282 
4283 		scan_ies->ies[band] = pb;
4284 		scan_ies->len[band] = p - pb;
4285 	}
4286 
4287 	/* Add common_ies */
4288 	pb = p;
4289 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4290 	    vap->iv_wpa_ie != NULL) {
4291 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4292 		p += 2 + vap->iv_wpa_ie[1];
4293 	}
4294 	if (vap->iv_appie_probereq != NULL) {
4295 		memcpy(p, vap->iv_appie_probereq->ie_data,
4296 		    vap->iv_appie_probereq->ie_len);
4297 		p += vap->iv_appie_probereq->ie_len;
4298 	}
4299 	scan_ies->common_ies = pb;
4300 	scan_ies->common_ie_len = p - pb;
4301 
4302 	return (p);
4303 }
4304 
4305 static void
4306 lkpi_ic_scan_start(struct ieee80211com *ic)
4307 {
4308 	struct lkpi_hw *lhw;
4309 	struct ieee80211_hw *hw;
4310 	struct lkpi_vif *lvif;
4311 	struct ieee80211_vif *vif;
4312 	struct ieee80211_scan_state *ss;
4313 	struct ieee80211vap *vap;
4314 	int error;
4315 	bool is_hw_scan;
4316 
4317 	lhw = ic->ic_softc;
4318 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4319 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4320 		/* A scan is still running. */
4321 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4322 		return;
4323 	}
4324 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4325 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4326 
4327 	ss = ic->ic_scan;
4328 	vap = ss->ss_vap;
4329 	if (vap->iv_state != IEEE80211_S_SCAN) {
4330 		IMPROVE("We need to be able to scan if not in S_SCAN");
4331 		return;
4332 	}
4333 
4334 	hw = LHW_TO_HW(lhw);
4335 	if (!is_hw_scan) {
4336 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4337 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4338 sw_scan:
4339 		lvif = VAP_TO_LVIF(vap);
4340 		vif = LVIF_TO_VIF(lvif);
4341 
4342 		if (vap->iv_state == IEEE80211_S_SCAN)
4343 			lkpi_hw_conf_idle(hw, false);
4344 
4345 		lkpi_update_mcast_filter(ic);
4346 
4347 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
4348 		/* net80211::scan_start() handled PS for us. */
4349 		IMPROVE();
4350 		/* XXX Also means it is too late to flush queues?
4351 		 * need to check iv_sta_ps or overload? */
4352 		/* XXX want to adjust ss end time/ maxdwell? */
4353 
4354 	} else {
4355 		struct ieee80211_scan_request *hw_req;
4356 		struct linuxkpi_ieee80211_channel *lc, **cpp;
4357 		struct cfg80211_ssid *ssids;
4358 		struct cfg80211_scan_6ghz_params *s6gp;
4359 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4360 		int band, i, ssid_count, common_ie_len;
4361 		uint32_t band_mask;
4362 		uint8_t *ie, *ieend;
4363 		bool running;
4364 
4365 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4366 		ssids_len = ssid_count * sizeof(*ssids);
4367 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
4368 
4369 		band_mask = 0;
4370 		nchan = 0;
4371 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4372 #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4373 			for (i = ss->ss_next; i < ss->ss_last; i++) {
4374 				nchan++;
4375 				band = lkpi_net80211_chan_to_nl80211_band(
4376 				    ss->ss_chans[ss->ss_next + i]);
4377 				band_mask |= (1 << band);
4378 			}
4379 #else
4380 			/* Instead we scan for all channels all the time. */
4381 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4382 				switch (band) {
4383 				case NL80211_BAND_2GHZ:
4384 				case NL80211_BAND_5GHZ:
4385 					break;
4386 				default:
4387 					continue;
4388 				}
4389 				if (hw->wiphy->bands[band] != NULL) {
4390 					nchan += hw->wiphy->bands[band]->n_channels;
4391 					band_mask |= (1 << band);
4392 				}
4393 			}
4394 #endif
4395 		} else {
4396 			IMPROVE("individual band scans not yet supported, only scanning first band");
4397 			/* In theory net80211 should drive this. */
4398 			/* Probably we need to add local logic for now;
4399 			 * need to deal with scan_complete
4400 			 * and cancel_scan and keep local state.
4401 			 * Also cut the nchan down above.
4402 			 */
4403 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
4404 		}
4405 
4406 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
4407 
4408 		common_ie_len = 0;
4409 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4410 		    vap->iv_wpa_ie != NULL)
4411 			common_ie_len += vap->iv_wpa_ie[1];
4412 		if (vap->iv_appie_probereq != NULL)
4413 			common_ie_len += vap->iv_appie_probereq->ie_len;
4414 
4415 		/* We would love to check this at an earlier stage... */
4416 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4417 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4418 			    "wiphy->max_scan_ie_len %d\n", __func__,
4419 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4420 		}
4421 
4422 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4423 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4424 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
4425 
4426 		hw_req->req.flags = 0;			/* XXX ??? */
4427 		/* hw_req->req.wdev */
4428 		hw_req->req.wiphy = hw->wiphy;
4429 		hw_req->req.no_cck = false;		/* XXX */
4430 #if 0
4431 		/* This seems to pessimise default scanning behaviour. */
4432 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
4433 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
4434 #endif
4435 #ifdef __notyet__
4436 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
4437 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
4438 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
4439 #endif
4440 		eth_broadcast_addr(hw_req->req.bssid);
4441 
4442 		hw_req->req.n_channels = nchan;
4443 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
4444 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
4445 		for (i = 0; i < nchan; i++) {
4446 			*(cpp + i) =
4447 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
4448 		}
4449 #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4450 		for (i = 0; i < nchan; i++) {
4451 			struct ieee80211_channel *c;
4452 
4453 			c = ss->ss_chans[ss->ss_next + i];
4454 			lc->center_freq = c->ic_freq;	/* XXX */
4455 			/* lc->flags */
4456 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
4457 			lc->max_power = c->ic_maxpower;
4458 			/* lc-> ... */
4459 			lc++;
4460 		}
4461 #else
4462 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4463 			struct ieee80211_supported_band *supband;
4464 			struct linuxkpi_ieee80211_channel *channels;
4465 
4466 			/* Band disabled for scanning? */
4467 			if ((band_mask & (1 << band)) == 0)
4468 				continue;
4469 
4470 			/* Nothing to scan in band? */
4471 			supband = hw->wiphy->bands[band];
4472 			if (supband == NULL || supband->n_channels == 0)
4473 				continue;
4474 
4475 			channels = supband->channels;
4476 			for (i = 0; i < supband->n_channels; i++) {
4477 				*lc = channels[i];
4478 				lc++;
4479 			}
4480 		}
4481 #endif
4482 		hw_req->req.n_ssids = ssid_count;
4483 		if (hw_req->req.n_ssids > 0) {
4484 			ssids = (struct cfg80211_ssid *)lc;
4485 			hw_req->req.ssids = ssids;
4486 			for (i = 0; i < ssid_count; i++) {
4487 				ssids->ssid_len = ss->ss_ssid[i].len;
4488 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
4489 				    ss->ss_ssid[i].len);
4490 				ssids++;
4491 			}
4492 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
4493 		} else {
4494 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
4495 		}
4496 
4497 		/* 6GHz one day. */
4498 		hw_req->req.n_6ghz_params = 0;
4499 		hw_req->req.scan_6ghz_params = NULL;
4500 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
4501 		/* s6gp->... */
4502 
4503 		ie = ieend = (uint8_t *)s6gp;
4504 		/* Copy per-band IEs, copy common IEs */
4505 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4506 		hw_req->req.ie = ie;
4507 		hw_req->req.ie_len = ieend - ie;
4508 
4509 		lvif = VAP_TO_LVIF(vap);
4510 		vif = LVIF_TO_VIF(lvif);
4511 
4512 		LKPI_80211_LHW_SCAN_LOCK(lhw);
4513 		/* Re-check under lock. */
4514 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
4515 		if (!running) {
4516 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
4517 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
4518 
4519 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
4520 			lhw->hw_req = hw_req;
4521 		}
4522 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4523 		if (running) {
4524 			free(hw_req, M_LKPI80211);
4525 			return;
4526 		}
4527 
4528 		lkpi_update_mcast_filter(ic);
4529 
4530 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
4531 		if (error != 0) {
4532 			ieee80211_cancel_scan(vap);
4533 
4534 			/*
4535 			 * ieee80211_scan_completed must be called in either
4536 			 * case of error or none.  So let the free happen there
4537 			 * and only there.
4538 			 * That would be fine in theory but in practice drivers
4539 			 * behave differently:
4540 			 * ath10k does not return hw_scan until after scan_complete
4541 			 *        and can then still return an error.
4542 			 * rtw88 can return 1 or -EBUSY without scan_complete
4543 			 * iwlwifi can return various errors before scan starts
4544 			 * ...
4545 			 * So we cannot rely on that behaviour and have to check
4546 			 * and balance between both code paths.
4547 			 */
4548 			LKPI_80211_LHW_SCAN_LOCK(lhw);
4549 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4550 				free(lhw->hw_req, M_LKPI80211);
4551 				lhw->hw_req = NULL;
4552 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4553 			}
4554 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4555 			lkpi_update_mcast_filter(ic);
4556 
4557 			/*
4558 			 * XXX-SIGH magic number.
4559 			 * rtw88 has a magic "return 1" if offloading scan is
4560 			 * not possible.  Fall back to sw scan in that case.
4561 			 */
4562 			if (error == 1) {
4563 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4564 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
4565 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4566 				/*
4567 				 * XXX If we clear this now and later a driver
4568 				 * thinks it * can do a hw_scan again, we will
4569 				 * currently not re-enable it?
4570 				 */
4571 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4572 				ieee80211_start_scan(vap,
4573 				    IEEE80211_SCAN_ACTIVE |
4574 				    IEEE80211_SCAN_NOPICK |
4575 				    IEEE80211_SCAN_ONCE,
4576 				    IEEE80211_SCAN_FOREVER,
4577 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4578 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4579 				    vap->iv_des_nssid, vap->iv_des_ssid);
4580 				goto sw_scan;
4581 			}
4582 
4583 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4584 			    __func__, error);
4585 		}
4586 	}
4587 }
4588 
4589 static void
4590 lkpi_ic_scan_end(struct ieee80211com *ic)
4591 {
4592 	struct lkpi_hw *lhw;
4593 	bool is_hw_scan;
4594 
4595 	lhw = ic->ic_softc;
4596 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4597 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
4598 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4599 		return;
4600 	}
4601 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4602 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4603 
4604 	if (!is_hw_scan) {
4605 		struct ieee80211_scan_state *ss;
4606 		struct ieee80211vap *vap;
4607 		struct ieee80211_hw *hw;
4608 		struct lkpi_vif *lvif;
4609 		struct ieee80211_vif *vif;
4610 
4611 		ss = ic->ic_scan;
4612 		vap = ss->ss_vap;
4613 		hw = LHW_TO_HW(lhw);
4614 		lvif = VAP_TO_LVIF(vap);
4615 		vif = LVIF_TO_VIF(lvif);
4616 
4617 		lkpi_80211_mo_sw_scan_complete(hw, vif);
4618 
4619 		/* Send PS to stop buffering if n80211 does not for us? */
4620 
4621 		if (vap->iv_state == IEEE80211_S_SCAN)
4622 			lkpi_hw_conf_idle(hw, true);
4623 	}
4624 }
4625 
4626 static void
4627 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4628     unsigned long maxdwell)
4629 {
4630 	struct lkpi_hw *lhw;
4631 	bool is_hw_scan;
4632 
4633 	lhw = ss->ss_ic->ic_softc;
4634 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4635 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4636 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4637 	if (!is_hw_scan)
4638 		lhw->ic_scan_curchan(ss, maxdwell);
4639 }
4640 
4641 static void
4642 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4643 {
4644 	struct lkpi_hw *lhw;
4645 	bool is_hw_scan;
4646 
4647 	lhw = ss->ss_ic->ic_softc;
4648 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4649 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4650 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4651 	if (!is_hw_scan)
4652 		lhw->ic_scan_mindwell(ss);
4653 }
4654 
4655 static void
4656 lkpi_ic_set_channel(struct ieee80211com *ic)
4657 {
4658 	struct lkpi_hw *lhw;
4659 	struct ieee80211_hw *hw;
4660 	struct ieee80211_channel *c;
4661 	struct linuxkpi_ieee80211_channel *chan;
4662 	int error;
4663 	bool hw_scan_running;
4664 
4665 	lhw = ic->ic_softc;
4666 
4667 	/* If we do not support (*config)() save us the work. */
4668 	if (lhw->ops->config == NULL)
4669 		return;
4670 
4671 	/* If we have a hw_scan running do not switch channels. */
4672 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4673 	hw_scan_running =
4674 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
4675 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
4676 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4677 	if (hw_scan_running)
4678 		return;
4679 
4680 	c = ic->ic_curchan;
4681 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
4682 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
4683 		    c, lhw->ops->config);
4684 		return;
4685 	}
4686 
4687 	chan = lkpi_find_lkpi80211_chan(lhw, c);
4688 	if (chan == NULL) {
4689 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
4690 		    c, chan);
4691 		return;
4692 	}
4693 
4694 	/* XXX max power for scanning? */
4695 	IMPROVE();
4696 
4697 	hw = LHW_TO_HW(lhw);
4698 	cfg80211_chandef_create(&hw->conf.chandef, chan,
4699 #ifdef LKPI_80211_HT
4700 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
4701 #endif
4702 	    NL80211_CHAN_NO_HT);
4703 
4704 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
4705 	if (error != 0 && error != EOPNOTSUPP) {
4706 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
4707 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
4708 		/* XXX should we unroll to the previous chandef? */
4709 		IMPROVE();
4710 	} else {
4711 		/* Update radiotap channels as well. */
4712 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
4713 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
4714 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
4715 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
4716 	}
4717 
4718 	/* Currently PS is hard coded off! Not sure it belongs here. */
4719 	IMPROVE();
4720 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
4721 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
4722 		hw->conf.flags &= ~IEEE80211_CONF_PS;
4723 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
4724 		if (error != 0 && error != EOPNOTSUPP)
4725 			ic_printf(ic, "ERROR: %s: config %#0x returned "
4726 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
4727 			    error);
4728 	}
4729 }
4730 
4731 static struct ieee80211_node *
4732 lkpi_ic_node_alloc(struct ieee80211vap *vap,
4733     const uint8_t mac[IEEE80211_ADDR_LEN])
4734 {
4735 	struct ieee80211com *ic;
4736 	struct lkpi_hw *lhw;
4737 	struct ieee80211_node *ni;
4738 	struct ieee80211_hw *hw;
4739 	struct lkpi_sta *lsta;
4740 
4741 	ic = vap->iv_ic;
4742 	lhw = ic->ic_softc;
4743 
4744 	/* We keep allocations de-coupled so we can deal with the two worlds. */
4745 	if (lhw->ic_node_alloc == NULL)
4746 		return (NULL);
4747 
4748 	ni = lhw->ic_node_alloc(vap, mac);
4749 	if (ni == NULL)
4750 		return (NULL);
4751 
4752 	hw = LHW_TO_HW(lhw);
4753 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
4754 	if (lsta == NULL) {
4755 		if (lhw->ic_node_free != NULL)
4756 			lhw->ic_node_free(ni);
4757 		return (NULL);
4758 	}
4759 
4760 	return (ni);
4761 }
4762 
4763 static int
4764 lkpi_ic_node_init(struct ieee80211_node *ni)
4765 {
4766 	struct ieee80211com *ic;
4767 	struct lkpi_hw *lhw;
4768 	int error;
4769 
4770 	ic = ni->ni_ic;
4771 	lhw = ic->ic_softc;
4772 
4773 	if (lhw->ic_node_init != NULL) {
4774 		error = lhw->ic_node_init(ni);
4775 		if (error != 0)
4776 			return (error);
4777 	}
4778 
4779 	/* XXX-BZ Sync other state over. */
4780 	IMPROVE();
4781 
4782 	return (0);
4783 }
4784 
4785 static void
4786 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
4787 {
4788 	struct ieee80211com *ic;
4789 	struct lkpi_hw *lhw;
4790 
4791 	ic = ni->ni_ic;
4792 	lhw = ic->ic_softc;
4793 
4794 	/* XXX-BZ remove from driver, ... */
4795 	IMPROVE();
4796 
4797 	if (lhw->ic_node_cleanup != NULL)
4798 		lhw->ic_node_cleanup(ni);
4799 }
4800 
4801 static void
4802 lkpi_ic_node_free(struct ieee80211_node *ni)
4803 {
4804 	struct ieee80211com *ic;
4805 	struct lkpi_hw *lhw;
4806 	struct lkpi_sta *lsta;
4807 
4808 	ic = ni->ni_ic;
4809 	lhw = ic->ic_softc;
4810 	lsta = ni->ni_drv_data;
4811 
4812 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
4813 
4814 	/*
4815 	 * Pass in the original ni just in case of error we could check that
4816 	 * it is the same as lsta->ni.
4817 	 */
4818 	lkpi_lsta_free(lsta, ni);
4819 
4820 	if (lhw->ic_node_free != NULL)
4821 		lhw->ic_node_free(ni);
4822 }
4823 
4824 /*
4825  * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
4826  * call path.
4827  * Unfortunately they have slightly different invariants.  See
4828  * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
4829  * Both take care of the ni reference in case of error, and otherwise during
4830  * the callback after transmit.
4831  * The difference is that in case of error (*ic_raw_xmit) needs us to release
4832  * the mbuf, while (*ic_transmit) will free the mbuf itself.
4833  */
4834 static int
4835 lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
4836     const struct ieee80211_bpf_params *params __unused,
4837     bool freem)
4838 {
4839 	struct lkpi_sta *lsta;
4840 	int error;
4841 
4842 	lsta = ni->ni_drv_data;
4843 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4844 #if 0
4845 	if (!lsta->added_to_drv || !lsta->txq_ready) {
4846 #else
4847 	/*
4848 	 * Backout this part of 886653492945f which breaks rtw88 or
4849 	 * in general drivers without (*sta_state)() but only the
4850 	 * legacy fallback to (*sta_add)().
4851 	 */
4852 	if (!lsta->txq_ready) {
4853 #endif
4854 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4855 		if (freem)
4856 			m_free(m);
4857 		return (ENETDOWN);
4858 	}
4859 
4860 	/* Queue the packet and enqueue the task to handle it. */
4861 	error = mbufq_enqueue(&lsta->txq, m);
4862 	if (error != 0) {
4863 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4864 		if (freem)
4865 			m_free(m);
4866 #ifdef LINUXKPI_DEBUG_80211
4867 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4868 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4869 			    __func__, error);
4870 #endif
4871 		return (ENETDOWN);
4872 	}
4873 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4874 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4875 
4876 #ifdef LINUXKPI_DEBUG_80211
4877 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4878 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
4879 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
4880 		    mbufq_len(&lsta->txq));
4881 #endif
4882 
4883 	return (0);
4884 }
4885 
4886 static int
4887 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
4888         const struct ieee80211_bpf_params *params __unused)
4889 {
4890 	return (lkpi_xmit(ni, m, NULL, true));
4891 }
4892 
4893 #ifdef LKPI_80211_HW_CRYPTO
4894 /*
4895  * This is a bit of a hack given we know we are operating on a
4896  * single frame and we know that hardware will deal with it.
4897  * But otherwise the enmic bit and the encrypt bit need to be
4898  * decoupled.
4899  */
4900 static int
4901 lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
4902     struct ieee80211_key_conf *kc, struct sk_buff *skb)
4903 {
4904 	struct ieee80211_hdr *hdr;
4905 	uint32_t hlen, hdrlen;
4906 	uint8_t *p;
4907 
4908 	/*
4909 	 * TKIP only happens on data.
4910 	 */
4911 	hdr = (void *)skb->data;
4912 	if (!ieee80211_is_data_present(hdr->frame_control))
4913 		return (0);
4914 
4915 	/*
4916 	 * "enmic" (though we do not do that).
4917 	 */
4918 	/* any conditions to not apply this? */
4919 	if (skb_tailroom(skb) < k->wk_cipher->ic_miclen)
4920 		return (ENOBUFS);
4921 
4922 	p = skb_put(skb, k->wk_cipher->ic_miclen);
4923 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
4924 		goto encrypt;
4925 
4926 	/*
4927 	 * (*enmic) which we hopefully do not have to do with hw accel.
4928 	 * That means if we make it here we have a problem.
4929 	 */
4930 	TODO("(*enmic)");
4931 	return (ENXIO);
4932 
4933 encrypt:
4934 	/*
4935 	 * "encrypt" (though we do not do that).
4936 	 */
4937 	/*
4938 	 * Check if we have anything to do as requested by driver
4939 	 * or if we are done?
4940 	 */
4941 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
4942 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
4943 			return (0);
4944 
4945 	hlen = k->wk_cipher->ic_header;
4946 	if (skb_headroom(skb) < hlen)
4947 		return (ENOBUFS);
4948 
4949 	hdr = (void *)skb->data;
4950 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4951 	p = skb_push(skb, hlen);
4952 	memmove(p, p + hlen, hdrlen);
4953 
4954 	/* If driver request space only we are done. */
4955 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
4956 		return (0);
4957 
4958 	p += hdrlen;
4959 	k->wk_cipher->ic_setiv(k, p);
4960 
4961 	/* If we make it hear we do sw encryption. */
4962 	TODO("sw encrypt");
4963 	return (ENXIO);
4964 }
4965 
4966 static int
4967 lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
4968     struct ieee80211_key_conf *kc, struct sk_buff *skb)
4969 {
4970 	struct ieee80211_hdr *hdr;
4971 	uint32_t hlen, hdrlen;
4972 	uint8_t *p;
4973 
4974 	hdr = (void *)skb->data;
4975 
4976 	/*
4977 	 * Check if we have anythig to do as requested by driver
4978 	 * or if we are done?
4979 	 */
4980 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
4981 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
4982 	    /* MFP */
4983 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
4984 		ieee80211_is_mgmt(hdr->frame_control)))
4985 			return (0);
4986 
4987 	hlen = k->wk_cipher->ic_header;
4988 	if (skb_headroom(skb) < hlen)
4989 		return (ENOBUFS);
4990 
4991 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4992 	p = skb_push(skb, hlen);
4993 	memmove(p, p + hlen, hdrlen);
4994 
4995 	/* If driver request space only we are done. */
4996 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
4997 		return (0);
4998 
4999 	p += hdrlen;
5000 	k->wk_cipher->ic_setiv(k, p);
5001 
5002 	return (0);
5003 }
5004 
5005 static int
5006 lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
5007     struct sk_buff *skb)
5008 {
5009 	struct ieee80211_tx_info *info;
5010 	struct ieee80211_key_conf *kc;
5011 
5012 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
5013 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
5014 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
5015 
5016 	kc = lsta->kc[k->wk_keyix];
5017 
5018 	info = IEEE80211_SKB_CB(skb);
5019 	info->control.hw_key = kc;
5020 
5021 	/* MUST NOT happen. KASSERT? */
5022 	if (kc == NULL) {
5023 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
5024 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
5025 		return (ENXIO);
5026 	}
5027 
5028 	switch (kc->cipher) {
5029 	case WLAN_CIPHER_SUITE_TKIP:
5030 		return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
5031 	case WLAN_CIPHER_SUITE_CCMP:
5032 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5033 	case WLAN_CIPHER_SUITE_GCMP:
5034 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5035 	case WLAN_CIPHER_SUITE_WEP40:
5036 	case WLAN_CIPHER_SUITE_WEP104:
5037 	case WLAN_CIPHER_SUITE_CCMP_256:
5038 	case WLAN_CIPHER_SUITE_GCMP_256:
5039 	case WLAN_CIPHER_SUITE_AES_CMAC:
5040 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5041 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5042 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5043 	default:
5044 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
5045 		    "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
5046 		    skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
5047 		return (EOPNOTSUPP);
5048 	}
5049 }
5050 
5051 static uint8_t
5052 lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
5053 {
5054 	struct ieee80211_key_conf *kc;
5055 
5056 	kc = lsta->kc[k->wk_keyix];
5057 	if (kc == NULL)
5058 		return (0);
5059 
5060 	IMPROVE("which other flags need tailroom?");
5061 	if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
5062 		return (32);	/* Large enough to hold everything and pow2. */
5063 
5064 	return (0);
5065 }
5066 #endif
5067 
5068 static void
5069 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
5070 {
5071 	struct ieee80211_node *ni;
5072 	struct ieee80211_frame *wh;
5073 	struct ieee80211_key *k;
5074 	struct sk_buff *skb;
5075 	struct ieee80211com *ic;
5076 	struct lkpi_hw *lhw;
5077 	struct ieee80211_hw *hw;
5078 	struct lkpi_vif *lvif;
5079 	struct ieee80211_vif *vif;
5080 	struct ieee80211_channel *c;
5081 	struct ieee80211_tx_control control;
5082 	struct ieee80211_tx_info *info;
5083 	struct ieee80211_sta *sta;
5084 	struct ieee80211_hdr *hdr;
5085 	struct lkpi_txq *ltxq;
5086 	void *buf;
5087 	ieee80211_keyix keyix;
5088 	uint8_t ac, tid, tailroom;
5089 
5090 	M_ASSERTPKTHDR(m);
5091 #ifdef LINUXKPI_DEBUG_80211
5092 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
5093 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
5094 #endif
5095 
5096 	ni = lsta->ni;
5097 	k = NULL;
5098 	keyix = IEEE80211_KEYIX_NONE;
5099 	wh = mtod(m, struct ieee80211_frame *);
5100 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
5101 
5102 #ifdef LKPI_80211_HW_CRYPTO
5103 		if (lkpi_hwcrypto) {
5104 			k = ieee80211_crypto_get_txkey(ni, m);
5105 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
5106 				keyix = k->wk_keyix;
5107 		}
5108 #endif
5109 
5110 		/* Encrypt the frame if need be. */
5111 		if (keyix == IEEE80211_KEYIX_NONE) {
5112 			/* Retrieve key for TX && do software encryption. */
5113 			k = ieee80211_crypto_encap(ni, m);
5114 			if (k == NULL) {
5115 				ieee80211_free_node(ni);
5116 				m_freem(m);
5117 				return;
5118 			}
5119 		}
5120 	}
5121 
5122 	ic = ni->ni_ic;
5123 	lhw = ic->ic_softc;
5124 	hw = LHW_TO_HW(lhw);
5125 	c = ni->ni_chan;
5126 
5127 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
5128 		struct lkpi_radiotap_tx_hdr *rtap;
5129 
5130 		rtap = &lhw->rtap_tx;
5131 		rtap->wt_flags = 0;
5132 		if (k != NULL)
5133 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
5134 		if (m->m_flags & M_FRAG)
5135 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
5136 		IMPROVE();
5137 		rtap->wt_rate = 0;
5138 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
5139 			rtap->wt_chan_freq = htole16(c->ic_freq);
5140 			rtap->wt_chan_flags = htole16(c->ic_flags);
5141 		}
5142 
5143 		ieee80211_radiotap_tx(ni->ni_vap, m);
5144 	}
5145 
5146 #ifdef LKPI_80211_HW_CRYPTO
5147 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
5148 		tailroom = lkpi_hw_crypto_tailroom(lsta, k);
5149 	else
5150 #endif
5151 		tailroom = 0;
5152 
5153 	/*
5154 	 * net80211 should handle hw->extra_tx_headroom.
5155 	 * Though for as long as we are copying we don't mind.
5156 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
5157 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
5158 	 */
5159 	skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
5160 	if (skb == NULL) {
5161 		static uint8_t skb_alloc_failures = 0;
5162 
5163 		if (skb_alloc_failures++ == 0) {
5164 			int tid;
5165 
5166 			sta = LSTA_TO_STA(lsta);
5167 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
5168 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
5169 			for (tid = 0; tid < nitems(sta->txq); tid++) {
5170 				if (sta->txq[tid] == NULL)
5171 					continue;
5172 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5173 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
5174 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
5175 			}
5176 		}
5177 		ieee80211_free_node(ni);
5178 		m_freem(m);
5179 		return;
5180 	}
5181 	skb_reserve(skb, hw->extra_tx_headroom);
5182 
5183 	/* XXX-BZ we need a SKB version understanding mbuf. */
5184 	/* Save the mbuf for ieee80211_tx_complete(). */
5185 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
5186 	skb->m = m;
5187 #if 0
5188 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
5189 #else
5190 	buf = skb_put(skb, m->m_pkthdr.len);
5191 	m_copydata(m, 0, m->m_pkthdr.len, buf);
5192 #endif
5193 	/* Save the ni. */
5194 	m->m_pkthdr.PH_loc.ptr = ni;
5195 
5196 	lvif = VAP_TO_LVIF(ni->ni_vap);
5197 	vif = LVIF_TO_VIF(lvif);
5198 
5199 	hdr = (void *)skb->data;
5200 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
5201 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
5202 		if (!ieee80211_is_data(hdr->frame_control)) {
5203 			/* MGMT and CTRL frames go on TID 7/VO. */
5204 			skb->priority = 7;
5205 			ac = IEEE80211_AC_VO;
5206 		} else {
5207 			/* Other non-QOS traffic goes to BE. */
5208 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
5209 			skb->priority = 0;
5210 			ac = IEEE80211_AC_BE;
5211 		}
5212 	} else {
5213 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
5214 		ac = ieee80211e_up_to_ac[tid & 7];
5215 	}
5216 	skb_set_queue_mapping(skb, ac);
5217 
5218 	info = IEEE80211_SKB_CB(skb);
5219 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5220 	/* Slight delay; probably only happens on scanning so fine? */
5221 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
5222 		c = ic->ic_curchan;
5223 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
5224 	info->hw_queue = vif->hw_queue[ac];
5225 	if (m->m_flags & M_EAPOL)
5226 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
5227 	info->control.vif = vif;
5228 	/* XXX-BZ info->control.rates */
5229 #ifdef __notyet__
5230 #ifdef LKPI_80211_HT
5231 	info->control.rts_cts_rate_idx=
5232 	info->control.use_rts= /* RTS */
5233 	info->control.use_cts_prot= /* RTS/CTS*/
5234 #endif
5235 #endif
5236 
5237 	sta = LSTA_TO_STA(lsta);
5238 #ifdef LKPI_80211_HW_CRYPTO
5239 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
5240 		int error;
5241 
5242 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
5243 		if (error != 0) {
5244 			/*
5245 			 * We only have to free the skb which will free the
5246 			 * mbuf and release the reference on the ni.
5247 			 */
5248 			dev_kfree_skb(skb);
5249 			return;
5250 		}
5251 	}
5252 #endif
5253 
5254 	IMPROVE();
5255 
5256 	ltxq = NULL;
5257 	if (!ieee80211_is_data_present(hdr->frame_control)) {
5258 		if (vif->type == NL80211_IFTYPE_STATION &&
5259 		    lsta->added_to_drv &&
5260 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
5261 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
5262 	} else if (lsta->added_to_drv &&
5263 	    sta->txq[skb->priority] != NULL) {
5264 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
5265 	}
5266 	if (ltxq == NULL)
5267 		goto ops_tx;
5268 
5269 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
5270 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
5271 
5272 	LKPI_80211_LTXQ_LOCK(ltxq);
5273 	skb_queue_tail(&ltxq->skbq, skb);
5274 #ifdef LINUXKPI_DEBUG_80211
5275 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5276 		printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
5277 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5278 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
5279 		    __func__, __LINE__,
5280 		    curthread->td_tid, jiffies,
5281 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5282 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
5283 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
5284 #endif
5285 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5286 	wiphy_lock(hw->wiphy);
5287 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
5288 	wiphy_unlock(hw->wiphy);
5289 	return;
5290 
5291 ops_tx:
5292 #ifdef LINUXKPI_DEBUG_80211
5293 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5294 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5295 		    "TX ac %d prio %u qmap %u\n",
5296 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
5297 		    skb, ac, skb->priority, skb->qmap);
5298 #endif
5299 	memset(&control, 0, sizeof(control));
5300 	control.sta = sta;
5301 	wiphy_lock(hw->wiphy);
5302 	lkpi_80211_mo_tx(hw, &control, skb);
5303 	wiphy_unlock(hw->wiphy);
5304 }
5305 
5306 static void
5307 lkpi_80211_txq_task(void *ctx, int pending)
5308 {
5309 	struct lkpi_sta *lsta;
5310 	struct mbufq mq;
5311 	struct mbuf *m;
5312 	bool shall_tx;
5313 
5314 	lsta = ctx;
5315 
5316 #ifdef LINUXKPI_DEBUG_80211
5317 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5318 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
5319 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
5320 		    pending, mbufq_len(&lsta->txq));
5321 #endif
5322 
5323 	mbufq_init(&mq, IFQ_MAXLEN);
5324 
5325 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
5326 	/*
5327 	 * Do not re-check lsta->txq_ready here; we may have a pending
5328 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
5329 	 * false we do not have a valid sta anymore in the firmware so no
5330 	 * point to try to TX.
5331 	 * We also use txq_ready as a semaphore and will drain the txq manually
5332 	 * if needed on our way towards SCAN/INIT in the state machine.
5333 	 */
5334 #if 0
5335 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
5336 #else
5337 	/*
5338 	 * Backout this part of 886653492945f which breaks rtw88 or
5339 	 * in general drivers without (*sta_state)() but only the
5340 	 * legacy fallback to (*sta_add)().
5341 	 */
5342 	shall_tx = lsta->txq_ready;
5343 #endif
5344 	if (__predict_true(shall_tx))
5345 		mbufq_concat(&mq, &lsta->txq);
5346 	/*
5347 	 * else a state change will push the packets out manually or
5348 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
5349 	 */
5350 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5351 
5352 	m = mbufq_dequeue(&mq);
5353 	while (m != NULL) {
5354 		lkpi_80211_txq_tx_one(lsta, m);
5355 		m = mbufq_dequeue(&mq);
5356 	}
5357 }
5358 
5359 static int
5360 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
5361 {
5362 
5363 	/* XXX TODO */
5364 	IMPROVE();
5365 
5366 	/* Quick and dirty cheating hack. */
5367 	struct ieee80211_node *ni;
5368 
5369 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
5370 	return (lkpi_xmit(ni, m, NULL, false));
5371 }
5372 
5373 #ifdef LKPI_80211_HT
5374 static int
5375 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
5376     const uint8_t *frm, const uint8_t *efrm)
5377 {
5378 	struct ieee80211com *ic;
5379 	struct lkpi_hw *lhw;
5380 
5381 	ic = ni->ni_ic;
5382 	lhw = ic->ic_softc;
5383 
5384 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
5385 
5386 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
5387 }
5388 
5389 static int
5390 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
5391 {
5392 	struct ieee80211com *ic;
5393 	struct lkpi_hw *lhw;
5394 
5395 	ic = ni->ni_ic;
5396 	lhw = ic->ic_softc;
5397 
5398 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
5399 
5400 	return (lhw->ic_send_action(ni, category, action, sa));
5401 }
5402 
5403 
5404 static int
5405 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5406 {
5407 	struct ieee80211com *ic;
5408 	struct lkpi_hw *lhw;
5409 
5410 	ic = ni->ni_ic;
5411 	lhw = ic->ic_softc;
5412 
5413 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
5414 
5415 	return (lhw->ic_ampdu_enable(ni, tap));
5416 }
5417 
5418 /*
5419  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5420  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5421  *
5422  * NB: returns 0 on ERROR!
5423  */
5424 static int
5425 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5426     int dialogtoken, int baparamset, int batimeout)
5427 {
5428 	struct ieee80211com *ic;
5429 	struct lkpi_hw *lhw;
5430 	struct ieee80211_hw *hw;
5431 	struct ieee80211vap *vap;
5432 	struct lkpi_vif *lvif;
5433 	struct ieee80211_vif *vif;
5434 	struct lkpi_sta *lsta;
5435 	struct ieee80211_sta *sta;
5436 	struct ieee80211_ampdu_params params = { };
5437 	int error;
5438 
5439 	ic = ni->ni_ic;
5440 	lhw = ic->ic_softc;
5441 	hw = LHW_TO_HW(lhw);
5442 	vap = ni->ni_vap;
5443 	lvif = VAP_TO_LVIF(vap);
5444 	vif = LVIF_TO_VIF(lvif);
5445 	lsta = ni->ni_drv_data;
5446 	sta = LSTA_TO_STA(lsta);
5447 
5448 	if (!lsta->added_to_drv) {
5449 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5450 		    __func__, lsta, ni, sta);
5451 		return (0);
5452 	}
5453 
5454 	params.sta = sta;
5455 	params.action = IEEE80211_AMPDU_TX_START;
5456 	/* Keep 0 here! */
5457 	params.buf_size = 0;
5458 	params.timeout = 0;
5459 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5460 	params.tid = tap->txa_tid;
5461 	params.amsdu = false;
5462 
5463 	IEEE80211_UNLOCK(ic);
5464 	wiphy_lock(hw->wiphy);
5465 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5466 	wiphy_unlock(hw->wiphy);
5467 	IEEE80211_LOCK(ic);
5468 	if (error != 0) {
5469 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5470 		    __func__, error, ni, tap);
5471 		return (0);
5472 	}
5473 
5474 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
5475 }
5476 
5477 /*
5478  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5479  * and calls the default ieee80211_addba_response() which always returns 1.
5480  *
5481  * NB: No error checking in net80211!
5482  * Staying with 0 is an error.
5483  */
5484 static int
5485 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5486     int status, int baparamset, int batimeout)
5487 {
5488 	struct ieee80211com *ic;
5489 	struct lkpi_hw *lhw;
5490 	struct ieee80211_hw *hw;
5491 	struct ieee80211vap *vap;
5492 	struct lkpi_vif *lvif;
5493 	struct ieee80211_vif *vif;
5494 	struct lkpi_sta *lsta;
5495 	struct ieee80211_sta *sta;
5496 	struct ieee80211_ampdu_params params = { };
5497 	int error;
5498 
5499 	ic = ni->ni_ic;
5500 	lhw = ic->ic_softc;
5501 	hw = LHW_TO_HW(lhw);
5502 	vap = ni->ni_vap;
5503 	lvif = VAP_TO_LVIF(vap);
5504 	vif = LVIF_TO_VIF(lvif);
5505 	lsta = ni->ni_drv_data;
5506 	sta = LSTA_TO_STA(lsta);
5507 
5508 	if (!lsta->added_to_drv) {
5509 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5510 		    __func__, lsta, ni, sta);
5511 		return (0);
5512 	}
5513 
5514 	if (status == IEEE80211_STATUS_SUCCESS) {
5515 		params.sta = sta;
5516 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5517 		params.buf_size = tap->txa_wnd;
5518 		params.timeout = 0;
5519 		params.ssn = 0;
5520 		params.tid = tap->txa_tid;
5521 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5522 			params.amsdu = true;
5523 		else
5524 			params.amsdu = false;
5525 	} else {
5526 		/* We need to free the allocated resources. */
5527 		params.sta = sta;
5528 		switch (status) {
5529 			/* params.action = FLUSH, FLUSH_CONT */
5530 		default:
5531 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5532 			break;
5533 		}
5534 		params.buf_size = 0;
5535 		params.timeout = 0;
5536 		params.ssn = 0;
5537 		params.tid = tap->txa_tid;
5538 		params.amsdu = false;
5539 	}
5540 
5541 	IEEE80211_UNLOCK(ic);
5542 	wiphy_lock(hw->wiphy);
5543 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5544 	wiphy_unlock(hw->wiphy);
5545 	IEEE80211_LOCK(ic);
5546 	if (error != 0) {
5547 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5548 		    __func__, error, ni, tap);
5549 		return (0);
5550 	}
5551 
5552 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
5553 
5554 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
5555 }
5556 
5557 /*
5558  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5559  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5560  */
5561 static void
5562 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5563 {
5564 	struct ieee80211com *ic;
5565 	struct lkpi_hw *lhw;
5566 	struct ieee80211_hw *hw;
5567 	struct ieee80211vap *vap;
5568 	struct lkpi_vif *lvif;
5569 	struct ieee80211_vif *vif;
5570 	struct lkpi_sta *lsta;
5571 	struct ieee80211_sta *sta;
5572 	struct ieee80211_ampdu_params params = { };
5573 	int error;
5574 
5575 	ic = ni->ni_ic;
5576 	lhw = ic->ic_softc;
5577 	hw = LHW_TO_HW(lhw);
5578 	vap = ni->ni_vap;
5579 	lvif = VAP_TO_LVIF(vap);
5580 	vif = LVIF_TO_VIF(lvif);
5581 	lsta = ni->ni_drv_data;
5582 	sta = LSTA_TO_STA(lsta);
5583 
5584 	if (!lsta->added_to_drv) {
5585 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5586 		    __func__, lsta, ni, sta);
5587 		goto n80211;
5588 	}
5589 
5590 	/* We need to free the allocated resources. */
5591 	params.sta = sta;
5592 	IMPROVE("net80211 does not provide a reason to us");
5593 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5594 	params.buf_size = 0;
5595 	params.timeout = 0;
5596 	params.ssn = 0;
5597 	params.tid = tap->txa_tid;
5598 	params.amsdu = false;
5599 
5600 	IEEE80211_UNLOCK(ic);
5601 	wiphy_lock(hw->wiphy);
5602 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5603 	wiphy_unlock(hw->wiphy);
5604 	IEEE80211_LOCK(ic);
5605 	if (error != 0) {
5606 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5607 		    __func__, error, ni, tap);
5608 		goto n80211;
5609 	}
5610 
5611 	IMPROVE_HT("anyting else?");
5612 
5613 n80211:
5614 	lhw->ic_addba_stop(ni, tap);
5615 }
5616 
5617 static void
5618 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5619 {
5620 	struct ieee80211com *ic;
5621 	struct lkpi_hw *lhw;
5622 
5623 	ic = ni->ni_ic;
5624 	lhw = ic->ic_softc;
5625 
5626 	IMPROVE_HT();
5627 
5628 	lhw->ic_addba_response_timeout(ni, tap);
5629 }
5630 
5631 static void
5632 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5633     int status)
5634 {
5635 	struct ieee80211com *ic;
5636 	struct lkpi_hw *lhw;
5637 
5638 	ic = ni->ni_ic;
5639 	lhw = ic->ic_softc;
5640 
5641 	IMPROVE_HT();
5642 
5643 	lhw->ic_bar_response(ni, tap, status);
5644 }
5645 
5646 static int
5647 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
5648     int baparamset, int batimeout, int baseqctl)
5649 {
5650 	struct ieee80211com *ic;
5651 	struct lkpi_hw *lhw;
5652 	struct ieee80211_hw *hw;
5653 	struct ieee80211vap *vap;
5654 	struct lkpi_vif *lvif;
5655 	struct ieee80211_vif *vif;
5656 	struct lkpi_sta *lsta;
5657 	struct ieee80211_sta *sta;
5658 	struct ieee80211_ampdu_params params = { };
5659 	int error;
5660 
5661 	ic = ni->ni_ic;
5662 	lhw = ic->ic_softc;
5663 	hw = LHW_TO_HW(lhw);
5664 	vap = ni->ni_vap;
5665 	lvif = VAP_TO_LVIF(vap);
5666 	vif = LVIF_TO_VIF(lvif);
5667 	lsta = ni->ni_drv_data;
5668 	sta = LSTA_TO_STA(lsta);
5669 
5670 	IEEE80211_UNLOCK_ASSERT(ic);
5671 
5672 	if (!lsta->added_to_drv) {
5673 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5674 		    __func__, lsta, ni, vap, sta);
5675 		return (-ENXIO);
5676 	}
5677 
5678 	if (lsta->state != IEEE80211_STA_AUTHORIZED) {
5679 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
5680 		    __func__, lsta, ni, vap, sta, lsta->state);
5681 		return (-ENXIO);
5682 	}
5683 
5684 	params.sta = sta;
5685 	params.action = IEEE80211_AMPDU_RX_START;
5686 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
5687 	if (params.buf_size == 0)
5688 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
5689 	else
5690 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5691 	if (hw->max_rx_aggregation_subframes > 0 &&
5692 	    params.buf_size > hw->max_rx_aggregation_subframes)
5693 		params.buf_size = hw->max_rx_aggregation_subframes;
5694 	params.timeout = le16toh(batimeout);
5695 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
5696 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5697 
5698 	/* Based on net80211::ampdu_rx_start(). */
5699 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5700 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5701 		params.amsdu = true;
5702 	else
5703 		params.amsdu = false;
5704 
5705 	wiphy_lock(hw->wiphy);
5706 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5707 	wiphy_unlock(hw->wiphy);
5708 	if (error != 0) {
5709 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
5710 		    __func__, error, ni, rap);
5711 		return (error);
5712 	}
5713 
5714 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5715 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5716 	}
5717 
5718 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
5719 
5720 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
5721 	return (error);
5722 }
5723 
5724 static void
5725 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
5726 {
5727 	struct ieee80211com *ic;
5728 	struct lkpi_hw *lhw;
5729 	struct ieee80211_hw *hw;
5730 	struct ieee80211vap *vap;
5731 	struct lkpi_vif *lvif;
5732 	struct ieee80211_vif *vif;
5733 	struct lkpi_sta *lsta;
5734 	struct ieee80211_sta *sta;
5735 	struct ieee80211_ampdu_params params = { };
5736 	int error;
5737 	uint8_t tid;
5738 	bool ic_locked;
5739 
5740 	ic = ni->ni_ic;
5741 	lhw = ic->ic_softc;
5742 
5743 	/*
5744 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
5745 	 * we did not START.  Some drivers pass it down to firmware which will
5746 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
5747 	 * ieee80211_ht_node_init() amongst others which will iterate over all
5748 	 * tid and call ic_ampdu_rx_stop() unconditionally.
5749 	 * XXX net80211 should probably be more "gentle" in these cases and
5750 	 * track some state itself.
5751 	 */
5752 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
5753 		goto net80211_only;
5754 
5755 	hw = LHW_TO_HW(lhw);
5756 	vap = ni->ni_vap;
5757 	lvif = VAP_TO_LVIF(vap);
5758 	vif = LVIF_TO_VIF(lvif);
5759 	lsta = ni->ni_drv_data;
5760 	if (lsta == NULL) {
5761 		ic_printf(ic, "%s: lsta %p ni %p vap %p, lsta is NULL\n",
5762 		    __func__, lsta, ni, vap);
5763 		goto net80211_only;
5764 	}
5765 	sta = LSTA_TO_STA(lsta);
5766 
5767 	if (!lsta->added_to_drv) {
5768 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5769 		    __func__, lsta, ni, vap, sta);
5770 		goto net80211_only;
5771 	}
5772 
5773 	if (lsta->state != IEEE80211_STA_AUTHORIZED) {
5774 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
5775 		    __func__, lsta, ni, vap, sta, lsta->state);
5776 		goto net80211_only;
5777 	}
5778 
5779 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
5780 	for (tid = 0; tid < WME_NUM_TID; tid++) {
5781 		if (&ni->ni_rx_ampdu[tid] == rap)
5782 			break;
5783 	}
5784 	if (tid == WME_NUM_TID) {
5785 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p TID not found\n",
5786 		    __func__, lsta, ni, vap, sta);
5787 		goto net80211_only;
5788 	}
5789 
5790 	params.sta = sta;
5791 	params.action = IEEE80211_AMPDU_RX_STOP;
5792 	params.buf_size = 0;
5793 	params.timeout = 0;
5794 	params.ssn = 0;
5795 	params.tid = tid;
5796 	params.amsdu = false;
5797 
5798 	ic_locked = IEEE80211_IS_LOCKED(ic);
5799 	if (ic_locked)
5800 		IEEE80211_UNLOCK(ic);
5801 	wiphy_lock(hw->wiphy);
5802 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5803 	wiphy_unlock(hw->wiphy);
5804 	if (ic_locked)
5805 		IEEE80211_LOCK(ic);
5806 	if (error != 0)
5807 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
5808 		    __func__, error, ni, rap);
5809 
5810 net80211_only:
5811 	lhw->ic_ampdu_rx_stop(ni, rap);
5812 }
5813 #endif
5814 
5815 static void
5816 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
5817     uint8_t *bands, int *chan_flags, enum nl80211_band band)
5818 {
5819 #ifdef LKPI_80211_HT
5820 	struct ieee80211_sta_ht_cap *ht_cap;
5821 
5822 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
5823 	if (!ht_cap->ht_supported)
5824 		return;
5825 
5826 	switch (band) {
5827 	case NL80211_BAND_2GHZ:
5828 		setbit(bands, IEEE80211_MODE_11NG);
5829 		break;
5830 	case NL80211_BAND_5GHZ:
5831 		setbit(bands, IEEE80211_MODE_11NA);
5832 		break;
5833 	default:
5834 		IMPROVE("Unsupported band %d", band);
5835 		return;
5836 	}
5837 
5838 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
5839 
5840 	/*
5841 	 * Rather than manually checking each flag and
5842 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
5843 	 * simply copy the 16bits.
5844 	 */
5845 	ic->ic_htcaps |= ht_cap->cap;
5846 
5847 	/* Then deal with the other flags. */
5848 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
5849 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
5850 #ifdef __notyet__
5851 	if (ieee80211_hw_check(hw, TX_AMSDU))
5852 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
5853 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
5854 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
5855 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
5856 #endif
5857 
5858 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
5859 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
5860 
5861 	/* Only add HT40 channels if supported. */
5862 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
5863 	    chan_flags != NULL)
5864 		*chan_flags |= NET80211_CBW_FLAG_HT40;
5865 #endif
5866 }
5867 
5868 static void
5869 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
5870     int *n, struct ieee80211_channel *c)
5871 {
5872 	struct lkpi_hw *lhw;
5873 	struct ieee80211_hw *hw;
5874 	struct linuxkpi_ieee80211_channel *channels;
5875 	uint8_t bands[IEEE80211_MODE_BYTES];
5876 	int chan_flags, error, i, nchans;
5877 
5878 	/* Channels */
5879 	lhw = ic->ic_softc;
5880 	hw = LHW_TO_HW(lhw);
5881 
5882 	/* NL80211_BAND_2GHZ */
5883 	nchans = 0;
5884 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
5885 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
5886 	if (nchans > 0) {
5887 		memset(bands, 0, sizeof(bands));
5888 		chan_flags = 0;
5889 		setbit(bands, IEEE80211_MODE_11B);
5890 		/* XXX-BZ unclear how to check for 11g. */
5891 
5892 		IMPROVE("the bitrates may have flags?");
5893 		setbit(bands, IEEE80211_MODE_11G);
5894 
5895 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
5896 		    NL80211_BAND_2GHZ);
5897 
5898 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5899 		for (i = 0; i < nchans && *n < maxchan; i++) {
5900 			uint32_t nflags = 0;
5901 			int cflags = chan_flags;
5902 
5903 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
5904 				ic_printf(ic, "%s: Skipping disabled chan "
5905 				    "[%u/%u/%#x]\n", __func__,
5906 				    channels[i].hw_value,
5907 				    channels[i].center_freq, channels[i].flags);
5908 				continue;
5909 			}
5910 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
5911 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
5912 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
5913 				nflags |= IEEE80211_CHAN_DFS;
5914 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
5915 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
5916 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
5917 				cflags &= ~NET80211_CBW_FLAG_VHT80;
5918 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
5919 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
5920 				cflags &= ~NET80211_CBW_FLAG_HT40;
5921 
5922 			error = ieee80211_add_channel_cbw(c, maxchan, n,
5923 			    ieee80211_mhz2ieee(channels[i].center_freq,
5924 				lkpi_nl80211_band_to_net80211_band(channels[i].band)),
5925 			    channels[i].center_freq, channels[i].max_power,
5926 			    nflags, bands, cflags);
5927 			/* net80211::ENOBUFS: *n >= maxchans */
5928 			if (error != 0 && error != ENOBUFS)
5929 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
5930 				    "returned error %d\n",
5931 				    __func__, channels[i].hw_value,
5932 				    channels[i].center_freq, channels[i].flags,
5933 				    nflags, chan_flags, cflags, error);
5934 			if (error != 0)
5935 				break;
5936 		}
5937 	}
5938 
5939 	/* NL80211_BAND_5GHZ */
5940 	nchans = 0;
5941 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
5942 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
5943 	if (nchans > 0) {
5944 		memset(bands, 0, sizeof(bands));
5945 		chan_flags = 0;
5946 		setbit(bands, IEEE80211_MODE_11A);
5947 
5948 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
5949 		    NL80211_BAND_5GHZ);
5950 
5951 #ifdef LKPI_80211_VHT
5952 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
5953 
5954 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5955 			ic->ic_vht_cap.vht_cap_info =
5956 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5957 			ic->ic_vht_cap.supp_mcs =
5958 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
5959 
5960 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
5961 			chan_flags |= NET80211_CBW_FLAG_VHT80;
5962 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5963 			    ic->ic_vht_cap.vht_cap_info))
5964 				chan_flags |= NET80211_CBW_FLAG_VHT160;
5965 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5966 			    ic->ic_vht_cap.vht_cap_info))
5967 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
5968 		}
5969 #endif
5970 
5971 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5972 		for (i = 0; i < nchans && *n < maxchan; i++) {
5973 			uint32_t nflags = 0;
5974 			int cflags = chan_flags;
5975 
5976 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
5977 				ic_printf(ic, "%s: Skipping disabled chan "
5978 				    "[%u/%u/%#x]\n", __func__,
5979 				    channels[i].hw_value,
5980 				    channels[i].center_freq, channels[i].flags);
5981 				continue;
5982 			}
5983 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
5984 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
5985 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
5986 				nflags |= IEEE80211_CHAN_DFS;
5987 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
5988 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
5989 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
5990 				cflags &= ~NET80211_CBW_FLAG_VHT80;
5991 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
5992 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
5993 				cflags &= ~NET80211_CBW_FLAG_HT40;
5994 
5995 			error = ieee80211_add_channel_cbw(c, maxchan, n,
5996 			    ieee80211_mhz2ieee(channels[i].center_freq,
5997 				lkpi_nl80211_band_to_net80211_band(channels[i].band)),
5998 			    channels[i].center_freq, channels[i].max_power,
5999 			    nflags, bands, cflags);
6000 			/* net80211::ENOBUFS: *n >= maxchans */
6001 			if (error != 0 && error != ENOBUFS)
6002 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
6003 				    "returned error %d\n",
6004 				    __func__, channels[i].hw_value,
6005 				    channels[i].center_freq, channels[i].flags,
6006 				    nflags, chan_flags, cflags, error);
6007 			if (error != 0)
6008 				break;
6009 		}
6010 	}
6011 }
6012 
6013 static void *
6014 lkpi_ieee80211_ifalloc(void)
6015 {
6016 	struct ieee80211com *ic;
6017 
6018 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
6019 
6020 	/* Setting these happens later when we have device information. */
6021 	ic->ic_softc = NULL;
6022 	ic->ic_name = "linuxkpi";
6023 
6024 	return (ic);
6025 }
6026 
6027 struct ieee80211_hw *
6028 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
6029 {
6030 	struct ieee80211_hw *hw;
6031 	struct lkpi_hw *lhw;
6032 	struct wiphy *wiphy;
6033 	int ac;
6034 
6035 	/* Get us and the driver data also allocated. */
6036 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
6037 	if (wiphy == NULL)
6038 		return (NULL);
6039 
6040 	lhw = wiphy_priv(wiphy);
6041 	lhw->ops = ops;
6042 
6043 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
6044 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
6045 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
6046 	LKPI_80211_LHW_MC_LOCK_INIT(lhw);
6047 	TAILQ_INIT(&lhw->lvif_head);
6048 	__hw_addr_init(&lhw->mc_list);
6049 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
6050 		lhw->txq_generation[ac] = 1;
6051 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
6052 	}
6053 
6054 	/* Chanctx_conf */
6055 	INIT_LIST_HEAD(&lhw->lchanctx_list);
6056 
6057 	/* Deferred RX path. */
6058 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
6059 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
6060 	mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
6061 	lhw->rxq_stopped = false;
6062 
6063 	/*
6064 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
6065 	 * not initialized.
6066 	 */
6067 	hw = LHW_TO_HW(lhw);
6068 	hw->wiphy = wiphy;
6069 	hw->conf.flags |= IEEE80211_CONF_IDLE;
6070 	hw->priv = (void *)(lhw + 1);
6071 
6072 	/* BSD Specific. */
6073 	lhw->ic = lkpi_ieee80211_ifalloc();
6074 
6075 	IMPROVE();
6076 
6077 	return (hw);
6078 }
6079 
6080 void
6081 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
6082 {
6083 	struct lkpi_hw *lhw;
6084 	struct mbuf *m;
6085 
6086 	lhw = HW_TO_LHW(hw);
6087 	free(lhw->ic, M_LKPI80211);
6088 	lhw->ic = NULL;
6089 
6090 	/*
6091 	 * Drain the deferred RX path.
6092 	 */
6093 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6094 	lhw->rxq_stopped = true;
6095 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6096 
6097 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
6098 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
6099 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
6100 
6101 	/* Flush mbufq (make sure to release ni refs!). */
6102 	m = mbufq_dequeue(&lhw->rxq);
6103 	while (m != NULL) {
6104 #ifdef LKPI_80211_USE_MTAG
6105 		struct m_tag *mtag;
6106 
6107 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6108 		if (mtag != NULL) {
6109 			struct lkpi_80211_tag_rxni *rxni;
6110 
6111 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6112 			ieee80211_free_node(rxni->ni);
6113 		}
6114 #else
6115 		if (m->m_pkthdr.PH_loc.ptr != NULL) {
6116 			struct ieee80211_node *ni;
6117 
6118 			ni = m->m_pkthdr.PH_loc.ptr;
6119 			ieee80211_free_node(ni);
6120 		}
6121 #endif
6122 		m_freem(m);
6123 		m = mbufq_dequeue(&lhw->rxq);
6124 	}
6125 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
6126 	    __func__, lhw, mbufq_len(&lhw->rxq)));
6127 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
6128 
6129 	/* Chanctx_conf. */
6130 	if (!list_empty_careful(&lhw->lchanctx_list)) {
6131 		struct lkpi_chanctx *lchanctx, *next;
6132 		struct ieee80211_chanctx_conf *chanctx_conf;
6133 
6134 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
6135 			if (lchanctx->added_to_drv) {
6136 				/* In reality we should panic? */
6137 				chanctx_conf = &lchanctx->chanctx_conf;
6138 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
6139 			}
6140 			list_del(&lchanctx->entry);
6141 			free(lchanctx, M_LKPI80211);
6142 		}
6143 	}
6144 
6145 	LKPI_80211_LHW_MC_LOCK(lhw);
6146 	lkpi_cleanup_mcast_list_locked(lhw);
6147 	LKPI_80211_LHW_MC_UNLOCK(lhw);
6148 
6149 	/* Cleanup more of lhw here or in wiphy_free()? */
6150 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
6151 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
6152 	sx_destroy(&lhw->lvif_sx);
6153 	LKPI_80211_LHW_MC_LOCK_DESTROY(lhw)
6154 	IMPROVE();
6155 }
6156 
6157 void
6158 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw)
6159 {
6160 	struct lkpi_hw *lhw;
6161 	struct ieee80211com *ic;
6162 	struct device *dev;
6163 
6164 	lhw = HW_TO_LHW(hw);
6165 	ic = lhw->ic;
6166 
6167 	/* Save the backpointer from net80211 to LinuxKPI. */
6168 	ic->ic_softc = lhw;
6169 
6170 	/*
6171 	 * Set a proper name before ieee80211_ifattach() if dev is set.
6172 	 * ath1xk also unset the dev so we need to check.
6173 	 */
6174 	dev = wiphy_dev(hw->wiphy);
6175 	if (dev != NULL) {
6176 		ic->ic_name = dev_name(dev);
6177 	} else {
6178 		TODO("adjust arguments to still have the old dev or go through "
6179 		    "the hoops of getting the bsddev from hw and detach; "
6180 		    "or do in XXX; check ath1kx drivers");
6181 	}
6182 
6183 	/* XXX-BZ do we also need to set wiphy name? */
6184 }
6185 
6186 struct ieee80211_hw *
6187 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
6188 {
6189 	struct lkpi_hw *lhw;
6190 
6191 	lhw = wiphy_priv(wiphy);
6192 	return (LHW_TO_HW(lhw));
6193 }
6194 
6195 static void
6196 lkpi_radiotap_attach(struct lkpi_hw *lhw)
6197 {
6198 	struct ieee80211com *ic;
6199 
6200 	ic = lhw->ic;
6201 	ieee80211_radiotap_attach(ic,
6202 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
6203 	    LKPI_RTAP_TX_FLAGS_PRESENT,
6204 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
6205 	    LKPI_RTAP_RX_FLAGS_PRESENT);
6206 }
6207 
6208 int
6209 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
6210 {
6211 	struct ieee80211com *ic;
6212 	struct lkpi_hw *lhw;
6213 	int band, i;
6214 
6215 	lhw = HW_TO_LHW(hw);
6216 	ic = lhw->ic;
6217 
6218 	/* We do it this late as wiphy->dev should be set for the name. */
6219 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
6220 	if (lhw->workq == NULL)
6221 		return (-EAGAIN);
6222 
6223 	/* XXX-BZ figure this out how they count his... */
6224 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
6225 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
6226 		    hw->wiphy->perm_addr);
6227 	} else if (hw->wiphy->n_addresses > 0) {
6228 		/* We take the first one. */
6229 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
6230 		    hw->wiphy->addresses[0].addr);
6231 	} else {
6232 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
6233 	}
6234 
6235 #ifdef __not_yet__
6236 	/* See comment in lkpi_80211_txq_tx_one(). */
6237 	ic->ic_headroom = hw->extra_tx_headroom;
6238 #endif
6239 
6240 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
6241 	ic->ic_opmode = IEEE80211_M_STA;
6242 
6243 	/* Set device capabilities. */
6244 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
6245 	ic->ic_caps =
6246 	    IEEE80211_C_STA |
6247 	    IEEE80211_C_MONITOR |
6248 	    IEEE80211_C_WPA |		/* WPA/RSN */
6249 #ifdef LKPI_80211_WME
6250 	    IEEE80211_C_WME |
6251 #endif
6252 #if 0
6253 	    IEEE80211_C_PMGT |
6254 #endif
6255 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
6256 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
6257 	    ;
6258 #if 0
6259 	/* Scanning is a different kind of beast to re-work. */
6260 	ic->ic_caps |= IEEE80211_C_BGSCAN;
6261 #endif
6262 	if (lhw->ops->hw_scan) {
6263 		/*
6264 		 * Advertise full-offload scanning.
6265 		 *
6266 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
6267 		 * we essentially disable hw_scan for all drivers not setting
6268 		 * the flag.
6269 		 */
6270 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
6271 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
6272 	}
6273 
6274 	/* Does HW support Fragmentation offload? */
6275 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
6276 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
6277 
6278 	/*
6279 	 * The wiphy variables report bitmasks of avail antennas.
6280 	 * (*get_antenna) get the current bitmask sets which can be
6281 	 * altered by (*set_antenna) for some drivers.
6282 	 * XXX-BZ will the count alone do us much good long-term in net80211?
6283 	 */
6284 	if (hw->wiphy->available_antennas_rx ||
6285 	    hw->wiphy->available_antennas_tx) {
6286 		uint32_t rxs, txs;
6287 
6288 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
6289 			ic->ic_rxstream = bitcount32(rxs);
6290 			ic->ic_txstream = bitcount32(txs);
6291 		}
6292 	}
6293 
6294 	ic->ic_cryptocaps = 0;
6295 #ifdef LKPI_80211_HW_CRYPTO
6296 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
6297 		uint32_t hwciphers;
6298 
6299 		hwciphers = 0;
6300 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
6301 			uint32_t cs;
6302 
6303 			cs = lkpi_l80211_to_net80211_cyphers(
6304 			    ic, hw->wiphy->cipher_suites[i]);
6305 			if (cs == IEEE80211_CRYPTO_TKIP) {
6306 				/*
6307 				 * We do set this here.  We will only find out
6308 				 * when doing a SET_KEY operation depending on
6309 				 * what the driver returns.
6310 				 * net80211::ieee80211_crypto_newkey()
6311 				 * checks this so we will have to do flags
6312 				 * surgery later.
6313 				 */
6314 				cs |= IEEE80211_CRYPTO_TKIPMIC;
6315 			}
6316 			hwciphers |= cs;
6317 		}
6318 		/*
6319 		 * (20250415) nothing anywhere in the path checks we actually
6320 		 * support all these in net80211.
6321 		 * net80211 supports _256 variants but the ioctl does not.
6322 		 */
6323 		IMPROVE("as net80211 grows more support, enable them");
6324 		hwciphers &= (IEEE80211_CRYPTO_WEP |
6325 		    IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
6326 		    IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
6327 		/*
6328 		 * We only support CCMP here, so further filter.
6329 		 * Also permit TKIP if turned on.
6330 		 */
6331 		hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
6332 		    IEEE80211_CRYPTO_AES_GCM_128 |
6333 		    (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
6334 		    IEEE80211_CRYPTO_TKIPMIC) : 0));
6335 		ieee80211_set_hardware_ciphers(ic, hwciphers);
6336 	}
6337 #endif
6338 
6339 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
6340 	    ic->ic_channels);
6341 
6342 	ieee80211_ifattach(ic);
6343 
6344 	ic->ic_update_mcast = lkpi_ic_update_mcast;
6345 	ic->ic_update_promisc = lkpi_ic_update_promisc;
6346 	ic->ic_update_chw = lkpi_ic_update_chw;
6347 	ic->ic_parent = lkpi_ic_parent;
6348 	ic->ic_scan_start = lkpi_ic_scan_start;
6349 	ic->ic_scan_end = lkpi_ic_scan_end;
6350 	ic->ic_set_channel = lkpi_ic_set_channel;
6351 	ic->ic_transmit = lkpi_ic_transmit;
6352 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
6353 	ic->ic_vap_create = lkpi_ic_vap_create;
6354 	ic->ic_vap_delete = lkpi_ic_vap_delete;
6355 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
6356 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
6357 
6358 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
6359 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6360 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6361 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6362 
6363 	lhw->ic_node_alloc = ic->ic_node_alloc;
6364 	ic->ic_node_alloc = lkpi_ic_node_alloc;
6365 	lhw->ic_node_init = ic->ic_node_init;
6366 	ic->ic_node_init = lkpi_ic_node_init;
6367 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
6368 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
6369 	lhw->ic_node_free = ic->ic_node_free;
6370 	ic->ic_node_free = lkpi_ic_node_free;
6371 
6372 #ifdef LKPI_80211_HT
6373 	/*
6374 	 * Only attach if the driver/firmware supports (*ampdu_action)().
6375 	 * Otherwise it is in the hands of net80211.
6376 	 */
6377 	if (lhw->ops->ampdu_action != NULL) {
6378 		lhw->ic_recv_action = ic->ic_recv_action;
6379 		ic->ic_recv_action = lkpi_ic_recv_action;
6380 		lhw->ic_send_action = ic->ic_send_action;
6381 		ic->ic_send_action = lkpi_ic_send_action;
6382 
6383 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
6384 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
6385 
6386 		lhw->ic_addba_request = ic->ic_addba_request;
6387 		ic->ic_addba_request = lkpi_ic_addba_request;
6388 		lhw->ic_addba_response = ic->ic_addba_response;
6389 		ic->ic_addba_response = lkpi_ic_addba_response;
6390 		lhw->ic_addba_stop = ic->ic_addba_stop;
6391 		ic->ic_addba_stop = lkpi_ic_addba_stop;
6392 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
6393 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
6394 
6395 		lhw->ic_bar_response = ic->ic_bar_response;
6396 		ic->ic_bar_response = lkpi_ic_bar_response;
6397 
6398 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
6399 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
6400 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
6401 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
6402 	}
6403 #endif
6404 
6405 	lkpi_radiotap_attach(lhw);
6406 
6407 	/*
6408 	 * Assign the first possible channel for now;  seems Realtek drivers
6409 	 * expect one.
6410 	 * Also remember the amount of bands we support and the most rates
6411 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6412 	 */
6413 	lhw->supbands = lhw->max_rates = 0;
6414 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6415 		struct ieee80211_supported_band *supband;
6416 		struct linuxkpi_ieee80211_channel *channels;
6417 
6418 		supband = hw->wiphy->bands[band];
6419 		if (supband == NULL || supband->n_channels == 0)
6420 			continue;
6421 
6422 		lhw->supbands++;
6423 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6424 
6425 		/* If we have a channel, we need to keep counting supbands. */
6426 		if (hw->conf.chandef.chan != NULL)
6427 			continue;
6428 
6429 		channels = supband->channels;
6430 		for (i = 0; i < supband->n_channels; i++) {
6431 
6432 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6433 				continue;
6434 
6435 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
6436 #ifdef LKPI_80211_HT
6437 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
6438 #endif
6439 			    NL80211_CHAN_NO_HT);
6440 			break;
6441 		}
6442 	}
6443 
6444 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6445 
6446 	/* Make sure we do not support more than net80211 is willing to take. */
6447 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6448 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6449 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6450 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6451 	}
6452 
6453 	/*
6454 	 * The maximum supported bitrates on any band + size for
6455 	 * DSSS Parameter Set give our per-band IE size.
6456 	 * SSID is the responsibility of the driver and goes on the side.
6457 	 * The user specified bits coming from the vap go into the
6458 	 * "common ies" fields.
6459 	 */
6460 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6461 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
6462 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
6463 
6464 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6465 		/*
6466 		 * net80211 does not seem to support the DSSS Parameter Set but
6467 		 * some of the drivers insert it so calculate the extra fixed
6468 		 * space in.
6469 		 */
6470 		lhw->scan_ie_len += 2 + 1;
6471 	}
6472 
6473 #if defined(LKPI_80211_HT)
6474 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
6475 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
6476 #endif
6477 #if defined(LKPI_80211_VHT)
6478 	if (IEEE80211_CONF_VHT(ic))
6479 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
6480 #endif
6481 
6482 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
6483 	if (hw->wiphy->max_scan_ie_len > 0) {
6484 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
6485 			goto err;
6486 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
6487 	}
6488 
6489 	if (bootverbose) {
6490 		ic_printf(ic, "netdev_features %b\n", hw->netdev_features, NETIF_F_BITS);
6491 		ieee80211_announce(ic);
6492 	}
6493 
6494 	return (0);
6495 err:
6496 	IMPROVE("TODO FIXME CLEANUP");
6497 	return (-EAGAIN);
6498 }
6499 
6500 void
6501 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
6502 {
6503 	struct lkpi_hw *lhw;
6504 	struct ieee80211com *ic;
6505 
6506 	lhw = HW_TO_LHW(hw);
6507 	ic = lhw->ic;
6508 	ieee80211_ifdetach(ic);
6509 }
6510 
6511 void
6512 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
6513     enum ieee80211_iface_iter flags,
6514     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
6515     void *arg)
6516 {
6517 	struct lkpi_hw *lhw;
6518 	struct lkpi_vif *lvif;
6519 	struct ieee80211_vif *vif;
6520 	bool active, atomic, nin_drv;
6521 
6522 	lhw = HW_TO_LHW(hw);
6523 
6524 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
6525 	    IEEE80211_IFACE_ITER_RESUME_ALL|
6526 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6527 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
6528 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
6529 		    __func__, flags);
6530 	}
6531 
6532 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
6533 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
6534 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
6535 
6536 	if (atomic)
6537 		LKPI_80211_LHW_LVIF_LOCK(lhw);
6538 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6539 		struct ieee80211vap *vap;
6540 
6541 		vif = LVIF_TO_VIF(lvif);
6542 
6543 		/*
6544 		 * If we want "active" interfaces, we need to distinguish on
6545 		 * whether the driver knows about them or not to be able to
6546 		 * handle the "resume" case correctly.  Skip the ones the
6547 		 * driver does not know about.
6548 		 */
6549 		if (active && !lvif->added_to_drv &&
6550 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
6551 			continue;
6552 
6553 		/*
6554 		 * If we shall skip interfaces not added to the driver do so
6555 		 * if we haven't yet.
6556 		 */
6557 		if (nin_drv && !lvif->added_to_drv)
6558 			continue;
6559 
6560 		/*
6561 		 * Run the iterator function if we are either not asking
6562 		 * asking for active only or if the VAP is "running".
6563 		 */
6564 		/* XXX-BZ probably should have state in the lvif as well. */
6565 		vap = LVIF_TO_VAP(lvif);
6566 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
6567 			iterfunc(arg, vif->addr, vif);
6568 	}
6569 	if (atomic)
6570 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6571 }
6572 
6573 static void
6574 lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6575     ieee80211_keyix keyix, struct lkpi_sta *lsta,
6576     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6577 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6578     void *arg)
6579 {
6580 	if (!lsta->added_to_drv)
6581 		return;
6582 
6583 	if (lsta->kc[keyix] == NULL)
6584 		return;
6585 
6586 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
6587 }
6588 
6589 void
6590 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
6591     struct ieee80211_vif *vif,
6592     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6593         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6594     void *arg, bool rcu)
6595 {
6596 	struct lkpi_sta *lsta;
6597 	struct lkpi_vif *lvif;
6598 
6599 	lvif = VIF_TO_LVIF(vif);
6600 
6601 	if (rcu) {
6602 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6603 
6604 		if (vif == NULL) {
6605 			TODO();
6606 		} else {
6607 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
6608 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6609 				    keyix++)
6610 					lkpi_ieee80211_iterate_keys(hw, vif,
6611 					    keyix, lsta, iterfunc, arg);
6612 			}
6613 		}
6614 	} else {
6615 		TODO("Used by suspend/resume; order of keys as installed to "
6616 		"firmware is important; we'll need to rewrite some code for that");
6617 		lockdep_assert_wiphy(hw->wiphy);
6618 
6619 		if (vif == NULL) {
6620 			TODO();
6621 		} else {
6622 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
6623 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6624 				    keyix++)
6625 					lkpi_ieee80211_iterate_keys(hw, vif,
6626 					    keyix, lsta, iterfunc, arg);
6627 			}
6628 		}
6629 	}
6630 }
6631 
6632 void
6633 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
6634     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
6635 	void *),
6636     void *arg)
6637 {
6638 	struct lkpi_hw *lhw;
6639 	struct lkpi_chanctx *lchanctx;
6640 
6641 	KASSERT(hw != NULL && iterfunc != NULL,
6642 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6643 
6644 	lhw = HW_TO_LHW(hw);
6645 
6646 	rcu_read_lock();
6647 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6648 		if (!lchanctx->added_to_drv)
6649 			continue;
6650 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6651 	}
6652 	rcu_read_unlock();
6653 }
6654 
6655 void
6656 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
6657    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
6658 {
6659 	struct lkpi_hw *lhw;
6660 	struct lkpi_vif *lvif;
6661 	struct lkpi_sta *lsta;
6662 	struct ieee80211_sta *sta;
6663 
6664 	KASSERT(hw != NULL && iterfunc != NULL,
6665 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6666 
6667 	lhw = HW_TO_LHW(hw);
6668 
6669 	LKPI_80211_LHW_LVIF_LOCK(lhw);
6670 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6671 
6672 		rcu_read_lock();
6673 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
6674 			if (!lsta->added_to_drv)
6675 				continue;
6676 			sta = LSTA_TO_STA(lsta);
6677 			iterfunc(arg, sta);
6678 		}
6679 		rcu_read_unlock();
6680 	}
6681 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6682 }
6683 
6684 struct linuxkpi_ieee80211_regdomain *
6685 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6686 {
6687 	struct linuxkpi_ieee80211_regdomain *regd;
6688 
6689 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6690 	    GFP_KERNEL);
6691 	return (regd);
6692 }
6693 
6694 int
6695 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
6696     struct linuxkpi_ieee80211_regdomain *regd)
6697 {
6698 	struct lkpi_hw *lhw;
6699 	struct ieee80211com *ic;
6700 	struct ieee80211_regdomain *rd;
6701 
6702 	lhw = wiphy_priv(wiphy);
6703 	ic = lhw->ic;
6704 
6705 	rd = &ic->ic_regdomain;
6706 	if (rd->isocc[0] == '\0') {
6707 		rd->isocc[0] = regd->alpha2[0];
6708 		rd->isocc[1] = regd->alpha2[1];
6709 	}
6710 
6711 	TODO();
6712 	/* XXX-BZ finish the rest. */
6713 
6714 	return (0);
6715 }
6716 
6717 void
6718 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
6719     struct cfg80211_scan_info *info)
6720 {
6721 	struct lkpi_hw *lhw;
6722 	struct ieee80211com *ic;
6723 	struct ieee80211_scan_state *ss;
6724 
6725 	lhw = wiphy_priv(hw->wiphy);
6726 	ic = lhw->ic;
6727 	ss = ic->ic_scan;
6728 
6729 	ieee80211_scan_done(ss->ss_vap);
6730 
6731 	LKPI_80211_LHW_SCAN_LOCK(lhw);
6732 	free(lhw->hw_req, M_LKPI80211);
6733 	lhw->hw_req = NULL;
6734 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
6735 	wakeup(lhw);
6736 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
6737 
6738 	return;
6739 }
6740 
6741 static void
6742 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6743 {
6744 	struct ieee80211_node *ni;
6745 #ifdef LKPI_80211_USE_MTAG
6746 	struct m_tag *mtag;
6747 #endif
6748 	int ok;
6749 
6750 	ni = NULL;
6751 #ifdef LKPI_80211_USE_MTAG
6752         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6753 	if (mtag != NULL) {
6754 		struct lkpi_80211_tag_rxni *rxni;
6755 
6756 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6757 		ni = rxni->ni;
6758 	}
6759 #else
6760 	if (m->m_pkthdr.PH_loc.ptr != NULL) {
6761 		ni = m->m_pkthdr.PH_loc.ptr;
6762 		m->m_pkthdr.PH_loc.ptr = NULL;
6763 	}
6764 #endif
6765 
6766 	if (ni != NULL) {
6767 		ok = ieee80211_input_mimo(ni, m);
6768 		ieee80211_free_node(ni);		/* Release the reference. */
6769 		if (ok < 0)
6770 			m_freem(m);
6771 	} else {
6772 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6773 		/* mbuf got consumed. */
6774 	}
6775 
6776 #ifdef LINUXKPI_DEBUG_80211
6777 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6778 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6779 #endif
6780 }
6781 
6782 static void
6783 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6784 {
6785 	struct lkpi_hw *lhw;
6786 	struct mbufq mq;
6787 	struct mbuf *m;
6788 
6789 	lhw = ctx;
6790 
6791 #ifdef LINUXKPI_DEBUG_80211
6792 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6793 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6794 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6795 #endif
6796 
6797 	mbufq_init(&mq, IFQ_MAXLEN);
6798 
6799 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6800 	mbufq_concat(&mq, &lhw->rxq);
6801 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6802 
6803 	m = mbufq_dequeue(&mq);
6804 	while (m != NULL) {
6805 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6806 		m = mbufq_dequeue(&mq);
6807 	}
6808 }
6809 
6810 static void
6811 lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
6812     struct ieee80211_rx_status *rx_status,
6813     struct ieee80211_rx_stats *rx_stats,
6814     uint8_t *rssip)
6815 {
6816 	struct ieee80211_supported_band *supband;
6817 	struct rate_info rxrate;
6818 	int i;
6819 	uint8_t rssi;
6820 
6821 	memset(&rxrate, 0, sizeof(rxrate));
6822 	memset(rx_stats, 0, sizeof(*rx_stats));
6823 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
6824 	/* XXX-BZ correct hardcoded noise floor, survey data? */
6825 	rx_stats->c_nf = -96;
6826 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
6827 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
6828 		rssi = rx_status->signal;
6829 	else
6830 		rssi = rx_stats->c_nf;
6831 	/*
6832 	 * net80211 signal strength data are in .5 dBm units relative to
6833 	 * the current noise floor (see comment in ieee80211_node.h).
6834 	 */
6835 	rssi -= rx_stats->c_nf;
6836 	if (rssip != NULL)
6837 		*rssip = rssi;
6838 	rx_stats->c_rssi = rssi * 2;
6839 	rx_stats->r_flags |= IEEE80211_R_BAND;
6840 	rx_stats->c_band =
6841 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
6842 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
6843 	rx_stats->c_freq = rx_status->freq;
6844 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
6845 
6846 	rx_stats->c_rx_tsf = rx_status->mactime;
6847 
6848 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
6849 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
6850 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
6851 		rx_stats->r_flags |= IEEE80211_R_TSF64;
6852 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
6853 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
6854 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
6855 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
6856 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
6857 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
6858 	}
6859 
6860 	if (rx_status->chains != 0) {
6861 		int cc;
6862 		int8_t crssi;
6863 
6864 		rx_stats->c_chain = rx_status->chains;
6865 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
6866 
6867 		cc = 0;
6868 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
6869 			if (!(rx_status->chains & BIT(i)))
6870 				continue;
6871 			crssi = rx_status->chain_signal[i];
6872 			crssi -= rx_stats->c_nf;
6873 			rx_stats->c_rssi_ctl[i] = crssi * 2;
6874 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
6875 			/* We currently only have the global noise floor value. */
6876 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
6877 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
6878 			cc++;
6879 		}
6880 		if (cc > 0)
6881 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
6882 	}
6883 
6884 	/* XXX-NET80211 We are not going to populate c_phytype! */
6885 
6886 	switch (rx_status->encoding) {
6887 	case RX_ENC_LEGACY:
6888 	{
6889 		uint32_t legacy = 0;
6890 
6891 		supband = hw->wiphy->bands[rx_status->band];
6892 		if (supband != NULL)
6893 			legacy = supband->bitrates[rx_status->rate_idx].bitrate;
6894 		rx_stats->c_rate = legacy;
6895 		rxrate.legacy = legacy;
6896 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
6897 		break;
6898 	}
6899 	case RX_ENC_HT:
6900 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
6901 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6902 		rxrate.flags |= RATE_INFO_FLAGS_MCS;
6903 		rxrate.mcs = rx_status->rate_idx;
6904 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6905 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6906 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6907 		}
6908 		break;
6909 	case RX_ENC_VHT:
6910 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
6911 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6912 		rx_stats->c_vhtnss = rx_status->nss;
6913 		rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
6914 		rxrate.mcs = rx_status->rate_idx;
6915 		rxrate.nss = rx_status->nss;
6916 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6917 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6918 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6919 		}
6920 		break;
6921 	case RX_ENC_HE:
6922 		rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
6923 		rxrate.mcs = rx_status->rate_idx;
6924 		rxrate.nss = rx_status->nss;
6925 		/* XXX TODO */
6926 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6927 		break;
6928 	case RX_ENC_EHT:
6929 		rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
6930 		rxrate.mcs = rx_status->rate_idx;
6931 		rxrate.nss = rx_status->nss;
6932 		/* XXX TODO */
6933 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6934 		break;
6935 	}
6936 
6937 	rxrate.bw = rx_status->bw;
6938 	switch (rx_status->bw) {
6939 	case RATE_INFO_BW_20:
6940 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
6941 		break;
6942 	case RATE_INFO_BW_40:
6943 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
6944 		break;
6945 	case RATE_INFO_BW_80:
6946 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
6947 		break;
6948 	case RATE_INFO_BW_160:
6949 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
6950 		break;
6951 	case RATE_INFO_BW_320:
6952 	case RATE_INFO_BW_HE_RU:
6953 	case RATE_INFO_BW_EHT_RU:
6954 	case RATE_INFO_BW_5:
6955 	case RATE_INFO_BW_10:
6956 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
6957 		break;
6958 	}
6959 
6960 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
6961 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
6962 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
6963 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
6964 
6965 	/*
6966 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
6967 	 * case the hardware does something we do not expect always leave
6968 	 * these enabled.  Leaving this commant as documentation for the || 1.
6969 	 */
6970 #if defined(LKPI_80211_HW_CRYPTO) || 1
6971 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
6972 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
6973 		/* Only valid if decrypted is set. */
6974 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
6975 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
6976 	}
6977 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
6978 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
6979 	if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
6980 		rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
6981 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6982 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
6983 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
6984 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6985 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6986 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
6987 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
6988 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
6989 #endif
6990 
6991 	/* Fill in some sinfo bits to fill gaps not reported byt the driver. */
6992 	if (lsta != NULL) {
6993 		memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
6994 		lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
6995 
6996 		if (rx_status->chains != 0) {
6997 			lsta->sinfo.chains = rx_status->chains;
6998 			memcpy(lsta->sinfo.chain_signal, rx_status->chain_signal,
6999 			    sizeof(lsta->sinfo.chain_signal));
7000 			lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
7001 		}
7002 	}
7003 }
7004 
7005 /* For %list see comment towards the end of the function. */
7006 void
7007 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
7008     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
7009     struct list_head *list __unused)
7010 {
7011 	struct lkpi_hw *lhw;
7012 	struct ieee80211com *ic;
7013 	struct mbuf *m;
7014 	struct skb_shared_info *shinfo;
7015 	struct ieee80211_rx_status *rx_status;
7016 	struct ieee80211_rx_stats rx_stats;
7017 	struct ieee80211_node *ni;
7018 	struct ieee80211vap *vap;
7019 	struct ieee80211_hdr *hdr;
7020 	struct lkpi_sta *lsta;
7021 	int i, offset, ok, error;
7022 	uint8_t rssi;
7023 	bool is_beacon;
7024 
7025 	lhw = HW_TO_LHW(hw);
7026 	ic = lhw->ic;
7027 
7028 	if (skb->len < 2) {
7029 		/* Need 80211 stats here. */
7030 		counter_u64_add(ic->ic_ierrors, 1);
7031 		IMPROVE();
7032 		goto err;
7033 	}
7034 
7035 	/*
7036 	 * For now do the data copy; we can later improve things. Might even
7037 	 * have an mbuf backing the skb data then?
7038 	 */
7039 	m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
7040 	if (m == NULL) {
7041 		counter_u64_add(ic->ic_ierrors, 1);
7042 		goto err;
7043 	}
7044 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
7045 
7046 	shinfo = skb_shinfo(skb);
7047 	offset = m->m_len;
7048 	for (i = 0; i < shinfo->nr_frags; i++) {
7049 		m_copyback(m, offset, shinfo->frags[i].size,
7050 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
7051 		    shinfo->frags[i].offset);
7052 		offset += shinfo->frags[i].size;
7053 	}
7054 
7055 	rx_status = IEEE80211_SKB_RXCB(skb);
7056 
7057 	hdr = (void *)skb->data;
7058 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
7059 
7060 #ifdef LINUXKPI_DEBUG_80211
7061 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
7062 		goto no_trace_beacons;
7063 
7064 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7065 		printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
7066 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
7067 		    __func__, skb, skb->len, skb->data_len,
7068 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
7069 		    shinfo, shinfo->nr_frags,
7070 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
7071 
7072 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
7073 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
7074 
7075 	/* Implement a dump_rxcb() !!! */
7076 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7077 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
7078 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
7079 			__func__,
7080 			(uintmax_t)rx_status->boottime_ns,
7081 			(uintmax_t)rx_status->mactime,
7082 			rx_status->device_timestamp,
7083 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
7084 			rx_status->freq,
7085 			rx_status->bw,
7086 			rx_status->encoding,
7087 			rx_status->ampdu_reference,
7088 			rx_status->band,
7089 			rx_status->chains,
7090 			rx_status->chain_signal[0],
7091 			rx_status->chain_signal[1],
7092 			rx_status->chain_signal[2],
7093 			rx_status->chain_signal[3],
7094 			rx_status->signal,
7095 			rx_status->enc_flags,
7096 			rx_status->he_dcm,
7097 			rx_status->he_gi,
7098 			rx_status->he_ru,
7099 			rx_status->zero_length_psdu_type,
7100 			rx_status->nss,
7101 			rx_status->rate_idx);
7102 no_trace_beacons:
7103 #endif
7104 
7105 	lsta = NULL;
7106 	if (sta != NULL) {
7107 		lsta = STA_TO_LSTA(sta);
7108 		ni = ieee80211_ref_node(lsta->ni);
7109 	} else {
7110 		struct ieee80211_frame_min *wh;
7111 
7112 		wh = mtod(m, struct ieee80211_frame_min *);
7113 		ni = ieee80211_find_rxnode(ic, wh);
7114 		if (ni != NULL)
7115 			lsta = ni->ni_drv_data;
7116 	}
7117 
7118 	rssi = 0;
7119 	lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
7120 
7121 	ok = ieee80211_add_rx_params(m, &rx_stats);
7122 	if (ok == 0) {
7123 		m_freem(m);
7124 		counter_u64_add(ic->ic_ierrors, 1);
7125 		goto err;
7126 	}
7127 
7128 	if (ni != NULL)
7129 		vap = ni->ni_vap;
7130 	else
7131 		/*
7132 		 * XXX-BZ can we improve this by looking at the frame hdr
7133 		 * or other meta-data passed up?
7134 		 */
7135 		vap = TAILQ_FIRST(&ic->ic_vaps);
7136 
7137 #ifdef LINUXKPI_DEBUG_80211
7138 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7139 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
7140 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
7141 		    ni, vap, is_beacon ? " beacon" : "");
7142 #endif
7143 
7144 	if (ni != NULL && vap != NULL && is_beacon &&
7145 	    rx_status->device_timestamp > 0 &&
7146 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
7147 		struct lkpi_vif *lvif;
7148 		struct ieee80211_vif *vif;
7149 		struct ieee80211_frame *wh;
7150 
7151 		wh = mtod(m, struct ieee80211_frame *);
7152 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
7153 			goto skip_device_ts;
7154 
7155 		lvif = VAP_TO_LVIF(vap);
7156 		vif = LVIF_TO_VIF(lvif);
7157 
7158 		IMPROVE("TIMING_BEACON_ONLY?");
7159 		/* mac80211 specific (not net80211) so keep it here. */
7160 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
7161 		/*
7162 		 * net80211 should take care of the other information (sync_tsf,
7163 		 * sync_dtim_count) as otherwise we need to parse the beacon.
7164 		 */
7165 skip_device_ts:
7166 		;
7167 	}
7168 
7169 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
7170 	    ieee80211_radiotap_active_vap(vap)) {
7171 		struct lkpi_radiotap_rx_hdr *rtap;
7172 
7173 		rtap = &lhw->rtap_rx;
7174 		rtap->wr_tsft = rx_status->device_timestamp;
7175 		rtap->wr_flags = 0;
7176 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
7177 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7178 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
7179 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
7180 #if 0	/* .. or it does not given we strip it below. */
7181 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7182 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
7183 #endif
7184 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
7185 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
7186 		rtap->wr_rate = 0;
7187 		IMPROVE();
7188 		/* XXX TODO status->encoding / rate_index / bw */
7189 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
7190 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
7191 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
7192 		rtap->wr_dbm_antsignal = rssi;
7193 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
7194 	}
7195 
7196 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7197 		m_adj(m, -IEEE80211_CRC_LEN);
7198 
7199 #if 0
7200 	if (list != NULL) {
7201 		/*
7202 		* Normally this would be queued up and delivered by
7203 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
7204 		* See mt76::mac80211.c as only current possible consumer.
7205 		*/
7206 		IMPROVE("we simply pass the packet to net80211 to deal with.");
7207 	}
7208 #endif
7209 
7210 	/* Attach meta-information to the mbuf for the deferred RX path. */
7211 	if (ni != NULL) {
7212 #ifdef LKPI_80211_USE_MTAG
7213 		struct m_tag *mtag;
7214 		struct lkpi_80211_tag_rxni *rxni;
7215 
7216 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
7217 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
7218 		if (mtag == NULL) {
7219 			m_freem(m);
7220 			counter_u64_add(ic->ic_ierrors, 1);
7221 			goto err;
7222 		}
7223 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7224 		rxni->ni = ni;		/* We hold a reference. */
7225 		m_tag_prepend(m, mtag);
7226 #else
7227 		m->m_pkthdr.PH_loc.ptr = ni;	/* We hold a reference. */
7228 #endif
7229 	}
7230 
7231 	LKPI_80211_LHW_RXQ_LOCK(lhw);
7232 	if (lhw->rxq_stopped) {
7233 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7234 		m_freem(m);
7235 		counter_u64_add(ic->ic_ierrors, 1);
7236 		goto err;
7237 	}
7238 
7239 	error = mbufq_enqueue(&lhw->rxq, m);
7240 	if (error != 0) {
7241 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7242 		m_freem(m);
7243 		counter_u64_add(ic->ic_ierrors, 1);
7244 #ifdef LINUXKPI_DEBUG_80211
7245 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7246 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
7247 			    __func__, error);
7248 #endif
7249 		goto err;
7250 	}
7251 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
7252 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7253 
7254 	IMPROVE();
7255 
7256 err:
7257 	/* The skb is ours so we can free it :-) */
7258 	kfree_skb(skb);
7259 }
7260 
7261 uint8_t
7262 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
7263 {
7264 	const struct ieee80211_frame *wh;
7265 	uint8_t tid;
7266 
7267 	/* Linux seems to assume this is a QOS-Data-Frame */
7268 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
7269 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
7270 	   hdr->frame_control));
7271 
7272 	wh = (const struct ieee80211_frame *)hdr;
7273 	tid = ieee80211_gettid(wh);
7274 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
7275 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
7276 
7277 	return (tid);
7278 }
7279 
7280 /* -------------------------------------------------------------------------- */
7281 
7282 static void
7283 lkpi_wiphy_work(struct work_struct *work)
7284 {
7285 	struct lkpi_wiphy *lwiphy;
7286 	struct wiphy *wiphy;
7287 	struct wiphy_work *wk;
7288 
7289 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
7290 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7291 
7292 	wiphy_lock(wiphy);
7293 
7294 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7295 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
7296 	/* If there is nothing we do nothing. */
7297 	if (wk == NULL) {
7298 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7299 		wiphy_unlock(wiphy);
7300 		return;
7301 	}
7302 	list_del_init(&wk->entry);
7303 
7304 	/* More work to do? */
7305 	if (!list_empty(&lwiphy->wwk_list))
7306 		schedule_work(work);
7307 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7308 
7309 	/* Finally call the (*wiphy_work_fn)() function. */
7310 	wk->fn(wiphy, wk);
7311 
7312 	wiphy_unlock(wiphy);
7313 }
7314 
7315 void
7316 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
7317 {
7318 	struct lkpi_wiphy *lwiphy;
7319 
7320 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7321 
7322 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7323 	/* Do not double-queue. */
7324 	if (list_empty(&wwk->entry))
7325 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
7326 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7327 
7328 	/*
7329 	 * See how ieee80211_queue_work() work continues in Linux or if things
7330 	 * migrate here over time?
7331 	 * Use a system queue from linux/workqueue.h for now.
7332 	 */
7333 	queue_work(system_wq, &lwiphy->wwk);
7334 }
7335 
7336 void
7337 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
7338 {
7339 	struct lkpi_wiphy *lwiphy;
7340 
7341 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7342 
7343 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7344 	/* Only cancel if queued. */
7345 	if (!list_empty(&wwk->entry))
7346 		list_del_init(&wwk->entry);
7347 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7348 }
7349 
7350 void
7351 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
7352 {
7353 	struct lkpi_wiphy *lwiphy;
7354 	struct wiphy_work *wk;
7355 
7356 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7357 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7358 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
7359 	if (wwk != NULL && list_empty(&wwk->entry)) {
7360 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7361 		return;
7362 	}
7363 
7364 	while (!list_empty(&lwiphy->wwk_list)) {
7365 
7366 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7367 		    entry);
7368 		list_del_init(&wk->entry);
7369 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7370 		wk->fn(wiphy, wk);
7371 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7372 		if (wk == wwk)
7373 			break;
7374 	}
7375 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7376 }
7377 
7378 void
7379 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7380 {
7381 	struct wiphy_delayed_work *wdwk;
7382 
7383 	wdwk = from_timer(wdwk, tl, timer);
7384         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7385 }
7386 
7387 void
7388 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7389     struct wiphy_delayed_work *wdwk, unsigned long delay)
7390 {
7391 	if (delay == 0) {
7392 		/* Run right away. */
7393 		del_timer(&wdwk->timer);
7394 		wiphy_work_queue(wiphy, &wdwk->work);
7395 	} else {
7396 		wdwk->wiphy = wiphy;
7397 		mod_timer(&wdwk->timer, jiffies + delay);
7398 	}
7399 }
7400 
7401 void
7402 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7403     struct wiphy_delayed_work *wdwk)
7404 {
7405 	del_timer_sync(&wdwk->timer);
7406 	wiphy_work_cancel(wiphy, &wdwk->work);
7407 }
7408 
7409 /* -------------------------------------------------------------------------- */
7410 
7411 struct wiphy *
7412 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
7413 {
7414 	struct lkpi_wiphy *lwiphy;
7415 	struct wiphy *wiphy;
7416 
7417 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
7418 	if (lwiphy == NULL)
7419 		return (NULL);
7420 	lwiphy->ops = ops;
7421 
7422 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7423 	INIT_LIST_HEAD(&lwiphy->wwk_list);
7424 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7425 
7426 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7427 
7428 	mutex_init(&wiphy->mtx);
7429 	TODO();
7430 
7431 	return (wiphy);
7432 }
7433 
7434 void
7435 linuxkpi_wiphy_free(struct wiphy *wiphy)
7436 {
7437 	struct lkpi_wiphy *lwiphy;
7438 
7439 	if (wiphy == NULL)
7440 		return;
7441 
7442 	linuxkpi_wiphy_work_flush(wiphy, NULL);
7443 	mutex_destroy(&wiphy->mtx);
7444 
7445 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7446 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7447 
7448 	kfree(lwiphy);
7449 }
7450 
7451 static uint32_t
7452 lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7453 {
7454 	TODO("cfg80211_calculate_bitrate_ht");
7455 	return (rate->legacy);
7456 }
7457 
7458 static uint32_t
7459 lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7460 {
7461 	TODO("cfg80211_calculate_bitrate_vht");
7462 	return (rate->legacy);
7463 }
7464 
7465 uint32_t
7466 linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7467 {
7468 
7469 	/* Beware: order! */
7470 	if (rate->flags & RATE_INFO_FLAGS_MCS)
7471 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7472 
7473 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7474 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7475 
7476 	IMPROVE("HE/EHT/...");
7477 
7478 	return (rate->legacy);
7479 }
7480 
7481 uint32_t
7482 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
7483     enum nl80211_band band)
7484 {
7485 
7486 	switch (band) {
7487 	case NL80211_BAND_2GHZ:
7488 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
7489 		break;
7490 	case NL80211_BAND_5GHZ:
7491 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
7492 		break;
7493 	default:
7494 		/* XXX abort, retry, error, panic? */
7495 		break;
7496 	}
7497 
7498 	return (0);
7499 }
7500 
7501 uint32_t
7502 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
7503 {
7504 
7505 	return (ieee80211_mhz2ieee(freq, 0));
7506 }
7507 
7508 #if 0
7509 static struct lkpi_sta *
7510 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
7511 {
7512 	struct lkpi_sta *lsta, *temp;
7513 
7514 	rcu_read_lock();
7515 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7516 		if (lsta->ni == ni) {
7517 			rcu_read_unlock();
7518 			return (lsta);
7519 		}
7520 	}
7521 	rcu_read_unlock();
7522 
7523 	return (NULL);
7524 }
7525 #endif
7526 
7527 struct ieee80211_sta *
7528 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
7529 {
7530 	struct lkpi_vif *lvif;
7531 	struct lkpi_sta *lsta;
7532 	struct ieee80211_sta *sta;
7533 
7534 	lvif = VIF_TO_LVIF(vif);
7535 
7536 	rcu_read_lock();
7537 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7538 		sta = LSTA_TO_STA(lsta);
7539 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7540 			rcu_read_unlock();
7541 			return (sta);
7542 		}
7543 	}
7544 	rcu_read_unlock();
7545 	return (NULL);
7546 }
7547 
7548 struct ieee80211_sta *
7549 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7550     const uint8_t *addr, const uint8_t *ourvifaddr)
7551 {
7552 	struct lkpi_hw *lhw;
7553 	struct lkpi_vif *lvif;
7554 	struct lkpi_sta *lsta;
7555 	struct ieee80211_vif *vif;
7556 	struct ieee80211_sta *sta;
7557 
7558 	lhw = wiphy_priv(hw->wiphy);
7559 	sta = NULL;
7560 
7561 	LKPI_80211_LHW_LVIF_LOCK(lhw);
7562 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7563 
7564 		/* XXX-BZ check our address from the vif. */
7565 
7566 		vif = LVIF_TO_VIF(lvif);
7567 		if (ourvifaddr != NULL &&
7568 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
7569 			continue;
7570 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
7571 		if (sta != NULL)
7572 			break;
7573 	}
7574 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7575 
7576 	if (sta != NULL) {
7577 		lsta = STA_TO_LSTA(sta);
7578 		if (!lsta->added_to_drv)
7579 			return (NULL);
7580 	}
7581 
7582 	return (sta);
7583 }
7584 
7585 struct sk_buff *
7586 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
7587     struct ieee80211_txq *txq)
7588 {
7589 	struct lkpi_txq *ltxq;
7590 	struct lkpi_vif *lvif;
7591 	struct sk_buff *skb;
7592 
7593 	IMPROVE("wiphy_lock? or assert?");
7594 	skb = NULL;
7595 	ltxq = TXQ_TO_LTXQ(txq);
7596 	ltxq->seen_dequeue = true;
7597 
7598 	if (ltxq->stopped)
7599 		goto stopped;
7600 
7601 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
7602 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
7603 		ltxq->stopped = true;
7604 		goto stopped;
7605 	}
7606 
7607 	IMPROVE("hw(TX_FRAG_LIST)");
7608 
7609 	LKPI_80211_LTXQ_LOCK(ltxq);
7610 	skb = skb_dequeue(&ltxq->skbq);
7611 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7612 
7613 stopped:
7614 	return (skb);
7615 }
7616 
7617 void
7618 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
7619     unsigned long *frame_cnt, unsigned long *byte_cnt)
7620 {
7621 	struct lkpi_txq *ltxq;
7622 	struct sk_buff *skb;
7623 	unsigned long fc, bc;
7624 
7625 	ltxq = TXQ_TO_LTXQ(txq);
7626 
7627 	fc = bc = 0;
7628 	LKPI_80211_LTXQ_LOCK(ltxq);
7629 	skb_queue_walk(&ltxq->skbq, skb) {
7630 		fc++;
7631 		bc += skb->len;
7632 	}
7633 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7634 	if (frame_cnt)
7635 		*frame_cnt = fc;
7636 	if (byte_cnt)
7637 		*byte_cnt = bc;
7638 
7639 	/* Validate that this is doing the correct thing. */
7640 	/* Should we keep track on en/dequeue? */
7641 	IMPROVE();
7642 }
7643 
7644 /*
7645  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
7646  * The latter tries to derive the success status from the info flags
7647  * passed back from the driver.  rawx_mit() saves the ni on the m and the
7648  * m on the skb for us to be able to give feedback to net80211.
7649  */
7650 static void
7651 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7652     int status)
7653 {
7654 	struct ieee80211_node *ni;
7655 	struct mbuf *m;
7656 
7657 	m = skb->m;
7658 	skb->m = NULL;
7659 
7660 	if (m != NULL) {
7661 		ni = m->m_pkthdr.PH_loc.ptr;
7662 		/* Status: 0 is ok, != 0 is error. */
7663 		ieee80211_tx_complete(ni, m, status);
7664 		/* ni & mbuf were consumed. */
7665 	}
7666 }
7667 
7668 void
7669 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7670     int status)
7671 {
7672 
7673 	_lkpi_ieee80211_free_txskb(hw, skb, status);
7674 	kfree_skb(skb);
7675 }
7676 
7677 void
7678 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7679     struct ieee80211_tx_status *txstat)
7680 {
7681 	struct sk_buff *skb;
7682 	struct ieee80211_tx_info *info;
7683 	struct ieee80211_ratectl_tx_status txs;
7684 	struct ieee80211_node *ni;
7685 	int status;
7686 
7687 	skb = txstat->skb;
7688 	if (skb->m != NULL) {
7689 		struct mbuf *m;
7690 
7691 		m = skb->m;
7692 		ni = m->m_pkthdr.PH_loc.ptr;
7693 		memset(&txs, 0, sizeof(txs));
7694 	} else {
7695 		ni = NULL;
7696 	}
7697 
7698 	info = txstat->info;
7699 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7700 		status = 0;	/* No error. */
7701 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7702 	} else {
7703 		status = 1;
7704 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7705 	}
7706 
7707 	if (ni != NULL) {
7708 		txs.pktlen = skb->len;
7709 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7710 		if (info->status.rates[0].count > 1) {
7711 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7712 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7713 		}
7714 #if 0		/* Unused in net80211 currently. */
7715 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7716 		txs.final_rate = info->status.rates[0].idx;
7717 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7718 #endif
7719 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7720 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7721 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7722 		}
7723 
7724 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7725 		ieee80211_ratectl_tx_complete(ni, &txs);
7726 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7727 
7728 #ifdef LINUXKPI_DEBUG_80211
7729 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7730 			printf("TX-RATE: %s: long_retries %d\n", __func__,
7731 			    txs.long_retries);
7732 		}
7733 #endif
7734 	}
7735 
7736 #ifdef LINUXKPI_DEBUG_80211
7737 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7738 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7739 		    "band %u hw_queue %u tx_time_est %d : "
7740 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7741 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7742 		    "tx_time %u flags %#x "
7743 		    "status_driver_data [ %p %p ]\n",
7744 		    __func__, hw, skb, status, info->flags,
7745 		    info->band, info->hw_queue, info->tx_time_est,
7746 		    info->status.rates[0].idx, info->status.rates[0].count,
7747 		    info->status.rates[0].flags,
7748 		    info->status.rates[1].idx, info->status.rates[1].count,
7749 		    info->status.rates[1].flags,
7750 		    info->status.rates[2].idx, info->status.rates[2].count,
7751 		    info->status.rates[2].flags,
7752 		    info->status.rates[3].idx, info->status.rates[3].count,
7753 		    info->status.rates[3].flags,
7754 		    info->status.ack_signal, info->status.ampdu_ack_len,
7755 		    info->status.ampdu_len, info->status.antenna,
7756 		    info->status.tx_time, info->status.flags,
7757 		    info->status.status_driver_data[0],
7758 		    info->status.status_driver_data[1]);
7759 #endif
7760 
7761 	if (txstat->free_list) {
7762 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7763 		list_add_tail(&skb->list, txstat->free_list);
7764 	} else {
7765 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7766 	}
7767 }
7768 
7769 void
7770 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7771 {
7772 	struct ieee80211_tx_status status;
7773 
7774 	memset(&status, 0, sizeof(status));
7775 	status.info = IEEE80211_SKB_CB(skb);
7776 	status.skb = skb;
7777 	/* sta, n_rates, rates, free_list? */
7778 
7779 	ieee80211_tx_status_ext(hw, &status);
7780 }
7781 
7782 /*
7783  * This is an internal bandaid for the moment for the way we glue
7784  * skbs and mbufs together for TX.  Once we have skbs backed by
7785  * mbufs this should go away.
7786  * This is a public function but kept on the private KPI (lkpi_)
7787  * and is not exposed by a header file.
7788  */
7789 static void
7790 lkpi_ieee80211_free_skb_mbuf(void *p)
7791 {
7792 	struct ieee80211_node *ni;
7793 	struct mbuf *m;
7794 
7795 	if (p == NULL)
7796 		return;
7797 
7798 	m = (struct mbuf *)p;
7799 	M_ASSERTPKTHDR(m);
7800 
7801 	ni = m->m_pkthdr.PH_loc.ptr;
7802 	m->m_pkthdr.PH_loc.ptr = NULL;
7803 	if (ni != NULL)
7804 		ieee80211_free_node(ni);
7805 	m_freem(m);
7806 }
7807 
7808 void
7809 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
7810     struct delayed_work *w, int delay)
7811 {
7812 	struct lkpi_hw *lhw;
7813 
7814 	/* Need to make sure hw is in a stable (non-suspended) state. */
7815 	IMPROVE();
7816 
7817 	lhw = HW_TO_LHW(hw);
7818 	queue_delayed_work(lhw->workq, w, delay);
7819 }
7820 
7821 void
7822 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
7823     struct work_struct *w)
7824 {
7825 	struct lkpi_hw *lhw;
7826 
7827 	/* Need to make sure hw is in a stable (non-suspended) state. */
7828 	IMPROVE();
7829 
7830 	lhw = HW_TO_LHW(hw);
7831 	queue_work(lhw->workq, w);
7832 }
7833 
7834 struct sk_buff *
7835 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, const uint8_t *addr,
7836     const uint8_t *ssid, size_t ssid_len, size_t tailroom)
7837 {
7838 	struct sk_buff *skb;
7839 	struct ieee80211_frame *wh;
7840 	uint8_t *p;
7841 	size_t len;
7842 
7843 	len = sizeof(*wh);
7844 	len += 2 + ssid_len;
7845 
7846 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7847 	if (skb == NULL)
7848 		return (NULL);
7849 
7850 	skb_reserve(skb, hw->extra_tx_headroom);
7851 
7852 	wh = skb_put_zero(skb, sizeof(*wh));
7853 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7854 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7855 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7856 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7857 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7858 
7859 	p = skb_put(skb, 2 + ssid_len);
7860 	*p++ = IEEE80211_ELEMID_SSID;
7861 	*p++ = ssid_len;
7862 	if (ssid_len > 0)
7863 		memcpy(p, ssid, ssid_len);
7864 
7865 	return (skb);
7866 }
7867 
7868 struct sk_buff *
7869 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
7870     struct ieee80211_vif *vif)
7871 {
7872 	struct lkpi_vif *lvif;
7873 	struct ieee80211vap *vap;
7874 	struct sk_buff *skb;
7875 	struct ieee80211_frame_pspoll *psp;
7876 	uint16_t v;
7877 
7878 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
7879 	if (skb == NULL)
7880 		return (NULL);
7881 
7882 	skb_reserve(skb, hw->extra_tx_headroom);
7883 
7884 	lvif = VIF_TO_LVIF(vif);
7885 	vap = LVIF_TO_VAP(lvif);
7886 
7887 	psp = skb_put_zero(skb, sizeof(*psp));
7888 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
7889 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7890 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
7891 	memcpy(&psp->i_aid, &v, sizeof(v));
7892 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
7893 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
7894 
7895 	return (skb);
7896 }
7897 
7898 struct sk_buff *
7899 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7900     struct ieee80211_vif *vif, int linkid, bool qos)
7901 {
7902 	struct lkpi_vif *lvif;
7903 	struct ieee80211vap *vap;
7904 	struct sk_buff *skb;
7905 	struct ieee80211_frame *nullf;
7906 
7907 	IMPROVE("linkid");
7908 
7909 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
7910 	if (skb == NULL)
7911 		return (NULL);
7912 
7913 	skb_reserve(skb, hw->extra_tx_headroom);
7914 
7915 	lvif = VIF_TO_LVIF(vif);
7916 	vap = LVIF_TO_VAP(lvif);
7917 
7918 	nullf = skb_put_zero(skb, sizeof(*nullf));
7919 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
7920 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
7921 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
7922 
7923 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
7924 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
7925 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
7926 
7927 	return (skb);
7928 }
7929 
7930 struct wireless_dev *
7931 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
7932 {
7933 	struct lkpi_vif *lvif;
7934 
7935 	lvif = VIF_TO_LVIF(vif);
7936 	return (&lvif->wdev);
7937 }
7938 
7939 void
7940 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
7941 {
7942 	struct lkpi_vif *lvif;
7943 	struct ieee80211vap *vap;
7944 	enum ieee80211_state nstate;
7945 	int arg;
7946 
7947 	lvif = VIF_TO_LVIF(vif);
7948 	vap = LVIF_TO_VAP(lvif);
7949 
7950 	/*
7951 	 * Go to init; otherwise we need to elaborately check state and
7952 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
7953 	 * Let the statemachine handle all neccessary changes.
7954 	 */
7955 	nstate = IEEE80211_S_INIT;
7956 	arg = 0;	/* Not a valid reason. */
7957 
7958 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
7959 	    "beacons %d dtim_period %d)\n", __func__, vif, vap,
7960 	    ieee80211_state_name[vap->iv_state],
7961 	    lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
7962 	    vif->bss_conf.dtim_period);
7963 	ieee80211_new_state(vap, nstate, arg);
7964 }
7965 
7966 void
7967 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7968 {
7969 	struct lkpi_vif *lvif;
7970 	struct ieee80211vap *vap;
7971 
7972 	lvif = VIF_TO_LVIF(vif);
7973 	vap = LVIF_TO_VAP(lvif);
7974 
7975 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
7976 	    "beacons %d dtim_period %d)\n", __func__, vif, vap,
7977 	    ieee80211_state_name[vap->iv_state],
7978 	    lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
7979 	    vif->bss_conf.dtim_period);
7980 	ieee80211_beacon_miss(vap->iv_ic);
7981 }
7982 
7983 /* -------------------------------------------------------------------------- */
7984 
7985 void
7986 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
7987 {
7988 	struct lkpi_hw *lhw;
7989 	struct lkpi_vif *lvif;
7990 	struct ieee80211_vif *vif;
7991 	int ac_count, ac;
7992 
7993 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
7994 	    __func__, qnum, hw->queues, hw));
7995 
7996 	lhw = wiphy_priv(hw->wiphy);
7997 
7998 	/* See lkpi_ic_vap_create(). */
7999 	if (hw->queues >= IEEE80211_NUM_ACS)
8000 		ac_count = IEEE80211_NUM_ACS;
8001 	else
8002 		ac_count = 1;
8003 
8004 	LKPI_80211_LHW_LVIF_LOCK(lhw);
8005 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8006 
8007 		vif = LVIF_TO_VIF(lvif);
8008 		for (ac = 0; ac < ac_count; ac++) {
8009 			IMPROVE_TXQ("LOCKING");
8010 			if (qnum == vif->hw_queue[ac]) {
8011 #ifdef LINUXKPI_DEBUG_80211
8012 				/*
8013 				 * For now log this to better understand
8014 				 * how this is supposed to work.
8015 				 */
8016 				if (lvif->hw_queue_stopped[ac] &&
8017 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8018 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8019 					    "lvif %p vif %p ac %d qnum %d already "
8020 					    "stopped\n", __func__, __LINE__,
8021 					    lhw, hw, lvif, vif, ac, qnum);
8022 #endif
8023 				lvif->hw_queue_stopped[ac] = true;
8024 			}
8025 		}
8026 	}
8027 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8028 }
8029 
8030 void
8031 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
8032 {
8033 	int i;
8034 
8035 	IMPROVE_TXQ("Locking; do we need further info?");
8036 	for (i = 0; i < hw->queues; i++)
8037 		linuxkpi_ieee80211_stop_queue(hw, i);
8038 }
8039 
8040 
8041 static void
8042 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
8043 {
8044 	struct lkpi_hw *lhw;
8045 	struct lkpi_vif *lvif;
8046 	struct lkpi_sta *lsta;
8047 	int ac_count, ac, tid;
8048 
8049 	/* See lkpi_ic_vap_create(). */
8050 	if (hw->queues >= IEEE80211_NUM_ACS)
8051 		ac_count = IEEE80211_NUM_ACS;
8052 	else
8053 		ac_count = 1;
8054 
8055 	lhw = wiphy_priv(hw->wiphy);
8056 
8057 	IMPROVE_TXQ("Locking");
8058 	LKPI_80211_LHW_LVIF_LOCK(lhw);
8059 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8060 		struct ieee80211_vif *vif;
8061 
8062 		vif = LVIF_TO_VIF(lvif);
8063 		for (ac = 0; ac < ac_count; ac++) {
8064 
8065 			if (hwq == vif->hw_queue[ac]) {
8066 
8067 				/* XXX-BZ what about software scan? */
8068 
8069 #ifdef LINUXKPI_DEBUG_80211
8070 				/*
8071 				 * For now log this to better understand
8072 				 * how this is supposed to work.
8073 				 */
8074 				if (!lvif->hw_queue_stopped[ac] &&
8075 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8076 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8077 					    "lvif %p vif %p ac %d hw_q not stopped\n",
8078 					    __func__, __LINE__,
8079 					    lhw, hw, lvif, vif, ac);
8080 #endif
8081 				lvif->hw_queue_stopped[ac] = false;
8082 
8083 				rcu_read_lock();
8084 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
8085 					struct ieee80211_sta *sta;
8086 
8087 					sta = LSTA_TO_STA(lsta);
8088 					for (tid = 0; tid < nitems(sta->txq); tid++) {
8089 						struct lkpi_txq *ltxq;
8090 
8091 						if (sta->txq[tid] == NULL)
8092 							continue;
8093 
8094 						if (sta->txq[tid]->ac != ac)
8095 							continue;
8096 
8097 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8098 						if (!ltxq->stopped)
8099 							continue;
8100 
8101 						ltxq->stopped = false;
8102 
8103 						if (!skb_queue_empty(&ltxq->skbq))
8104 							lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
8105 					}
8106 				}
8107 				rcu_read_unlock();
8108 			}
8109 		}
8110 	}
8111 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8112 }
8113 
8114 static void
8115 lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *hw)
8116 {
8117 	int i;
8118 
8119 	IMPROVE_TXQ("Is this all/enough here?");
8120 	for (i = 0; i < hw->queues; i++)
8121 		lkpi_ieee80211_wake_queues(hw, i);
8122 }
8123 
8124 void
8125 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
8126 {
8127 	wiphy_lock(hw->wiphy);
8128 	lkpi_ieee80211_wake_queues_locked(hw);
8129 	wiphy_unlock(hw->wiphy);
8130 }
8131 
8132 void
8133 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
8134 {
8135 
8136 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
8137 	    __func__, qnum, hw->queues, hw));
8138 
8139 	wiphy_lock(hw->wiphy);
8140 	lkpi_ieee80211_wake_queues(hw, qnum);
8141 	wiphy_unlock(hw->wiphy);
8142 }
8143 
8144 /* This is just hardware queues. */
8145 void
8146 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
8147 {
8148 	struct lkpi_hw *lhw;
8149 
8150 	lhw = HW_TO_LHW(hw);
8151 
8152 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
8153 	IMPROVE_TXQ("LOCKING");
8154 	if (++lhw->txq_generation[ac] == 0)
8155 		lhw->txq_generation[ac]++;
8156 }
8157 
8158 struct ieee80211_txq *
8159 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
8160 {
8161 	struct lkpi_hw *lhw;
8162 	struct ieee80211_txq *txq;
8163 	struct lkpi_txq *ltxq;
8164 
8165 	lhw = HW_TO_LHW(hw);
8166 	txq = NULL;
8167 
8168 	IMPROVE_TXQ("LOCKING");
8169 
8170 	/* Check that we are scheduled. */
8171 	if (lhw->txq_generation[ac] == 0)
8172 		goto out;
8173 
8174 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
8175 	if (ltxq == NULL)
8176 		goto out;
8177 	if (ltxq->txq_generation == lhw->txq_generation[ac])
8178 		goto out;
8179 
8180 	ltxq->txq_generation = lhw->txq_generation[ac];
8181 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
8182 	txq = &ltxq->txq;
8183 	TAILQ_ELEM_INIT(ltxq, txq_entry);
8184 
8185 out:
8186 	return (txq);
8187 }
8188 
8189 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
8190     struct ieee80211_txq *txq, bool withoutpkts)
8191 {
8192 	struct lkpi_hw *lhw;
8193 	struct lkpi_txq *ltxq;
8194 	bool ltxq_empty;
8195 
8196 	ltxq = TXQ_TO_LTXQ(txq);
8197 
8198 	IMPROVE_TXQ("LOCKING");
8199 
8200 	/* Only schedule if work to do or asked to anyway. */
8201 	LKPI_80211_LTXQ_LOCK(ltxq);
8202 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
8203 	LKPI_80211_LTXQ_UNLOCK(ltxq);
8204 	if (!withoutpkts && ltxq_empty)
8205 		goto out;
8206 
8207 	/*
8208 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
8209 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
8210 	 * is queued, tqe_next may be NULL if this is the only element in the list.
8211 	 */
8212 	if (ltxq->txq_entry.tqe_prev != NULL)
8213 		goto out;
8214 
8215 	lhw = HW_TO_LHW(hw);
8216 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
8217 out:
8218 	return;
8219 }
8220 
8221 void
8222 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
8223     struct ieee80211_txq *txq)
8224 {
8225 	struct lkpi_hw *lhw;
8226 	struct ieee80211_txq *ntxq;
8227 	struct ieee80211_tx_control control;
8228         struct sk_buff *skb;
8229 
8230 	lhw = HW_TO_LHW(hw);
8231 
8232 	LKPI_80211_LHW_TXQ_LOCK(lhw);
8233 	ieee80211_txq_schedule_start(hw, txq->ac);
8234 	do {
8235 		ntxq = ieee80211_next_txq(hw, txq->ac);
8236 		if (ntxq == NULL)
8237 			break;
8238 
8239 		memset(&control, 0, sizeof(control));
8240 		control.sta = ntxq->sta;
8241 		do {
8242 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
8243 			if (skb == NULL)
8244 				break;
8245 			lkpi_80211_mo_tx(hw, &control, skb);
8246 		} while(1);
8247 
8248 		ieee80211_return_txq(hw, ntxq, false);
8249 	} while (1);
8250 	ieee80211_txq_schedule_end(hw, txq->ac);
8251 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
8252 }
8253 
8254 /* -------------------------------------------------------------------------- */
8255 
8256 struct lkpi_cfg80211_bss {
8257 	u_int refcnt;
8258 	struct cfg80211_bss bss;
8259 };
8260 
8261 struct lkpi_cfg80211_get_bss_iter_lookup {
8262 	struct wiphy *wiphy;
8263 	struct linuxkpi_ieee80211_channel *chan;
8264 	const uint8_t *bssid;
8265 	const uint8_t *ssid;
8266 	size_t ssid_len;
8267 	enum ieee80211_bss_type bss_type;
8268 	enum ieee80211_privacy privacy;
8269 
8270 	/*
8271 	 * Something to store a copy of the result as the net80211 scan cache
8272 	 * is not refoucnted so a scan entry might go away any time.
8273 	 */
8274 	bool match;
8275 	struct cfg80211_bss *bss;
8276 };
8277 
8278 static void
8279 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
8280 {
8281 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
8282 	size_t ielen;
8283 
8284 	lookup = arg;
8285 
8286 	/* Do not try to find another match. */
8287 	if (lookup->match)
8288 		return;
8289 
8290 	/* Nothing to store result. */
8291 	if (lookup->bss == NULL)
8292 		return;
8293 
8294 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
8295 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
8296 		/* We have no idea what to compare to as the drivers only request ANY */
8297 		return;
8298 	}
8299 
8300 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
8301 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
8302 		/* We have no idea what to compare to as the drivers only request ANY */
8303 		return;
8304 	}
8305 
8306 	if (lookup->chan != NULL) {
8307 		struct linuxkpi_ieee80211_channel *chan;
8308 
8309 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
8310 		    se->se_chan->ic_freq);
8311 		if (chan == NULL || chan != lookup->chan)
8312 			return;
8313 	}
8314 
8315 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
8316 		return;
8317 
8318 	if (lookup->ssid) {
8319 		if (lookup->ssid_len != se->se_ssid[1] ||
8320 		    se->se_ssid[1] == 0)
8321 			return;
8322 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
8323 			return;
8324 	}
8325 
8326 	ielen = se->se_ies.len;
8327 
8328 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
8329 	    M_LKPI80211, M_NOWAIT | M_ZERO);
8330 	if (lookup->bss->ies == NULL)
8331 		return;
8332 
8333 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
8334 	lookup->bss->ies->len = ielen;
8335 	if (ielen)
8336 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
8337 
8338 	lookup->match = true;
8339 }
8340 
8341 struct cfg80211_bss *
8342 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
8343     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
8344     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
8345 {
8346 	struct lkpi_cfg80211_bss *lbss;
8347 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
8348 	struct lkpi_hw *lhw;
8349 	struct ieee80211vap *vap;
8350 
8351 	lhw = wiphy_priv(wiphy);
8352 
8353 	/* Let's hope we can alloc. */
8354 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
8355 	if (lbss == NULL) {
8356 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
8357 		return (NULL);
8358 	}
8359 
8360 	lookup.wiphy = wiphy;
8361 	lookup.chan = chan;
8362 	lookup.bssid = bssid;
8363 	lookup.ssid = ssid;
8364 	lookup.ssid_len = ssid_len;
8365 	lookup.bss_type = bss_type;
8366 	lookup.privacy = privacy;
8367 	lookup.match = false;
8368 	lookup.bss = &lbss->bss;
8369 
8370 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
8371 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
8372 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
8373 	if (!lookup.match) {
8374 		free(lbss, M_LKPI80211);
8375 		return (NULL);
8376 	}
8377 
8378 	refcount_init(&lbss->refcnt, 1);
8379 	return (&lbss->bss);
8380 }
8381 
8382 void
8383 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
8384 {
8385 	struct lkpi_cfg80211_bss *lbss;
8386 
8387 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
8388 
8389 	/* Free everything again on refcount ... */
8390 	if (refcount_release(&lbss->refcnt)) {
8391 		free(lbss->bss.ies, M_LKPI80211);
8392 		free(lbss, M_LKPI80211);
8393 	}
8394 }
8395 
8396 void
8397 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
8398 {
8399 	struct lkpi_hw *lhw;
8400 	struct ieee80211com *ic;
8401 	struct ieee80211vap *vap;
8402 
8403 	lhw = wiphy_priv(wiphy);
8404 	ic = lhw->ic;
8405 
8406 	/*
8407 	 * If we haven't called ieee80211_ifattach() yet
8408 	 * or there is no VAP, there are no scans to flush.
8409 	 */
8410 	if (ic == NULL ||
8411 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
8412 		return;
8413 
8414 	/* Should only happen on the current one? Not seen it late enough. */
8415 	IEEE80211_LOCK(ic);
8416 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8417 		ieee80211_scan_flush(vap);
8418 	IEEE80211_UNLOCK(ic);
8419 }
8420 
8421 /* -------------------------------------------------------------------------- */
8422 
8423 /*
8424  * hw->conf get initialized/set in various places for us:
8425  * - linuxkpi_ieee80211_alloc_hw(): flags
8426  * - linuxkpi_ieee80211_ifattach(): chandef
8427  * - lkpi_ic_vap_create(): listen_interval
8428  * - lkpi_ic_set_channel(): chandef, flags
8429  */
8430 
8431 int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8432     struct ieee80211_chanctx_conf *new)
8433 {
8434 	struct cfg80211_chan_def *cd;
8435 	uint32_t changed;
8436 	int error;
8437 
8438 	changed = 0;
8439 	if (new == NULL || new->def.chan == NULL)
8440 		cd = NULL;
8441 	else
8442 		cd = &new->def;
8443 
8444 	if (cd && cd->chan != hw->conf.chandef.chan) {
8445 		/* Copy; the chan pointer is fine and will stay valid. */
8446 		hw->conf.chandef = *cd;
8447 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8448 	}
8449 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8450 
8451 	if (changed == 0)
8452 		return (0);
8453 
8454 	error = lkpi_80211_mo_config(hw, changed);
8455 	return (error);
8456 }
8457 
8458 /* -------------------------------------------------------------------------- */
8459 
8460 MODULE_VERSION(linuxkpi_wlan, 1);
8461 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
8462 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8463