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