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