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