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