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