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