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