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