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