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