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