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