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