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