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