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