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