xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * Copyright (c) 2020-2023 The FreeBSD Foundation
3  * Copyright (c) 2020-2022 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 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/queue.h>
53 #include <sys/taskqueue.h>
54 #include <sys/libkern.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_proto.h>
63 #include <net80211/ieee80211_ratectl.h>
64 #include <net80211/ieee80211_radiotap.h>
65 
66 #define	LINUXKPI_NET80211
67 #include <net/mac80211.h>
68 
69 #include <linux/workqueue.h>
70 #include "linux_80211.h"
71 
72 #define	LKPI_80211_WME
73 /* #define	LKPI_80211_HW_CRYPTO */
74 
75 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
76 
77 /* XXX-BZ really want this and others in queue.h */
78 #define	TAILQ_ELEM_INIT(elm, field) do {				\
79 	(elm)->field.tqe_next = NULL;					\
80 	(elm)->field.tqe_prev = NULL;					\
81 } while (0)
82 
83 /* -------------------------------------------------------------------------- */
84 
85 /* Keep public for as long as header files are using it too. */
86 int linuxkpi_debug_80211;
87 
88 #ifdef LINUXKPI_DEBUG_80211
89 SYSCTL_DECL(_compat_linuxkpi);
90 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
91     "LinuxKPI 802.11 compatibility layer");
92 
93 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
94     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
95 
96 #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
97     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
98 #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
99     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
100 #else
101 #define	UNIMPLEMENTED		do { } while (0)
102 #define	TRACEOK()		do { } while (0)
103 #endif
104 
105 /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
106 #ifndef PREP_TX_INFO_DURATION
107 #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
108 #endif
109 
110 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
111 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
112 
113 /* IEEE 802.11-05/0257r1 */
114 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
115 
116 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
117 static const uint8_t ieee80211e_up_to_ac[] = {
118 	IEEE80211_AC_BE,
119 	IEEE80211_AC_BK,
120 	IEEE80211_AC_BK,
121 	IEEE80211_AC_BE,
122 	IEEE80211_AC_VI,
123 	IEEE80211_AC_VI,
124 	IEEE80211_AC_VO,
125 	IEEE80211_AC_VO,
126 #if 0
127 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
128 #endif
129 };
130 
131 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
132 	/*
133 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
134 	 * mac80211 and to the driver or net80211.
135 	 * Can we pass some on 1:1? Need to compare the (*f)().
136 	 */
137 };
138 
139 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
140     struct ieee80211_node *);
141 static void lkpi_80211_txq_task(void *, int);
142 static void lkpi_ieee80211_free_skb_mbuf(void *);
143 #ifdef LKPI_80211_WME
144 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
145 #endif
146 
147 static void
148 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
149     const char *_f, int _l)
150 {
151 
152 #ifdef LINUXKPI_DEBUG_80211
153 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
154 		return;
155 	if (lsta == NULL)
156 		return;
157 
158 	printf("%s:%d lsta %p ni %p sta %p\n",
159 	    _f, _l, lsta, ni, &lsta->sta);
160 	if (ni != NULL)
161 		ieee80211_dump_node(NULL, ni);
162 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
163 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
164 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
165 #endif
166 }
167 
168 static void
169 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
170 {
171 	struct ieee80211_node *ni;
172 
173 	IMPROVE("XXX-BZ remove tqe_prev check once ni-sta-state-sync is fixed");
174 
175 	ni = lsta->ni;
176 
177 	LKPI_80211_LVIF_LOCK(lvif);
178 	if (lsta->lsta_entry.tqe_prev != NULL)
179 		TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
180 	LKPI_80211_LVIF_UNLOCK(lvif);
181 
182 	lsta->ni = NULL;
183 	ni->ni_drv_data = NULL;
184 	if (ni != NULL)
185 		ieee80211_free_node(ni);
186 
187 	IMPROVE("more from lkpi_ic_node_free() should happen here.");
188 
189 	free(lsta, M_LKPI80211);
190 }
191 
192 static struct lkpi_sta *
193 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
194     struct ieee80211_hw *hw, struct ieee80211_node *ni)
195 {
196 	struct lkpi_sta *lsta;
197 	struct lkpi_vif *lvif;
198 	struct ieee80211_vif *vif;
199 	struct ieee80211_sta *sta;
200 	int band, i, tid;
201 
202 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
203 	    M_NOWAIT | M_ZERO);
204 	if (lsta == NULL)
205 		return (NULL);
206 
207 	lsta->added_to_drv = false;
208 	lsta->state = IEEE80211_STA_NOTEXIST;
209 #if 0
210 	/*
211 	 * This needs to be done in node_init() as ieee80211_alloc_node()
212 	 * will initialise the refcount after us.
213 	 */
214 	lsta->ni = ieee80211_ref_node(ni);
215 #endif
216 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
217 	ni->ni_drv_data = lsta;
218 
219 	lvif = VAP_TO_LVIF(vap);
220 	vif = LVIF_TO_VIF(lvif);
221 	sta = LSTA_TO_STA(lsta);
222 
223 	IEEE80211_ADDR_COPY(sta->addr, mac);
224 
225 	/* TXQ */
226 	for (tid = 0; tid < nitems(sta->txq); tid++) {
227 		struct lkpi_txq *ltxq;
228 
229 		/* We are not limiting ourselves to hw.queues here. */
230 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
231 		    M_LKPI80211, M_NOWAIT | M_ZERO);
232 		if (ltxq == NULL)
233 			goto cleanup;
234 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
235 		if (tid == IEEE80211_NUM_TIDS) {
236 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
237 				free(ltxq, M_LKPI80211);
238 				continue;
239 			}
240 			IMPROVE("AP/if we support non-STA here too");
241 			ltxq->txq.ac = IEEE80211_AC_VO;
242 		} else {
243 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
244 		}
245 		ltxq->seen_dequeue = false;
246 		ltxq->stopped = false;
247 		ltxq->txq.vif = vif;
248 		ltxq->txq.tid = tid;
249 		ltxq->txq.sta = sta;
250 		TAILQ_ELEM_INIT(ltxq, txq_entry);
251 		skb_queue_head_init(&ltxq->skbq);
252 		sta->txq[tid] = &ltxq->txq;
253 	}
254 
255 	/* Deflink information. */
256 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
257 		struct ieee80211_supported_band *supband;
258 
259 		supband = hw->wiphy->bands[band];
260 		if (supband == NULL)
261 			continue;
262 
263 		for (i = 0; i < supband->n_bitrates; i++) {
264 
265 			IMPROVE("Further supband->bitrates[i]* checks?");
266 			/* or should we get them from the ni? */
267 			sta->deflink.supp_rates[band] |= BIT(i);
268 		}
269 	}
270 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
271 	IMPROVE("ht, vht, he, ... bandwidth, smps_mode, ..");
272 	/* bandwidth = IEEE80211_STA_RX_... */
273 
274 	/* Link configuration. */
275 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
276 	sta->link[0] = &sta->deflink;
277 	for (i = 1; i < nitems(sta->link); i++) {
278 		IMPROVE("more links; only link[0] = deflink currently.");
279 	}
280 
281 	/* Deferred TX path. */
282 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
283 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
284 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
285 
286 	return (lsta);
287 
288 cleanup:
289 	for (; tid >= 0; tid--)
290 		free(sta->txq[tid], M_LKPI80211);
291 	free(lsta, M_LKPI80211);
292 	return (NULL);
293 }
294 
295 static enum nl80211_band
296 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
297 {
298 
299 	if (IEEE80211_IS_CHAN_2GHZ(c))
300 		return (NL80211_BAND_2GHZ);
301 	else if (IEEE80211_IS_CHAN_5GHZ(c))
302 		return (NL80211_BAND_5GHZ);
303 #ifdef __notyet__
304 	else if ()
305 		return (NL80211_BAND_6GHZ);
306 	else if ()
307 		return (NL80211_BAND_60GHZ);
308 	else if (IEEE80211_IS_CHAN_GSM(c))
309 		return (NL80211_BAND_XXX);
310 #endif
311 	else
312 		panic("%s: unsupported band. c %p flags %#x\n",
313 		    __func__, c, c->ic_flags);
314 }
315 
316 static uint32_t
317 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
318 {
319 
320 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
321 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
322 	switch (band) {
323 	case NL80211_BAND_2GHZ:
324 		return (IEEE80211_CHAN_2GHZ);
325 		break;
326 	case NL80211_BAND_5GHZ:
327 		return (IEEE80211_CHAN_5GHZ);
328 		break;
329 	case NL80211_BAND_60GHZ:
330 		break;
331 	case NL80211_BAND_6GHZ:
332 		break;
333 	default:
334 		panic("%s: unsupported band %u\n", __func__, band);
335 		break;
336 	}
337 
338 	IMPROVE();
339 	return (0x00);
340 }
341 
342 #if 0
343 static enum ieee80211_ac_numbers
344 lkpi_ac_net_to_l80211(int ac)
345 {
346 
347 	switch (ac) {
348 	case WME_AC_VO:
349 		return (IEEE80211_AC_VO);
350 	case WME_AC_VI:
351 		return (IEEE80211_AC_VI);
352 	case WME_AC_BE:
353 		return (IEEE80211_AC_BE);
354 	case WME_AC_BK:
355 		return (IEEE80211_AC_BK);
356 	default:
357 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
358 		return (IEEE80211_AC_BE);
359 	}
360 }
361 #endif
362 
363 static enum nl80211_iftype
364 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
365 {
366 
367 	switch (opmode) {
368 	case IEEE80211_M_IBSS:
369 		return (NL80211_IFTYPE_ADHOC);
370 		break;
371 	case IEEE80211_M_STA:
372 		return (NL80211_IFTYPE_STATION);
373 		break;
374 	case IEEE80211_M_WDS:
375 		return (NL80211_IFTYPE_WDS);
376 		break;
377 	case IEEE80211_M_HOSTAP:
378 		return (NL80211_IFTYPE_AP);
379 		break;
380 	case IEEE80211_M_MONITOR:
381 		return (NL80211_IFTYPE_MONITOR);
382 		break;
383 	case IEEE80211_M_MBSS:
384 		return (NL80211_IFTYPE_MESH_POINT);
385 		break;
386 	case IEEE80211_M_AHDEMO:
387 		/* FALLTHROUGH */
388 	default:
389 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
390 		/* FALLTHROUGH */
391 	}
392 	return (NL80211_IFTYPE_UNSPECIFIED);
393 }
394 
395 #ifdef LKPI_80211_HW_CRYPTO
396 static uint32_t
397 lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
398 {
399 
400 	switch (wlan_cipher_suite) {
401 	case WLAN_CIPHER_SUITE_WEP40:
402 		return (IEEE80211_CRYPTO_WEP);
403 	case WLAN_CIPHER_SUITE_TKIP:
404 		return (IEEE80211_CRYPTO_TKIP);
405 	case WLAN_CIPHER_SUITE_CCMP:
406 		return (IEEE80211_CIPHER_AES_CCM);
407 	case WLAN_CIPHER_SUITE_WEP104:
408 		return (IEEE80211_CRYPTO_WEP);
409 	case WLAN_CIPHER_SUITE_AES_CMAC:
410 	case WLAN_CIPHER_SUITE_GCMP:
411 	case WLAN_CIPHER_SUITE_GCMP_256:
412 	case WLAN_CIPHER_SUITE_CCMP_256:
413 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
414 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
415 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
416 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
417 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
418 		break;
419 	default:
420 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
421 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
422 	}
423 
424 	return (0);
425 }
426 
427 static uint32_t
428 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
429 {
430 
431 	switch (cipher) {
432 	case IEEE80211_CIPHER_TKIP:
433 		return (WLAN_CIPHER_SUITE_TKIP);
434 	case IEEE80211_CIPHER_AES_CCM:
435 		return (WLAN_CIPHER_SUITE_CCMP);
436 	case IEEE80211_CIPHER_WEP:
437 		if (keylen < 8)
438 			return (WLAN_CIPHER_SUITE_WEP40);
439 		else
440 			return (WLAN_CIPHER_SUITE_WEP104);
441 		break;
442 	case IEEE80211_CIPHER_AES_OCB:
443 	case IEEE80211_CIPHER_TKIPMIC:
444 	case IEEE80211_CIPHER_CKIP:
445 	case IEEE80211_CIPHER_NONE:
446 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
447 		break;
448 	default:
449 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
450 	};
451 	return (0);
452 }
453 #endif
454 
455 #ifdef __notyet__
456 static enum ieee80211_sta_state
457 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
458 {
459 
460 	/*
461 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
462 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
463 	 */
464 	switch (state) {
465 	case IEEE80211_S_INIT:
466 		return (IEEE80211_STA_NOTEXIST);
467 	case IEEE80211_S_SCAN:
468 		return (IEEE80211_STA_NONE);
469 	case IEEE80211_S_AUTH:
470 		return (IEEE80211_STA_AUTH);
471 	case IEEE80211_S_ASSOC:
472 		return (IEEE80211_STA_ASSOC);
473 	case IEEE80211_S_RUN:
474 		return (IEEE80211_STA_AUTHORIZED);
475 	case IEEE80211_S_CAC:
476 	case IEEE80211_S_CSA:
477 	case IEEE80211_S_SLEEP:
478 	default:
479 		UNIMPLEMENTED;
480 	};
481 
482 	return (IEEE80211_STA_NOTEXIST);
483 }
484 #endif
485 
486 static struct linuxkpi_ieee80211_channel *
487 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
488     struct ieee80211_channel *c)
489 {
490 	struct ieee80211_hw *hw;
491 	struct linuxkpi_ieee80211_channel *channels;
492 	enum nl80211_band band;
493 	int i, nchans;
494 
495 	hw = LHW_TO_HW(lhw);
496 	band = lkpi_net80211_chan_to_nl80211_band(c);
497 	if (hw->wiphy->bands[band] == NULL)
498 		return (NULL);
499 
500 	nchans = hw->wiphy->bands[band]->n_channels;
501 	if (nchans <= 0)
502 		return (NULL);
503 
504 	channels = hw->wiphy->bands[band]->channels;
505 	for (i = 0; i < nchans; i++) {
506 		if (channels[i].hw_value == c->ic_ieee)
507 			return (&channels[i]);
508 	}
509 
510 	return (NULL);
511 }
512 
513 static struct linuxkpi_ieee80211_channel *
514 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
515 {
516 	struct linuxkpi_ieee80211_channel *chan;
517 	struct ieee80211_channel *c;
518 	struct lkpi_hw *lhw;
519 
520 	chan = NULL;
521 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
522 		c = ni->ni_chan;
523 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
524 		c = ic->ic_bsschan;
525 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
526 		c = ic->ic_curchan;
527 	else
528 		c = NULL;
529 
530 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
531 		lhw = ic->ic_softc;
532 		chan = lkpi_find_lkpi80211_chan(lhw, c);
533 	}
534 
535 	return (chan);
536 }
537 
538 struct linuxkpi_ieee80211_channel *
539 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
540 {
541 	enum nl80211_band band;
542 
543 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
544 		struct ieee80211_supported_band *supband;
545 		struct linuxkpi_ieee80211_channel *channels;
546 		int i;
547 
548 		supband = wiphy->bands[band];
549 		if (supband == NULL || supband->n_channels == 0)
550 			continue;
551 
552 		channels = supband->channels;
553 		for (i = 0; i < supband->n_channels; i++) {
554 			if (channels[i].center_freq == freq)
555 				return (&channels[i]);
556 		}
557 	}
558 
559 	return (NULL);
560 }
561 
562 #ifdef LKPI_80211_HW_CRYPTO
563 static int
564 _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
565     enum set_key_cmd cmd)
566 {
567 	struct ieee80211com *ic;
568 	struct lkpi_hw *lhw;
569 	struct ieee80211_hw *hw;
570 	struct lkpi_vif *lvif;
571 	struct ieee80211_vif *vif;
572 	struct ieee80211_sta *sta;
573 	struct ieee80211_node *ni;
574 	struct ieee80211_key_conf *kc;
575 	int error;
576 
577 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
578 
579 	ic = vap->iv_ic;
580 	lhw = ic->ic_softc;
581 	hw = LHW_TO_HW(lhw);
582 	lvif = VAP_TO_LVIF(vap);
583 	vif = LVIF_TO_VIF(lvif);
584 
585 	memset(&kc, 0, sizeof(kc));
586 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
587 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
588 	    k->wk_cipher->ic_cipher, k->wk_keylen);
589 	kc->keyidx = k->wk_keyix;
590 #if 0
591 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
592 #endif
593 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
594 	kc->keylen = k->wk_keylen;
595 	memcpy(kc->key, k->wk_key, k->wk_keylen);
596 
597 	switch (kc->cipher) {
598 	case WLAN_CIPHER_SUITE_CCMP:
599 		kc->iv_len = k->wk_cipher->ic_header;
600 		kc->icv_len = k->wk_cipher->ic_trailer;
601 		break;
602 	case WLAN_CIPHER_SUITE_TKIP:
603 	default:
604 		IMPROVE();
605 		return (0);
606 	};
607 
608 	ni = vap->iv_bss;
609 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
610 	if (sta != NULL) {
611 		struct lkpi_sta *lsta;
612 
613 		lsta = STA_TO_LSTA(sta);
614 		lsta->kc = kc;
615 	}
616 
617 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
618 	if (error != 0) {
619 		/* XXX-BZ leaking kc currently */
620 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
621 		return (0);
622 	} else {
623 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
624 		    "flags %#10x\n", __func__,
625 		    kc->keyidx, kc->hw_key_idx, kc->flags);
626 		return (1);
627 	}
628 }
629 
630 static int
631 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
632 {
633 
634 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
635 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
636 }
637 static  int
638 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
639 {
640 
641 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
642 }
643 #endif
644 
645 static u_int
646 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
647 {
648 	struct netdev_hw_addr_list *mc_list;
649 	struct netdev_hw_addr *addr;
650 
651 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
652 	    __func__, arg, sdl, cnt));
653 
654 	mc_list = arg;
655 	/* If it is on the list already skip it. */
656 	netdev_hw_addr_list_for_each(addr, mc_list) {
657 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
658 			return (0);
659 	}
660 
661 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
662 	if (addr == NULL)
663 		return (0);
664 
665 	INIT_LIST_HEAD(&addr->addr_list);
666 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
667 	/* XXX this should be a netdev function? */
668 	list_add(&addr->addr_list, &mc_list->addr_list);
669 	mc_list->count++;
670 
671 #ifdef LINUXKPI_DEBUG_80211
672 	if (linuxkpi_debug_80211 & D80211_TRACE)
673 		printf("%s:%d: mc_list count %d: added %6D\n",
674 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
675 #endif
676 
677 	return (1);
678 }
679 
680 static void
681 lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
682 {
683 	struct lkpi_hw *lhw;
684 	struct ieee80211_hw *hw;
685 	struct netdev_hw_addr_list mc_list;
686 	struct list_head *le, *next;
687 	struct netdev_hw_addr *addr;
688 	struct ieee80211vap *vap;
689 	u64 mc;
690 	unsigned int changed_flags, total_flags;
691 
692 	lhw = ic->ic_softc;
693 
694 	if (lhw->ops->prepare_multicast == NULL ||
695 	    lhw->ops->configure_filter == NULL)
696 		return;
697 
698 	if (!lhw->update_mc && !force)
699 		return;
700 
701 	changed_flags = total_flags = 0;
702 	mc_list.count = 0;
703 	INIT_LIST_HEAD(&mc_list.addr_list);
704 	if (ic->ic_allmulti == 0) {
705 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
706 			if_foreach_llmaddr(vap->iv_ifp,
707 			    lkpi_ic_update_mcast_copy, &mc_list);
708 	} else {
709 		changed_flags |= FIF_ALLMULTI;
710 	}
711 
712 	hw = LHW_TO_HW(lhw);
713 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
714 	/*
715 	 * XXX-BZ make sure to get this sorted what is a change,
716 	 * what gets all set; what was already set?
717 	 */
718 	total_flags = changed_flags;
719 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
720 
721 #ifdef LINUXKPI_DEBUG_80211
722 	if (linuxkpi_debug_80211 & D80211_TRACE)
723 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
724 		    __func__, changed_flags, mc_list.count, total_flags);
725 #endif
726 
727 	if (mc_list.count != 0) {
728 		list_for_each_safe(le, next, &mc_list.addr_list) {
729 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
730 			free(addr, M_LKPI80211);
731 			mc_list.count--;
732 		}
733 	}
734 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
735 	    __func__, &mc_list, mc_list.count));
736 }
737 
738 static enum ieee80211_bss_changed
739 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
740     struct ieee80211vap *vap, const char *_f, int _l)
741 {
742 	enum ieee80211_bss_changed bss_changed;
743 
744 	bss_changed = 0;
745 
746 #ifdef LINUXKPI_DEBUG_80211
747 	if (linuxkpi_debug_80211 & D80211_TRACE)
748 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
749 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
750 		    "sync_device_ts %u bss_changed %#08x\n",
751 			__func__, __LINE__, _f, _l,
752 			vif->cfg.assoc, vif->cfg.aid,
753 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
754 			vif->bss_conf.sync_dtim_count,
755 			(uintmax_t)vif->bss_conf.sync_tsf,
756 			vif->bss_conf.sync_device_ts,
757 			bss_changed);
758 #endif
759 
760 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
761 		vif->bss_conf.beacon_int = ni->ni_intval;
762 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
763 		if (vif->bss_conf.beacon_int < 16)
764 			vif->bss_conf.beacon_int = 16;
765 		bss_changed |= BSS_CHANGED_BEACON_INT;
766 	}
767 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
768 	    vap->iv_dtim_period > 0) {
769 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
770 		bss_changed |= BSS_CHANGED_BEACON_INFO;
771 	}
772 
773 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
774 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
775 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
776 
777 #ifdef LINUXKPI_DEBUG_80211
778 	if (linuxkpi_debug_80211 & D80211_TRACE)
779 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
780 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
781 		    "sync_device_ts %u bss_changed %#08x\n",
782 			__func__, __LINE__, _f, _l,
783 			vif->cfg.assoc, vif->cfg.aid,
784 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
785 			vif->bss_conf.sync_dtim_count,
786 			(uintmax_t)vif->bss_conf.sync_tsf,
787 			vif->bss_conf.sync_device_ts,
788 			bss_changed);
789 #endif
790 
791 	return (bss_changed);
792 }
793 
794 static void
795 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
796 {
797 	struct ieee80211_hw *hw;
798 	int error;
799 	bool cancel;
800 
801 	LKPI_80211_LHW_SCAN_LOCK(lhw);
802 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
803 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
804 	if (!cancel)
805 		return;
806 
807 	hw = LHW_TO_HW(lhw);
808 
809 	IEEE80211_UNLOCK(lhw->ic);
810 	LKPI_80211_LHW_LOCK(lhw);
811 	/* Need to cancel the scan. */
812 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
813 	LKPI_80211_LHW_UNLOCK(lhw);
814 
815 	/* Need to make sure we see ieee80211_scan_completed. */
816 	LKPI_80211_LHW_SCAN_LOCK(lhw);
817 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
818 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
819 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
820 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
821 
822 	IEEE80211_LOCK(lhw->ic);
823 
824 	if (cancel)
825 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
826 		    __func__, error, lhw, vif);
827 }
828 
829 static void
830 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
831 {
832 	struct lkpi_hw *lhw;
833 	int error;
834 	bool old;
835 
836 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
837 	if (old == new)
838 		return;
839 
840 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
841 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
842 	if (error != 0 && error != EOPNOTSUPP) {
843 		lhw = HW_TO_LHW(hw);
844 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
845 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
846 	}
847 }
848 
849 static void
850 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
851     struct lkpi_hw *lhw)
852 {
853 	sta->aid = 0;
854 	if (vif->cfg.assoc) {
855 		struct ieee80211_hw *hw;
856 		enum ieee80211_bss_changed changed;
857 
858 		lhw->update_mc = true;
859 		lkpi_update_mcast_filter(lhw->ic, true);
860 
861 		changed = 0;
862 		vif->cfg.assoc = false;
863 		vif->cfg.aid = 0;
864 		changed |= BSS_CHANGED_ASSOC;
865 		/*
866 		 * This will remove the sta from firmware for iwlwifi.
867 		 * So confusing that they use state and flags and ... ^%$%#%$^.
868 		 */
869 		IMPROVE();
870 		hw = LHW_TO_HW(lhw);
871 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
872 		    changed);
873 
874 		lkpi_hw_conf_idle(hw, true);
875 	}
876 }
877 
878 static void
879 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
880     bool dequeue_seen, bool no_emptyq)
881 {
882 	struct lkpi_txq *ltxq;
883 	int tid;
884 
885 	/* Wake up all queues to know they are allocated in the driver. */
886 	for (tid = 0; tid < nitems(sta->txq); tid++) {
887 
888 		if (tid == IEEE80211_NUM_TIDS) {
889 			IMPROVE("station specific?");
890 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
891 				continue;
892 		} else if (tid >= hw->queues)
893 			continue;
894 
895 		if (sta->txq[tid] == NULL)
896 			continue;
897 
898 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
899 		if (dequeue_seen && !ltxq->seen_dequeue)
900 			continue;
901 
902 		if (no_emptyq && skb_queue_empty(&ltxq->skbq))
903 			continue;
904 
905 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
906 	}
907 }
908 
909 /* -------------------------------------------------------------------------- */
910 
911 static int
912 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
913 {
914 
915 	return (0);
916 }
917 
918 /* lkpi_iv_newstate() handles the stop scan case generally. */
919 #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
920 
921 static int
922 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
923 {
924 	struct linuxkpi_ieee80211_channel *chan;
925 	struct lkpi_chanctx *lchanctx;
926 	struct ieee80211_chanctx_conf *conf;
927 	struct lkpi_hw *lhw;
928 	struct ieee80211_hw *hw;
929 	struct lkpi_vif *lvif;
930 	struct ieee80211_vif *vif;
931 	struct ieee80211_node *ni;
932 	struct lkpi_sta *lsta;
933 	enum ieee80211_bss_changed bss_changed;
934 	struct ieee80211_prep_tx_info prep_tx_info;
935 	uint32_t changed;
936 	int error;
937 
938 	chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
939 	if (chan == NULL) {
940 		ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
941 		return (ESRCH);
942 	}
943 
944 	lhw = vap->iv_ic->ic_softc;
945 	hw = LHW_TO_HW(lhw);
946 	lvif = VAP_TO_LVIF(vap);
947 	vif = LVIF_TO_VIF(lvif);
948 
949 	ni = ieee80211_ref_node(vap->iv_bss);
950 
951 	IEEE80211_UNLOCK(vap->iv_ic);
952 	LKPI_80211_LHW_LOCK(lhw);
953 
954 	/* Add chanctx (or if exists, change it). */
955 	if (vif->chanctx_conf != NULL) {
956 		conf = vif->chanctx_conf;
957 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
958 		IMPROVE("diff changes for changed, working on live copy, rcu");
959 	} else {
960 		/* Keep separate alloc as in Linux this is rcu managed? */
961 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
962 		    M_LKPI80211, M_WAITOK | M_ZERO);
963 		conf = &lchanctx->conf;
964 	}
965 
966 	conf->rx_chains_dynamic = 1;
967 	conf->rx_chains_static = 1;
968 	conf->radar_enabled =
969 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
970 	conf->def.chan = chan;
971 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
972 	conf->def.center_freq1 = chan->center_freq;
973 	conf->def.center_freq2 = 0;
974 	/* Responder ... */
975 	conf->min_def.chan = chan;
976 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
977 	conf->min_def.center_freq1 = chan->center_freq;
978 	conf->min_def.center_freq2 = 0;
979 	IMPROVE("currently 20_NOHT only");
980 
981 	/* Set bss info (bss_info_changed). */
982 	bss_changed = 0;
983 	vif->bss_conf.bssid = ni->ni_bssid;
984 	bss_changed |= BSS_CHANGED_BSSID;
985 	vif->bss_conf.txpower = ni->ni_txpower;
986 	bss_changed |= BSS_CHANGED_TXPOWER;
987 	vif->cfg.idle = false;
988 	bss_changed |= BSS_CHANGED_IDLE;
989 
990 	/* vif->bss_conf.basic_rates ? Where exactly? */
991 
992 	/* Should almost assert it is this. */
993 	vif->cfg.assoc = false;
994 	vif->cfg.aid = 0;
995 
996 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
997 
998 	error = 0;
999 	if (vif->chanctx_conf != NULL) {
1000 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
1001 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
1002 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
1003 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1004 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
1005 	} else {
1006 		error = lkpi_80211_mo_add_chanctx(hw, conf);
1007 		if (error == 0 || error == EOPNOTSUPP) {
1008 			vif->bss_conf.chandef.chan = conf->def.chan;
1009 			vif->bss_conf.chandef.width = conf->def.width;
1010 			vif->bss_conf.chandef.center_freq1 =
1011 			    conf->def.center_freq1;
1012 			vif->bss_conf.chandef.center_freq2 =
1013 			    conf->def.center_freq2;
1014 		} else {
1015 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1016 			    "failed: %d\n", __func__, __LINE__, error);
1017 			goto out;
1018 		}
1019 
1020 		vif->bss_conf.chanctx_conf = conf;
1021 
1022 		/* Assign vif chanctx. */
1023 		if (error == 0)
1024 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1025 			    &vif->bss_conf, conf);
1026 		if (error == EOPNOTSUPP)
1027 			error = 0;
1028 		if (error != 0) {
1029 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1030 			    "failed: %d\n", __func__, __LINE__, error);
1031 			lkpi_80211_mo_remove_chanctx(hw, conf);
1032 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1033 			free(lchanctx, M_LKPI80211);
1034 			goto out;
1035 		}
1036 	}
1037 	IMPROVE("update radiotap chan fields too");
1038 
1039 	/* RATES */
1040 	IMPROVE("bss info: not all needs to come now and rates are missing");
1041 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1042 
1043 	/*
1044 	 * This is a bandaid for now.  If we went through (*iv_update_bss)()
1045 	 * and then removed the lsta we end up here without a lsta and have
1046 	 * to manually allocate and link it in as lkpi_ic_node_alloc()/init()
1047 	 * would normally do.
1048 	 * XXX-BZ I do not like this but currently we have no good way of
1049 	 * intercepting the bss swap and state changes and packets going out
1050 	 * workflow so live with this.  It is a compat layer after all.
1051 	 */
1052 	if (ni->ni_drv_data == NULL) {
1053 		lsta = lkpi_lsta_alloc(vap, ni->ni_macaddr, hw, ni);
1054 		if (lsta == NULL) {
1055 			error = ENOMEM;
1056 			ic_printf(vap->iv_ic, "%s:%d: lkpi_lsta_alloc "
1057 			    "failed: %d\n", __func__, __LINE__, error);
1058 			goto out;
1059 		}
1060 		lsta->ni = ieee80211_ref_node(ni);
1061 	} else {
1062 		lsta = ni->ni_drv_data;
1063 	}
1064 
1065 	/* Insert the [l]sta into the list of known stations. */
1066 	LKPI_80211_LVIF_LOCK(lvif);
1067 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1068 	LKPI_80211_LVIF_UNLOCK(lvif);
1069 
1070 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
1071 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1072 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
1073 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1074 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1075 	if (error != 0) {
1076 		IMPROVE("do we need to undo the chan ctx?");
1077 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1078 		    "failed: %d\n", __func__, __LINE__, error);
1079 		goto out;
1080 	}
1081 #if 0
1082 	lsta->added_to_drv = true;	/* mo manages. */
1083 #endif
1084 
1085 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1086 
1087 	/*
1088 	 * Wakeup all queues now that sta is there so we have as much time to
1089 	 * possibly prepare the queue in the driver to be ready for the 1st
1090 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
1091 	 * is no guarantee or way to check.
1092 	 * XXX-BZ and by now we know that this does not work on all drivers
1093 	 * for all queues.
1094 	 */
1095 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
1096 
1097 	/* Start mgd_prepare_tx. */
1098 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1099 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
1100 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1101 	lsta->in_mgd = true;
1102 
1103 	/*
1104 	 * What is going to happen next:
1105 	 * - <twiddle> .. we should end up in "auth_to_assoc"
1106 	 * - event_callback
1107 	 * - update sta_state (NONE to AUTH)
1108 	 * - mgd_complete_tx
1109 	 * (ideally we'd do that on a callback for something else ...)
1110 	 */
1111 
1112 out:
1113 	LKPI_80211_LHW_UNLOCK(lhw);
1114 	IEEE80211_LOCK(vap->iv_ic);
1115 	if (ni != NULL)
1116 		ieee80211_free_node(ni);
1117 	return (error);
1118 }
1119 
1120 static int
1121 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1122 {
1123 	struct lkpi_hw *lhw;
1124 	struct ieee80211_hw *hw;
1125 	struct lkpi_vif *lvif;
1126 	struct ieee80211_vif *vif;
1127 	struct ieee80211_node *ni;
1128 	struct lkpi_sta *lsta;
1129 	struct ieee80211_sta *sta;
1130 	struct ieee80211_prep_tx_info prep_tx_info;
1131 	int error;
1132 
1133 	lhw = vap->iv_ic->ic_softc;
1134 	hw = LHW_TO_HW(lhw);
1135 	lvif = VAP_TO_LVIF(vap);
1136 	vif = LVIF_TO_VIF(lvif);
1137 
1138 	/* Keep ni around. */
1139 	ni = ieee80211_ref_node(vap->iv_bss);
1140 	lsta = ni->ni_drv_data;
1141 	sta = LSTA_TO_STA(lsta);
1142 
1143 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1144 
1145 	IEEE80211_UNLOCK(vap->iv_ic);
1146 	LKPI_80211_LHW_LOCK(lhw);
1147 
1148 	/* flush, drop. */
1149 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1150 
1151 	/* Wake tx queues to get packet(s) out. */
1152 	lkpi_wake_tx_queues(hw, sta, true, true);
1153 
1154 	/* flush, no drop */
1155 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1156 
1157 	/* End mgd_complete_tx. */
1158 	if (lsta->in_mgd) {
1159 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1160 		prep_tx_info.success = false;
1161 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1162 		lsta->in_mgd = false;
1163 	}
1164 
1165 	/* sync_rx_queues */
1166 	lkpi_80211_mo_sync_rx_queues(hw);
1167 
1168 	/* sta_pre_rcu_remove */
1169         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1170 
1171 	/* Take the station down. */
1172 
1173 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1174 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1175 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1176 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1177 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1178 	if (error != 0) {
1179 		IMPROVE("do we need to undo the chan ctx?");
1180 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1181 		    "failed: %d\n", __func__, __LINE__, error);
1182 		goto out;
1183 	}
1184 #if 0
1185 	lsta->added_to_drv = false;	/* mo manages. */
1186 #endif
1187 
1188 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1189 
1190 	lkpi_lsta_remove(lsta, lvif);
1191 
1192 	/* conf_tx */
1193 
1194 	/* Take the chan ctx down. */
1195 	if (vif->chanctx_conf != NULL) {
1196 		struct lkpi_chanctx *lchanctx;
1197 		struct ieee80211_chanctx_conf *conf;
1198 
1199 		conf = vif->chanctx_conf;
1200 		/* Remove vif context. */
1201 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1202 		/* NB: vif->chanctx_conf is NULL now. */
1203 
1204 		/* Remove chan ctx. */
1205 		lkpi_80211_mo_remove_chanctx(hw, conf);
1206 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1207 		free(lchanctx, M_LKPI80211);
1208 	}
1209 
1210 out:
1211 	LKPI_80211_LHW_UNLOCK(lhw);
1212 	IEEE80211_LOCK(vap->iv_ic);
1213 	if (ni != NULL)
1214 		ieee80211_free_node(ni);
1215 	return (error);
1216 }
1217 
1218 static int
1219 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1220 {
1221 	int error;
1222 
1223 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1224 	if (error == 0)
1225 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
1226 	return (error);
1227 }
1228 
1229 static int
1230 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1231 {
1232 	struct lkpi_hw *lhw;
1233 	struct ieee80211_hw *hw;
1234 	struct lkpi_vif *lvif;
1235 	struct ieee80211_vif *vif;
1236 	struct ieee80211_node *ni;
1237 	struct lkpi_sta *lsta;
1238 	struct ieee80211_prep_tx_info prep_tx_info;
1239 	int error;
1240 
1241 	lhw = vap->iv_ic->ic_softc;
1242 	hw = LHW_TO_HW(lhw);
1243 	lvif = VAP_TO_LVIF(vap);
1244 	vif = LVIF_TO_VIF(lvif);
1245 
1246 	IEEE80211_UNLOCK(vap->iv_ic);
1247 	LKPI_80211_LHW_LOCK(lhw);
1248 	ni = NULL;
1249 
1250 	/* Finish auth. */
1251 	IMPROVE("event callback");
1252 
1253 	/* Update sta_state (NONE to AUTH). */
1254 	ni = ieee80211_ref_node(vap->iv_bss);
1255 	lsta = ni->ni_drv_data;
1256 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1257 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1258 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1259 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1260 	if (error != 0) {
1261 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1262 		    "failed: %d\n", __func__, __LINE__, error);
1263 		goto out;
1264 	}
1265 
1266 	/* End mgd_complete_tx. */
1267 	if (lsta->in_mgd) {
1268 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1269 		prep_tx_info.success = true;
1270 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1271 		lsta->in_mgd = false;
1272 	}
1273 
1274 	/* Now start assoc. */
1275 
1276 	/* Start mgd_prepare_tx. */
1277 	if (!lsta->in_mgd) {
1278 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1279 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1280 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1281 		lsta->in_mgd = true;
1282 	}
1283 
1284 	/* Wake tx queue to get packet out. */
1285 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), true, true);
1286 
1287 	/*
1288 	 * <twiddle> .. we end up in "assoc_to_run"
1289 	 * - update sta_state (AUTH to ASSOC)
1290 	 * - conf_tx [all]
1291 	 * - bss_info_changed (assoc, aid, ssid, ..)
1292 	 * - change_chanctx (if needed)
1293 	 * - event_callback
1294 	 * - mgd_complete_tx
1295 	 */
1296 
1297 out:
1298 	LKPI_80211_LHW_UNLOCK(lhw);
1299 	IEEE80211_LOCK(vap->iv_ic);
1300 	if (ni != NULL)
1301 		ieee80211_free_node(ni);
1302 	return (error);
1303 }
1304 
1305 /* auth_to_auth, assoc_to_assoc. */
1306 static int
1307 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1308 {
1309 	struct lkpi_hw *lhw;
1310 	struct ieee80211_hw *hw;
1311 	struct lkpi_vif *lvif;
1312 	struct ieee80211_vif *vif;
1313 	struct ieee80211_node *ni;
1314 	struct lkpi_sta *lsta;
1315 	struct ieee80211_prep_tx_info prep_tx_info;
1316 
1317 	lhw = vap->iv_ic->ic_softc;
1318 	hw = LHW_TO_HW(lhw);
1319 	lvif = VAP_TO_LVIF(vap);
1320 	vif = LVIF_TO_VIF(lvif);
1321 
1322 	ni = ieee80211_ref_node(vap->iv_bss);
1323 
1324 	IEEE80211_UNLOCK(vap->iv_ic);
1325 	LKPI_80211_LHW_LOCK(lhw);
1326 	lsta = ni->ni_drv_data;
1327 
1328 	IMPROVE("event callback?");
1329 
1330 	/* End mgd_complete_tx. */
1331 	if (lsta->in_mgd) {
1332 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1333 		prep_tx_info.success = false;
1334 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1335 		lsta->in_mgd = false;
1336 	}
1337 
1338 	/* Now start assoc. */
1339 
1340 	/* Start mgd_prepare_tx. */
1341 	if (!lsta->in_mgd) {
1342 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1343 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1344 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1345 		lsta->in_mgd = true;
1346 	}
1347 
1348 	LKPI_80211_LHW_UNLOCK(lhw);
1349 	IEEE80211_LOCK(vap->iv_ic);
1350 	if (ni != NULL)
1351 		ieee80211_free_node(ni);
1352 
1353 	return (0);
1354 }
1355 
1356 static int
1357 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1358 {
1359 	struct lkpi_hw *lhw;
1360 	struct ieee80211_hw *hw;
1361 	struct lkpi_vif *lvif;
1362 	struct ieee80211_vif *vif;
1363 	struct ieee80211_node *ni;
1364 	struct lkpi_sta *lsta;
1365 	struct ieee80211_sta *sta;
1366 	struct ieee80211_prep_tx_info prep_tx_info;
1367 	enum ieee80211_bss_changed bss_changed;
1368 	int error;
1369 
1370 	lhw = vap->iv_ic->ic_softc;
1371 	hw = LHW_TO_HW(lhw);
1372 	lvif = VAP_TO_LVIF(vap);
1373 	vif = LVIF_TO_VIF(lvif);
1374 
1375 	/* Keep ni around. */
1376 	ni = ieee80211_ref_node(vap->iv_bss);
1377 	lsta = ni->ni_drv_data;
1378 	sta = LSTA_TO_STA(lsta);
1379 
1380 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1381 
1382 	IEEE80211_UNLOCK(vap->iv_ic);
1383 	LKPI_80211_LHW_LOCK(lhw);
1384 
1385 	/* flush, drop. */
1386 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1387 
1388 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1389 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1390 	    !lsta->in_mgd) {
1391 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1392 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1393 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1394 		lsta->in_mgd = true;
1395 	}
1396 
1397 	LKPI_80211_LHW_UNLOCK(lhw);
1398 	IEEE80211_LOCK(vap->iv_ic);
1399 
1400 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1401 	error = lvif->iv_newstate(vap, nstate, arg);
1402 	if (error != 0) {
1403 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1404 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1405 		goto outni;
1406 	}
1407 
1408 	IEEE80211_UNLOCK(vap->iv_ic);
1409 	LKPI_80211_LHW_LOCK(lhw);
1410 
1411 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1412 
1413 	/* Wake tx queues to get packet(s) out. */
1414 	lkpi_wake_tx_queues(hw, sta, true, true);
1415 
1416 	/* flush, no drop */
1417 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1418 
1419 	/* End mgd_complete_tx. */
1420 	if (lsta->in_mgd) {
1421 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1422 		prep_tx_info.success = false;
1423 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1424 		lsta->in_mgd = false;
1425 	}
1426 
1427 	/* sync_rx_queues */
1428 	lkpi_80211_mo_sync_rx_queues(hw);
1429 
1430 	/* sta_pre_rcu_remove */
1431         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1432 
1433 	/* Take the station down. */
1434 
1435 	/* Update sta and change state (from AUTH) to NONE. */
1436 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1437 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1438 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1439 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1440 	if (error != 0) {
1441 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1442 		    "failed: %d\n", __func__, __LINE__, error);
1443 		goto out;
1444 	}
1445 
1446 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1447 
1448 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1449 	/*
1450 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
1451 	 * as otherwise drivers (iwlwifi at least) will silently not remove
1452 	 * the sta from the firmware and when we will add a new one trigger
1453 	 * a fw assert.
1454 	 */
1455 	lkpi_disassoc(sta, vif, lhw);
1456 
1457 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1458 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1459 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1460 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1461 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1462 	if (error != 0) {
1463 		IMPROVE("do we need to undo the chan ctx?");
1464 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1465 		    "failed: %d\n", __func__, __LINE__, error);
1466 		goto out;
1467 	}
1468 
1469 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1470 
1471 	IMPROVE("Any bss_info changes to announce?");
1472 	bss_changed = 0;
1473 	vif->bss_conf.qos = 0;
1474 	bss_changed |= BSS_CHANGED_QOS;
1475 	vif->cfg.ssid_len = 0;
1476 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1477 	bss_changed |= BSS_CHANGED_BSSID;
1478 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1479 
1480 	lkpi_lsta_remove(lsta, lvif);
1481 
1482 	/* conf_tx */
1483 
1484 	/* Take the chan ctx down. */
1485 	if (vif->chanctx_conf != NULL) {
1486 		struct lkpi_chanctx *lchanctx;
1487 		struct ieee80211_chanctx_conf *conf;
1488 
1489 		conf = vif->chanctx_conf;
1490 		/* Remove vif context. */
1491 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1492 		/* NB: vif->chanctx_conf is NULL now. */
1493 
1494 		/* Remove chan ctx. */
1495 		lkpi_80211_mo_remove_chanctx(hw, conf);
1496 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1497 		free(lchanctx, M_LKPI80211);
1498 	}
1499 
1500 	error = EALREADY;
1501 out:
1502 	LKPI_80211_LHW_UNLOCK(lhw);
1503 	IEEE80211_LOCK(vap->iv_ic);
1504 outni:
1505 	if (ni != NULL)
1506 		ieee80211_free_node(ni);
1507 	return (error);
1508 }
1509 
1510 static int
1511 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1512 {
1513 	int error;
1514 
1515 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1516 	if (error != 0 && error != EALREADY)
1517 		return (error);
1518 
1519 	/* At this point iv_bss is long a new node! */
1520 
1521 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1522 	return (error);
1523 }
1524 
1525 static int
1526 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1527 {
1528 	int error;
1529 
1530 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1531 	return (error);
1532 }
1533 
1534 static int
1535 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1536 {
1537 	int error;
1538 
1539 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1540 	return (error);
1541 }
1542 
1543 static int
1544 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1545 {
1546 	struct lkpi_hw *lhw;
1547 	struct ieee80211_hw *hw;
1548 	struct lkpi_vif *lvif;
1549 	struct ieee80211_vif *vif;
1550 	struct ieee80211_node *ni;
1551 	struct lkpi_sta *lsta;
1552 	struct ieee80211_sta *sta;
1553 	struct ieee80211_prep_tx_info prep_tx_info;
1554 	enum ieee80211_bss_changed bss_changed;
1555 	int error;
1556 
1557 	lhw = vap->iv_ic->ic_softc;
1558 	hw = LHW_TO_HW(lhw);
1559 	lvif = VAP_TO_LVIF(vap);
1560 	vif = LVIF_TO_VIF(lvif);
1561 
1562 	IEEE80211_UNLOCK(vap->iv_ic);
1563 	LKPI_80211_LHW_LOCK(lhw);
1564 	ni = NULL;
1565 
1566 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1567 	    "and to lesser extend ieee80211_notify_node_join");
1568 
1569 	/* Finish assoc. */
1570 	/* Update sta_state (AUTH to ASSOC) and set aid. */
1571 	ni = ieee80211_ref_node(vap->iv_bss);
1572 	lsta = ni->ni_drv_data;
1573 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1574 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1575 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1576 	sta = LSTA_TO_STA(lsta);
1577 	sta->aid = IEEE80211_NODE_AID(ni);
1578 #ifdef LKPI_80211_WME
1579 	if (vap->iv_flags & IEEE80211_F_WME)
1580 		sta->wme = true;
1581 #endif
1582 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1583 	if (error != 0) {
1584 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1585 		    "failed: %d\n", __func__, __LINE__, error);
1586 		goto out;
1587 	}
1588 
1589 	IMPROVE("wme / conf_tx [all]");
1590 
1591 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1592 	bss_changed = 0;
1593 #ifdef LKPI_80211_WME
1594 	bss_changed |= lkpi_wme_update(lhw, vap, true);
1595 #endif
1596 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
1597 		vif->cfg.assoc = true;
1598 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
1599 		bss_changed |= BSS_CHANGED_ASSOC;
1600 	}
1601 	/* We set SSID but this is not BSSID! */
1602 	vif->cfg.ssid_len = ni->ni_esslen;
1603 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
1604 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
1605 	    vif->bss_conf.use_short_preamble) {
1606 		vif->bss_conf.use_short_preamble ^= 1;
1607 		/* bss_changed |= BSS_CHANGED_??? */
1608 	}
1609 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
1610 	    vif->bss_conf.use_short_slot) {
1611 		vif->bss_conf.use_short_slot ^= 1;
1612 		/* bss_changed |= BSS_CHANGED_??? */
1613 	}
1614 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
1615 	    vif->bss_conf.qos) {
1616 		vif->bss_conf.qos ^= 1;
1617 		bss_changed |= BSS_CHANGED_QOS;
1618 	}
1619 
1620 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1621 
1622 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1623 
1624 	/* - change_chanctx (if needed)
1625 	 * - event_callback
1626 	 */
1627 
1628 	/* End mgd_complete_tx. */
1629 	if (lsta->in_mgd) {
1630 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1631 		prep_tx_info.success = true;
1632 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1633 		lsta->in_mgd = false;
1634 	}
1635 
1636 	lkpi_hw_conf_idle(hw, false);
1637 
1638 	/*
1639 	 * And then:
1640 	 * - (more packets)?
1641 	 * - set_key
1642 	 * - set_default_unicast_key
1643 	 * - set_key (?)
1644 	 * - ipv6_addr_change (?)
1645 	 */
1646 	/* Prepare_multicast && configure_filter. */
1647 	lhw->update_mc = true;
1648 	lkpi_update_mcast_filter(vap->iv_ic, true);
1649 
1650 	if (!ieee80211_node_is_authorized(ni)) {
1651 		IMPROVE("net80211 does not consider node authorized");
1652 	}
1653 
1654 	/* Update sta_state (ASSOC to AUTHORIZED). */
1655 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1656 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1657 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1658 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
1659 	if (error != 0) {
1660 		IMPROVE("undo some changes?");
1661 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
1662 		    "failed: %d\n", __func__, __LINE__, error);
1663 		goto out;
1664 	}
1665 
1666 	/* - drv_config (?)
1667 	 * - bss_info_changed
1668 	 * - set_rekey_data (?)
1669 	 *
1670 	 * And now we should be passing packets.
1671 	 */
1672 	IMPROVE("Need that bssid setting, and the keys");
1673 
1674 	bss_changed = 0;
1675 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1676 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1677 
1678 out:
1679 	LKPI_80211_LHW_UNLOCK(lhw);
1680 	IEEE80211_LOCK(vap->iv_ic);
1681 	if (ni != NULL)
1682 		ieee80211_free_node(ni);
1683 	return (error);
1684 }
1685 
1686 static int
1687 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1688 {
1689 	int error;
1690 
1691 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
1692 	if (error == 0)
1693 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
1694 	return (error);
1695 }
1696 
1697 static int
1698 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1699 {
1700 	struct lkpi_hw *lhw;
1701 	struct ieee80211_hw *hw;
1702 	struct lkpi_vif *lvif;
1703 	struct ieee80211_vif *vif;
1704 	struct ieee80211_node *ni;
1705 	struct lkpi_sta *lsta;
1706 	struct ieee80211_sta *sta;
1707 	struct ieee80211_prep_tx_info prep_tx_info;
1708 #if 0
1709 	enum ieee80211_bss_changed bss_changed;
1710 #endif
1711 	int error;
1712 
1713 	lhw = vap->iv_ic->ic_softc;
1714 	hw = LHW_TO_HW(lhw);
1715 	lvif = VAP_TO_LVIF(vap);
1716 	vif = LVIF_TO_VIF(lvif);
1717 
1718 	/* Keep ni around. */
1719 	ni = ieee80211_ref_node(vap->iv_bss);
1720 	lsta = ni->ni_drv_data;
1721 	sta = LSTA_TO_STA(lsta);
1722 
1723 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1724 
1725 	IEEE80211_UNLOCK(vap->iv_ic);
1726 	LKPI_80211_LHW_LOCK(lhw);
1727 
1728 	/* flush, drop. */
1729 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1730 
1731 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1732 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1733 	    !lsta->in_mgd) {
1734 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1735 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1736 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1737 		lsta->in_mgd = true;
1738 	}
1739 
1740 	LKPI_80211_LHW_UNLOCK(lhw);
1741 	IEEE80211_LOCK(vap->iv_ic);
1742 
1743 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1744 	error = lvif->iv_newstate(vap, nstate, arg);
1745 	if (error != 0) {
1746 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1747 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1748 		goto outni;
1749 	}
1750 
1751 	IEEE80211_UNLOCK(vap->iv_ic);
1752 	LKPI_80211_LHW_LOCK(lhw);
1753 
1754 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1755 
1756 	/* Wake tx queues to get packet(s) out. */
1757 	lkpi_wake_tx_queues(hw, sta, true, true);
1758 
1759 	/* flush, no drop */
1760 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1761 
1762 	/* End mgd_complete_tx. */
1763 	if (lsta->in_mgd) {
1764 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1765 		prep_tx_info.success = false;
1766 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1767 		lsta->in_mgd = false;
1768 	}
1769 
1770 #if 0
1771 	/* sync_rx_queues */
1772 	lkpi_80211_mo_sync_rx_queues(hw);
1773 
1774 	/* sta_pre_rcu_remove */
1775         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1776 #endif
1777 
1778 	/* Take the station down. */
1779 
1780 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1781 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1782 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1783 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1784 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1785 	if (error != 0) {
1786 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1787 		    "failed: %d\n", __func__, __LINE__, error);
1788 		goto out;
1789 	}
1790 
1791 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1792 
1793 	/* Update sta_state (ASSOC to AUTH). */
1794 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1795 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1796 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1797 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1798 	if (error != 0) {
1799 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1800 		    "failed: %d\n", __func__, __LINE__, error);
1801 		goto out;
1802 	}
1803 
1804 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1805 
1806 #if 0
1807 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1808 	lkpi_disassoc(sta, vif, lhw);
1809 #endif
1810 
1811 	error = EALREADY;
1812 out:
1813 	LKPI_80211_LHW_UNLOCK(lhw);
1814 	IEEE80211_LOCK(vap->iv_ic);
1815 outni:
1816 	if (ni != NULL)
1817 		ieee80211_free_node(ni);
1818 	return (error);
1819 }
1820 
1821 static int
1822 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1823 {
1824 	struct lkpi_hw *lhw;
1825 	struct ieee80211_hw *hw;
1826 	struct lkpi_vif *lvif;
1827 	struct ieee80211_vif *vif;
1828 	struct ieee80211_node *ni;
1829 	struct lkpi_sta *lsta;
1830 	struct ieee80211_sta *sta;
1831 	struct ieee80211_prep_tx_info prep_tx_info;
1832 	enum ieee80211_bss_changed bss_changed;
1833 	int error;
1834 
1835 	lhw = vap->iv_ic->ic_softc;
1836 	hw = LHW_TO_HW(lhw);
1837 	lvif = VAP_TO_LVIF(vap);
1838 	vif = LVIF_TO_VIF(lvif);
1839 
1840 	/* Keep ni around. */
1841 	ni = ieee80211_ref_node(vap->iv_bss);
1842 	lsta = ni->ni_drv_data;
1843 	sta = LSTA_TO_STA(lsta);
1844 
1845 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1846 
1847 	IEEE80211_UNLOCK(vap->iv_ic);
1848 	LKPI_80211_LHW_LOCK(lhw);
1849 
1850 	/* flush, drop. */
1851 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1852 
1853 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1854 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1855 	    !lsta->in_mgd) {
1856 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1857 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1858 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1859 		lsta->in_mgd = true;
1860 	}
1861 
1862 	LKPI_80211_LHW_UNLOCK(lhw);
1863 	IEEE80211_LOCK(vap->iv_ic);
1864 
1865 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1866 	error = lvif->iv_newstate(vap, nstate, arg);
1867 	if (error != 0) {
1868 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1869 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1870 		goto outni;
1871 	}
1872 
1873 	IEEE80211_UNLOCK(vap->iv_ic);
1874 	LKPI_80211_LHW_LOCK(lhw);
1875 
1876 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1877 
1878 	/* Wake tx queues to get packet(s) out. */
1879 	lkpi_wake_tx_queues(hw, sta, true, true);
1880 
1881 	/* flush, no drop */
1882 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1883 
1884 	/* End mgd_complete_tx. */
1885 	if (lsta->in_mgd) {
1886 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1887 		prep_tx_info.success = false;
1888 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1889 		lsta->in_mgd = false;
1890 	}
1891 
1892 	/* sync_rx_queues */
1893 	lkpi_80211_mo_sync_rx_queues(hw);
1894 
1895 	/* sta_pre_rcu_remove */
1896         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1897 
1898 	/* Take the station down. */
1899 
1900 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1901 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1902 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1903 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1904 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1905 	if (error != 0) {
1906 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1907 		    "failed: %d\n", __func__, __LINE__, error);
1908 		goto out;
1909 	}
1910 
1911 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1912 
1913 	/* Update sta_state (ASSOC to AUTH). */
1914 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1915 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1916 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1917 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1918 	if (error != 0) {
1919 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1920 		    "failed: %d\n", __func__, __LINE__, error);
1921 		goto out;
1922 	}
1923 
1924 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1925 
1926 	/* Update sta and change state (from AUTH) to NONE. */
1927 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1928 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1929 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1930 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1931 	if (error != 0) {
1932 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1933 		    "failed: %d\n", __func__, __LINE__, error);
1934 		goto out;
1935 	}
1936 
1937 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1938 
1939 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1940 	/*
1941 	 * One would expect this to happen when going off AUTHORIZED.
1942 	 * See comment there; removes the sta from fw.
1943 	 */
1944 	lkpi_disassoc(sta, vif, lhw);
1945 
1946 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1947 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1948 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1949 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1950 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1951 	if (error != 0) {
1952 		IMPROVE("do we need to undo the chan ctx?");
1953 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1954 		    "failed: %d\n", __func__, __LINE__, error);
1955 		goto out;
1956 	}
1957 
1958 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1959 
1960 	IMPROVE("Any bss_info changes to announce?");
1961 	bss_changed = 0;
1962 	vif->bss_conf.qos = 0;
1963 	bss_changed |= BSS_CHANGED_QOS;
1964 	vif->cfg.ssid_len = 0;
1965 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1966 	bss_changed |= BSS_CHANGED_BSSID;
1967 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1968 
1969 	lkpi_lsta_remove(lsta, lvif);
1970 
1971 	/* conf_tx */
1972 
1973 	/* Take the chan ctx down. */
1974 	if (vif->chanctx_conf != NULL) {
1975 		struct lkpi_chanctx *lchanctx;
1976 		struct ieee80211_chanctx_conf *conf;
1977 
1978 		conf = vif->chanctx_conf;
1979 		/* Remove vif context. */
1980 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1981 		/* NB: vif->chanctx_conf is NULL now. */
1982 
1983 		/* Remove chan ctx. */
1984 		lkpi_80211_mo_remove_chanctx(hw, conf);
1985 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1986 		free(lchanctx, M_LKPI80211);
1987 	}
1988 
1989 	error = EALREADY;
1990 out:
1991 	LKPI_80211_LHW_UNLOCK(lhw);
1992 	IEEE80211_LOCK(vap->iv_ic);
1993 outni:
1994 	if (ni != NULL)
1995 		ieee80211_free_node(ni);
1996 	return (error);
1997 }
1998 
1999 static int
2000 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2001 {
2002 
2003 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2004 }
2005 
2006 static int
2007 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2008 {
2009 	int error;
2010 
2011 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2012 	if (error != 0 && error != EALREADY)
2013 		return (error);
2014 
2015 	/* At this point iv_bss is long a new node! */
2016 
2017 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2018 	return (error);
2019 }
2020 
2021 /* -------------------------------------------------------------------------- */
2022 
2023 /*
2024  * The matches the documented state changes in net80211::sta_newstate().
2025  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
2026  * there are "invalid" (so there is a room for failure here).
2027  */
2028 struct fsm_state {
2029 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
2030 	enum ieee80211_state ostate;
2031 	enum ieee80211_state nstate;
2032 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
2033 } sta_state_fsm[] = {
2034 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
2035 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
2036 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2037 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2038 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
2039 
2040 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2041 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2042 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
2043 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2044 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
2045 
2046 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2047 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2048 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2049 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2050 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
2051 
2052 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2053 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2054 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
2055 
2056 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
2057 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
2058 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
2059 
2060 	/* Dummy at the end without handler. */
2061 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
2062 };
2063 
2064 static int
2065 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2066 {
2067 	struct ieee80211com *ic;
2068 	struct lkpi_hw *lhw;
2069 	struct lkpi_vif *lvif;
2070 	struct ieee80211_vif *vif;
2071 	struct fsm_state *s;
2072 	enum ieee80211_state ostate;
2073 	int error;
2074 
2075 	ic = vap->iv_ic;
2076 	IEEE80211_LOCK_ASSERT(ic);
2077 	ostate = vap->iv_state;
2078 
2079 #ifdef LINUXKPI_DEBUG_80211
2080 	if (linuxkpi_debug_80211 & D80211_TRACE)
2081 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
2082 		    __func__, __LINE__, vap, nstate, arg);
2083 #endif
2084 
2085 	if (vap->iv_opmode == IEEE80211_M_STA) {
2086 
2087 		lhw = ic->ic_softc;
2088 		lvif = VAP_TO_LVIF(vap);
2089 		vif = LVIF_TO_VIF(lvif);
2090 
2091 		/* No need to replicate this in most state handlers. */
2092 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2093 			lkpi_stop_hw_scan(lhw, vif);
2094 
2095 		s = sta_state_fsm;
2096 
2097 	} else {
2098 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2099 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2100 		return (ENOSYS);
2101 	}
2102 
2103 	error = 0;
2104 	for (; s->handler != NULL; s++) {
2105 		if (ostate == s->ostate && nstate == s->nstate) {
2106 #ifdef LINUXKPI_DEBUG_80211
2107 			if (linuxkpi_debug_80211 & D80211_TRACE)
2108 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2109 				    " %d (%s): arg %d.\n", __func__,
2110 				    ostate, ieee80211_state_name[ostate],
2111 				    nstate, ieee80211_state_name[nstate], arg);
2112 #endif
2113 			error = s->handler(vap, nstate, arg);
2114 			break;
2115 		}
2116 	}
2117 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2118 
2119 	if (s->handler == NULL) {
2120 		IMPROVE("turn this into a KASSERT\n");
2121 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
2122 		    "%d (%s) -> %d (%s)\n", __func__,
2123 		    ostate, ieee80211_state_name[ostate],
2124 		    nstate, ieee80211_state_name[nstate]);
2125 		return (ENOSYS);
2126 	}
2127 
2128 	if (error == EALREADY) {
2129 #ifdef LINUXKPI_DEBUG_80211
2130 		if (linuxkpi_debug_80211 & D80211_TRACE)
2131 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2132 			    "%d (%s): iv_newstate already handled: %d.\n",
2133 			    __func__, ostate, ieee80211_state_name[ostate],
2134 			    nstate, ieee80211_state_name[nstate], error);
2135 #endif
2136 		return (0);
2137 	}
2138 
2139 	if (error != 0) {
2140 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
2141 		    "%d (%s) -> %d (%s)\n", __func__, error,
2142 		    ostate, ieee80211_state_name[ostate],
2143 		    nstate, ieee80211_state_name[nstate]);
2144 		return (error);
2145 	}
2146 
2147 #ifdef LINUXKPI_DEBUG_80211
2148 	if (linuxkpi_debug_80211 & D80211_TRACE)
2149 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2150 		    "calling net80211 parent\n",
2151 		    __func__, __LINE__, vap, nstate, arg);
2152 #endif
2153 
2154 	return (lvif->iv_newstate(vap, nstate, arg));
2155 }
2156 
2157 /* -------------------------------------------------------------------------- */
2158 
2159 /*
2160  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2161  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2162  * new node without us knowing and thus our ni/lsta are out of sync.
2163  */
2164 static struct ieee80211_node *
2165 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2166 {
2167 	struct lkpi_vif *lvif;
2168 	struct ieee80211_node *obss;
2169 	struct lkpi_sta *lsta;
2170 	struct ieee80211_sta *sta;
2171 
2172 	obss = vap->iv_bss;
2173 
2174 #ifdef LINUXKPI_DEBUG_80211
2175 	if (linuxkpi_debug_80211 & D80211_TRACE)
2176 		ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
2177 		    "ni %p ni_drv_data %p\n", __func__,
2178 		    obss, (obss != NULL) ? obss->ni_drv_data : NULL,
2179 		    ni, (ni != NULL) ? ni->ni_drv_data : NULL);
2180 #endif
2181 
2182 	/* Nothing to copy from.  Just return. */
2183 	if (obss == NULL || obss->ni_drv_data == NULL)
2184 		goto out;
2185 
2186 	/* Nothing to copy to.  Just return. */
2187 	IMPROVE("clearing the obss might still be needed?");
2188 	if (ni == NULL)
2189 		goto out;
2190 
2191 	/* Nothing changed? panic? */
2192 	if (obss == ni)
2193 		goto out;
2194 
2195 	lsta = obss->ni_drv_data;
2196 	obss->ni_drv_data = ni->ni_drv_data;
2197 	ni->ni_drv_data = lsta;
2198 	if (lsta != NULL) {
2199 		lsta->ni = ni;
2200 		sta = LSTA_TO_STA(lsta);
2201 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2202 		IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
2203 	}
2204 	lsta = obss->ni_drv_data;
2205 	if (lsta != NULL) {
2206 		lsta->ni = obss;
2207 		sta = LSTA_TO_STA(lsta);
2208 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2209 		IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
2210 	}
2211 
2212 out:
2213 	lvif = VAP_TO_LVIF(vap);
2214 	return (lvif->iv_update_bss(vap, ni));
2215 }
2216 
2217 #ifdef LKPI_80211_WME
2218 static int
2219 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
2220 {
2221 	struct ieee80211com *ic;
2222 	struct ieee80211_hw *hw;
2223 	struct lkpi_vif *lvif;
2224 	struct ieee80211_vif *vif;
2225 	struct chanAccParams chp;
2226 	struct wmeParams wmeparr[WME_NUM_AC];
2227 	struct ieee80211_tx_queue_params txqp;
2228 	enum ieee80211_bss_changed changed;
2229 	int error;
2230 	uint16_t ac;
2231 
2232 	IMPROVE();
2233 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2234 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2235 
2236 	if (vap == NULL)
2237 		return (0);
2238 
2239 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
2240 		return (0);
2241 
2242 	if (lhw->ops->conf_tx == NULL)
2243 		return (0);
2244 
2245 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
2246 		lhw->update_wme = true;
2247 		return (0);
2248 	}
2249 	lhw->update_wme = false;
2250 
2251 	ic = lhw->ic;
2252 	ieee80211_wme_ic_getparams(ic, &chp);
2253 	IEEE80211_LOCK(ic);
2254 	for (ac = 0; ac < WME_NUM_AC; ac++)
2255 		wmeparr[ac] = chp.cap_wmeParams[ac];
2256 	IEEE80211_UNLOCK(ic);
2257 
2258 	hw = LHW_TO_HW(lhw);
2259 	lvif = VAP_TO_LVIF(vap);
2260 	vif = LVIF_TO_VIF(lvif);
2261 
2262 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2263 	LKPI_80211_LHW_LOCK(lhw);
2264 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2265 		struct wmeParams *wmep;
2266 
2267 		wmep = &wmeparr[ac];
2268 		bzero(&txqp, sizeof(txqp));
2269 		txqp.cw_min = wmep->wmep_logcwmin;
2270 		txqp.cw_max = wmep->wmep_logcwmax;
2271 		txqp.txop = wmep->wmep_txopLimit;
2272 		txqp.aifs = wmep->wmep_aifsn;
2273 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2274 		if (error != 0)
2275 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2276 			    __func__, ac, error);
2277 	}
2278 	LKPI_80211_LHW_UNLOCK(lhw);
2279 	changed = BSS_CHANGED_QOS;
2280 	if (!planned)
2281 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2282 
2283 	return (changed);
2284 }
2285 #endif
2286 
2287 static int
2288 lkpi_ic_wme_update(struct ieee80211com *ic)
2289 {
2290 #ifdef LKPI_80211_WME
2291 	struct ieee80211vap *vap;
2292 	struct lkpi_hw *lhw;
2293 
2294 	IMPROVE("Use the per-VAP callback in net80211.");
2295 	vap = TAILQ_FIRST(&ic->ic_vaps);
2296 	if (vap == NULL)
2297 		return (0);
2298 
2299 	lhw = ic->ic_softc;
2300 
2301 	lkpi_wme_update(lhw, vap, false);
2302 #endif
2303 	return (0);	/* unused */
2304 }
2305 
2306 static struct ieee80211vap *
2307 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2308     int unit, enum ieee80211_opmode opmode, int flags,
2309     const uint8_t bssid[IEEE80211_ADDR_LEN],
2310     const uint8_t mac[IEEE80211_ADDR_LEN])
2311 {
2312 	struct lkpi_hw *lhw;
2313 	struct ieee80211_hw *hw;
2314 	struct lkpi_vif *lvif;
2315 	struct ieee80211vap *vap;
2316 	struct ieee80211_vif *vif;
2317 	struct ieee80211_tx_queue_params txqp;
2318 	enum ieee80211_bss_changed changed;
2319 	size_t len;
2320 	int error, i;
2321 	uint16_t ac;
2322 
2323 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
2324 		return (NULL);
2325 
2326 	lhw = ic->ic_softc;
2327 	hw = LHW_TO_HW(lhw);
2328 
2329 	len = sizeof(*lvif);
2330 	len += hw->vif_data_size;	/* vif->drv_priv */
2331 
2332 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2333 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2334 	TAILQ_INIT(&lvif->lsta_head);
2335 	vap = LVIF_TO_VAP(lvif);
2336 
2337 	vif = LVIF_TO_VIF(lvif);
2338 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2339 	vif->p2p = false;
2340 	vif->probe_req_reg = false;
2341 	vif->type = lkpi_opmode_to_vif_type(opmode);
2342 	lvif->wdev.iftype = vif->type;
2343 	/* Need to fill in other fields as well. */
2344 	IMPROVE();
2345 
2346 	/* XXX-BZ hardcoded for now! */
2347 #if 1
2348 	vif->chanctx_conf = NULL;
2349 	vif->bss_conf.vif = vif;
2350 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
2351 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
2352 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
2353 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2354 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2355 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
2356 	vif->bss_conf.qos = false;
2357 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
2358 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2359 	vif->cfg.aid = 0;
2360 	vif->cfg.assoc = false;
2361 	vif->cfg.idle = true;
2362 	vif->cfg.ps = false;
2363 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2364 	/*
2365 	 * We need to initialize it to something as the bss_info_changed call
2366 	 * will try to copy from it in iwlwifi and NULL is a panic.
2367 	 * We will set the proper one in scan_to_auth() before being assoc.
2368 	 */
2369 	vif->bss_conf.bssid = ieee80211broadcastaddr;
2370 #endif
2371 #if 0
2372 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2373 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2374 	vif->bss_conf.beacon_int = ic->ic_bintval;
2375 	/* iwlwifi bug. */
2376 	if (vif->bss_conf.beacon_int < 16)
2377 		vif->bss_conf.beacon_int = 16;
2378 #endif
2379 
2380 	/* Link Config */
2381 	vif->link_conf[0] = &vif->bss_conf;
2382 	for (i = 0; i < nitems(vif->link_conf); i++) {
2383 		IMPROVE("more than 1 link one day");
2384 	}
2385 
2386 	/* Setup queue defaults; driver may override in (*add_interface). */
2387 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2388 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2389 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2390 		else if (hw->queues >= IEEE80211_NUM_ACS)
2391 			vif->hw_queue[i] = i;
2392 		else
2393 			vif->hw_queue[i] = 0;
2394 
2395 		/* Initialize the queue to running. Stopped? */
2396 		lvif->hw_queue_stopped[i] = false;
2397 	}
2398 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2399 
2400 	IMPROVE();
2401 
2402 	error = lkpi_80211_mo_start(hw);
2403 	if (error != 0) {
2404 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
2405 		mtx_destroy(&lvif->mtx);
2406 		free(lvif, M_80211_VAP);
2407 		return (NULL);
2408 	}
2409 
2410 	error = lkpi_80211_mo_add_interface(hw, vif);
2411 	if (error != 0) {
2412 		IMPROVE();	/* XXX-BZ mo_stop()? */
2413 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
2414 		mtx_destroy(&lvif->mtx);
2415 		free(lvif, M_80211_VAP);
2416 		return (NULL);
2417 	}
2418 
2419 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2420 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2421 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2422 
2423 	/* Set bss_info. */
2424 	changed = 0;
2425 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2426 
2427 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2428 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2429 	LKPI_80211_LHW_LOCK(lhw);
2430 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2431 
2432 		bzero(&txqp, sizeof(txqp));
2433 		txqp.cw_min = 15;
2434 		txqp.cw_max = 1023;
2435 		txqp.txop = 0;
2436 		txqp.aifs = 2;
2437 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2438 		if (error != 0)
2439 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2440 			    __func__, ac, error);
2441 	}
2442 	LKPI_80211_LHW_UNLOCK(lhw);
2443 	changed = BSS_CHANGED_QOS;
2444 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2445 
2446 	/* Force MC init. */
2447 	lkpi_update_mcast_filter(ic, true);
2448 
2449 	IMPROVE();
2450 
2451 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2452 
2453 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2454 	lvif->iv_newstate = vap->iv_newstate;
2455 	vap->iv_newstate = lkpi_iv_newstate;
2456 	lvif->iv_update_bss = vap->iv_update_bss;
2457 	vap->iv_update_bss = lkpi_iv_update_bss;
2458 
2459 	/* Key management. */
2460 	if (lhw->ops->set_key != NULL) {
2461 #ifdef LKPI_80211_HW_CRYPTO
2462 		vap->iv_key_set = lkpi_iv_key_set;
2463 		vap->iv_key_delete = lkpi_iv_key_delete;
2464 #endif
2465 	}
2466 
2467 	ieee80211_ratectl_init(vap);
2468 
2469 	/* Complete setup. */
2470 	ieee80211_vap_attach(vap, ieee80211_media_change,
2471 	    ieee80211_media_status, mac);
2472 
2473 	if (hw->max_listen_interval == 0)
2474 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2475 	hw->conf.listen_interval = hw->max_listen_interval;
2476 	ic->ic_set_channel(ic);
2477 
2478 	/* XXX-BZ do we need to be able to update these? */
2479 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
2480 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2481 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
2482 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2483 	/* any others? */
2484 	IMPROVE();
2485 
2486 	return (vap);
2487 }
2488 
2489 void
2490 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
2491 {
2492 
2493 	wiphy_unregister(hw->wiphy);
2494 	linuxkpi_ieee80211_ifdetach(hw);
2495 
2496 	IMPROVE();
2497 }
2498 
2499 void
2500 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
2501 {
2502 
2503 	TODO();
2504 }
2505 
2506 static void
2507 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2508 {
2509 	struct ieee80211com *ic;
2510 	struct lkpi_hw *lhw;
2511 	struct ieee80211_hw *hw;
2512 	struct lkpi_vif *lvif;
2513 	struct ieee80211_vif *vif;
2514 
2515 	lvif = VAP_TO_LVIF(vap);
2516 	vif = LVIF_TO_VIF(lvif);
2517 	ic = vap->iv_ic;
2518 	lhw = ic->ic_softc;
2519 	hw = LHW_TO_HW(lhw);
2520 
2521 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2522 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
2523 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2524 
2525 	ieee80211_ratectl_deinit(vap);
2526 	ieee80211_vap_detach(vap);
2527 
2528 	IMPROVE("clear up other bits in this state");
2529 
2530 	lkpi_80211_mo_remove_interface(hw, vif);
2531 
2532 	/* Single VAP, so we can do this here. */
2533 	lkpi_80211_mo_stop(hw);
2534 
2535 	mtx_destroy(&lvif->mtx);
2536 	free(lvif, M_80211_VAP);
2537 }
2538 
2539 static void
2540 lkpi_ic_update_mcast(struct ieee80211com *ic)
2541 {
2542 
2543 	lkpi_update_mcast_filter(ic, false);
2544 	TRACEOK();
2545 }
2546 
2547 static void
2548 lkpi_ic_update_promisc(struct ieee80211com *ic)
2549 {
2550 
2551 	UNIMPLEMENTED;
2552 }
2553 
2554 static void
2555 lkpi_ic_update_chw(struct ieee80211com *ic)
2556 {
2557 
2558 	UNIMPLEMENTED;
2559 }
2560 
2561 /* Start / stop device. */
2562 static void
2563 lkpi_ic_parent(struct ieee80211com *ic)
2564 {
2565 	struct lkpi_hw *lhw;
2566 #ifdef HW_START_STOP
2567 	struct ieee80211_hw *hw;
2568 	int error;
2569 #endif
2570 	bool start_all;
2571 
2572 	IMPROVE();
2573 
2574 	lhw = ic->ic_softc;
2575 #ifdef HW_START_STOP
2576 	hw = LHW_TO_HW(lhw);
2577 #endif
2578 	start_all = false;
2579 
2580 	/* IEEE80211_UNLOCK(ic); */
2581 	LKPI_80211_LHW_LOCK(lhw);
2582 	if (ic->ic_nrunning > 0) {
2583 #ifdef HW_START_STOP
2584 		error = lkpi_80211_mo_start(hw);
2585 		if (error == 0)
2586 #endif
2587 			start_all = true;
2588 	} else {
2589 #ifdef HW_START_STOP
2590 		lkpi_80211_mo_stop(hw);
2591 #endif
2592 	}
2593 	LKPI_80211_LHW_UNLOCK(lhw);
2594 	/* IEEE80211_LOCK(ic); */
2595 
2596 	if (start_all)
2597 		ieee80211_start_all(ic);
2598 }
2599 
2600 bool
2601 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
2602     size_t ie_ids_len)
2603 {
2604 	int i;
2605 
2606 	for (i = 0; i < ie_ids_len; i++) {
2607 		if (ie == *ie_ids)
2608 			return (true);
2609 	}
2610 
2611 	return (false);
2612 }
2613 
2614 /* Return true if skipped; false if error. */
2615 bool
2616 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
2617 {
2618 	size_t x;
2619 	uint8_t l;
2620 
2621 	x = *xp;
2622 
2623 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
2624 	    __func__, x, ies_len, ies));
2625 	l = ies[x + 1];
2626 	x += 2 + l;
2627 
2628 	if (x > ies_len)
2629 		return (false);
2630 
2631 	*xp = x;
2632 	return (true);
2633 }
2634 
2635 static uint8_t *
2636 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2637     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
2638 {
2639 	struct ieee80211_supported_band *supband;
2640 	struct linuxkpi_ieee80211_channel *channels;
2641 	struct ieee80211com *ic;
2642 	const struct ieee80211_channel *chan;
2643 	const struct ieee80211_rateset *rs;
2644 	uint8_t *pb;
2645 	int band, i;
2646 
2647 	ic = vap->iv_ic;
2648 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2649 		if ((band_mask & (1 << band)) == 0)
2650 			continue;
2651 
2652 		supband = hw->wiphy->bands[band];
2653 		/*
2654 		 * This should not happen;
2655 		 * band_mask is a bitmask of valid bands to scan on.
2656 		 */
2657 		if (supband == NULL || supband->n_channels == 0)
2658 			continue;
2659 
2660 		/* Find a first channel to get the mode and rates from. */
2661 		channels = supband->channels;
2662 		chan = NULL;
2663 		for (i = 0; i < supband->n_channels; i++) {
2664 
2665 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2666 				continue;
2667 
2668 			chan = ieee80211_find_channel(ic,
2669 			    channels[i].center_freq, 0);
2670 			if (chan != NULL)
2671 				break;
2672 		}
2673 
2674 		/* This really should not happen. */
2675 		if (chan == NULL)
2676 			continue;
2677 
2678 		pb = p;
2679 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
2680 		p = ieee80211_add_rates(p, rs);
2681 		p = ieee80211_add_xrates(p, rs);
2682 
2683 #if defined(LKPI_80211_HT)
2684 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
2685 			struct ieee80211_channel *c;
2686 
2687 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2688 			    vap->iv_flags_ht);
2689 			p = ieee80211_add_htcap_ch(p, vap, c);
2690 		}
2691 #endif
2692 #if defined(LKPI_80211_VHT)
2693 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
2694 			struct ieee80211_channel *c;
2695 
2696 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2697 			    vap->iv_flags_ht);
2698 			c = ieee80211_vht_adjust_channel(ic, c,
2699 			    vap->iv_vht_flags);
2700 			p = ieee80211_add_vhtcap_ch(p, vap, c);
2701 		}
2702 #endif
2703 
2704 		scan_ies->ies[band] = pb;
2705 		scan_ies->len[band] = p - pb;
2706 	}
2707 
2708 	/* Add common_ies */
2709 	pb = p;
2710 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2711 	    vap->iv_wpa_ie != NULL) {
2712 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2713 		p += 2 + vap->iv_wpa_ie[1];
2714 	}
2715 	if (vap->iv_appie_probereq != NULL) {
2716 		memcpy(p, vap->iv_appie_probereq->ie_data,
2717 		    vap->iv_appie_probereq->ie_len);
2718 		p += vap->iv_appie_probereq->ie_len;
2719 	}
2720 	scan_ies->common_ies = pb;
2721 	scan_ies->common_ie_len = p - pb;
2722 
2723 	return (p);
2724 }
2725 
2726 static void
2727 lkpi_ic_scan_start(struct ieee80211com *ic)
2728 {
2729 	struct lkpi_hw *lhw;
2730 	struct ieee80211_hw *hw;
2731 	struct lkpi_vif *lvif;
2732 	struct ieee80211_vif *vif;
2733 	struct ieee80211_scan_state *ss;
2734 	struct ieee80211vap *vap;
2735 	int error;
2736 	bool is_hw_scan;
2737 
2738 	lhw = ic->ic_softc;
2739 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2740 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2741 		/* A scan is still running. */
2742 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2743 		return;
2744 	}
2745 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2746 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2747 
2748 	ss = ic->ic_scan;
2749 	vap = ss->ss_vap;
2750 	if (vap->iv_state != IEEE80211_S_SCAN) {
2751 		IMPROVE("We need to be able to scan if not in S_SCAN");
2752 		return;
2753 	}
2754 
2755 	hw = LHW_TO_HW(lhw);
2756 	if (!is_hw_scan) {
2757 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
2758 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2759 sw_scan:
2760 		lvif = VAP_TO_LVIF(vap);
2761 		vif = LVIF_TO_VIF(lvif);
2762 
2763 		if (vap->iv_state == IEEE80211_S_SCAN)
2764 			lkpi_hw_conf_idle(hw, false);
2765 
2766 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
2767 		/* net80211::scan_start() handled PS for us. */
2768 		IMPROVE();
2769 		/* XXX Also means it is too late to flush queues?
2770 		 * need to check iv_sta_ps or overload? */
2771 		/* XXX want to adjust ss end time/ maxdwell? */
2772 
2773 	} else {
2774 		struct ieee80211_channel *c;
2775 		struct ieee80211_scan_request *hw_req;
2776 		struct linuxkpi_ieee80211_channel *lc, **cpp;
2777 		struct cfg80211_ssid *ssids;
2778 		struct cfg80211_scan_6ghz_params *s6gp;
2779 		size_t chan_len, nchan, ssids_len, s6ghzlen;
2780 		int band, i, ssid_count, common_ie_len;
2781 		uint32_t band_mask;
2782 		uint8_t *ie, *ieend;
2783 		bool running;
2784 
2785 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
2786 		ssids_len = ssid_count * sizeof(*ssids);
2787 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
2788 
2789 		band_mask = 0;
2790 		nchan = 0;
2791 		for (i = ss->ss_next; i < ss->ss_last; i++) {
2792 			nchan++;
2793 			band = lkpi_net80211_chan_to_nl80211_band(
2794 			    ss->ss_chans[ss->ss_next + i]);
2795 			band_mask |= (1 << band);
2796 		}
2797 
2798 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2799 			IMPROVE("individual band scans not yet supported, only scanning first band");
2800 			/* In theory net80211 should drive this. */
2801 			/* Probably we need to add local logic for now;
2802 			 * need to deal with scan_complete
2803 			 * and cancel_scan and keep local state.
2804 			 * Also cut the nchan down above.
2805 			 */
2806 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
2807 		}
2808 
2809 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
2810 
2811 		common_ie_len = 0;
2812 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2813 		    vap->iv_wpa_ie != NULL)
2814 			common_ie_len += vap->iv_wpa_ie[1];
2815 		if (vap->iv_appie_probereq != NULL)
2816 			common_ie_len += vap->iv_appie_probereq->ie_len;
2817 
2818 		/* We would love to check this at an earlier stage... */
2819 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
2820 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
2821 			    "wiphy->max_scan_ie_len %d\n", __func__,
2822 			    common_ie_len, hw->wiphy->max_scan_ie_len);
2823 		}
2824 
2825 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
2826 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
2827 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
2828 
2829 		hw_req->req.flags = 0;			/* XXX ??? */
2830 		/* hw_req->req.wdev */
2831 		hw_req->req.wiphy = hw->wiphy;
2832 		hw_req->req.no_cck = false;		/* XXX */
2833 #if 0
2834 		/* This seems to pessimise default scanning behaviour. */
2835 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
2836 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
2837 #endif
2838 #ifdef __notyet__
2839 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
2840 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
2841 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
2842 #endif
2843 		eth_broadcast_addr(hw_req->req.bssid);
2844 
2845 		hw_req->req.n_channels = nchan;
2846 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
2847 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
2848 		for (i = 0; i < nchan; i++) {
2849 			*(cpp + i) =
2850 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
2851 		}
2852 		for (i = 0; i < nchan; i++) {
2853 			c = ss->ss_chans[ss->ss_next + i];
2854 
2855 			lc->hw_value = c->ic_ieee;
2856 			lc->center_freq = c->ic_freq;	/* XXX */
2857 			/* lc->flags */
2858 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
2859 			lc->max_power = c->ic_maxpower;
2860 			/* lc-> ... */
2861 			lc++;
2862 		}
2863 
2864 		hw_req->req.n_ssids = ssid_count;
2865 		if (hw_req->req.n_ssids > 0) {
2866 			ssids = (struct cfg80211_ssid *)lc;
2867 			hw_req->req.ssids = ssids;
2868 			for (i = 0; i < ssid_count; i++) {
2869 				ssids->ssid_len = ss->ss_ssid[i].len;
2870 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
2871 				    ss->ss_ssid[i].len);
2872 				ssids++;
2873 			}
2874 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
2875 		} else {
2876 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
2877 		}
2878 
2879 		/* 6GHz one day. */
2880 		hw_req->req.n_6ghz_params = 0;
2881 		hw_req->req.scan_6ghz_params = NULL;
2882 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
2883 		/* s6gp->... */
2884 
2885 		ie = ieend = (uint8_t *)s6gp;
2886 		/* Copy per-band IEs, copy common IEs */
2887 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
2888 		hw_req->req.ie = ie;
2889 		hw_req->req.ie_len = ieend - ie;
2890 
2891 		lvif = VAP_TO_LVIF(vap);
2892 		vif = LVIF_TO_VIF(lvif);
2893 
2894 		LKPI_80211_LHW_SCAN_LOCK(lhw);
2895 		/* Re-check under lock. */
2896 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2897 		if (!running) {
2898 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
2899 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
2900 
2901 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
2902 			lhw->hw_req = hw_req;
2903 		}
2904 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2905 		if (running) {
2906 			free(hw_req, M_LKPI80211);
2907 			return;
2908 		}
2909 
2910 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
2911 		if (error != 0) {
2912 			ieee80211_cancel_scan(vap);
2913 
2914 			/*
2915 			 * ieee80211_scan_completed must be called in either
2916 			 * case of error or none.  So let the free happen there
2917 			 * and only there.
2918 			 * That would be fine in theory but in practice drivers
2919 			 * behave differently:
2920 			 * ath10k does not return hw_scan until after scan_complete
2921 			 *        and can then still return an error.
2922 			 * rtw88 can return 1 or -EBUSY without scan_complete
2923 			 * iwlwifi can return various errors before scan starts
2924 			 * ...
2925 			 * So we cannot rely on that behaviour and have to check
2926 			 * and balance between both code paths.
2927 			 */
2928 			LKPI_80211_LHW_SCAN_LOCK(lhw);
2929 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2930 				free(lhw->hw_req, M_LKPI80211);
2931 				lhw->hw_req = NULL;
2932 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
2933 			}
2934 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2935 
2936 			/*
2937 			 * XXX-SIGH magic number.
2938 			 * rtw88 has a magic "return 1" if offloading scan is
2939 			 * not possible.  Fall back to sw scan in that case.
2940 			 */
2941 			if (error == 1) {
2942 				LKPI_80211_LHW_SCAN_LOCK(lhw);
2943 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
2944 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2945 				/*
2946 				 * XXX If we clear this now and later a driver
2947 				 * thinks it * can do a hw_scan again, we will
2948 				 * currently not re-enable it?
2949 				 */
2950 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2951 				ieee80211_start_scan(vap,
2952 				    IEEE80211_SCAN_ACTIVE |
2953 				    IEEE80211_SCAN_NOPICK |
2954 				    IEEE80211_SCAN_ONCE,
2955 				    IEEE80211_SCAN_FOREVER,
2956 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2957 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2958 				    vap->iv_des_nssid, vap->iv_des_ssid);
2959 				goto sw_scan;
2960 			}
2961 
2962 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2963 			    __func__, error);
2964 		}
2965 	}
2966 }
2967 
2968 static void
2969 lkpi_ic_scan_end(struct ieee80211com *ic)
2970 {
2971 	struct lkpi_hw *lhw;
2972 	bool is_hw_scan;
2973 
2974 	lhw = ic->ic_softc;
2975 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2976 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
2977 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2978 		return;
2979 	}
2980 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2981 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2982 
2983 	if (!is_hw_scan) {
2984 		struct ieee80211_scan_state *ss;
2985 		struct ieee80211vap *vap;
2986 		struct ieee80211_hw *hw;
2987 		struct lkpi_vif *lvif;
2988 		struct ieee80211_vif *vif;
2989 
2990 		ss = ic->ic_scan;
2991 		vap = ss->ss_vap;
2992 		hw = LHW_TO_HW(lhw);
2993 		lvif = VAP_TO_LVIF(vap);
2994 		vif = LVIF_TO_VIF(lvif);
2995 
2996 		lkpi_80211_mo_sw_scan_complete(hw, vif);
2997 
2998 		/* Send PS to stop buffering if n80211 does not for us? */
2999 
3000 		if (vap->iv_state == IEEE80211_S_SCAN)
3001 			lkpi_hw_conf_idle(hw, true);
3002 	}
3003 }
3004 
3005 static void
3006 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3007     unsigned long maxdwell)
3008 {
3009 	struct lkpi_hw *lhw;
3010 	bool is_hw_scan;
3011 
3012 	lhw = ss->ss_ic->ic_softc;
3013 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3014 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3015 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3016 	if (!is_hw_scan)
3017 		lhw->ic_scan_curchan(ss, maxdwell);
3018 }
3019 
3020 static void
3021 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3022 {
3023 	struct lkpi_hw *lhw;
3024 	bool is_hw_scan;
3025 
3026 	lhw = ss->ss_ic->ic_softc;
3027 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3028 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3029 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3030 	if (!is_hw_scan)
3031 		lhw->ic_scan_mindwell(ss);
3032 }
3033 
3034 static void
3035 lkpi_ic_set_channel(struct ieee80211com *ic)
3036 {
3037 	struct lkpi_hw *lhw;
3038 	struct ieee80211_hw *hw;
3039 	struct ieee80211_channel *c;
3040 	struct linuxkpi_ieee80211_channel *chan;
3041 	int error;
3042 	bool hw_scan_running;
3043 
3044 	lhw = ic->ic_softc;
3045 
3046 	/* If we do not support (*config)() save us the work. */
3047 	if (lhw->ops->config == NULL)
3048 		return;
3049 
3050 	/* If we have a hw_scan running do not switch channels. */
3051 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3052 	hw_scan_running =
3053 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
3054 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
3055 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3056 	if (hw_scan_running)
3057 		return;
3058 
3059 	c = ic->ic_curchan;
3060 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
3061 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
3062 		    c, lhw->ops->config);
3063 		return;
3064 	}
3065 
3066 	chan = lkpi_find_lkpi80211_chan(lhw, c);
3067 	if (chan == NULL) {
3068 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
3069 		    c, chan);
3070 		return;
3071 	}
3072 
3073 	/* XXX max power for scanning? */
3074 	IMPROVE();
3075 
3076 	hw = LHW_TO_HW(lhw);
3077 	cfg80211_chandef_create(&hw->conf.chandef, chan,
3078 	    NL80211_CHAN_NO_HT);
3079 
3080 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
3081 	if (error != 0 && error != EOPNOTSUPP) {
3082 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
3083 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
3084 		/* XXX should we unroll to the previous chandef? */
3085 		IMPROVE();
3086 	} else {
3087 		/* Update radiotap channels as well. */
3088 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
3089 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
3090 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
3091 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
3092 	}
3093 
3094 	/* Currently PS is hard coded off! Not sure it belongs here. */
3095 	IMPROVE();
3096 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
3097 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
3098 		hw->conf.flags &= ~IEEE80211_CONF_PS;
3099 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
3100 		if (error != 0 && error != EOPNOTSUPP)
3101 			ic_printf(ic, "ERROR: %s: config %#0x returned "
3102 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
3103 			    error);
3104 	}
3105 }
3106 
3107 static struct ieee80211_node *
3108 lkpi_ic_node_alloc(struct ieee80211vap *vap,
3109     const uint8_t mac[IEEE80211_ADDR_LEN])
3110 {
3111 	struct ieee80211com *ic;
3112 	struct lkpi_hw *lhw;
3113 	struct ieee80211_node *ni;
3114 	struct ieee80211_hw *hw;
3115 	struct lkpi_sta *lsta;
3116 
3117 	ic = vap->iv_ic;
3118 	lhw = ic->ic_softc;
3119 
3120 	/* We keep allocations de-coupled so we can deal with the two worlds. */
3121 	if (lhw->ic_node_alloc == NULL)
3122 		return (NULL);
3123 
3124 	ni = lhw->ic_node_alloc(vap, mac);
3125 	if (ni == NULL)
3126 		return (NULL);
3127 
3128 	hw = LHW_TO_HW(lhw);
3129 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
3130 	if (lsta == NULL) {
3131 		if (lhw->ic_node_free != NULL)
3132 			lhw->ic_node_free(ni);
3133 		return (NULL);
3134 	}
3135 
3136 	return (ni);
3137 }
3138 
3139 static int
3140 lkpi_ic_node_init(struct ieee80211_node *ni)
3141 {
3142 	struct ieee80211com *ic;
3143 	struct lkpi_hw *lhw;
3144 	struct lkpi_sta *lsta;
3145 	int error;
3146 
3147 	ic = ni->ni_ic;
3148 	lhw = ic->ic_softc;
3149 
3150 	if (lhw->ic_node_init != NULL) {
3151 		error = lhw->ic_node_init(ni);
3152 		if (error != 0)
3153 			return (error);
3154 	}
3155 
3156 	lsta = ni->ni_drv_data;
3157 
3158 	/* Now take the reference before linking it to the table. */
3159 	lsta->ni = ieee80211_ref_node(ni);
3160 
3161 	/* XXX-BZ Sync other state over. */
3162 	IMPROVE();
3163 
3164 	return (0);
3165 }
3166 
3167 static void
3168 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
3169 {
3170 	struct ieee80211com *ic;
3171 	struct lkpi_hw *lhw;
3172 
3173 	ic = ni->ni_ic;
3174 	lhw = ic->ic_softc;
3175 
3176 	/* XXX-BZ remove from driver, ... */
3177 	IMPROVE();
3178 
3179 	if (lhw->ic_node_cleanup != NULL)
3180 		lhw->ic_node_cleanup(ni);
3181 }
3182 
3183 static void
3184 lkpi_ic_node_free(struct ieee80211_node *ni)
3185 {
3186 	struct ieee80211com *ic;
3187 	struct lkpi_hw *lhw;
3188 	struct lkpi_sta *lsta;
3189 
3190 	ic = ni->ni_ic;
3191 	lhw = ic->ic_softc;
3192 	lsta = ni->ni_drv_data;
3193 	if (lsta == NULL)
3194 		goto out;
3195 
3196 	/* XXX-BZ free resources, ... */
3197 	IMPROVE();
3198 
3199 	/* Flush mbufq (make sure to release ni refs!). */
3200 #ifdef __notyet__
3201 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
3202 	    __func__, lsta, mbufq_len(&lsta->txq)));
3203 #endif
3204 	/* Drain taskq. */
3205 
3206 	/* Drain sta->txq[] */
3207 	mtx_destroy(&lsta->txq_mtx);
3208 
3209 	/* Remove lsta if added_to_drv. */
3210 
3211 	/* Remove lsta from vif */
3212 	/* Remove ref from lsta node... */
3213 	/* Free lsta. */
3214 	lkpi_lsta_remove(lsta, VAP_TO_LVIF(ni->ni_vap));
3215 
3216 out:
3217 	if (lhw->ic_node_free != NULL)
3218 		lhw->ic_node_free(ni);
3219 }
3220 
3221 static int
3222 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3223         const struct ieee80211_bpf_params *params __unused)
3224 {
3225 	struct lkpi_sta *lsta;
3226 
3227 	lsta = ni->ni_drv_data;
3228 
3229 	/* Queue the packet and enqueue the task to handle it. */
3230 	LKPI_80211_LSTA_LOCK(lsta);
3231 	mbufq_enqueue(&lsta->txq, m);
3232 	LKPI_80211_LSTA_UNLOCK(lsta);
3233 
3234 #ifdef LINUXKPI_DEBUG_80211
3235 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3236 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
3237 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
3238 		    mbufq_len(&lsta->txq));
3239 #endif
3240 
3241 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3242 	return (0);
3243 }
3244 
3245 static void
3246 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
3247 {
3248 	struct ieee80211_node *ni;
3249 #ifndef LKPI_80211_HW_CRYPTO
3250 	struct ieee80211_frame *wh;
3251 #endif
3252 	struct ieee80211_key *k;
3253 	struct sk_buff *skb;
3254 	struct ieee80211com *ic;
3255 	struct lkpi_hw *lhw;
3256 	struct ieee80211_hw *hw;
3257 	struct lkpi_vif *lvif;
3258 	struct ieee80211_vif *vif;
3259 	struct ieee80211_channel *c;
3260 	struct ieee80211_tx_control control;
3261 	struct ieee80211_tx_info *info;
3262 	struct ieee80211_sta *sta;
3263 	struct ieee80211_hdr *hdr;
3264 	void *buf;
3265 	uint8_t ac, tid;
3266 
3267 	M_ASSERTPKTHDR(m);
3268 #ifdef LINUXKPI_DEBUG_80211
3269 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
3270 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
3271 #endif
3272 
3273 	ni = lsta->ni;
3274 	k = NULL;
3275 #ifndef LKPI_80211_HW_CRYPTO
3276 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
3277 	wh = mtod(m, struct ieee80211_frame *);
3278 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
3279 		/* Retrieve key for TX && do software encryption. */
3280 		k = ieee80211_crypto_encap(ni, m);
3281 		if (k == NULL) {
3282 			ieee80211_free_node(ni);
3283 			m_freem(m);
3284 			return;
3285 		}
3286 	}
3287 #endif
3288 
3289 	ic = ni->ni_ic;
3290 	lhw = ic->ic_softc;
3291 	hw = LHW_TO_HW(lhw);
3292 	c = ni->ni_chan;
3293 
3294 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
3295 		struct lkpi_radiotap_tx_hdr *rtap;
3296 
3297 		rtap = &lhw->rtap_tx;
3298 		rtap->wt_flags = 0;
3299 		if (k != NULL)
3300 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3301 		if (m->m_flags & M_FRAG)
3302 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
3303 		IMPROVE();
3304 		rtap->wt_rate = 0;
3305 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3306 			rtap->wt_chan_freq = htole16(c->ic_freq);
3307 			rtap->wt_chan_flags = htole16(c->ic_flags);
3308 		}
3309 
3310 		ieee80211_radiotap_tx(ni->ni_vap, m);
3311 	}
3312 
3313 	/*
3314 	 * net80211 should handle hw->extra_tx_headroom.
3315 	 * Though for as long as we are copying we don't mind.
3316 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
3317 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
3318 	 */
3319 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
3320 	if (skb == NULL) {
3321 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
3322 		ieee80211_free_node(ni);
3323 		m_freem(m);
3324 		return;
3325 	}
3326 	skb_reserve(skb, hw->extra_tx_headroom);
3327 
3328 	/* XXX-BZ we need a SKB version understanding mbuf. */
3329 	/* Save the mbuf for ieee80211_tx_complete(). */
3330 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3331 	skb->m = m;
3332 #if 0
3333 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3334 #else
3335 	buf = skb_put(skb, m->m_pkthdr.len);
3336 	m_copydata(m, 0, m->m_pkthdr.len, buf);
3337 #endif
3338 	/* Save the ni. */
3339 	m->m_pkthdr.PH_loc.ptr = ni;
3340 
3341 	lvif = VAP_TO_LVIF(ni->ni_vap);
3342 	vif = LVIF_TO_VIF(lvif);
3343 
3344 	hdr = (void *)skb->data;
3345 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3346 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3347 		skb->priority = 0;
3348 		ac = IEEE80211_AC_BE;
3349 	} else {
3350 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3351 		ac = ieee80211e_up_to_ac[tid & 7];
3352 	}
3353 	skb_set_queue_mapping(skb, ac);
3354 
3355 	info = IEEE80211_SKB_CB(skb);
3356 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3357 	/* Slight delay; probably only happens on scanning so fine? */
3358 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
3359 		c = ic->ic_curchan;
3360 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3361 	info->hw_queue = vif->hw_queue[ac];
3362 	if (m->m_flags & M_EAPOL)
3363 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3364 	info->control.vif = vif;
3365 	/* XXX-BZ info->control.rates */
3366 
3367 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
3368 	if (lsta != NULL) {
3369 		sta = LSTA_TO_STA(lsta);
3370 #ifdef LKPI_80211_HW_CRYPTO
3371 		info->control.hw_key = lsta->kc;
3372 #endif
3373 	} else {
3374 		sta = NULL;
3375 	}
3376 
3377 	IMPROVE();
3378 
3379 	if (sta != NULL) {
3380 		struct lkpi_txq *ltxq;
3381 
3382 		ltxq = NULL;
3383 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3384 			if (vif->type == NL80211_IFTYPE_STATION &&
3385 			    lsta->added_to_drv &&
3386 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3387 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3388 		} else if (lsta->added_to_drv &&
3389 		    sta->txq[skb->priority] != NULL) {
3390 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3391 		}
3392 		if (ltxq == NULL)
3393 			goto ops_tx;
3394 
3395 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3396 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3397 
3398 		skb_queue_tail(&ltxq->skbq, skb);
3399 #ifdef LINUXKPI_DEBUG_80211
3400 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3401 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3402 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3403 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3404 			    __func__, __LINE__,
3405 			    curthread->td_tid, (unsigned int)ticks,
3406 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3407 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3408 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
3409 #endif
3410 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
3411 		return;
3412 	}
3413 
3414 ops_tx:
3415 #ifdef LINUXKPI_DEBUG_80211
3416 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3417 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3418 		    "TX ac %d prio %u qmap %u\n",
3419 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3420 		    skb, ac, skb->priority, skb->qmap);
3421 #endif
3422 	memset(&control, 0, sizeof(control));
3423 	control.sta = sta;
3424 
3425 	lkpi_80211_mo_tx(hw, &control, skb);
3426 	return;
3427 }
3428 
3429 static void
3430 lkpi_80211_txq_task(void *ctx, int pending)
3431 {
3432 	struct lkpi_sta *lsta;
3433 	struct mbufq mq;
3434 	struct mbuf *m;
3435 
3436 	lsta = ctx;
3437 
3438 #ifdef LINUXKPI_DEBUG_80211
3439 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3440 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3441 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3442 		    pending, mbufq_len(&lsta->txq));
3443 #endif
3444 
3445 	mbufq_init(&mq, IFQ_MAXLEN);
3446 
3447 	LKPI_80211_LSTA_LOCK(lsta);
3448 	mbufq_concat(&mq, &lsta->txq);
3449 	LKPI_80211_LSTA_UNLOCK(lsta);
3450 
3451 	m = mbufq_dequeue(&mq);
3452 	while (m != NULL) {
3453 		lkpi_80211_txq_tx_one(lsta, m);
3454 		m = mbufq_dequeue(&mq);
3455 	}
3456 }
3457 
3458 static int
3459 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3460 {
3461 
3462 	/* XXX TODO */
3463 	IMPROVE();
3464 
3465 	/* Quick and dirty cheating hack. */
3466 	struct ieee80211_node *ni;
3467 
3468 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3469 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3470 }
3471 
3472 static void
3473 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
3474     int *n, struct ieee80211_channel *c)
3475 {
3476 	struct lkpi_hw *lhw;
3477 	struct ieee80211_hw *hw;
3478 	struct linuxkpi_ieee80211_channel *channels;
3479 	uint8_t bands[IEEE80211_MODE_BYTES];
3480 	int chan_flags, error, i, nchans;
3481 
3482 	/* Channels */
3483 	lhw = ic->ic_softc;
3484 	hw = LHW_TO_HW(lhw);
3485 
3486 	/* NL80211_BAND_2GHZ */
3487 	nchans = 0;
3488 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
3489 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
3490 	if (nchans > 0) {
3491 		memset(bands, 0, sizeof(bands));
3492 		chan_flags = 0;
3493 		setbit(bands, IEEE80211_MODE_11B);
3494 		/* XXX-BZ unclear how to check for 11g. */
3495 		setbit(bands, IEEE80211_MODE_11G);
3496 #ifdef __notyet__
3497 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
3498 			setbit(bands, IEEE80211_MODE_11NG);
3499 			chan_flags |= NET80211_CBW_FLAG_HT40;
3500 		}
3501 #endif
3502 
3503 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3504 		for (i = 0; i < nchans && *n < maxchan; i++) {
3505 			uint32_t nflags = 0;
3506 			int cflags = chan_flags;
3507 
3508 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3509 				ic_printf(ic, "%s: Skipping disabled chan "
3510 				    "[%u/%u/%#x]\n", __func__,
3511 				    channels[i].hw_value,
3512 				    channels[i].center_freq, channels[i].flags);
3513 				continue;
3514 			}
3515 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3516 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3517 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3518 				nflags |= IEEE80211_CHAN_DFS;
3519 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3520 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3521 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3522 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3523 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
3524 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3525 				cflags &= ~NET80211_CBW_FLAG_HT40;
3526 
3527 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3528 			    channels[i].hw_value, channels[i].center_freq,
3529 			    channels[i].max_power,
3530 			    nflags, bands, cflags);
3531 			/* net80211::ENOBUFS: *n >= maxchans */
3532 			if (error != 0 && error != ENOBUFS)
3533 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3534 				    "returned error %d\n",
3535 				    __func__, channels[i].hw_value,
3536 				    channels[i].center_freq, channels[i].flags,
3537 				    nflags, chan_flags, cflags, error);
3538 			if (error != 0)
3539 				break;
3540 		}
3541 	}
3542 
3543 	/* NL80211_BAND_5GHZ */
3544 	nchans = 0;
3545 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
3546 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
3547 	if (nchans > 0) {
3548 		memset(bands, 0, sizeof(bands));
3549 		chan_flags = 0;
3550 		setbit(bands, IEEE80211_MODE_11A);
3551 #ifdef __not_yet__
3552 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
3553 			setbit(bands, IEEE80211_MODE_11NA);
3554 			chan_flags |= NET80211_CBW_FLAG_HT40;
3555 		}
3556 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
3557 
3558 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
3559 			ic->ic_vht_cap.vht_cap_info =
3560 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
3561 
3562 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
3563 			chan_flags |= NET80211_CBW_FLAG_VHT80;
3564 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
3565 			    ic->ic_vht_cap.vht_cap_info))
3566 				chan_flags |= NET80211_CBW_FLAG_VHT160;
3567 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
3568 			    ic->ic_vht_cap.vht_cap_info))
3569 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
3570 		}
3571 #endif
3572 
3573 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3574 		for (i = 0; i < nchans && *n < maxchan; i++) {
3575 			uint32_t nflags = 0;
3576 			int cflags = chan_flags;
3577 
3578 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3579 				ic_printf(ic, "%s: Skipping disabled chan "
3580 				    "[%u/%u/%#x]\n", __func__,
3581 				    channels[i].hw_value,
3582 				    channels[i].center_freq, channels[i].flags);
3583 				continue;
3584 			}
3585 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3586 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3587 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3588 				nflags |= IEEE80211_CHAN_DFS;
3589 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3590 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3591 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3592 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3593 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
3594 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3595 				cflags &= ~NET80211_CBW_FLAG_HT40;
3596 
3597 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3598 			    channels[i].hw_value, channels[i].center_freq,
3599 			    channels[i].max_power,
3600 			    nflags, bands, cflags);
3601 			/* net80211::ENOBUFS: *n >= maxchans */
3602 			if (error != 0 && error != ENOBUFS)
3603 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3604 				    "returned error %d\n",
3605 				    __func__, channels[i].hw_value,
3606 				    channels[i].center_freq, channels[i].flags,
3607 				    nflags, chan_flags, cflags, error);
3608 			if (error != 0)
3609 				break;
3610 		}
3611 	}
3612 }
3613 
3614 static void *
3615 lkpi_ieee80211_ifalloc(void)
3616 {
3617 	struct ieee80211com *ic;
3618 
3619 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
3620 	if (ic == NULL)
3621 		return (NULL);
3622 
3623 	/* Setting these happens later when we have device information. */
3624 	ic->ic_softc = NULL;
3625 	ic->ic_name = "linuxkpi";
3626 
3627 	return (ic);
3628 }
3629 
3630 struct ieee80211_hw *
3631 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
3632 {
3633 	struct ieee80211_hw *hw;
3634 	struct lkpi_hw *lhw;
3635 	struct wiphy *wiphy;
3636 	int ac;
3637 
3638 	/* Get us and the driver data also allocated. */
3639 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
3640 	if (wiphy == NULL)
3641 		return (NULL);
3642 
3643 	lhw = wiphy_priv(wiphy);
3644 	lhw->ops = ops;
3645 
3646 	LKPI_80211_LHW_LOCK_INIT(lhw);
3647 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
3648 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
3649 	TAILQ_INIT(&lhw->lvif_head);
3650 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3651 		lhw->txq_generation[ac] = 1;
3652 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
3653 	}
3654 
3655 	/*
3656 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
3657 	 * not initialized.
3658 	 */
3659 	hw = LHW_TO_HW(lhw);
3660 	hw->wiphy = wiphy;
3661 	hw->conf.flags |= IEEE80211_CONF_IDLE;
3662 	hw->priv = (void *)(lhw + 1);
3663 
3664 	/* BSD Specific. */
3665 	lhw->ic = lkpi_ieee80211_ifalloc();
3666 	if (lhw->ic == NULL) {
3667 		ieee80211_free_hw(hw);
3668 		return (NULL);
3669 	}
3670 
3671 	IMPROVE();
3672 
3673 	return (hw);
3674 }
3675 
3676 void
3677 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
3678 {
3679 	struct lkpi_hw *lhw;
3680 
3681 	lhw = HW_TO_LHW(hw);
3682 	free(lhw->ic, M_LKPI80211);
3683 	lhw->ic = NULL;
3684 
3685 	/* Cleanup more of lhw here or in wiphy_free()? */
3686 	sx_destroy(&lhw->lvif_sx);
3687 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
3688 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
3689 	IMPROVE();
3690 }
3691 
3692 void
3693 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
3694 {
3695 	struct lkpi_hw *lhw;
3696 	struct ieee80211com *ic;
3697 
3698 	lhw = HW_TO_LHW(hw);
3699 	ic = lhw->ic;
3700 
3701 	/* Now set a proper name before ieee80211_ifattach(). */
3702 	ic->ic_softc = lhw;
3703 	ic->ic_name = name;
3704 
3705 	/* XXX-BZ do we also need to set wiphy name? */
3706 }
3707 
3708 struct ieee80211_hw *
3709 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
3710 {
3711 	struct lkpi_hw *lhw;
3712 
3713 	lhw = wiphy_priv(wiphy);
3714 	return (LHW_TO_HW(lhw));
3715 }
3716 
3717 static void
3718 lkpi_radiotap_attach(struct lkpi_hw *lhw)
3719 {
3720 	struct ieee80211com *ic;
3721 
3722 	ic = lhw->ic;
3723 	ieee80211_radiotap_attach(ic,
3724 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
3725 	    LKPI_RTAP_TX_FLAGS_PRESENT,
3726 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
3727 	    LKPI_RTAP_RX_FLAGS_PRESENT);
3728 }
3729 
3730 int
3731 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
3732 {
3733 	struct ieee80211com *ic;
3734 	struct lkpi_hw *lhw;
3735 	int band, i;
3736 
3737 	lhw = HW_TO_LHW(hw);
3738 	ic = lhw->ic;
3739 
3740 	/* We do it this late as wiphy->dev should be set for the name. */
3741 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3742 	if (lhw->workq == NULL)
3743 		return (-EAGAIN);
3744 
3745 	/* XXX-BZ figure this out how they count his... */
3746 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
3747 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3748 		    hw->wiphy->perm_addr);
3749 	} else if (hw->wiphy->n_addresses > 0) {
3750 		/* We take the first one. */
3751 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3752 		    hw->wiphy->addresses[0].addr);
3753 	} else {
3754 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
3755 	}
3756 
3757 #ifdef __not_yet__
3758 	/* See comment in lkpi_80211_txq_tx_one(). */
3759 	ic->ic_headroom = hw->extra_tx_headroom;
3760 #endif
3761 
3762 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
3763 	ic->ic_opmode = IEEE80211_M_STA;
3764 
3765 	/* Set device capabilities. */
3766 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
3767 	ic->ic_caps =
3768 	    IEEE80211_C_STA |
3769 	    IEEE80211_C_MONITOR |
3770 	    IEEE80211_C_WPA |		/* WPA/RSN */
3771 #ifdef LKPI_80211_WME
3772 	    IEEE80211_C_WME |
3773 #endif
3774 #if 0
3775 	    IEEE80211_C_PMGT |
3776 #endif
3777 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
3778 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
3779 	    ;
3780 #if 0
3781 	/* Scanning is a different kind of beast to re-work. */
3782 	ic->ic_caps |= IEEE80211_C_BGSCAN;
3783 #endif
3784 	if (lhw->ops->hw_scan) {
3785 		/*
3786 		 * Advertise full-offload scanning.
3787 		 *
3788 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3789 		 * we essentially disable hw_scan for all drivers not setting
3790 		 * the flag.
3791 		 */
3792 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3793 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
3794 	}
3795 
3796 #ifdef __notyet__
3797 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
3798 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
3799 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
3800 		    | IEEE80211_HTCAP_MAXAMSDU_3839
3801 						/* max A-MSDU length */
3802 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
3803 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
3804 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
3805 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
3806 #endif
3807 
3808 	/*
3809 	 * The wiphy variables report bitmasks of avail antennas.
3810 	 * (*get_antenna) get the current bitmask sets which can be
3811 	 * altered by (*set_antenna) for some drivers.
3812 	 * XXX-BZ will the count alone do us much good long-term in net80211?
3813 	 */
3814 	if (hw->wiphy->available_antennas_rx ||
3815 	    hw->wiphy->available_antennas_tx) {
3816 		uint32_t rxs, txs;
3817 
3818 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
3819 			ic->ic_rxstream = bitcount32(rxs);
3820 			ic->ic_txstream = bitcount32(txs);
3821 		}
3822 	}
3823 
3824 	ic->ic_cryptocaps = 0;
3825 #ifdef LKPI_80211_HW_CRYPTO
3826 	if (hw->wiphy->n_cipher_suites > 0) {
3827 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
3828 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
3829 			    hw->wiphy->cipher_suites[i]);
3830 	}
3831 #endif
3832 
3833 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3834 	    ic->ic_channels);
3835 
3836 	ieee80211_ifattach(ic);
3837 
3838 	ic->ic_update_mcast = lkpi_ic_update_mcast;
3839 	ic->ic_update_promisc = lkpi_ic_update_promisc;
3840 	ic->ic_update_chw = lkpi_ic_update_chw;
3841 	ic->ic_parent = lkpi_ic_parent;
3842 	ic->ic_scan_start = lkpi_ic_scan_start;
3843 	ic->ic_scan_end = lkpi_ic_scan_end;
3844 	ic->ic_set_channel = lkpi_ic_set_channel;
3845 	ic->ic_transmit = lkpi_ic_transmit;
3846 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
3847 	ic->ic_vap_create = lkpi_ic_vap_create;
3848 	ic->ic_vap_delete = lkpi_ic_vap_delete;
3849 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
3850 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
3851 
3852 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
3853 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
3854 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
3855 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
3856 
3857 	lhw->ic_node_alloc = ic->ic_node_alloc;
3858 	ic->ic_node_alloc = lkpi_ic_node_alloc;
3859 	lhw->ic_node_init = ic->ic_node_init;
3860 	ic->ic_node_init = lkpi_ic_node_init;
3861 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
3862 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
3863 	lhw->ic_node_free = ic->ic_node_free;
3864 	ic->ic_node_free = lkpi_ic_node_free;
3865 
3866 	lkpi_radiotap_attach(lhw);
3867 
3868 	/*
3869 	 * Assign the first possible channel for now;  seems Realtek drivers
3870 	 * expect one.
3871 	 * Also remember the amount of bands we support and the most rates
3872 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
3873 	 */
3874 	lhw->supbands = lhw->max_rates = 0;
3875 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3876 		struct ieee80211_supported_band *supband;
3877 		struct linuxkpi_ieee80211_channel *channels;
3878 
3879 		supband = hw->wiphy->bands[band];
3880 		if (supband == NULL || supband->n_channels == 0)
3881 			continue;
3882 
3883 		lhw->supbands++;
3884 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
3885 
3886 		/* If we have a channel, we need to keep counting supbands. */
3887 		if (hw->conf.chandef.chan != NULL)
3888 			continue;
3889 
3890 		channels = supband->channels;
3891 		for (i = 0; i < supband->n_channels; i++) {
3892 
3893 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3894 				continue;
3895 
3896 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3897 			    NL80211_CHAN_NO_HT);
3898 			break;
3899 		}
3900 	}
3901 
3902 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
3903 
3904 	/* Make sure we do not support more than net80211 is willing to take. */
3905 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
3906 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
3907 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
3908 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
3909 	}
3910 
3911 	/*
3912 	 * The maximum supported bitrates on any band + size for
3913 	 * DSSS Parameter Set give our per-band IE size.
3914 	 * XXX-BZ FIXME add HT VHT ... later
3915 	 * SSID is the responsibility of the driver and goes on the side.
3916 	 * The user specified bits coming from the vap go into the
3917 	 * "common ies" fields.
3918 	 */
3919 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
3920 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
3921 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
3922 
3923 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
3924 		/*
3925 		 * net80211 does not seem to support the DSSS Parameter Set but
3926 		 * some of the drivers insert it so calculate the extra fixed
3927 		 * space in.
3928 		 */
3929 		lhw->scan_ie_len += 2 + 1;
3930 	}
3931 
3932 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
3933 	if (hw->wiphy->max_scan_ie_len > 0) {
3934 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
3935 			goto err;
3936 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
3937 	}
3938 
3939 	if (bootverbose)
3940 		ieee80211_announce(ic);
3941 
3942 	return (0);
3943 err:
3944 	IMPROVE("TODO FIXME CLEANUP");
3945 	return (-EAGAIN);
3946 }
3947 
3948 void
3949 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
3950 {
3951 	struct lkpi_hw *lhw;
3952 	struct ieee80211com *ic;
3953 
3954 	lhw = HW_TO_LHW(hw);
3955 	ic = lhw->ic;
3956 	ieee80211_ifdetach(ic);
3957 }
3958 
3959 void
3960 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
3961     enum ieee80211_iface_iter flags,
3962     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
3963     void *arg)
3964 {
3965 	struct lkpi_hw *lhw;
3966 	struct lkpi_vif *lvif;
3967 	struct ieee80211_vif *vif;
3968 	bool active, atomic, nin_drv;
3969 
3970 	lhw = HW_TO_LHW(hw);
3971 
3972 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
3973 	    IEEE80211_IFACE_ITER_RESUME_ALL|
3974 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
3975 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
3976 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
3977 		    __func__, flags);
3978 	}
3979 
3980 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
3981 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
3982 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
3983 
3984 	if (atomic)
3985 		LKPI_80211_LHW_LVIF_LOCK(lhw);
3986 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3987 		struct ieee80211vap *vap;
3988 
3989 		vif = LVIF_TO_VIF(lvif);
3990 
3991 		/*
3992 		 * If we want "active" interfaces, we need to distinguish on
3993 		 * whether the driver knows about them or not to be able to
3994 		 * handle the "resume" case correctly.  Skip the ones the
3995 		 * driver does not know about.
3996 		 */
3997 		if (active && !lvif->added_to_drv &&
3998 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
3999 			continue;
4000 
4001 		/*
4002 		 * If we shall skip interfaces not added to the driver do so
4003 		 * if we haven't yet.
4004 		 */
4005 		if (nin_drv && !lvif->added_to_drv)
4006 			continue;
4007 
4008 		/*
4009 		 * Run the iterator function if we are either not asking
4010 		 * asking for active only or if the VAP is "running".
4011 		 */
4012 		/* XXX-BZ probably should have state in the lvif as well. */
4013 		vap = LVIF_TO_VAP(lvif);
4014 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
4015 			iterfunc(arg, vif->addr, vif);
4016 	}
4017 	if (atomic)
4018 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4019 }
4020 
4021 void
4022 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
4023     struct ieee80211_vif *vif,
4024     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
4025         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
4026     void *arg)
4027 {
4028 
4029 	UNIMPLEMENTED;
4030 }
4031 
4032 void
4033 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
4034     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
4035 	void *),
4036     void *arg)
4037 {
4038 	struct lkpi_hw *lhw;
4039 	struct lkpi_vif *lvif;
4040 	struct ieee80211_vif *vif;
4041 	struct lkpi_chanctx *lchanctx;
4042 
4043 	KASSERT(hw != NULL && iterfunc != NULL,
4044 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4045 
4046 	lhw = HW_TO_LHW(hw);
4047 
4048 	IMPROVE("lchanctx should be its own list somewhere");
4049 
4050 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4051 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4052 
4053 		vif = LVIF_TO_VIF(lvif);
4054 		if (vif->chanctx_conf == NULL)
4055 			continue;
4056 
4057 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4058 		if (!lchanctx->added_to_drv)
4059 			continue;
4060 
4061 		iterfunc(hw, &lchanctx->conf, arg);
4062 	}
4063 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4064 }
4065 
4066 void
4067 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
4068    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
4069 {
4070 	struct lkpi_hw *lhw;
4071 	struct lkpi_vif *lvif;
4072 	struct lkpi_sta *lsta;
4073 	struct ieee80211_sta *sta;
4074 
4075 	KASSERT(hw != NULL && iterfunc != NULL,
4076 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4077 
4078 	lhw = HW_TO_LHW(hw);
4079 
4080 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4081 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4082 
4083 		LKPI_80211_LVIF_LOCK(lvif);
4084 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4085 			if (!lsta->added_to_drv)
4086 				continue;
4087 			sta = LSTA_TO_STA(lsta);
4088 			iterfunc(arg, sta);
4089 		}
4090 		LKPI_80211_LVIF_UNLOCK(lvif);
4091 	}
4092 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4093 }
4094 
4095 struct linuxkpi_ieee80211_regdomain *
4096 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4097 {
4098 	struct linuxkpi_ieee80211_regdomain *regd;
4099 
4100 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4101 	    GFP_KERNEL);
4102 	return (regd);
4103 }
4104 
4105 int
4106 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
4107     struct linuxkpi_ieee80211_regdomain *regd)
4108 {
4109 	struct lkpi_hw *lhw;
4110 	struct ieee80211com *ic;
4111 	struct ieee80211_regdomain *rd;
4112 
4113 	lhw = wiphy_priv(wiphy);
4114 	ic = lhw->ic;
4115 
4116 	rd = &ic->ic_regdomain;
4117 	if (rd->isocc[0] == '\0') {
4118 		rd->isocc[0] = regd->alpha2[0];
4119 		rd->isocc[1] = regd->alpha2[1];
4120 	}
4121 
4122 	TODO();
4123 	/* XXX-BZ finish the rest. */
4124 
4125 	return (0);
4126 }
4127 
4128 void
4129 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
4130     struct cfg80211_scan_info *info)
4131 {
4132 	struct lkpi_hw *lhw;
4133 	struct ieee80211com *ic;
4134 	struct ieee80211_scan_state *ss;
4135 
4136 	lhw = wiphy_priv(hw->wiphy);
4137 	ic = lhw->ic;
4138 	ss = ic->ic_scan;
4139 
4140 	ieee80211_scan_done(ss->ss_vap);
4141 
4142 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4143 	free(lhw->hw_req, M_LKPI80211);
4144 	lhw->hw_req = NULL;
4145 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4146 	wakeup(lhw);
4147 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4148 
4149 	return;
4150 }
4151 
4152 /* For %list see comment towards the end of the function. */
4153 void
4154 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4155     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4156     struct list_head *list __unused)
4157 {
4158 	struct epoch_tracker et;
4159 	struct lkpi_hw *lhw;
4160 	struct ieee80211com *ic;
4161 	struct mbuf *m;
4162 	struct skb_shared_info *shinfo;
4163 	struct ieee80211_rx_status *rx_status;
4164 	struct ieee80211_rx_stats rx_stats;
4165 	struct ieee80211_node *ni;
4166 	struct ieee80211vap *vap;
4167 	struct ieee80211_hdr *hdr;
4168 	struct lkpi_sta *lsta;
4169 	int i, offset, ok;
4170 	int8_t rssi;
4171 	bool is_beacon;
4172 
4173 	if (skb->len < 2) {
4174 		/* Need 80211 stats here. */
4175 		IMPROVE();
4176 		goto err;
4177 	}
4178 
4179 	/*
4180 	 * For now do the data copy; we can later improve things. Might even
4181 	 * have an mbuf backing the skb data then?
4182 	 */
4183 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
4184 	if (m == NULL)
4185 		goto err;
4186 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
4187 
4188 	shinfo = skb_shinfo(skb);
4189 	offset = m->m_len;
4190 	for (i = 0; i < shinfo->nr_frags; i++) {
4191 		m_copyback(m, offset, shinfo->frags[i].size,
4192 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
4193 		    shinfo->frags[i].offset);
4194 		offset += shinfo->frags[i].size;
4195 	}
4196 
4197 	rx_status = IEEE80211_SKB_RXCB(skb);
4198 
4199 	hdr = (void *)skb->data;
4200 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4201 
4202 #ifdef LINUXKPI_DEBUG_80211
4203 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
4204 		goto no_trace_beacons;
4205 
4206 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4207 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4208 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
4209 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
4210 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
4211 		    shinfo, shinfo->nr_frags,
4212 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
4213 
4214 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
4215 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
4216 
4217 	/* Implement a dump_rxcb() !!! */
4218 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4219 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
4220 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
4221 			__func__,
4222 			(uintmax_t)rx_status->boottime_ns,
4223 			(uintmax_t)rx_status->mactime,
4224 			rx_status->device_timestamp,
4225 			rx_status->flag,
4226 			rx_status->freq,
4227 			rx_status->bw,
4228 			rx_status->encoding,
4229 			rx_status->ampdu_reference,
4230 			rx_status->band,
4231 			rx_status->chains,
4232 			rx_status->chain_signal[0],
4233 			rx_status->chain_signal[1],
4234 			rx_status->chain_signal[2],
4235 			rx_status->chain_signal[3],
4236 			rx_status->signal,
4237 			rx_status->enc_flags,
4238 			rx_status->he_dcm,
4239 			rx_status->he_gi,
4240 			rx_status->he_ru,
4241 			rx_status->zero_length_psdu_type,
4242 			rx_status->nss,
4243 			rx_status->rate_idx);
4244 no_trace_beacons:
4245 #endif
4246 
4247 	memset(&rx_stats, 0, sizeof(rx_stats));
4248 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
4249 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
4250 	rx_stats.c_nf = -96;
4251 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
4252 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4253 		rssi = rx_status->signal;
4254 	else
4255 		rssi = rx_stats.c_nf;
4256 	/*
4257 	 * net80211 signal strength data are in .5 dBm units relative to
4258 	 * the current noise floor (see comment in ieee80211_node.h).
4259 	 */
4260 	rssi -= rx_stats.c_nf;
4261 	rx_stats.c_rssi = rssi * 2;
4262 	rx_stats.r_flags |= IEEE80211_R_BAND;
4263 	rx_stats.c_band =
4264 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
4265 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
4266 	rx_stats.c_freq = rx_status->freq;
4267 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
4268 
4269 	/* XXX (*sta_statistics)() to get to some of that? */
4270 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
4271 
4272 	lhw = HW_TO_LHW(hw);
4273 	ic = lhw->ic;
4274 
4275 	ok = ieee80211_add_rx_params(m, &rx_stats);
4276 	if (ok == 0) {
4277 		m_freem(m);
4278 		counter_u64_add(ic->ic_ierrors, 1);
4279 		goto err;
4280 	}
4281 
4282 	if (sta != NULL) {
4283 		lsta = STA_TO_LSTA(sta);
4284 		ni = ieee80211_ref_node(lsta->ni);
4285 	} else {
4286 		struct ieee80211_frame_min *wh;
4287 
4288 		wh = mtod(m, struct ieee80211_frame_min *);
4289 		ni = ieee80211_find_rxnode(ic, wh);
4290 		if (ni != NULL)
4291 			lsta = ni->ni_drv_data;
4292 	}
4293 
4294 	if (ni != NULL)
4295 		vap = ni->ni_vap;
4296 	else
4297 		/*
4298 		 * XXX-BZ can we improve this by looking at the frame hdr
4299 		 * or other meta-data passed up?
4300 		 */
4301 		vap = TAILQ_FIRST(&ic->ic_vaps);
4302 
4303 #ifdef LINUXKPI_DEBUG_80211
4304 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4305 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
4306 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
4307 		    ni, vap, is_beacon ? " beacon" : "");
4308 #endif
4309 
4310 	if (ni != NULL && vap != NULL && is_beacon &&
4311 	    rx_status->device_timestamp > 0 &&
4312 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4313 		struct lkpi_vif *lvif;
4314 		struct ieee80211_vif *vif;
4315 		struct ieee80211_frame *wh;
4316 
4317 		wh = mtod(m, struct ieee80211_frame *);
4318 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4319 			goto skip_device_ts;
4320 
4321 		lvif = VAP_TO_LVIF(vap);
4322 		vif = LVIF_TO_VIF(lvif);
4323 
4324 		IMPROVE("TIMING_BEACON_ONLY?");
4325 		/* mac80211 specific (not net80211) so keep it here. */
4326 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4327 		/*
4328 		 * net80211 should take care of the other information (sync_tsf,
4329 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4330 		 */
4331 	}
4332 skip_device_ts:
4333 
4334 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
4335 	    ieee80211_radiotap_active_vap(vap)) {
4336 		struct lkpi_radiotap_rx_hdr *rtap;
4337 
4338 		rtap = &lhw->rtap_rx;
4339 		rtap->wr_tsft = rx_status->device_timestamp;
4340 		rtap->wr_flags = 0;
4341 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
4342 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4343 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4344 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4345 #if 0	/* .. or it does not given we strip it below. */
4346 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4347 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
4348 #endif
4349 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
4350 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
4351 		rtap->wr_rate = 0;
4352 		IMPROVE();
4353 		/* XXX TODO status->encoding / rate_index / bw */
4354 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
4355 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
4356 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4357 		rtap->wr_dbm_antsignal = rssi;
4358 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
4359 	}
4360 
4361 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4362 		m_adj(m, -IEEE80211_CRC_LEN);
4363 
4364 #if 0
4365 	if (list != NULL) {
4366 		/*
4367 		* Normally this would be queued up and delivered by
4368 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
4369 		* See mt76::mac80211.c as only current possible consumer.
4370 		*/
4371 		IMPROVE("we simply pass the packet to net80211 to deal with.");
4372 	}
4373 #endif
4374 
4375 	NET_EPOCH_ENTER(et);
4376 	if (ni != NULL) {
4377 		ok = ieee80211_input_mimo(ni, m);
4378 		ieee80211_free_node(ni);
4379 		if (ok < 0)
4380 			m_freem(m);
4381 	} else {
4382 		ok = ieee80211_input_mimo_all(ic, m);
4383 		/* mbuf got consumed. */
4384 	}
4385 	NET_EPOCH_EXIT(et);
4386 
4387 #ifdef LINUXKPI_DEBUG_80211
4388 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4389 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4390 #endif
4391 
4392 	IMPROVE();
4393 
4394 err:
4395 	/* The skb is ours so we can free it :-) */
4396 	kfree_skb(skb);
4397 }
4398 
4399 uint8_t
4400 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
4401 {
4402 	const struct ieee80211_frame *wh;
4403 	uint8_t tid;
4404 
4405 	/* Linux seems to assume this is a QOS-Data-Frame */
4406 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
4407 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
4408 	   hdr->frame_control));
4409 
4410 	wh = (const struct ieee80211_frame *)hdr;
4411 	tid = ieee80211_gettid(wh);
4412 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
4413 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
4414 
4415 	return (tid);
4416 }
4417 
4418 struct wiphy *
4419 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
4420 {
4421 	struct lkpi_wiphy *lwiphy;
4422 
4423 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
4424 	if (lwiphy == NULL)
4425 		return (NULL);
4426 	lwiphy->ops = ops;
4427 
4428 	/* XXX TODO */
4429 	return (LWIPHY_TO_WIPHY(lwiphy));
4430 }
4431 
4432 void
4433 linuxkpi_wiphy_free(struct wiphy *wiphy)
4434 {
4435 	struct lkpi_wiphy *lwiphy;
4436 
4437 	if (wiphy == NULL)
4438 		return;
4439 
4440 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
4441 	kfree(lwiphy);
4442 }
4443 
4444 uint32_t
4445 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
4446     enum nl80211_band band)
4447 {
4448 
4449 	switch (band) {
4450 	case NL80211_BAND_2GHZ:
4451 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
4452 		break;
4453 	case NL80211_BAND_5GHZ:
4454 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
4455 		break;
4456 	default:
4457 		/* XXX abort, retry, error, panic? */
4458 		break;
4459 	}
4460 
4461 	return (0);
4462 }
4463 
4464 uint32_t
4465 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
4466 {
4467 
4468 	return (ieee80211_mhz2ieee(freq, 0));
4469 }
4470 
4471 static struct lkpi_sta *
4472 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
4473 {
4474 	struct lkpi_sta *lsta, *temp;
4475 
4476 	LKPI_80211_LVIF_LOCK(lvif);
4477 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4478 		if (lsta->ni == ni) {
4479 			LKPI_80211_LVIF_UNLOCK(lvif);
4480 			return (lsta);
4481 		}
4482 	}
4483 	LKPI_80211_LVIF_UNLOCK(lvif);
4484 
4485 	return (NULL);
4486 }
4487 
4488 struct ieee80211_sta *
4489 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
4490 {
4491 	struct lkpi_vif *lvif;
4492 	struct lkpi_sta *lsta, *temp;
4493 	struct ieee80211_sta *sta;
4494 
4495 	lvif = VIF_TO_LVIF(vif);
4496 
4497 	LKPI_80211_LVIF_LOCK(lvif);
4498 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4499 		sta = LSTA_TO_STA(lsta);
4500 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
4501 			LKPI_80211_LVIF_UNLOCK(lvif);
4502 			return (sta);
4503 		}
4504 	}
4505 	LKPI_80211_LVIF_UNLOCK(lvif);
4506 	return (NULL);
4507 }
4508 
4509 struct ieee80211_sta *
4510 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
4511     const uint8_t *addr, const uint8_t *ourvifaddr)
4512 {
4513 	struct lkpi_hw *lhw;
4514 	struct lkpi_vif *lvif;
4515 	struct lkpi_sta *lsta;
4516 	struct ieee80211_vif *vif;
4517 	struct ieee80211_sta *sta;
4518 
4519 	lhw = wiphy_priv(hw->wiphy);
4520 	sta = NULL;
4521 
4522 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4523 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4524 
4525 		/* XXX-BZ check our address from the vif. */
4526 
4527 		vif = LVIF_TO_VIF(lvif);
4528 		if (ourvifaddr != NULL &&
4529 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
4530 			continue;
4531 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
4532 		if (sta != NULL)
4533 			break;
4534 	}
4535 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4536 
4537 	if (sta != NULL) {
4538 		lsta = STA_TO_LSTA(sta);
4539 		if (!lsta->added_to_drv)
4540 			return (NULL);
4541 	}
4542 
4543 	return (sta);
4544 }
4545 
4546 struct sk_buff *
4547 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
4548     struct ieee80211_txq *txq)
4549 {
4550 	struct lkpi_txq *ltxq;
4551 	struct lkpi_vif *lvif;
4552 	struct sk_buff *skb;
4553 
4554 	skb = NULL;
4555 	ltxq = TXQ_TO_LTXQ(txq);
4556 	ltxq->seen_dequeue = true;
4557 
4558 	if (ltxq->stopped)
4559 		goto stopped;
4560 
4561 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
4562 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
4563 		ltxq->stopped = true;
4564 		goto stopped;
4565 	}
4566 
4567 	skb = skb_dequeue(&ltxq->skbq);
4568 
4569 stopped:
4570 	return (skb);
4571 }
4572 
4573 void
4574 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4575     unsigned long *frame_cnt, unsigned long *byte_cnt)
4576 {
4577 	struct lkpi_txq *ltxq;
4578 	struct sk_buff *skb;
4579 	unsigned long fc, bc;
4580 
4581 	ltxq = TXQ_TO_LTXQ(txq);
4582 
4583 	fc = bc = 0;
4584 	skb_queue_walk(&ltxq->skbq, skb) {
4585 		fc++;
4586 		bc += skb->len;
4587 	}
4588 	if (frame_cnt)
4589 		*frame_cnt = fc;
4590 	if (byte_cnt)
4591 		*byte_cnt = bc;
4592 
4593 	/* Validate that this is doing the correct thing. */
4594 	/* Should we keep track on en/dequeue? */
4595 	IMPROVE();
4596 }
4597 
4598 /*
4599  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
4600  * The latter tries to derive the success status from the info flags
4601  * passed back from the driver.  rawx_mit() saves the ni on the m and the
4602  * m on the skb for us to be able to give feedback to net80211.
4603  */
4604 static void
4605 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4606     int status)
4607 {
4608 	struct ieee80211_node *ni;
4609 	struct mbuf *m;
4610 
4611 	m = skb->m;
4612 	skb->m = NULL;
4613 
4614 	if (m != NULL) {
4615 		ni = m->m_pkthdr.PH_loc.ptr;
4616 		/* Status: 0 is ok, != 0 is error. */
4617 		ieee80211_tx_complete(ni, m, status);
4618 		/* ni & mbuf were consumed. */
4619 	}
4620 }
4621 
4622 void
4623 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4624     int status)
4625 {
4626 
4627 	_lkpi_ieee80211_free_txskb(hw, skb, status);
4628 	kfree_skb(skb);
4629 }
4630 
4631 void
4632 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
4633     struct ieee80211_tx_status *txstat)
4634 {
4635 	struct sk_buff *skb;
4636 	struct ieee80211_tx_info *info;
4637 	struct ieee80211_ratectl_tx_status txs;
4638 	struct ieee80211_node *ni;
4639 	int status;
4640 
4641 	skb = txstat->skb;
4642 	if (skb->m != NULL) {
4643 		struct mbuf *m;
4644 
4645 		m = skb->m;
4646 		ni = m->m_pkthdr.PH_loc.ptr;
4647 		memset(&txs, 0, sizeof(txs));
4648 	} else {
4649 		ni = NULL;
4650 	}
4651 
4652 	info = txstat->info;
4653 	if (info->flags & IEEE80211_TX_STAT_ACK) {
4654 		status = 0;	/* No error. */
4655 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
4656 	} else {
4657 		status = 1;
4658 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
4659 	}
4660 
4661 	if (ni != NULL) {
4662 		int ridx __unused;
4663 #ifdef LINUXKPI_DEBUG_80211
4664 		int old_rate;
4665 
4666 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
4667 #endif
4668 		txs.pktlen = skb->len;
4669 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
4670 		if (info->status.rates[0].count > 1) {
4671 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
4672 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
4673 		}
4674 #if 0		/* Unused in net80211 currently. */
4675 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
4676 		txs.final_rate = info->status.rates[0].idx;
4677 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
4678 #endif
4679 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
4680 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
4681 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
4682 		}
4683 
4684 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
4685 		ieee80211_ratectl_tx_complete(ni, &txs);
4686 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
4687 
4688 #ifdef LINUXKPI_DEBUG_80211
4689 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
4690 			printf("TX-RATE: %s: old %d new %d ridx %d, "
4691 			    "long_retries %d\n", __func__,
4692 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
4693 			    ridx, txs.long_retries);
4694 		}
4695 #endif
4696 	}
4697 
4698 #ifdef LINUXKPI_DEBUG_80211
4699 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4700 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
4701 		    "band %u hw_queue %u tx_time_est %d : "
4702 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
4703 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
4704 		    "tx_time %u flags %#x "
4705 		    "status_driver_data [ %p %p ]\n",
4706 		    __func__, hw, skb, status, info->flags,
4707 		    info->band, info->hw_queue, info->tx_time_est,
4708 		    info->status.rates[0].idx, info->status.rates[0].count,
4709 		    info->status.rates[0].flags,
4710 		    info->status.rates[1].idx, info->status.rates[1].count,
4711 		    info->status.rates[1].flags,
4712 		    info->status.rates[2].idx, info->status.rates[2].count,
4713 		    info->status.rates[2].flags,
4714 		    info->status.rates[3].idx, info->status.rates[3].count,
4715 		    info->status.rates[3].flags,
4716 		    info->status.ack_signal, info->status.ampdu_ack_len,
4717 		    info->status.ampdu_len, info->status.antenna,
4718 		    info->status.tx_time, info->status.flags,
4719 		    info->status.status_driver_data[0],
4720 		    info->status.status_driver_data[1]);
4721 #endif
4722 
4723 	if (txstat->free_list) {
4724 		_lkpi_ieee80211_free_txskb(hw, skb, status);
4725 		list_add_tail(&skb->list, txstat->free_list);
4726 	} else {
4727 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
4728 	}
4729 }
4730 
4731 void
4732 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
4733 {
4734 	struct ieee80211_tx_status status;
4735 
4736 	memset(&status, 0, sizeof(status));
4737 	status.info = IEEE80211_SKB_CB(skb);
4738 	status.skb = skb;
4739 	/* sta, n_rates, rates, free_list? */
4740 
4741 	ieee80211_tx_status_ext(hw, &status);
4742 }
4743 
4744 /*
4745  * This is an internal bandaid for the moment for the way we glue
4746  * skbs and mbufs together for TX.  Once we have skbs backed by
4747  * mbufs this should go away.
4748  * This is a public function but kept on the private KPI (lkpi_)
4749  * and is not exposed by a header file.
4750  */
4751 static void
4752 lkpi_ieee80211_free_skb_mbuf(void *p)
4753 {
4754 	struct ieee80211_node *ni;
4755 	struct mbuf *m;
4756 
4757 	if (p == NULL)
4758 		return;
4759 
4760 	m = (struct mbuf *)p;
4761 	M_ASSERTPKTHDR(m);
4762 
4763 	ni = m->m_pkthdr.PH_loc.ptr;
4764 	m->m_pkthdr.PH_loc.ptr = NULL;
4765 	if (ni != NULL)
4766 		ieee80211_free_node(ni);
4767 	m_freem(m);
4768 }
4769 
4770 void
4771 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
4772     struct delayed_work *w, int delay)
4773 {
4774 	struct lkpi_hw *lhw;
4775 
4776 	/* Need to make sure hw is in a stable (non-suspended) state. */
4777 	IMPROVE();
4778 
4779 	lhw = HW_TO_LHW(hw);
4780 	queue_delayed_work(lhw->workq, w, delay);
4781 }
4782 
4783 void
4784 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
4785     struct work_struct *w)
4786 {
4787 	struct lkpi_hw *lhw;
4788 
4789 	/* Need to make sure hw is in a stable (non-suspended) state. */
4790 	IMPROVE();
4791 
4792 	lhw = HW_TO_LHW(hw);
4793 	queue_work(lhw->workq, w);
4794 }
4795 
4796 struct sk_buff *
4797 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
4798     uint8_t *ssid, size_t ssid_len, size_t tailroom)
4799 {
4800 	struct sk_buff *skb;
4801 	struct ieee80211_frame *wh;
4802 	uint8_t *p;
4803 	size_t len;
4804 
4805 	len = sizeof(*wh);
4806 	len += 2 + ssid_len;
4807 
4808 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
4809 	if (skb == NULL)
4810 		return (NULL);
4811 
4812 	skb_reserve(skb, hw->extra_tx_headroom);
4813 
4814 	wh = skb_put_zero(skb, sizeof(*wh));
4815 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
4816 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
4817 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4818 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
4819 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4820 
4821 	p = skb_put(skb, 2 + ssid_len);
4822 	*p++ = IEEE80211_ELEMID_SSID;
4823 	*p++ = ssid_len;
4824 	if (ssid_len > 0)
4825 		memcpy(p, ssid, ssid_len);
4826 
4827 	return (skb);
4828 }
4829 
4830 struct sk_buff *
4831 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
4832     struct ieee80211_vif *vif)
4833 {
4834 	struct lkpi_vif *lvif;
4835 	struct ieee80211vap *vap;
4836 	struct sk_buff *skb;
4837 	struct ieee80211_frame_pspoll *psp;
4838 	uint16_t v;
4839 
4840 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
4841 	if (skb == NULL)
4842 		return (NULL);
4843 
4844 	skb_reserve(skb, hw->extra_tx_headroom);
4845 
4846 	lvif = VIF_TO_LVIF(vif);
4847 	vap = LVIF_TO_VAP(lvif);
4848 
4849 	psp = skb_put_zero(skb, sizeof(*psp));
4850 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
4851 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
4852 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
4853 	memcpy(&psp->i_aid, &v, sizeof(v));
4854 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
4855 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
4856 
4857 	return (skb);
4858 }
4859 
4860 struct sk_buff *
4861 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4862     struct ieee80211_vif *vif, int linkid, bool qos)
4863 {
4864 	struct lkpi_vif *lvif;
4865 	struct ieee80211vap *vap;
4866 	struct sk_buff *skb;
4867 	struct ieee80211_frame *nullf;
4868 
4869 	IMPROVE("linkid");
4870 
4871 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
4872 	if (skb == NULL)
4873 		return (NULL);
4874 
4875 	skb_reserve(skb, hw->extra_tx_headroom);
4876 
4877 	lvif = VIF_TO_LVIF(vif);
4878 	vap = LVIF_TO_VAP(lvif);
4879 
4880 	nullf = skb_put_zero(skb, sizeof(*nullf));
4881 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
4882 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
4883 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
4884 
4885 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
4886 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
4887 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
4888 
4889 	return (skb);
4890 }
4891 
4892 struct wireless_dev *
4893 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
4894 {
4895 	struct lkpi_vif *lvif;
4896 
4897 	lvif = VIF_TO_LVIF(vif);
4898 	return (&lvif->wdev);
4899 }
4900 
4901 void
4902 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
4903 {
4904 	struct lkpi_vif *lvif;
4905 	struct ieee80211vap *vap;
4906 	enum ieee80211_state nstate;
4907 	int arg;
4908 
4909 	lvif = VIF_TO_LVIF(vif);
4910 	vap = LVIF_TO_VAP(lvif);
4911 
4912 	/*
4913 	 * Go to init; otherwise we need to elaborately check state and
4914 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4915 	 * Let the statemachine handle all neccessary changes.
4916 	 */
4917 	nstate = IEEE80211_S_INIT;
4918 	arg = 0;	/* Not a valid reason. */
4919 
4920 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4921 	    vif, vap, ieee80211_state_name[vap->iv_state]);
4922 	ieee80211_new_state(vap, nstate, arg);
4923 }
4924 
4925 void
4926 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4927 {
4928 	struct lkpi_vif *lvif;
4929 	struct ieee80211vap *vap;
4930 
4931 	lvif = VIF_TO_LVIF(vif);
4932 	vap = LVIF_TO_VAP(lvif);
4933 
4934 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4935 	    vif, vap, ieee80211_state_name[vap->iv_state]);
4936 	ieee80211_beacon_miss(vap->iv_ic);
4937 }
4938 
4939 /* -------------------------------------------------------------------------- */
4940 
4941 void
4942 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
4943 {
4944 	struct lkpi_hw *lhw;
4945 	struct lkpi_vif *lvif;
4946 	struct ieee80211_vif *vif;
4947 	int ac_count, ac;
4948 
4949 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
4950 	    __func__, qnum, hw->queues, hw));
4951 
4952 	lhw = wiphy_priv(hw->wiphy);
4953 
4954 	/* See lkpi_ic_vap_create(). */
4955 	if (hw->queues >= IEEE80211_NUM_ACS)
4956 		ac_count = IEEE80211_NUM_ACS;
4957 	else
4958 		ac_count = 1;
4959 
4960 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4961 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4962 
4963 		vif = LVIF_TO_VIF(lvif);
4964 		for (ac = 0; ac < ac_count; ac++) {
4965 			IMPROVE_TXQ("LOCKING");
4966 			if (qnum == vif->hw_queue[ac]) {
4967 #ifdef LINUXKPI_DEBUG_80211
4968 				/*
4969 				 * For now log this to better understand
4970 				 * how this is supposed to work.
4971 				 */
4972 				if (lvif->hw_queue_stopped[ac] &&
4973 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
4974 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
4975 					    "lvif %p vif %p ac %d qnum %d already "
4976 					    "stopped\n", __func__, __LINE__,
4977 					    lhw, hw, lvif, vif, ac, qnum);
4978 #endif
4979 				lvif->hw_queue_stopped[ac] = true;
4980 			}
4981 		}
4982 	}
4983 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4984 }
4985 
4986 void
4987 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
4988 {
4989 	int i;
4990 
4991 	IMPROVE_TXQ("Locking; do we need further info?");
4992 	for (i = 0; i < hw->queues; i++)
4993 		linuxkpi_ieee80211_stop_queue(hw, i);
4994 }
4995 
4996 
4997 static void
4998 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
4999 {
5000 	struct lkpi_hw *lhw;
5001 	struct lkpi_vif *lvif;
5002 	struct lkpi_sta *lsta;
5003 	int ac_count, ac, tid;
5004 
5005 	/* See lkpi_ic_vap_create(). */
5006 	if (hw->queues >= IEEE80211_NUM_ACS)
5007 		ac_count = IEEE80211_NUM_ACS;
5008 	else
5009 		ac_count = 1;
5010 
5011 	lhw = wiphy_priv(hw->wiphy);
5012 
5013 	IMPROVE_TXQ("Locking");
5014 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5015 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5016 		struct ieee80211_vif *vif;
5017 
5018 		vif = LVIF_TO_VIF(lvif);
5019 		for (ac = 0; ac < ac_count; ac++) {
5020 
5021 			if (hwq == vif->hw_queue[ac]) {
5022 
5023 				/* XXX-BZ what about software scan? */
5024 
5025 #ifdef LINUXKPI_DEBUG_80211
5026 				/*
5027 				 * For now log this to better understand
5028 				 * how this is supposed to work.
5029 				 */
5030 				if (!lvif->hw_queue_stopped[ac] &&
5031 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
5032 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
5033 					    "lvif %p vif %p ac %d hw_q not stopped\n",
5034 					    __func__, __LINE__,
5035 					    lhw, hw, lvif, vif, ac);
5036 #endif
5037 				lvif->hw_queue_stopped[ac] = false;
5038 
5039 				LKPI_80211_LVIF_LOCK(lvif);
5040 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
5041 					struct ieee80211_sta *sta;
5042 
5043 					sta = LSTA_TO_STA(lsta);
5044 					for (tid = 0; tid < nitems(sta->txq); tid++) {
5045 						struct lkpi_txq *ltxq;
5046 
5047 						if (sta->txq[tid] == NULL)
5048 							continue;
5049 
5050 						if (sta->txq[tid]->ac != ac)
5051 							continue;
5052 
5053 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5054 						if (!ltxq->stopped)
5055 							continue;
5056 
5057 						ltxq->stopped = false;
5058 
5059 						/* XXX-BZ see when this explodes with all the locking. taskq? */
5060 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
5061 					}
5062 				}
5063 				LKPI_80211_LVIF_UNLOCK(lvif);
5064 			}
5065 		}
5066 	}
5067 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5068 }
5069 
5070 void
5071 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
5072 {
5073 	int i;
5074 
5075 	IMPROVE_TXQ("Is this all/enough here?");
5076 	for (i = 0; i < hw->queues; i++)
5077 		lkpi_ieee80211_wake_queues(hw, i);
5078 }
5079 
5080 void
5081 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
5082 {
5083 
5084 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5085 	    __func__, qnum, hw->queues, hw));
5086 
5087 	lkpi_ieee80211_wake_queues(hw, qnum);
5088 }
5089 
5090 /* This is just hardware queues. */
5091 void
5092 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
5093 {
5094 	struct lkpi_hw *lhw;
5095 
5096 	lhw = HW_TO_LHW(hw);
5097 
5098 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
5099 	IMPROVE_TXQ("LOCKING");
5100 	if (++lhw->txq_generation[ac] == 0)
5101 		lhw->txq_generation[ac]++;
5102 }
5103 
5104 struct ieee80211_txq *
5105 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
5106 {
5107 	struct lkpi_hw *lhw;
5108 	struct ieee80211_txq *txq;
5109 	struct lkpi_txq *ltxq;
5110 
5111 	lhw = HW_TO_LHW(hw);
5112 	txq = NULL;
5113 
5114 	IMPROVE_TXQ("LOCKING");
5115 
5116 	/* Check that we are scheduled. */
5117 	if (lhw->txq_generation[ac] == 0)
5118 		goto out;
5119 
5120 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
5121 	if (ltxq == NULL)
5122 		goto out;
5123 	if (ltxq->txq_generation == lhw->txq_generation[ac])
5124 		goto out;
5125 
5126 	ltxq->txq_generation = lhw->txq_generation[ac];
5127 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
5128 	txq = &ltxq->txq;
5129 	TAILQ_ELEM_INIT(ltxq, txq_entry);
5130 
5131 out:
5132 	return (txq);
5133 }
5134 
5135 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
5136     struct ieee80211_txq *txq, bool withoutpkts)
5137 {
5138 	struct lkpi_hw *lhw;
5139 	struct lkpi_txq *ltxq;
5140 
5141 	ltxq = TXQ_TO_LTXQ(txq);
5142 
5143 	IMPROVE_TXQ("LOCKING");
5144 
5145 	/* Only schedule if work to do or asked to anyway. */
5146 	if (!withoutpkts && skb_queue_empty(&ltxq->skbq))
5147 		goto out;
5148 
5149 	/* Make sure we do not double-schedule. */
5150 	if (ltxq->txq_entry.tqe_next != NULL)
5151 		goto out;
5152 
5153 	lhw = HW_TO_LHW(hw);
5154 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
5155 out:
5156 	return;
5157 }
5158 
5159 /* -------------------------------------------------------------------------- */
5160 
5161 struct lkpi_cfg80211_bss {
5162 	u_int refcnt;
5163 	struct cfg80211_bss bss;
5164 };
5165 
5166 struct lkpi_cfg80211_get_bss_iter_lookup {
5167 	struct wiphy *wiphy;
5168 	struct linuxkpi_ieee80211_channel *chan;
5169 	const uint8_t *bssid;
5170 	const uint8_t *ssid;
5171 	size_t ssid_len;
5172 	enum ieee80211_bss_type bss_type;
5173 	enum ieee80211_privacy privacy;
5174 
5175 	/*
5176 	 * Something to store a copy of the result as the net80211 scan cache
5177 	 * is not refoucnted so a scan entry might go away any time.
5178 	 */
5179 	bool match;
5180 	struct cfg80211_bss *bss;
5181 };
5182 
5183 static void
5184 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
5185 {
5186 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
5187 	size_t ielen;
5188 
5189 	lookup = arg;
5190 
5191 	/* Do not try to find another match. */
5192 	if (lookup->match)
5193 		return;
5194 
5195 	/* Nothing to store result. */
5196 	if (lookup->bss == NULL)
5197 		return;
5198 
5199 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
5200 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
5201 		/* We have no idea what to compare to as the drivers only request ANY */
5202 		return;
5203 	}
5204 
5205 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
5206 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
5207 		/* We have no idea what to compare to as the drivers only request ANY */
5208 		return;
5209 	}
5210 
5211 	if (lookup->chan != NULL) {
5212 		struct linuxkpi_ieee80211_channel *chan;
5213 
5214 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
5215 		    se->se_chan->ic_freq);
5216 		if (chan == NULL || chan != lookup->chan)
5217 			return;
5218 	}
5219 
5220 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
5221 		return;
5222 
5223 	if (lookup->ssid) {
5224 		if (lookup->ssid_len != se->se_ssid[1] ||
5225 		    se->se_ssid[1] == 0)
5226 			return;
5227 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
5228 			return;
5229 	}
5230 
5231 	ielen = se->se_ies.len;
5232 
5233 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
5234 	    M_LKPI80211, M_NOWAIT | M_ZERO);
5235 	if (lookup->bss->ies == NULL)
5236 		return;
5237 
5238 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
5239 	lookup->bss->ies->len = ielen;
5240 	if (ielen)
5241 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
5242 
5243 	lookup->match = true;
5244 }
5245 
5246 struct cfg80211_bss *
5247 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
5248     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
5249     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
5250 {
5251 	struct lkpi_cfg80211_bss *lbss;
5252 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
5253 	struct lkpi_hw *lhw;
5254 	struct ieee80211vap *vap;
5255 
5256 	lhw = wiphy_priv(wiphy);
5257 
5258 	/* Let's hope we can alloc. */
5259 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
5260 	if (lbss == NULL) {
5261 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
5262 		return (NULL);
5263 	}
5264 
5265 	lookup.wiphy = wiphy;
5266 	lookup.chan = chan;
5267 	lookup.bssid = bssid;
5268 	lookup.ssid = ssid;
5269 	lookup.ssid_len = ssid_len;
5270 	lookup.bss_type = bss_type;
5271 	lookup.privacy = privacy;
5272 	lookup.match = false;
5273 	lookup.bss = &lbss->bss;
5274 
5275 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
5276 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
5277 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
5278 	if (!lookup.match) {
5279 		free(lbss, M_LKPI80211);
5280 		return (NULL);
5281 	}
5282 
5283 	refcount_init(&lbss->refcnt, 1);
5284 	return (&lbss->bss);
5285 }
5286 
5287 void
5288 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
5289 {
5290 	struct lkpi_cfg80211_bss *lbss;
5291 
5292 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
5293 
5294 	/* Free everything again on refcount ... */
5295 	if (refcount_release(&lbss->refcnt)) {
5296 		free(lbss->bss.ies, M_LKPI80211);
5297 		free(lbss, M_LKPI80211);
5298 	}
5299 }
5300 
5301 void
5302 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
5303 {
5304 	struct lkpi_hw *lhw;
5305 	struct ieee80211com *ic;
5306 	struct ieee80211vap *vap;
5307 
5308 	lhw = wiphy_priv(wiphy);
5309 	ic = lhw->ic;
5310 
5311 	/*
5312 	 * If we haven't called ieee80211_ifattach() yet
5313 	 * or there is no VAP, there are no scans to flush.
5314 	 */
5315 	if (ic == NULL ||
5316 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
5317 		return;
5318 
5319 	/* Should only happen on the current one? Not seen it late enough. */
5320 	IEEE80211_LOCK(ic);
5321 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
5322 		ieee80211_scan_flush(vap);
5323 	IEEE80211_UNLOCK(ic);
5324 }
5325 
5326 /* -------------------------------------------------------------------------- */
5327 
5328 MODULE_VERSION(linuxkpi_wlan, 1);
5329 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
5330 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
5331