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