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