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