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