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