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