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