xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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 	ni = ieee80211_ref_node(vap->iv_bss);
1594 	lsta = ni->ni_drv_data;
1595 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1596 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1597 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1598 	sta = LSTA_TO_STA(lsta);
1599 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTHORIZED);
1600 	if (error != 0) {
1601 		IMPROVE("undo some changes?");
1602 		goto out;
1603 	}
1604 
1605 	/* - drv_config (?)
1606 	 * - bss_info_changed
1607 	 * - set_rekey_data (?)
1608 	 *
1609 	 * And now we should be passing packets.
1610 	 */
1611 	IMPROVE("Need that bssid setting, and the keys");
1612 
1613 	bss_changed = 0;
1614 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1615 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1616 
1617 out:
1618 	IEEE80211_LOCK(vap->iv_ic);
1619 	if (ni != NULL)
1620 		ieee80211_free_node(ni);
1621 	return (error);
1622 }
1623 
1624 static int
1625 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1626 {
1627 	int error;
1628 
1629 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
1630 	if (error == 0)
1631 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
1632 	return (error);
1633 }
1634 
1635 static int
1636 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1637 {
1638 	struct lkpi_hw *lhw;
1639 	struct ieee80211_hw *hw;
1640 	struct lkpi_vif *lvif;
1641 	struct ieee80211_vif *vif;
1642 	struct ieee80211_node *ni;
1643 	struct lkpi_sta *lsta;
1644 	struct ieee80211_sta *sta;
1645 	struct ieee80211_prep_tx_info prep_tx_info;
1646 #if 0
1647 	enum ieee80211_bss_changed bss_changed;
1648 #endif
1649 	int error;
1650 
1651 	lhw = vap->iv_ic->ic_softc;
1652 	hw = LHW_TO_HW(lhw);
1653 	lvif = VAP_TO_LVIF(vap);
1654 	vif = LVIF_TO_VIF(lvif);
1655 
1656 	/* Keep ni around. */
1657 	ni = ieee80211_ref_node(vap->iv_bss);
1658 	lsta = ni->ni_drv_data;
1659 	sta = LSTA_TO_STA(lsta);
1660 
1661 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1662 
1663 	IEEE80211_UNLOCK(vap->iv_ic);
1664 
1665 	/* flush, drop. */
1666 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1667 
1668 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1669 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1670 	    !lsta->in_mgd) {
1671 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1672 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1673 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1674 		lsta->in_mgd = true;
1675 	}
1676 
1677 	IEEE80211_LOCK(vap->iv_ic);
1678 
1679 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1680 	error = lvif->iv_newstate(vap, nstate, arg);
1681 	if (error != 0)
1682 		goto outni;
1683 
1684 	IEEE80211_UNLOCK(vap->iv_ic);
1685 
1686 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1687 
1688 	/* Wake tx queues to get packet(s) out. */
1689 	lkpi_wake_tx_queues(hw, sta, true, true);
1690 
1691 	/* flush, no drop */
1692 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1693 
1694 	/* End mgd_complete_tx. */
1695 	if (lsta->in_mgd) {
1696 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1697 		prep_tx_info.success = false;
1698 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1699 		lsta->in_mgd = false;
1700 	}
1701 
1702 #if 0
1703 	/* sync_rx_queues */
1704 	lkpi_80211_mo_sync_rx_queues(hw);
1705 
1706 	/* sta_pre_rcu_remove */
1707         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1708 #endif
1709 
1710 	/* Take the station down. */
1711 
1712 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1713 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1714 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1715 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1716 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1717 	if (error != 0)
1718 		goto out;
1719 
1720 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1721 
1722 	/* Update sta_state (ASSOC to AUTH). */
1723 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1724 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1725 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1726 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1727 	if (error != 0)
1728 		goto out;
1729 
1730 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1731 
1732 #if 0
1733 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1734 	lkpi_disassoc(sta, vif, lhw);
1735 #endif
1736 
1737 	error = EALREADY;
1738 out:
1739 	IEEE80211_LOCK(vap->iv_ic);
1740 outni:
1741 	if (ni != NULL)
1742 		ieee80211_free_node(ni);
1743 	return (error);
1744 }
1745 
1746 static int
1747 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1748 {
1749 	struct lkpi_hw *lhw;
1750 	struct ieee80211_hw *hw;
1751 	struct lkpi_vif *lvif;
1752 	struct ieee80211_vif *vif;
1753 	struct ieee80211_node *ni;
1754 	struct lkpi_sta *lsta;
1755 	struct ieee80211_sta *sta;
1756 	struct ieee80211_prep_tx_info prep_tx_info;
1757 	enum ieee80211_bss_changed bss_changed;
1758 	int error;
1759 
1760 	lhw = vap->iv_ic->ic_softc;
1761 	hw = LHW_TO_HW(lhw);
1762 	lvif = VAP_TO_LVIF(vap);
1763 	vif = LVIF_TO_VIF(lvif);
1764 
1765 	/* Keep ni around. */
1766 	ni = ieee80211_ref_node(vap->iv_bss);
1767 	lsta = ni->ni_drv_data;
1768 	sta = LSTA_TO_STA(lsta);
1769 
1770 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1771 
1772 	IEEE80211_UNLOCK(vap->iv_ic);
1773 
1774 	/* flush, drop. */
1775 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1776 
1777 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1778 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1779 	    !lsta->in_mgd) {
1780 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1781 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1782 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1783 		lsta->in_mgd = true;
1784 	}
1785 
1786 	IEEE80211_LOCK(vap->iv_ic);
1787 
1788 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1789 	error = lvif->iv_newstate(vap, nstate, arg);
1790 	if (error != 0)
1791 		goto outni;
1792 
1793 	IEEE80211_UNLOCK(vap->iv_ic);
1794 
1795 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1796 
1797 	/* Wake tx queues to get packet(s) out. */
1798 	lkpi_wake_tx_queues(hw, sta, true, true);
1799 
1800 	/* flush, no drop */
1801 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1802 
1803 	/* End mgd_complete_tx. */
1804 	if (lsta->in_mgd) {
1805 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1806 		prep_tx_info.success = false;
1807 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1808 		lsta->in_mgd = false;
1809 	}
1810 
1811 	/* sync_rx_queues */
1812 	lkpi_80211_mo_sync_rx_queues(hw);
1813 
1814 	/* sta_pre_rcu_remove */
1815         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1816 
1817 	/* Take the station down. */
1818 
1819 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1820 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1821 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1822 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1823 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1824 	if (error != 0)
1825 		goto out;
1826 
1827 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1828 
1829 	/* Update sta_state (ASSOC to AUTH). */
1830 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1831 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1832 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1833 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1834 	if (error != 0)
1835 		goto out;
1836 
1837 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1838 
1839 	/* Update sta and change state (from AUTH) to NONE. */
1840 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1841 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1842 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1843 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1844 	if (error != 0)
1845 		goto out;
1846 
1847 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1848 
1849 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1850 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1851 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1852 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1853 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1854 	if (error != 0) {
1855 		IMPROVE("do we need to undo the chan ctx?");
1856 		goto out;
1857 	}
1858 #if 0
1859 	lsta->added_to_drv = false;	/* mo manages. */
1860 #endif
1861 
1862 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1863 
1864 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1865 	/*
1866 	 * One would expect this to happen when going off AUTHORIZED.
1867 	 * See comment there; removes the sta from fw.
1868 	 */
1869 	lkpi_disassoc(sta, vif, lhw);
1870 
1871 	IMPROVE("Any bss_info changes to announce?");
1872 	bss_changed = 0;
1873 	vif->bss_conf.qos = 0;
1874 	bss_changed |= BSS_CHANGED_QOS;
1875 	vif->bss_conf.ssid_len = 0;
1876 	memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1877 	bss_changed |= BSS_CHANGED_BSSID;
1878 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1879 
1880 	lkpi_lsta_remove(lsta, lvif);
1881 
1882 	/* conf_tx */
1883 
1884 	/* Take the chan ctx down. */
1885 	if (vif->chanctx_conf != NULL) {
1886 		struct ieee80211_chanctx_conf *conf;
1887 
1888 		conf = vif->chanctx_conf;
1889 		/* Remove vif context. */
1890 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1891 		/* NB: vif->chanctx_conf is NULL now. */
1892 
1893 		/* Remove chan ctx. */
1894 		lkpi_80211_mo_remove_chanctx(hw, conf);
1895 		free(conf, M_LKPI80211);
1896 	}
1897 
1898 	error = EALREADY;
1899 out:
1900 	IEEE80211_LOCK(vap->iv_ic);
1901 outni:
1902 	if (ni != NULL)
1903 		ieee80211_free_node(ni);
1904 	return (error);
1905 }
1906 
1907 static int
1908 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1909 {
1910 
1911 	return (lkpi_sta_run_to_init(vap, nstate, arg));
1912 }
1913 
1914 static int
1915 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1916 {
1917 	int error;
1918 
1919 	error = lkpi_sta_run_to_init(vap, nstate, arg);
1920 	if (error != 0 && error != EALREADY)
1921 		return (error);
1922 
1923 	/* At this point iv_bss is long a new node! */
1924 
1925 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1926 	return (error);
1927 }
1928 
1929 /* -------------------------------------------------------------------------- */
1930 
1931 /*
1932  * The matches the documented state changes in net80211::sta_newstate().
1933  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
1934  * there are "invalid" (so there is a room for failure here).
1935  */
1936 struct fsm_state {
1937 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
1938 	enum ieee80211_state ostate;
1939 	enum ieee80211_state nstate;
1940 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
1941 } sta_state_fsm[] = {
1942 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
1943 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
1944 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
1945 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
1946 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
1947 
1948 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1949 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1950 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
1951 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
1952 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
1953 
1954 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1955 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1956 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
1957 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
1958 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
1959 
1960 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
1961 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
1962 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
1963 
1964 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
1965 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
1966 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
1967 
1968 	/* Dummy at the end without handler. */
1969 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
1970 };
1971 
1972 static int
1973 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1974 {
1975 	struct ieee80211com *ic;
1976 	struct lkpi_hw *lhw;
1977 	struct lkpi_vif *lvif;
1978 	struct ieee80211_vif *vif;
1979 	struct fsm_state *s;
1980 	enum ieee80211_state ostate;
1981 	int error;
1982 
1983 	ic = vap->iv_ic;
1984 	IEEE80211_LOCK_ASSERT(ic);
1985 	ostate = vap->iv_state;
1986 
1987 #ifdef LINUXKPI_DEBUG_80211
1988 	if (linuxkpi_debug_80211 & D80211_TRACE)
1989 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
1990 		    __func__, __LINE__, vap, nstate, arg);
1991 #endif
1992 
1993 	if (vap->iv_opmode == IEEE80211_M_STA) {
1994 
1995 		lhw = ic->ic_softc;
1996 		lvif = VAP_TO_LVIF(vap);
1997 		vif = LVIF_TO_VIF(lvif);
1998 
1999 		/* No need to replicate this in most state handlers. */
2000 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2001 			lkpi_stop_hw_scan(lhw, vif);
2002 
2003 		s = sta_state_fsm;
2004 
2005 	} else {
2006 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2007 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2008 		return (ENOSYS);
2009 	}
2010 
2011 	error = 0;
2012 	for (; s->handler != NULL; s++) {
2013 		if (ostate == s->ostate && nstate == s->nstate) {
2014 #ifdef LINUXKPI_DEBUG_80211
2015 			if (linuxkpi_debug_80211 & D80211_TRACE)
2016 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2017 				    " %d (%s): arg %d.\n", __func__,
2018 				    ostate, ieee80211_state_name[ostate],
2019 				    nstate, ieee80211_state_name[nstate], arg);
2020 #endif
2021 			error = s->handler(vap, nstate, arg);
2022 			break;
2023 		}
2024 	}
2025 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2026 
2027 	if (s->handler == NULL) {
2028 		IMPROVE("turn this into a KASSERT\n");
2029 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
2030 		    "%d (%s) -> %d (%s)\n", __func__,
2031 		    ostate, ieee80211_state_name[ostate],
2032 		    nstate, ieee80211_state_name[nstate]);
2033 		return (ENOSYS);
2034 	}
2035 
2036 	if (error == EALREADY) {
2037 #ifdef LINUXKPI_DEBUG_80211
2038 		if (linuxkpi_debug_80211 & D80211_TRACE)
2039 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2040 			    "%d (%s): iv_newstate already handled: %d.\n",
2041 			    __func__, ostate, ieee80211_state_name[ostate],
2042 			    nstate, ieee80211_state_name[nstate], error);
2043 #endif
2044 		return (0);
2045 	}
2046 
2047 	if (error != 0) {
2048 		/* XXX-BZ currently expected so ignore. */
2049 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
2050 		    "%d (%s) -> %d (%s)\n", __func__, error,
2051 		    ostate, ieee80211_state_name[ostate],
2052 		    nstate, ieee80211_state_name[nstate]);
2053 		/* return (error); */
2054 	}
2055 
2056 #ifdef LINUXKPI_DEBUG_80211
2057 	if (linuxkpi_debug_80211 & D80211_TRACE)
2058 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2059 		    "calling net80211 parent\n",
2060 		    __func__, __LINE__, vap, nstate, arg);
2061 #endif
2062 
2063 	return (lvif->iv_newstate(vap, nstate, arg));
2064 }
2065 
2066 /* -------------------------------------------------------------------------- */
2067 
2068 /*
2069  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2070  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2071  * new node without us knowing and thus our ni/lsta are out of sync.
2072  */
2073 static struct ieee80211_node *
2074 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2075 {
2076 	struct lkpi_vif *lvif;
2077 	struct ieee80211_node *obss;
2078 	struct lkpi_sta *lsta;
2079 
2080 	lvif = VAP_TO_LVIF(vap);
2081 	obss = vap->iv_bss;
2082 
2083 #ifdef LINUXKPI_DEBUG_80211
2084 	if (linuxkpi_debug_80211 & D80211_TRACE)
2085 		ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
2086 		    "ni %p ni_drv_data %p\n", __func__,
2087 		    obss, (obss != NULL) ? obss->ni_drv_data : NULL,
2088 		    ni, (ni != NULL) ? ni->ni_drv_data : NULL);
2089 #endif
2090 
2091 	/* Nothing to copy from.  Just return. */
2092 	if (obss == NULL || obss->ni_drv_data == NULL)
2093 		goto out;
2094 
2095 	/* Nothing to copy to.  Just return. */
2096 	IMPROVE("clearing the obss might still be needed?");
2097 	if (ni == NULL)
2098 		goto out;
2099 
2100 	/* Nothing changed? panic? */
2101 	if (obss == ni)
2102 		goto out;
2103 
2104 	lsta = obss->ni_drv_data;
2105 	obss->ni_drv_data = ni->ni_drv_data;
2106 	ni->ni_drv_data = lsta;
2107 	if (lsta != NULL)
2108 		lsta->ni = ni;
2109 	lsta = obss->ni_drv_data;
2110 	if (lsta != NULL)
2111 		lsta->ni = obss;
2112 
2113 out:
2114 	return (lvif->iv_update_bss(vap, ni));
2115 }
2116 
2117 static int
2118 lkpi_ic_wme_update(struct ieee80211com *ic)
2119 {
2120 	/* This needs queuing and go at the right moment. */
2121 #ifdef WITH_WME_UPDATE
2122 	struct ieee80211vap *vap;
2123 	struct lkpi_hw *lhw;
2124 	struct ieee80211_hw *hw;
2125 	struct lkpi_vif *lvif;
2126 	struct ieee80211_vif *vif;
2127 	struct chanAccParams chp;
2128 	struct wmeParams wmeparr[WME_NUM_AC];
2129 	struct ieee80211_tx_queue_params txqp;
2130 	enum ieee80211_bss_changed changed;
2131 	int error;
2132 	uint16_t ac;
2133 #endif
2134 
2135 	IMPROVE();
2136 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2137 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2138 
2139 #ifdef WITH_WME_UPDATE
2140 	vap = TAILQ_FIRST(&ic->ic_vaps);
2141 	if (vap == NULL)
2142 		return (0);
2143 
2144 	/* We should factor this out into per-vap (*wme_update). */
2145 	lhw = ic->ic_softc;
2146 	if (lhw->ops->conf_tx == NULL)
2147 		return (0);
2148 
2149 	/* XXX-BZ check amount of hw queues */
2150 	hw = LHW_TO_HW(lhw);
2151 	lvif = VAP_TO_LVIF(vap);
2152 	vif = LVIF_TO_VIF(lvif);
2153 
2154 	ieee80211_wme_ic_getparams(ic, &chp);
2155 	IEEE80211_LOCK(ic);
2156 	for (ac = 0; ac < WME_NUM_AC; ac++)
2157 		wmeparr[ac] = chp.cap_wmeParams[ac];
2158 	IEEE80211_UNLOCK(ic);
2159 
2160 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2161 	LKPI_80211_LHW_LOCK(lhw);
2162 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2163 		struct wmeParams *wmep;
2164 
2165 		/* XXX-BZ should keep this in lvif? */
2166 		wmep = &wmeparr[ac];
2167 		bzero(&txqp, sizeof(txqp));
2168 		txqp.cw_min = wmep->wmep_logcwmin;
2169 		txqp.cw_max = wmep->wmep_logcwmax;
2170 		txqp.txop = wmep->wmep_txopLimit;
2171 		txqp.aifs = wmep->wmep_aifsn;
2172 		error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
2173 		if (error != 0)
2174 			printf("%s: conf_tx ac %u failed %d\n",
2175 			    __func__, ac, error);
2176 	}
2177 	LKPI_80211_LHW_UNLOCK(lhw);
2178 	changed = BSS_CHANGED_QOS;
2179 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2180 #endif
2181 
2182 	return (0);
2183 }
2184 
2185 static struct ieee80211vap *
2186 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2187     int unit, enum ieee80211_opmode opmode, int flags,
2188     const uint8_t bssid[IEEE80211_ADDR_LEN],
2189     const uint8_t mac[IEEE80211_ADDR_LEN])
2190 {
2191 	struct lkpi_hw *lhw;
2192 	struct ieee80211_hw *hw;
2193 	struct lkpi_vif *lvif;
2194 	struct ieee80211vap *vap;
2195 	struct ieee80211_vif *vif;
2196 	enum ieee80211_bss_changed changed;
2197 	size_t len;
2198 	int error;
2199 
2200 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
2201 		return (NULL);
2202 
2203 	lhw = ic->ic_softc;
2204 	hw = LHW_TO_HW(lhw);
2205 
2206 	len = sizeof(*lvif);
2207 	len += hw->vif_data_size;	/* vif->drv_priv */
2208 
2209 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2210 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2211 	TAILQ_INIT(&lvif->lsta_head);
2212 	vap = LVIF_TO_VAP(lvif);
2213 
2214 	vif = LVIF_TO_VIF(lvif);
2215 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2216 	vif->p2p = false;
2217 	vif->probe_req_reg = false;
2218 	vif->type = lkpi_opmode_to_vif_type(opmode);
2219 	lvif->wdev.iftype = vif->type;
2220 	/* Need to fill in other fields as well. */
2221 	IMPROVE();
2222 
2223 	/* XXX-BZ hardcoded for now! */
2224 #if 1
2225 	vif->chanctx_conf = NULL;
2226 	vif->bss_conf.idle = true;
2227 	vif->bss_conf.ps = false;
2228 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2229 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2230 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
2231 	vif->bss_conf.qos = false;
2232 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
2233 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2234 	vif->bss_conf.assoc = false;
2235 	vif->bss_conf.aid = 0;
2236 #endif
2237 #if 0
2238 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2239 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2240 	vif->bss_conf.beacon_int = ic->ic_bintval;
2241 	/* iwlwifi bug. */
2242 	if (vif->bss_conf.beacon_int < 16)
2243 		vif->bss_conf.beacon_int = 16;
2244 #endif
2245 	IMPROVE();
2246 
2247 	error = lkpi_80211_mo_start(hw);
2248 	if (error != 0) {
2249 		printf("%s: failed to start hw: %d\n", __func__, error);
2250 		mtx_destroy(&lvif->mtx);
2251 		free(lvif, M_80211_VAP);
2252 		return (NULL);
2253 	}
2254 
2255 	error = lkpi_80211_mo_add_interface(hw, vif);
2256 	if (error != 0) {
2257 		IMPROVE();	/* XXX-BZ mo_stop()? */
2258 		printf("%s: failed to add interface: %d\n", __func__, error);
2259 		mtx_destroy(&lvif->mtx);
2260 		free(lvif, M_80211_VAP);
2261 		return (NULL);
2262 	}
2263 
2264 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2265 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2266 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2267 
2268 	/* Set bss_info. */
2269 	changed = 0;
2270 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2271 
2272 	/* conf_tx setup; default WME? */
2273 
2274 	/* Force MC init. */
2275 	lkpi_update_mcast_filter(ic, true);
2276 
2277 	IMPROVE();
2278 
2279 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2280 
2281 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2282 	lvif->iv_newstate = vap->iv_newstate;
2283 	vap->iv_newstate = lkpi_iv_newstate;
2284 	lvif->iv_update_bss = vap->iv_update_bss;
2285 	vap->iv_update_bss = lkpi_iv_update_bss;
2286 
2287 	/* Key management. */
2288 	if (lhw->ops->set_key != NULL) {
2289 #ifdef TRY_HW_CRYPTO
2290 		vap->iv_key_set = lkpi_iv_key_set;
2291 		vap->iv_key_delete = lkpi_iv_key_delete;
2292 #endif
2293 	}
2294 
2295 	ieee80211_ratectl_init(vap);
2296 
2297 	/* Complete setup. */
2298 	ieee80211_vap_attach(vap, ieee80211_media_change,
2299 	    ieee80211_media_status, mac);
2300 
2301 	if (hw->max_listen_interval == 0)
2302 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2303 	hw->conf.listen_interval = hw->max_listen_interval;
2304 	ic->ic_set_channel(ic);
2305 
2306 	/* XXX-BZ do we need to be able to update these? */
2307 	hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
2308 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2309 	hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
2310 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2311 	/* any others? */
2312 	IMPROVE();
2313 
2314 	return (vap);
2315 }
2316 
2317 static void
2318 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2319 {
2320 	struct ieee80211com *ic;
2321 	struct lkpi_hw *lhw;
2322 	struct ieee80211_hw *hw;
2323 	struct lkpi_vif *lvif;
2324 	struct ieee80211_vif *vif;
2325 
2326 	lvif = VAP_TO_LVIF(vap);
2327 	vif = LVIF_TO_VIF(lvif);
2328 	ic = vap->iv_ic;
2329 	lhw = ic->ic_softc;
2330 	hw = LHW_TO_HW(lhw);
2331 
2332 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2333 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
2334 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2335 	lkpi_80211_mo_remove_interface(hw, vif);
2336 
2337 	ieee80211_ratectl_deinit(vap);
2338 	ieee80211_vap_detach(vap);
2339 	mtx_destroy(&lvif->mtx);
2340 	free(lvif, M_80211_VAP);
2341 }
2342 
2343 static void
2344 lkpi_ic_update_mcast(struct ieee80211com *ic)
2345 {
2346 
2347 	lkpi_update_mcast_filter(ic, false);
2348 	TRACEOK();
2349 }
2350 
2351 static void
2352 lkpi_ic_update_promisc(struct ieee80211com *ic)
2353 {
2354 
2355 	UNIMPLEMENTED;
2356 }
2357 
2358 static void
2359 lkpi_ic_update_chw(struct ieee80211com *ic)
2360 {
2361 
2362 	UNIMPLEMENTED;
2363 }
2364 
2365 /* Start / stop device. */
2366 static void
2367 lkpi_ic_parent(struct ieee80211com *ic)
2368 {
2369 	struct lkpi_hw *lhw;
2370 	struct ieee80211_hw *hw;
2371 	int error;
2372 	bool start_all;
2373 
2374 	IMPROVE();
2375 
2376 	lhw = ic->ic_softc;
2377 	hw = LHW_TO_HW(lhw);
2378 	start_all = false;
2379 
2380 	if (ic->ic_nrunning > 0) {
2381 		error = lkpi_80211_mo_start(hw);
2382 		if (error == 0)
2383 			start_all = true;
2384 	} else {
2385 		lkpi_80211_mo_stop(hw);
2386 	}
2387 
2388 	if (start_all)
2389 		ieee80211_start_all(ic);
2390 }
2391 
2392 bool
2393 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
2394     size_t ie_ids_len)
2395 {
2396 	int i;
2397 
2398 	for (i = 0; i < ie_ids_len; i++) {
2399 		if (ie == *ie_ids)
2400 			return (true);
2401 	}
2402 
2403 	return (false);
2404 }
2405 
2406 /* Return true if skipped; false if error. */
2407 bool
2408 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
2409 {
2410 	size_t x;
2411 	uint8_t l;
2412 
2413 	x = *xp;
2414 
2415 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
2416 	    __func__, x, ies_len, ies));
2417 	l = ies[x + 1];
2418 	x += 2 + l;
2419 
2420 	if (x > ies_len)
2421 		return (false);
2422 
2423 	*xp = x;
2424 	return (true);
2425 }
2426 
2427 static uint8_t *
2428 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2429     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
2430 {
2431 	struct ieee80211_supported_band *supband;
2432 	struct linuxkpi_ieee80211_channel *channels;
2433 	const struct ieee80211_channel *chan;
2434 	const struct ieee80211_rateset *rs;
2435 	uint8_t *pb;
2436 	int band, i;
2437 
2438 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2439 		if ((band_mask & (1 << band)) == 0)
2440 			continue;
2441 
2442 		supband = hw->wiphy->bands[band];
2443 		/*
2444 		 * This should not happen;
2445 		 * band_mask is a bitmask of valid bands to scan on.
2446 		 */
2447 		if (supband == NULL || supband->n_channels == 0)
2448 			continue;
2449 
2450 		/* Find a first channel to get the mode and rates from. */
2451 		channels = supband->channels;
2452 		chan = NULL;
2453 		for (i = 0; i < supband->n_channels; i++) {
2454 
2455 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2456 				continue;
2457 
2458 			chan = ieee80211_find_channel(vap->iv_ic,
2459 			    channels[i].center_freq, 0);
2460 			if (chan != NULL)
2461 				break;
2462 		}
2463 
2464 		/* This really should not happen. */
2465 		if (chan == NULL)
2466 			continue;
2467 
2468 		pb = p;
2469 		rs = ieee80211_get_suprates(vap->iv_ic, chan);	/* calls chan2mode */
2470 		p = ieee80211_add_rates(p, rs);
2471 		p = ieee80211_add_xrates(p, rs);
2472 
2473 		scan_ies->ies[band] = pb;
2474 		scan_ies->len[band] = p - pb;
2475 	}
2476 
2477 	/* Add common_ies */
2478 	pb = p;
2479 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2480 	    vap->iv_wpa_ie != NULL) {
2481 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2482 		p += 2 + vap->iv_wpa_ie[1];
2483 	}
2484 	if (vap->iv_appie_probereq != NULL) {
2485 		memcpy(p, vap->iv_appie_probereq->ie_data,
2486 		    vap->iv_appie_probereq->ie_len);
2487 		p += vap->iv_appie_probereq->ie_len;
2488 	}
2489 	scan_ies->common_ies = pb;
2490 	scan_ies->common_ie_len = p - pb;
2491 
2492 	return (p);
2493 }
2494 
2495 static void
2496 lkpi_ic_scan_start(struct ieee80211com *ic)
2497 {
2498 	struct lkpi_hw *lhw;
2499 	struct ieee80211_hw *hw;
2500 	struct lkpi_vif *lvif;
2501 	struct ieee80211_vif *vif;
2502 	struct ieee80211_scan_state *ss;
2503 	struct ieee80211vap *vap;
2504 	int error;
2505 
2506 	lhw = ic->ic_softc;
2507 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0) {
2508 		/* A scan is still running. */
2509 		return;
2510 	}
2511 
2512 	ss = ic->ic_scan;
2513 	vap = ss->ss_vap;
2514 	if (vap->iv_state != IEEE80211_S_SCAN) {
2515 		IMPROVE("We need to be able to scan if not in S_SCAN");
2516 		return;
2517 	}
2518 
2519 	hw = LHW_TO_HW(lhw);
2520 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) {
2521 sw_scan:
2522 		lvif = VAP_TO_LVIF(vap);
2523 		vif = LVIF_TO_VIF(lvif);
2524 
2525 		if (vap->iv_state == IEEE80211_S_SCAN)
2526 			lkpi_hw_conf_idle(hw, false);
2527 
2528 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
2529 		/* net80211::scan_start() handled PS for us. */
2530 		IMPROVE();
2531 		/* XXX Also means it is too late to flush queues?
2532 		 * need to check iv_sta_ps or overload? */
2533 		/* XXX want to adjust ss end time/ maxdwell? */
2534 
2535 	} else {
2536 		struct ieee80211_channel *c;
2537 		struct ieee80211_scan_request *hw_req;
2538 		struct linuxkpi_ieee80211_channel *lc, **cpp;
2539 		struct cfg80211_ssid *ssids;
2540 		struct cfg80211_scan_6ghz_params *s6gp;
2541 		size_t chan_len, nchan, ssids_len, s6ghzlen;
2542 		int band, i, ssid_count, common_ie_len;
2543 		uint32_t band_mask;
2544 		uint8_t *ie, *ieend;
2545 
2546 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2547 			IMPROVE("individual band scans not yet supported");
2548 			/* In theory net80211 would have to drive this. */
2549 			return;
2550 		}
2551 
2552 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
2553 		ssids_len = ssid_count * sizeof(*ssids);
2554 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
2555 
2556 		band_mask = 0;
2557 		nchan = 0;
2558 		for (i = ss->ss_next; i < ss->ss_last; i++) {
2559 			nchan++;
2560 			band = lkpi_net80211_chan_to_nl80211_band(
2561 			    ss->ss_chans[ss->ss_next + i]);
2562 			band_mask |= (1 << band);
2563 		}
2564 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
2565 
2566 		common_ie_len = 0;
2567 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2568 		    vap->iv_wpa_ie != NULL)
2569 			common_ie_len += vap->iv_wpa_ie[1];
2570 		if (vap->iv_appie_probereq != NULL)
2571 			common_ie_len += vap->iv_appie_probereq->ie_len;
2572 
2573 		/* We would love to check this at an earlier stage... */
2574 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
2575 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
2576 			    "wiphy->max_scan_ie_len %d\n", __func__,
2577 			    common_ie_len, hw->wiphy->max_scan_ie_len);
2578 		}
2579 
2580 		KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
2581 		    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
2582 
2583 		lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
2584 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
2585 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
2586 
2587 		hw_req->req.flags = 0;			/* XXX ??? */
2588 		/* hw_req->req.wdev */
2589 		hw_req->req.wiphy = hw->wiphy;
2590 		hw_req->req.no_cck = false;		/* XXX */
2591 #if 0
2592 		/* This seems to pessimise default scanning behaviour. */
2593 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
2594 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
2595 #endif
2596 #ifdef __notyet__
2597 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
2598 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
2599 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
2600 #endif
2601 
2602 		hw_req->req.n_channels = nchan;
2603 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
2604 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
2605 		for (i = 0; i < nchan; i++) {
2606 			*(cpp + i) =
2607 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
2608 		}
2609 		for (i = 0; i < nchan; i++) {
2610 			c = ss->ss_chans[ss->ss_next + i];
2611 
2612 			lc->hw_value = c->ic_ieee;
2613 			lc->center_freq = c->ic_freq;
2614 			/* lc->flags */
2615 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
2616 			lc->max_power = c->ic_maxpower;
2617 			/* lc-> ... */
2618 			lc++;
2619 		}
2620 
2621 		hw_req->req.n_ssids = ssid_count;
2622 		if (hw_req->req.n_ssids > 0) {
2623 			ssids = (struct cfg80211_ssid *)lc;
2624 			hw_req->req.ssids = ssids;
2625 			for (i = 0; i < ssid_count; i++) {
2626 				ssids->ssid_len = ss->ss_ssid[i].len;
2627 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
2628 				    ss->ss_ssid[i].len);
2629 				ssids++;
2630 			}
2631 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
2632 		} else {
2633 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
2634 		}
2635 
2636 		/* 6GHz one day. */
2637 		hw_req->req.n_6ghz_params = 0;
2638 		hw_req->req.scan_6ghz_params = NULL;
2639 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
2640 		/* s6gp->... */
2641 
2642 		ie = ieend = (uint8_t *)s6gp;
2643 		/* Copy per-band IEs, copy common IEs */
2644 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
2645 		hw_req->req.ie = ie;
2646 		hw_req->req.ie_len = ieend - ie;
2647 
2648 		lvif = VAP_TO_LVIF(vap);
2649 		vif = LVIF_TO_VIF(lvif);
2650 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
2651 		if (error != 0) {
2652 			ieee80211_cancel_scan(vap);
2653 
2654 			free(hw_req, M_LKPI80211);
2655 			lhw->hw_req = NULL;
2656 
2657 			/*
2658 			 * XXX-SIGH magic number.
2659 			 * rtw88 has a magic "return 1" if offloading scan is
2660 			 * not possible.  Fall back to sw scan in that case.
2661 			 */
2662 			if (error == 1) {
2663 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2664 				ieee80211_start_scan(vap,
2665 				    IEEE80211_SCAN_ACTIVE |
2666 				    IEEE80211_SCAN_NOPICK |
2667 				    IEEE80211_SCAN_ONCE,
2668 				    IEEE80211_SCAN_FOREVER,
2669 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2670 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2671 				    vap->iv_des_nssid, vap->iv_des_ssid);
2672 				goto sw_scan;
2673 			}
2674 
2675 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2676 			    __func__, error);
2677 		}
2678 	}
2679 }
2680 
2681 static void
2682 lkpi_ic_scan_end(struct ieee80211com *ic)
2683 {
2684 	struct lkpi_hw *lhw;
2685 	struct ieee80211_scan_state *ss;
2686 	struct ieee80211vap *vap;
2687 
2688 	lhw = ic->ic_softc;
2689 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
2690 		return;
2691 	}
2692 
2693 	ss = ic->ic_scan;
2694 	vap = ss->ss_vap;
2695 	if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
2696 		/* Nothing to do. */
2697 	} else {
2698 		struct ieee80211_hw *hw;
2699 		struct lkpi_vif *lvif;
2700 		struct ieee80211_vif *vif;
2701 
2702 		hw = LHW_TO_HW(lhw);
2703 		lvif = VAP_TO_LVIF(vap);
2704 		vif = LVIF_TO_VIF(lvif);
2705 		lkpi_80211_mo_sw_scan_complete(hw, vif);
2706 
2707 		/* Send PS to stop buffering if n80211 does not for us? */
2708 
2709 		if (vap->iv_state == IEEE80211_S_SCAN)
2710 			lkpi_hw_conf_idle(hw, true);
2711 	}
2712 }
2713 
2714 static void
2715 lkpi_ic_set_channel(struct ieee80211com *ic)
2716 {
2717 	struct lkpi_hw *lhw;
2718 	struct ieee80211_hw *hw;
2719 	struct ieee80211_channel *c;
2720 	struct linuxkpi_ieee80211_channel *chan;
2721 	int error;
2722 
2723 	lhw = ic->ic_softc;
2724 
2725 	/* If we do not support (*config)() save us the work. */
2726 	if (lhw->ops->config == NULL)
2727 		return;
2728 
2729 	c = ic->ic_curchan;
2730 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
2731 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
2732 		    c, lhw->ops->config);
2733 		return;
2734 	}
2735 
2736 	chan = lkpi_find_lkpi80211_chan(lhw, c);
2737 	if (chan == NULL) {
2738 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
2739 		    c, chan);
2740 		return;
2741 	}
2742 
2743 	/* XXX max power for scanning? */
2744 	IMPROVE();
2745 
2746 	hw = LHW_TO_HW(lhw);
2747 	cfg80211_chandef_create(&hw->conf.chandef, chan,
2748 	    NL80211_CHAN_NO_HT);
2749 
2750 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
2751 	if (error != 0 && error != EOPNOTSUPP) {
2752 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
2753 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
2754 		/* XXX should we unroll to the previous chandef? */
2755 		IMPROVE();
2756 	} else {
2757 		/* Update radiotap channels as well. */
2758 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
2759 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
2760 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
2761 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
2762 	}
2763 
2764 	/* Currently PS is hard coded off! Not sure it belongs here. */
2765 	IMPROVE();
2766 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
2767 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
2768 		hw->conf.flags &= ~IEEE80211_CONF_PS;
2769 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
2770 		if (error != 0 && error != EOPNOTSUPP)
2771 			ic_printf(ic, "ERROR: %s: config %#0x returned "
2772 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
2773 			    error);
2774 	}
2775 }
2776 
2777 static struct ieee80211_node *
2778 lkpi_ic_node_alloc(struct ieee80211vap *vap,
2779     const uint8_t mac[IEEE80211_ADDR_LEN])
2780 {
2781 	struct ieee80211com *ic;
2782 	struct lkpi_hw *lhw;
2783 	struct ieee80211_node *ni;
2784 	struct ieee80211_hw *hw;
2785 	struct lkpi_sta *lsta;
2786 
2787 	ic = vap->iv_ic;
2788 	lhw = ic->ic_softc;
2789 
2790 	/* We keep allocations de-coupled so we can deal with the two worlds. */
2791 	if (lhw->ic_node_alloc == NULL)
2792 		return (NULL);
2793 
2794 	ni = lhw->ic_node_alloc(vap, mac);
2795 	if (ni == NULL)
2796 		return (NULL);
2797 
2798 	hw = LHW_TO_HW(lhw);
2799 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
2800 	if (lsta == NULL) {
2801 		if (lhw->ic_node_free != NULL)
2802 			lhw->ic_node_free(ni);
2803 		return (NULL);
2804 	}
2805 
2806 	return (ni);
2807 }
2808 
2809 static int
2810 lkpi_ic_node_init(struct ieee80211_node *ni)
2811 {
2812 	struct ieee80211com *ic;
2813 	struct lkpi_hw *lhw;
2814 	struct lkpi_sta *lsta;
2815 	struct lkpi_vif *lvif;
2816 	int error;
2817 
2818 	ic = ni->ni_ic;
2819 	lhw = ic->ic_softc;
2820 
2821 	if (lhw->ic_node_init != NULL) {
2822 		error = lhw->ic_node_init(ni);
2823 		if (error != 0)
2824 			return (error);
2825 	}
2826 
2827 	lvif = VAP_TO_LVIF(ni->ni_vap);
2828 
2829 	lsta = ni->ni_drv_data;
2830 
2831 	/* Now take the reference before linking it to the table. */
2832 	lsta->ni = ieee80211_ref_node(ni);
2833 
2834 	LKPI_80211_LVIF_LOCK(lvif);
2835 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
2836 	LKPI_80211_LVIF_UNLOCK(lvif);
2837 
2838 	/* XXX-BZ Sync other state over. */
2839 	IMPROVE();
2840 
2841 	return (0);
2842 }
2843 
2844 static void
2845 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
2846 {
2847 	struct ieee80211com *ic;
2848 	struct lkpi_hw *lhw;
2849 
2850 	ic = ni->ni_ic;
2851 	lhw = ic->ic_softc;
2852 
2853 	/* XXX-BZ remove from driver, ... */
2854 	IMPROVE();
2855 
2856 	if (lhw->ic_node_cleanup != NULL)
2857 		lhw->ic_node_cleanup(ni);
2858 }
2859 
2860 static void
2861 lkpi_ic_node_free(struct ieee80211_node *ni)
2862 {
2863 	struct ieee80211com *ic;
2864 	struct lkpi_hw *lhw;
2865 	struct lkpi_sta *lsta;
2866 
2867 	ic = ni->ni_ic;
2868 	lhw = ic->ic_softc;
2869 	lsta = ni->ni_drv_data;
2870 	if (lsta == NULL)
2871 		goto out;
2872 
2873 	/* XXX-BZ free resources, ... */
2874 	IMPROVE();
2875 
2876 	/* Flush mbufq (make sure to release ni refs!). */
2877 #ifdef __notyet__
2878 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
2879 	    __func__, lsta, mbufq_len(&lsta->txq)));
2880 #endif
2881 	/* Drain taskq. */
2882 
2883 	/* Drain sta->txq[] */
2884 	mtx_destroy(&lsta->txq_mtx);
2885 
2886 	/* Remove lsta if added_to_drv. */
2887 	/* Remove lsta from vif */
2888 
2889 	/* remove ref from lsta node... */
2890 
2891 	/* Free lsta. */
2892 
2893 out:
2894 	if (lhw->ic_node_free != NULL)
2895 		lhw->ic_node_free(ni);
2896 }
2897 
2898 static int
2899 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2900         const struct ieee80211_bpf_params *params __unused)
2901 {
2902 	struct lkpi_sta *lsta;
2903 
2904 	lsta = ni->ni_drv_data;
2905 
2906 	/* Queue the packet and enqueue the task to handle it. */
2907 	LKPI_80211_LSTA_LOCK(lsta);
2908 	mbufq_enqueue(&lsta->txq, m);
2909 	LKPI_80211_LSTA_UNLOCK(lsta);
2910 
2911 #ifdef LINUXKPI_DEBUG_80211
2912 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
2913 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
2914 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
2915 		    mbufq_len(&lsta->txq));
2916 #endif
2917 
2918 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
2919 	return (0);
2920 }
2921 
2922 static void
2923 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
2924 {
2925 	struct ieee80211_node *ni;
2926 	struct ieee80211_frame *wh;
2927 	struct ieee80211_key *k;
2928 	struct sk_buff *skb;
2929 	struct ieee80211com *ic;
2930 	struct lkpi_hw *lhw;
2931 	struct ieee80211_hw *hw;
2932 	struct lkpi_vif *lvif;
2933 	struct ieee80211_vif *vif;
2934 	struct ieee80211_channel *c;
2935 	struct ieee80211_tx_control control;
2936 	struct ieee80211_tx_info *info;
2937 	struct ieee80211_sta *sta;
2938 	void *buf;
2939 	int ac;
2940 
2941 	M_ASSERTPKTHDR(m);
2942 #ifdef LINUXKPI_DEBUG_80211
2943 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
2944 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
2945 #endif
2946 
2947 	ni = lsta->ni;
2948 #ifndef TRY_HW_CRYPTO
2949 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
2950 	wh = mtod(m, struct ieee80211_frame *);
2951 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2952 		/* Retrieve key for TX && do software encryption. */
2953 		k = ieee80211_crypto_encap(ni, m);
2954 		if (k == NULL) {
2955 			ieee80211_free_node(ni);
2956 			m_freem(m);
2957 			return;
2958 		}
2959 	}
2960 #endif
2961 
2962 	ic = ni->ni_ic;
2963 	lhw = ic->ic_softc;
2964 	hw = LHW_TO_HW(lhw);
2965 	c = ni->ni_chan;
2966 
2967 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
2968 		struct lkpi_radiotap_tx_hdr *rtap;
2969 
2970 		rtap = &lhw->rtap_tx;
2971 		rtap->wt_flags = 0;
2972 		if (k != NULL)
2973 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2974 		if (m->m_flags & M_FRAG)
2975 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2976 		IMPROVE();
2977 		rtap->wt_rate = 0;
2978 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
2979 			rtap->wt_chan_freq = htole16(c->ic_freq);
2980 			rtap->wt_chan_flags = htole16(c->ic_flags);
2981 		}
2982 
2983 		ieee80211_radiotap_tx(ni->ni_vap, m);
2984 	}
2985 
2986 	/*
2987 	 * net80211 should handle hw->extra_tx_headroom.
2988 	 * Though for as long as we are copying we don't mind.
2989 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
2990 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
2991 	 */
2992 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
2993 	if (skb == NULL) {
2994 		printf("XXX ERROR %s: skb alloc failed\n", __func__);
2995 		ieee80211_free_node(ni);
2996 		m_freem(m);
2997 		return;
2998 	}
2999 	skb_reserve(skb, hw->extra_tx_headroom);
3000 
3001 	/* XXX-BZ we need a SKB version understanding mbuf. */
3002 	/* Save the mbuf for ieee80211_tx_complete(). */
3003 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3004 	skb->m = m;
3005 #if 0
3006 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3007 #else
3008 	buf = skb_put(skb, m->m_pkthdr.len);
3009 	m_copydata(m, 0, m->m_pkthdr.len, buf);
3010 #endif
3011 	/* Save the ni. */
3012 	m->m_pkthdr.PH_loc.ptr = ni;
3013 
3014 	lvif = VAP_TO_LVIF(ni->ni_vap);
3015 	vif = LVIF_TO_VIF(lvif);
3016 
3017 	/* XXX-BZ review this at some point [4] vs. [8] vs. [16](TID). */
3018 	ac = M_WME_GETAC(m);
3019 	skb->priority = WME_AC_TO_TID(ac);
3020 	ac = lkpi_ac_net_to_l80211(ac);
3021 	skb_set_queue_mapping(skb, ac);
3022 
3023 	info = IEEE80211_SKB_CB(skb);
3024 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3025 	/* Slight delay; probably only happens on scanning so fine? */
3026 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
3027 		c = ic->ic_curchan;
3028 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3029 	info->hw_queue = ac;		/* XXX-BZ is this correct? */
3030 	if (m->m_flags & M_EAPOL)
3031 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3032 	info->control.vif = vif;
3033 	/* XXX-BZ info->control.rates */
3034 
3035 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
3036 	if (lsta != NULL) {
3037 		sta = LSTA_TO_STA(lsta);
3038 #ifdef TRY_HW_CRYPTO
3039 		info->control.hw_key = lsta->kc;
3040 #endif
3041 	} else {
3042 		sta = NULL;
3043 	}
3044 
3045 	IMPROVE();
3046 
3047 	if (sta != NULL) {
3048 		struct lkpi_txq *ltxq;
3049 
3050 		ltxq = TXQ_TO_LTXQ(sta->txq[ac]);	/* XXX-BZ re-check */
3051 		/*
3052 		 * We currently do not use queues but do direct TX.
3053 		 * The exception to the rule is initial packets, as we cannot
3054 		 * TX until queues are allocated (at least for iwlwifi).
3055 		 * So we wake_tx_queue in newstate and register any dequeue
3056 		 * calls.  In the time until then we queue packets and
3057 		 * let the driver deal with them.
3058 		 */
3059 		if (!ltxq->seen_dequeue) {
3060 
3061 			/* Prevent an ordering problem, likely other issues. */
3062 			while (!skb_queue_empty(&ltxq->skbq)) {
3063 				struct sk_buff *skb2;
3064 
3065 				skb2 = skb_dequeue(&ltxq->skbq);
3066 				if (skb2 != NULL) {
3067 					memset(&control, 0, sizeof(control));
3068 					control.sta = sta;
3069 					lkpi_80211_mo_tx(hw, &control, skb2);
3070 				}
3071 			}
3072 			goto ops_tx;
3073 		}
3074 		if (0 && ltxq->seen_dequeue && skb_queue_empty(&ltxq->skbq))
3075 			goto ops_tx;
3076 
3077 		skb_queue_tail(&ltxq->skbq, skb);
3078 #ifdef LINUXKPI_DEBUG_80211
3079 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3080 			printf("%s:%d lsta %p sta %p ni %p %6D skb %p lxtq %p "
3081 			    "qlen %u WAKE_TX_Q ac %d prio %u qmap %u\n",
3082 			    __func__, __LINE__, lsta, sta, ni,
3083 			    ni->ni_macaddr, ":", skb, ltxq,
3084 			    skb_queue_len(&ltxq->skbq), ac,
3085 			    skb->priority, skb->qmap);
3086 #endif
3087 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[ac]);	/* XXX-BZ */
3088 		return;
3089 	}
3090 
3091 ops_tx:
3092 #ifdef LINUXKPI_DEBUG_80211
3093 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3094 		printf("%s:%d lsta %p sta %p ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
3095 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3096 		    skb, ac, skb->priority, skb->qmap);
3097 #endif
3098 	memset(&control, 0, sizeof(control));
3099 	control.sta = sta;
3100 
3101 	lkpi_80211_mo_tx(hw, &control, skb);
3102 	return;
3103 }
3104 
3105 static void
3106 lkpi_80211_txq_task(void *ctx, int pending)
3107 {
3108 	struct lkpi_sta *lsta;
3109 	struct mbufq mq;
3110 	struct mbuf *m;
3111 
3112 	lsta = ctx;
3113 
3114 #ifdef LINUXKPI_DEBUG_80211
3115 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3116 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3117 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3118 		    pending, mbufq_len(&lsta->txq));
3119 #endif
3120 
3121 	mbufq_init(&mq, IFQ_MAXLEN);
3122 
3123 	LKPI_80211_LSTA_LOCK(lsta);
3124 	mbufq_concat(&mq, &lsta->txq);
3125 	LKPI_80211_LSTA_UNLOCK(lsta);
3126 
3127 	m = mbufq_dequeue(&mq);
3128 	while (m != NULL) {
3129 		lkpi_80211_txq_tx_one(lsta, m);
3130 		m = mbufq_dequeue(&mq);
3131 	}
3132 }
3133 
3134 static int
3135 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3136 {
3137 
3138 	/* XXX TODO */
3139 	IMPROVE();
3140 
3141 	/* Quick and dirty cheating hack. */
3142 	struct ieee80211_node *ni;
3143 
3144 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3145 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3146 }
3147 
3148 static void
3149 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
3150     int *n, struct ieee80211_channel *c)
3151 {
3152 	struct lkpi_hw *lhw;
3153 	struct ieee80211_hw *hw;
3154 	struct linuxkpi_ieee80211_channel *channels;
3155 	uint8_t bands[IEEE80211_MODE_BYTES];
3156 	int chan_flags, error, i, nchans;
3157 
3158 	/* Channels */
3159 	lhw = ic->ic_softc;
3160 	hw = LHW_TO_HW(lhw);
3161 
3162 	/* NL80211_BAND_2GHZ */
3163 	nchans = 0;
3164 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
3165 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
3166 	if (nchans > 0) {
3167 		memset(bands, 0, sizeof(bands));
3168 		chan_flags = 0;
3169 		setbit(bands, IEEE80211_MODE_11B);
3170 		/* XXX-BZ unclear how to check for 11g. */
3171 		setbit(bands, IEEE80211_MODE_11G);
3172 #ifdef __notyet__
3173 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
3174 			setbit(bands, IEEE80211_MODE_11NG);
3175 			chan_flags |= NET80211_CBW_FLAG_HT40;
3176 		}
3177 #endif
3178 
3179 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3180 		for (i = 0; i < nchans && *n < maxchan; i++) {
3181 			uint32_t nflags = 0;
3182 			int cflags = chan_flags;
3183 
3184 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3185 				printf("%s: %s: Skipping disabled chan "
3186 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
3187 				    channels[i].hw_value,
3188 				    channels[i].center_freq, channels[i].flags);
3189 				continue;
3190 			}
3191 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3192 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3193 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3194 				nflags |= IEEE80211_CHAN_DFS;
3195 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3196 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3197 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3198 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3199 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
3200 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3201 				cflags &= ~NET80211_CBW_FLAG_HT40;
3202 
3203 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3204 			    channels[i].hw_value, channels[i].center_freq,
3205 			    channels[i].max_power,
3206 			    nflags, bands, chan_flags);
3207 			/* net80211::ENOBUFS: *n >= maxchans */
3208 			if (error != 0 && error != ENOBUFS)
3209 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3210 				    "returned error %d\n", ic->ic_name,
3211 				    __func__, channels[i].hw_value,
3212 				    channels[i].center_freq, channels[i].flags,
3213 				    nflags, chan_flags, cflags, error);
3214 			if (error != 0)
3215 				break;
3216 		}
3217 	}
3218 
3219 	/* NL80211_BAND_5GHZ */
3220 	nchans = 0;
3221 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
3222 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
3223 	if (nchans > 0) {
3224 		memset(bands, 0, sizeof(bands));
3225 		chan_flags = 0;
3226 		setbit(bands, IEEE80211_MODE_11A);
3227 #ifdef __not_yet__
3228 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
3229 			setbit(bands, IEEE80211_MODE_11NA);
3230 			chan_flags |= NET80211_CBW_FLAG_HT40;
3231 		}
3232 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
3233 
3234 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
3235 			ic->ic_vhtcaps =
3236 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
3237 
3238 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
3239 			chan_flags |= NET80211_CBW_FLAG_VHT80;
3240 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
3241 			    ic->ic_vhtcaps))
3242 				chan_flags |= NET80211_CBW_FLAG_VHT160;
3243 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
3244 			    ic->ic_vhtcaps))
3245 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
3246 		}
3247 #endif
3248 
3249 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3250 		for (i = 0; i < nchans && *n < maxchan; i++) {
3251 			uint32_t nflags = 0;
3252 			int cflags = chan_flags;
3253 
3254 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3255 				printf("%s: %s: Skipping disabled chan "
3256 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
3257 				    channels[i].hw_value,
3258 				    channels[i].center_freq, channels[i].flags);
3259 				continue;
3260 			}
3261 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3262 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3263 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3264 				nflags |= IEEE80211_CHAN_DFS;
3265 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3266 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3267 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3268 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3269 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
3270 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3271 				cflags &= ~NET80211_CBW_FLAG_HT40;
3272 
3273 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3274 			    channels[i].hw_value, channels[i].center_freq,
3275 			    channels[i].max_power,
3276 			    nflags, bands, chan_flags);
3277 			/* net80211::ENOBUFS: *n >= maxchans */
3278 			if (error != 0 && error != ENOBUFS)
3279 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3280 				    "returned error %d\n", ic->ic_name,
3281 				    __func__, channels[i].hw_value,
3282 				    channels[i].center_freq, channels[i].flags,
3283 				    nflags, chan_flags, cflags, error);
3284 			if (error != 0)
3285 				break;
3286 		}
3287 	}
3288 }
3289 
3290 static void *
3291 lkpi_ieee80211_ifalloc(void)
3292 {
3293 	struct ieee80211com *ic;
3294 
3295 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
3296 	if (ic == NULL)
3297 		return (NULL);
3298 
3299 	/* Setting these happens later when we have device information. */
3300 	ic->ic_softc = NULL;
3301 	ic->ic_name = "linuxkpi";
3302 
3303 	return (ic);
3304 }
3305 
3306 struct ieee80211_hw *
3307 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
3308 {
3309 	struct ieee80211_hw *hw;
3310 	struct lkpi_hw *lhw;
3311 	struct wiphy *wiphy;
3312 
3313 	/* Get us and the driver data also allocated. */
3314 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
3315 	if (wiphy == NULL)
3316 		return (NULL);
3317 
3318 	lhw = wiphy_priv(wiphy);
3319 	lhw->ops = ops;
3320 
3321 	mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
3322 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
3323 	TAILQ_INIT(&lhw->lvif_head);
3324 
3325 	/*
3326 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
3327 	 * not initialized.
3328 	 */
3329 	hw = LHW_TO_HW(lhw);
3330 	hw->wiphy = wiphy;
3331 	hw->conf.flags |= IEEE80211_CONF_IDLE;
3332 	hw->priv = (void *)(lhw + 1);
3333 
3334 	/* BSD Specific. */
3335 	lhw->ic = lkpi_ieee80211_ifalloc();
3336 	if (lhw->ic == NULL) {
3337 		ieee80211_free_hw(hw);
3338 		return (NULL);
3339 	}
3340 
3341 	IMPROVE();
3342 
3343 	return (hw);
3344 }
3345 
3346 void
3347 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
3348 {
3349 	struct lkpi_hw *lhw;
3350 
3351 	lhw = HW_TO_LHW(hw);
3352 	free(lhw->ic, M_LKPI80211);
3353 	lhw->ic = NULL;
3354 
3355 	/* Cleanup more of lhw here or in wiphy_free()? */
3356 	sx_destroy(&lhw->lvif_sx);
3357 	mtx_destroy(&lhw->mtx);
3358 	IMPROVE();
3359 }
3360 
3361 void
3362 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
3363 {
3364 	struct lkpi_hw *lhw;
3365 	struct ieee80211com *ic;
3366 
3367 	lhw = HW_TO_LHW(hw);
3368 	ic = lhw->ic;
3369 
3370 	/* Now set a proper name before ieee80211_ifattach(). */
3371 	ic->ic_softc = lhw;
3372 	ic->ic_name = name;
3373 
3374 	/* XXX-BZ do we also need to set wiphy name? */
3375 }
3376 
3377 struct ieee80211_hw *
3378 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
3379 {
3380 	struct lkpi_hw *lhw;
3381 
3382 	lhw = wiphy_priv(wiphy);
3383 	return (LHW_TO_HW(lhw));
3384 }
3385 
3386 static void
3387 lkpi_radiotap_attach(struct lkpi_hw *lhw)
3388 {
3389 	struct ieee80211com *ic;
3390 
3391 	ic = lhw->ic;
3392 	ieee80211_radiotap_attach(ic,
3393 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
3394 	    LKPI_RTAP_TX_FLAGS_PRESENT,
3395 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
3396 	    LKPI_RTAP_RX_FLAGS_PRESENT);
3397 }
3398 
3399 int
3400 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
3401 {
3402 	struct ieee80211com *ic;
3403 	struct lkpi_hw *lhw;
3404 	int band, i;
3405 
3406 	lhw = HW_TO_LHW(hw);
3407 	ic = lhw->ic;
3408 
3409 	/* We do it this late as wiphy->dev should be set for the name. */
3410 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3411 	if (lhw->workq == NULL)
3412 		return (-EAGAIN);
3413 
3414 	/* XXX-BZ figure this out how they count his... */
3415 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
3416 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3417 		    hw->wiphy->perm_addr);
3418 	} else if (hw->wiphy->n_addresses > 0) {
3419 		/* We take the first one. */
3420 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3421 		    hw->wiphy->addresses[0].addr);
3422 	} else {
3423 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
3424 	}
3425 
3426 #ifdef __not_yet__
3427 	/* See comment in lkpi_80211_txq_tx_one(). */
3428 	ic->ic_headroom = hw->extra_tx_headroom;
3429 #endif
3430 
3431 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
3432 	ic->ic_opmode = IEEE80211_M_STA;
3433 
3434 	/* Set device capabilities. */
3435 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
3436 	ic->ic_caps =
3437 	    IEEE80211_C_STA |
3438 	    IEEE80211_C_MONITOR |
3439 	    IEEE80211_C_WPA |		/* WPA/RSN */
3440 	    IEEE80211_C_WME |
3441 #if 0
3442 	    IEEE80211_C_PMGT |
3443 #endif
3444 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
3445 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
3446 	    ;
3447 #if 0
3448 	/* Scanning is a different kind of beast to re-work. */
3449 	ic->ic_caps |= IEEE80211_C_BGSCAN;
3450 #endif
3451 	if (lhw->ops->hw_scan) {
3452 		/*
3453 		 * Advertise full-offload scanning.
3454 		 *
3455 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3456 		 * we essentially disable hw_scan for all drivers not setting
3457 		 * the flag.
3458 		 */
3459 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3460 	}
3461 
3462 #ifdef __notyet__
3463 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
3464 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
3465 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
3466 		    | IEEE80211_HTCAP_MAXAMSDU_3839
3467 						/* max A-MSDU length */
3468 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
3469 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
3470 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
3471 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
3472 #endif
3473 
3474 	ic->ic_cryptocaps = 0;
3475 #ifdef TRY_HW_CRYPTO
3476 	if (hw->wiphy->n_cipher_suites > 0) {
3477 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
3478 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
3479 			    hw->wiphy->cipher_suites[i]);
3480 	}
3481 #endif
3482 
3483 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3484 	    ic->ic_channels);
3485 
3486 	ieee80211_ifattach(ic);
3487 
3488 	ic->ic_update_mcast = lkpi_ic_update_mcast;
3489 	ic->ic_update_promisc = lkpi_ic_update_promisc;
3490 	ic->ic_update_chw = lkpi_ic_update_chw;
3491 	ic->ic_parent = lkpi_ic_parent;
3492 	ic->ic_scan_start = lkpi_ic_scan_start;
3493 	ic->ic_scan_end = lkpi_ic_scan_end;
3494 	ic->ic_set_channel = lkpi_ic_set_channel;
3495 	ic->ic_transmit = lkpi_ic_transmit;
3496 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
3497 	ic->ic_vap_create = lkpi_ic_vap_create;
3498 	ic->ic_vap_delete = lkpi_ic_vap_delete;
3499 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
3500 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
3501 
3502 	lhw->ic_node_alloc = ic->ic_node_alloc;
3503 	ic->ic_node_alloc = lkpi_ic_node_alloc;
3504 	lhw->ic_node_init = ic->ic_node_init;
3505 	ic->ic_node_init = lkpi_ic_node_init;
3506 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
3507 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
3508 	lhw->ic_node_free = ic->ic_node_free;
3509 	ic->ic_node_free = lkpi_ic_node_free;
3510 
3511 	lkpi_radiotap_attach(lhw);
3512 
3513 	/*
3514 	 * Assign the first possible channel for now;  seems Realtek drivers
3515 	 * expect one.
3516 	 * Also remember the amount of bands we support and the most rates
3517 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
3518 	 */
3519 	lhw->supbands = lhw->max_rates = 0;
3520 	for (band = 0; band < NUM_NL80211_BANDS &&
3521 	    hw->conf.chandef.chan == NULL; band++) {
3522 		struct ieee80211_supported_band *supband;
3523 		struct linuxkpi_ieee80211_channel *channels;
3524 
3525 		supband = hw->wiphy->bands[band];
3526 		if (supband == NULL || supband->n_channels == 0)
3527 			continue;
3528 
3529 		lhw->supbands++;
3530 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
3531 
3532 		channels = supband->channels;
3533 		for (i = 0; i < supband->n_channels; i++) {
3534 
3535 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3536 				continue;
3537 
3538 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3539 			    NL80211_CHAN_NO_HT);
3540 			break;
3541 		}
3542 	}
3543 
3544 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
3545 
3546 	/* Make sure we do not support more than net80211 is willing to take. */
3547 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
3548 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
3549 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
3550 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
3551 	}
3552 
3553 	/*
3554 	 * The maximum supported bitrates on any band + size for
3555 	 * DSSS Parameter Set give our per-band IE size.
3556 	 * XXX-BZ FIXME add HT VHT ... later
3557 	 * SSID is the responsibility of the driver and goes on the side.
3558 	 * The user specified bits coming from the vap go into the
3559 	 * "common ies" fields.
3560 	 */
3561 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
3562 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
3563 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
3564 	/*
3565 	 * net80211 does not seem to support the DSSS Parameter Set but some of
3566 	 * the drivers insert it so calculate the extra fixed space in.
3567 	 */
3568 	lhw->scan_ie_len += 2 + 1;
3569 
3570 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
3571 	if (hw->wiphy->max_scan_ie_len > 0)
3572 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
3573 
3574 	if (bootverbose)
3575 		ieee80211_announce(ic);
3576 
3577 	return (0);
3578 }
3579 
3580 void
3581 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
3582 {
3583 	struct lkpi_hw *lhw;
3584 	struct ieee80211com *ic;
3585 
3586 	lhw = HW_TO_LHW(hw);
3587 	ic = lhw->ic;
3588 	ieee80211_ifdetach(ic);
3589 }
3590 
3591 void
3592 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
3593     enum ieee80211_iface_iter flags,
3594     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
3595     void *arg)
3596 {
3597 	struct lkpi_hw *lhw;
3598 	struct lkpi_vif *lvif;
3599 	struct ieee80211_vif *vif;
3600 	bool active, atomic, nin_drv;
3601 
3602 	lhw = HW_TO_LHW(hw);
3603 
3604 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
3605 	    IEEE80211_IFACE_ITER_RESUME_ALL|
3606 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
3607 	    IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
3608 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
3609 		    __func__, flags);
3610 	}
3611 
3612 	active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
3613 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
3614 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
3615 
3616 	if (atomic)
3617 		LKPI_80211_LHW_LVIF_LOCK(lhw);
3618 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3619 		struct ieee80211vap *vap;
3620 
3621 		vif = LVIF_TO_VIF(lvif);
3622 
3623 		/*
3624 		 * If we want "active" interfaces, we need to distinguish on
3625 		 * whether the driver knows about them or not to be able to
3626 		 * handle the "resume" case correctly.  Skip the ones the
3627 		 * driver does not know about.
3628 		 */
3629 		if (active && !lvif->added_to_drv &&
3630 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
3631 			continue;
3632 
3633 		/*
3634 		 * If we shall skip interfaces not added to the driver do so
3635 		 * if we haven't yet.
3636 		 */
3637 		if (nin_drv && !lvif->added_to_drv)
3638 			continue;
3639 
3640 		/*
3641 		 * Run the iterator function if we are either not asking
3642 		 * asking for active only or if the VAP is "running".
3643 		 */
3644 		/* XXX-BZ probably should have state in the lvif as well. */
3645 		vap = LVIF_TO_VAP(lvif);
3646 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
3647 			iterfunc(arg, vif->addr, vif);
3648 	}
3649 	if (atomic)
3650 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3651 }
3652 
3653 void
3654 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
3655     struct ieee80211_vif *vif,
3656     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
3657         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
3658     void *arg)
3659 {
3660 
3661 	UNIMPLEMENTED;
3662 }
3663 
3664 void
3665 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
3666     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
3667 	void *),
3668     void *arg)
3669 {
3670 
3671 	UNIMPLEMENTED;
3672 }
3673 
3674 void
3675 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
3676    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
3677 {
3678 	struct lkpi_hw *lhw;
3679 	struct lkpi_vif *lvif;
3680 	struct lkpi_sta *lsta;
3681 	struct ieee80211_sta *sta;
3682 
3683 	KASSERT(hw != NULL && iterfunc != NULL,
3684 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
3685 
3686 	lhw = HW_TO_LHW(hw);
3687 
3688 	LKPI_80211_LHW_LVIF_LOCK(lhw);
3689 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3690 
3691 		LKPI_80211_LVIF_LOCK(lvif);
3692 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
3693 			if (!lsta->added_to_drv)
3694 				continue;
3695 			sta = LSTA_TO_STA(lsta);
3696 			iterfunc(arg, sta);
3697 		}
3698 		LKPI_80211_LVIF_UNLOCK(lvif);
3699 	}
3700 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3701 }
3702 
3703 int
3704 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
3705     struct linuxkpi_ieee80211_regdomain *regd)
3706 {
3707 	struct lkpi_hw *lhw;
3708 	struct ieee80211com *ic;
3709 	struct ieee80211_regdomain *rd;
3710 
3711 	lhw = wiphy_priv(wiphy);
3712 	ic = lhw->ic;
3713 
3714 	rd = &ic->ic_regdomain;
3715 	if (rd->isocc[0] == '\0') {
3716 		rd->isocc[0] = regd->alpha2[0];
3717 		rd->isocc[1] = regd->alpha2[1];
3718 	}
3719 
3720 	TODO();
3721 	/* XXX-BZ finish the rest. */
3722 
3723 	return (0);
3724 }
3725 
3726 void
3727 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
3728     struct cfg80211_scan_info *info)
3729 {
3730 	struct lkpi_hw *lhw;
3731 	struct ieee80211com *ic;
3732 	struct ieee80211_scan_state *ss;
3733 
3734 	lhw = wiphy_priv(hw->wiphy);
3735 	ic = lhw->ic;
3736 	ss = ic->ic_scan;
3737 
3738 	ieee80211_scan_done(ss->ss_vap);
3739 
3740 	LKPI_80211_LHW_LOCK(lhw);
3741 	free(lhw->hw_req, M_LKPI80211);
3742 	lhw->hw_req = NULL;
3743 	lhw->scan_flags &= ~LKPI_SCAN_RUNNING;
3744 	wakeup(lhw);
3745 	LKPI_80211_LHW_UNLOCK(lhw);
3746 
3747 	return;
3748 }
3749 
3750 void
3751 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3752     struct ieee80211_sta *sta, struct napi_struct *napi __unused)
3753 {
3754 	struct epoch_tracker et;
3755 	struct lkpi_hw *lhw;
3756 	struct ieee80211com *ic;
3757 	struct mbuf *m;
3758 	struct skb_shared_info *shinfo;
3759 	struct ieee80211_rx_status *rx_status;
3760 	struct ieee80211_rx_stats rx_stats;
3761 	struct ieee80211_node *ni;
3762 	struct ieee80211vap *vap;
3763 	struct ieee80211_hdr *hdr;
3764 	struct lkpi_sta *lsta;
3765 	int i, offset, ok;
3766 	int8_t rssi;
3767 	bool is_beacon;
3768 
3769 	if (skb->len < 2) {
3770 		/* Need 80211 stats here. */
3771 		IMPROVE();
3772 		goto err;
3773 	}
3774 
3775 	/*
3776 	 * For now do the data copy; we can later improve things. Might even
3777 	 * have an mbuf backing the skb data then?
3778 	 */
3779 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
3780 	if (m == NULL)
3781 		goto err;
3782 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
3783 
3784 	shinfo = skb_shinfo(skb);
3785 	offset = m->m_len;
3786 	for (i = 0; i < shinfo->nr_frags; i++) {
3787 		m_copyback(m, offset, shinfo->frags[i].size,
3788 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
3789 		    shinfo->frags[i].offset);
3790 		offset += shinfo->frags[i].size;
3791 	}
3792 
3793 	rx_status = IEEE80211_SKB_RXCB(skb);
3794 
3795 	hdr = (void *)skb->data;
3796 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
3797 
3798 #ifdef LINUXKPI_DEBUG_80211
3799 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
3800 		goto no_trace_beacons;
3801 
3802 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3803 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
3804 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
3805 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
3806 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
3807 		    shinfo, shinfo->nr_frags,
3808 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
3809 
3810 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
3811 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
3812 
3813 	/* Implement a dump_rxcb() !!! */
3814 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3815 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
3816 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
3817 			__func__,
3818 			(uintmax_t)rx_status->boottime_ns,
3819 			(uintmax_t)rx_status->mactime,
3820 			rx_status->device_timestamp,
3821 			rx_status->flag,
3822 			rx_status->freq,
3823 			rx_status->bw,
3824 			rx_status->encoding,
3825 			rx_status->ampdu_reference,
3826 			rx_status->band,
3827 			rx_status->chains,
3828 			rx_status->chain_signal[0],
3829 			rx_status->chain_signal[1],
3830 			rx_status->chain_signal[2],
3831 			rx_status->chain_signal[3],
3832 			rx_status->signal,
3833 			rx_status->enc_flags,
3834 			rx_status->he_dcm,
3835 			rx_status->he_gi,
3836 			rx_status->he_ru,
3837 			rx_status->zero_length_psdu_type,
3838 			rx_status->nss,
3839 			rx_status->rate_idx);
3840 no_trace_beacons:
3841 #endif
3842 
3843 	memset(&rx_stats, 0, sizeof(rx_stats));
3844 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
3845 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
3846 	rx_stats.c_nf = -96;
3847 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
3848 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3849 		rssi = rx_status->signal;
3850 	else
3851 		rssi = rx_stats.c_nf;
3852 	/*
3853 	 * net80211 signal strength data are in .5 dBm units relative to
3854 	 * the current noise floor (see comment in ieee80211_node.h).
3855 	 */
3856 	rssi -= rx_stats.c_nf;
3857 	rx_stats.c_rssi = rssi * 2;
3858 	rx_stats.r_flags |= IEEE80211_R_BAND;
3859 	rx_stats.c_band =
3860 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
3861 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
3862 	rx_stats.c_freq = rx_status->freq;
3863 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
3864 
3865 	/* XXX (*sta_statistics)() to get to some of that? */
3866 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
3867 
3868 	lhw = HW_TO_LHW(hw);
3869 	ic = lhw->ic;
3870 
3871 	ok = ieee80211_add_rx_params(m, &rx_stats);
3872 	if (ok == 0) {
3873 		counter_u64_add(ic->ic_ierrors, 1);
3874 		goto err;
3875 	}
3876 
3877 	if (sta != NULL) {
3878 		lsta = STA_TO_LSTA(sta);
3879 		ni = ieee80211_ref_node(lsta->ni);
3880 	} else {
3881 		struct ieee80211_frame_min *wh;
3882 
3883 		wh = mtod(m, struct ieee80211_frame_min *);
3884 		ni = ieee80211_find_rxnode(ic, wh);
3885 		if (ni != NULL)
3886 			lsta = ni->ni_drv_data;
3887 	}
3888 
3889 	if (ni != NULL)
3890 		vap = ni->ni_vap;
3891 	else
3892 		/*
3893 		 * XXX-BZ can we improve this by looking at the frame hdr
3894 		 * or other meta-data passed up?
3895 		 */
3896 		vap = TAILQ_FIRST(&ic->ic_vaps);
3897 
3898 #ifdef LINUXKPI_DEBUG_80211
3899 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3900 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
3901 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
3902 		    ni, vap, is_beacon ? " beacon" : "");
3903 #endif
3904 
3905 	if (ni != NULL && vap != NULL && is_beacon &&
3906 	    rx_status->device_timestamp > 0 &&
3907 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
3908 		struct lkpi_vif *lvif;
3909 		struct ieee80211_vif *vif;
3910 		struct ieee80211_frame *wh;
3911 
3912 		wh = mtod(m, struct ieee80211_frame *);
3913 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
3914 			goto skip_device_ts;
3915 
3916 		lvif = VAP_TO_LVIF(vap);
3917 		vif = LVIF_TO_VIF(lvif);
3918 
3919 		IMPROVE("TIMING_BEACON_ONLY?");
3920 		/* mac80211 specific (not net80211) so keep it here. */
3921 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
3922 		/*
3923 		 * net80211 should take care of the other information (sync_tsf,
3924 		 * sync_dtim_count) as otherwise we need to parse the beacon.
3925 		 */
3926 	}
3927 skip_device_ts:
3928 
3929 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
3930 	    ieee80211_radiotap_active_vap(vap)) {
3931 		struct lkpi_radiotap_rx_hdr *rtap;
3932 
3933 		rtap = &lhw->rtap_rx;
3934 		rtap->wr_tsft = rx_status->device_timestamp;
3935 		rtap->wr_flags = 0;
3936 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
3937 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3938 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3939 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3940 #if 0	/* .. or it does not given we strip it below. */
3941 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3942 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
3943 #endif
3944 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
3945 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
3946 		rtap->wr_rate = 0;
3947 		IMPROVE();
3948 		/* XXX TODO status->encoding / rate_index / bw */
3949 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
3950 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
3951 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
3952 		rtap->wr_dbm_antsignal = rssi;
3953 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
3954 	}
3955 
3956 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3957 		m_adj(m, -IEEE80211_CRC_LEN);
3958 
3959 	NET_EPOCH_ENTER(et);
3960 	if (ni != NULL) {
3961 		ok = ieee80211_input_mimo(ni, m);
3962 		ieee80211_free_node(ni);
3963 	} else {
3964 		ok = ieee80211_input_mimo_all(ic, m);
3965 	}
3966 	NET_EPOCH_EXIT(et);
3967 
3968 #ifdef LINUXKPI_DEBUG_80211
3969 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3970 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
3971 #endif
3972 
3973 	IMPROVE();
3974 
3975 err:
3976 	/* The skb is ours so we can free it :-) */
3977 	kfree_skb(skb);
3978 }
3979 
3980 uint8_t
3981 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr)
3982 {
3983 	const struct ieee80211_frame *wh;
3984 
3985 	wh = (const struct ieee80211_frame *)hdr;
3986 	return (ieee80211_gettid(wh));
3987 }
3988 
3989 struct wiphy *
3990 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
3991 {
3992 	struct lkpi_wiphy *lwiphy;
3993 
3994 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
3995 	if (lwiphy == NULL)
3996 		return (NULL);
3997 	lwiphy->ops = ops;
3998 
3999 	/* XXX TODO */
4000 	return (LWIPHY_TO_WIPHY(lwiphy));
4001 }
4002 
4003 void
4004 linuxkpi_wiphy_free(struct wiphy *wiphy)
4005 {
4006 	struct lkpi_wiphy *lwiphy;
4007 
4008 	if (wiphy == NULL)
4009 		return;
4010 
4011 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
4012 	kfree(lwiphy);
4013 }
4014 
4015 uint32_t
4016 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
4017     enum nl80211_band band)
4018 {
4019 
4020 	switch (band) {
4021 	case NL80211_BAND_2GHZ:
4022 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
4023 		break;
4024 	case NL80211_BAND_5GHZ:
4025 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
4026 		break;
4027 	default:
4028 		/* XXX abort, retry, error, panic? */
4029 		break;
4030 	}
4031 
4032 	return (0);
4033 }
4034 
4035 uint32_t
4036 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
4037 {
4038 
4039 	return (ieee80211_mhz2ieee(freq, 0));
4040 }
4041 
4042 static struct lkpi_sta *
4043 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
4044 {
4045 	struct lkpi_sta *lsta, *temp;
4046 
4047 	LKPI_80211_LVIF_LOCK(lvif);
4048 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4049 		if (lsta->ni == ni) {
4050 			LKPI_80211_LVIF_UNLOCK(lvif);
4051 			return (lsta);
4052 		}
4053 	}
4054 	LKPI_80211_LVIF_UNLOCK(lvif);
4055 
4056 	return (NULL);
4057 }
4058 
4059 struct ieee80211_sta *
4060 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
4061 {
4062 	struct lkpi_vif *lvif;
4063 	struct lkpi_sta *lsta, *temp;
4064 	struct ieee80211_sta *sta;
4065 
4066 	lvif = VIF_TO_LVIF(vif);
4067 
4068 	LKPI_80211_LVIF_LOCK(lvif);
4069 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4070 		sta = LSTA_TO_STA(lsta);
4071 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
4072 			LKPI_80211_LVIF_UNLOCK(lvif);
4073 			return (sta);
4074 		}
4075 	}
4076 	LKPI_80211_LVIF_UNLOCK(lvif);
4077 	return (NULL);
4078 }
4079 
4080 struct ieee80211_sta *
4081 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
4082     const uint8_t *addr, const uint8_t *ourvifaddr)
4083 {
4084 	struct lkpi_hw *lhw;
4085 	struct lkpi_vif *lvif;
4086 	struct lkpi_sta *lsta;
4087 	struct ieee80211_vif *vif;
4088 	struct ieee80211_sta *sta;
4089 
4090 	lhw = wiphy_priv(hw->wiphy);
4091 	sta = NULL;
4092 
4093 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4094 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4095 
4096 		/* XXX-BZ check our address from the vif. */
4097 
4098 		vif = LVIF_TO_VIF(lvif);
4099 		if (ourvifaddr != NULL &&
4100 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
4101 			continue;
4102 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
4103 		if (sta != NULL)
4104 			break;
4105 	}
4106 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4107 
4108 	if (sta != NULL) {
4109 		lsta = STA_TO_LSTA(sta);
4110 		if (!lsta->added_to_drv)
4111 			return (NULL);
4112 	}
4113 
4114 	return (sta);
4115 }
4116 
4117 struct sk_buff *
4118 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
4119     struct ieee80211_txq *txq)
4120 {
4121 	struct lkpi_txq *ltxq;
4122 	struct sk_buff *skb;
4123 
4124 	ltxq = TXQ_TO_LTXQ(txq);
4125 	ltxq->seen_dequeue = true;
4126 
4127 	skb = skb_dequeue(&ltxq->skbq);
4128 
4129 	return (skb);
4130 }
4131 
4132 void
4133 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4134     unsigned long *frame_cnt, unsigned long *byte_cnt)
4135 {
4136 	struct lkpi_txq *ltxq;
4137 	struct sk_buff *skb;
4138 	unsigned long fc, bc;
4139 
4140 	ltxq = TXQ_TO_LTXQ(txq);
4141 
4142 	fc = bc = 0;
4143 	skb_queue_walk(&ltxq->skbq, skb) {
4144 		fc++;
4145 		bc += skb->len;
4146 	}
4147 	if (frame_cnt)
4148 		*frame_cnt = fc;
4149 	if (byte_cnt)
4150 		*byte_cnt = bc;
4151 
4152 	/* Validate that this is doing the correct thing. */
4153 	/* Should we keep track on en/dequeue? */
4154 	IMPROVE();
4155 }
4156 
4157 /*
4158  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
4159  * The latter tries to derive the success status from the info flags
4160  * passed back from the driver.  rawx_mit() saves the ni on the m and the
4161  * m on the skb for us to be able to give feedback to net80211.
4162  */
4163 void
4164 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4165     int status)
4166 {
4167 	struct ieee80211_node *ni;
4168 	struct mbuf *m;
4169 
4170 	m = skb->m;
4171 	skb->m = NULL;
4172 
4173 	if (m != NULL) {
4174 		ni = m->m_pkthdr.PH_loc.ptr;
4175 		/* Status: 0 is ok, != 0 is error. */
4176 		ieee80211_tx_complete(ni, m, status);
4177 		/* ni & mbuf were consumed. */
4178 	}
4179 
4180 	kfree_skb(skb);
4181 }
4182 
4183 void
4184 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
4185 {
4186 	struct ieee80211_tx_info *info;
4187 	struct ieee80211_ratectl_tx_status txs;
4188 	struct ieee80211_node *ni;
4189 	int status;
4190 
4191 	info = IEEE80211_SKB_CB(skb);
4192 
4193 	if (skb->m != NULL) {
4194 		struct mbuf *m;
4195 
4196 		m = skb->m;
4197 		ni = m->m_pkthdr.PH_loc.ptr;
4198 		memset(&txs, 0, sizeof(txs));
4199 	} else {
4200 		ni = NULL;
4201 	}
4202 
4203 	if (info->flags & IEEE80211_TX_STAT_ACK) {
4204 		status = 0;	/* No error. */
4205 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
4206 	} else {
4207 		status = 1;
4208 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
4209 	}
4210 
4211 	if (ni != NULL) {
4212 		int ridx __unused;
4213 #ifdef LINUXKPI_DEBUG_80211
4214 		int old_rate;
4215 
4216 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
4217 #endif
4218 		txs.pktlen = skb->len;
4219 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
4220 		if (info->status.rates[0].count > 1) {
4221 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
4222 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
4223 		}
4224 #if 0		/* Unused in net80211 currently. */
4225 		/* XXX-BZ conver;t check .flags for MCS/VHT/.. */
4226 		txs.final_rate = info->status.rates[0].idx;
4227 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
4228 #endif
4229 		if (info->status.is_valid_ack_signal) {
4230 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
4231 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
4232 		}
4233 
4234 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
4235 		ieee80211_ratectl_tx_complete(ni, &txs);
4236 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
4237 
4238 #ifdef LINUXKPI_DEBUG_80211
4239 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
4240 			printf("TX-RATE: %s: old %d new %d ridx %d, "
4241 			    "long_retries %d\n", __func__,
4242 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
4243 			    ridx, txs.long_retries);
4244 		}
4245 #endif
4246 	}
4247 
4248 #ifdef LINUXKPI_DEBUG_80211
4249 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4250 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
4251 		    "band %u hw_queue %u tx_time_est %d : "
4252 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
4253 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
4254 		    "tx_time %u is_valid_ack_signal %u "
4255 		    "status_driver_data [ %p %p ]\n",
4256 		    __func__, hw, skb, status, info->flags,
4257 		    info->band, info->hw_queue, info->tx_time_est,
4258 		    info->status.rates[0].idx, info->status.rates[0].count,
4259 		    info->status.rates[0].flags,
4260 		    info->status.rates[1].idx, info->status.rates[1].count,
4261 		    info->status.rates[1].flags,
4262 		    info->status.rates[2].idx, info->status.rates[2].count,
4263 		    info->status.rates[2].flags,
4264 		    info->status.rates[3].idx, info->status.rates[3].count,
4265 		    info->status.rates[3].flags,
4266 		    info->status.ack_signal, info->status.ampdu_ack_len,
4267 		    info->status.ampdu_len, info->status.antenna,
4268 		    info->status.tx_time, info->status.is_valid_ack_signal,
4269 		    info->status.status_driver_data[0],
4270 		    info->status.status_driver_data[1]);
4271 #endif
4272 
4273 	linuxkpi_ieee80211_free_txskb(hw, skb, status);
4274 }
4275 
4276 /*
4277  * This is an internal bandaid for the moment for the way we glue
4278  * skbs and mbufs together for TX.  Once we have skbs backed by
4279  * mbufs this should go away.
4280  * This is a public function but kept on the private KPI (lkpi_)
4281  * and is not exposed by a header file.
4282  */
4283 static void
4284 lkpi_ieee80211_free_skb_mbuf(void *p)
4285 {
4286 	struct ieee80211_node *ni;
4287 	struct mbuf *m;
4288 
4289 	if (p == NULL)
4290 		return;
4291 
4292 	m = (struct mbuf *)p;
4293 	M_ASSERTPKTHDR(m);
4294 
4295 	ni = m->m_pkthdr.PH_loc.ptr;
4296 	m->m_pkthdr.PH_loc.ptr = NULL;
4297 	if (ni != NULL)
4298 		ieee80211_free_node(ni);
4299 	m_freem(m);
4300 }
4301 
4302 void
4303 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
4304     struct delayed_work *w, int delay)
4305 {
4306 	struct lkpi_hw *lhw;
4307 
4308 	/* Need to make sure hw is in a stable (non-suspended) state. */
4309 	IMPROVE();
4310 
4311 	lhw = HW_TO_LHW(hw);
4312 	queue_delayed_work(lhw->workq, w, delay);
4313 }
4314 
4315 void
4316 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
4317     struct work_struct *w)
4318 {
4319 	struct lkpi_hw *lhw;
4320 
4321 	/* Need to make sure hw is in a stable (non-suspended) state. */
4322 	IMPROVE();
4323 
4324 	lhw = HW_TO_LHW(hw);
4325 	queue_work(lhw->workq, w);
4326 }
4327 
4328 struct sk_buff *
4329 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
4330     uint8_t *ssid, size_t ssid_len, size_t tailroom)
4331 {
4332 	struct sk_buff *skb;
4333 	struct ieee80211_frame *wh;
4334 	uint8_t *p;
4335 	size_t len;
4336 
4337 	len = sizeof(*wh);
4338 	len += 2 + ssid_len;
4339 
4340 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
4341 	if (skb == NULL)
4342 		return (NULL);
4343 
4344 	skb_reserve(skb, hw->extra_tx_headroom);
4345 
4346 	wh = skb_put_zero(skb, sizeof(*wh));
4347 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
4348 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
4349 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4350 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
4351 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4352 
4353 	p = skb_put(skb, 2 + ssid_len);
4354 	*p++ = IEEE80211_ELEMID_SSID;
4355 	*p++ = ssid_len;
4356 	if (ssid_len > 0)
4357 		memcpy(p, ssid, ssid_len);
4358 
4359 	return (skb);
4360 }
4361 
4362 struct sk_buff *
4363 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
4364     struct ieee80211_vif *vif)
4365 {
4366 	struct lkpi_vif *lvif;
4367 	struct ieee80211vap *vap;
4368 	struct sk_buff *skb;
4369 	struct ieee80211_frame_pspoll *psp;
4370 	uint16_t v;
4371 
4372 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
4373 	if (skb == NULL)
4374 		return (NULL);
4375 
4376 	skb_reserve(skb, hw->extra_tx_headroom);
4377 
4378 	lvif = VIF_TO_LVIF(vif);
4379 	vap = LVIF_TO_VAP(lvif);
4380 
4381 	psp = skb_put_zero(skb, sizeof(*psp));
4382 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
4383 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
4384 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
4385 	memcpy(&psp->i_aid, &v, sizeof(v));
4386 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
4387 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
4388 
4389 	return (skb);
4390 }
4391 
4392 struct sk_buff *
4393 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4394     struct ieee80211_vif *vif, bool qos)
4395 {
4396 	struct lkpi_vif *lvif;
4397 	struct ieee80211vap *vap;
4398 	struct sk_buff *skb;
4399 	struct ieee80211_frame *nullf;
4400 
4401 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
4402 	if (skb == NULL)
4403 		return (NULL);
4404 
4405 	skb_reserve(skb, hw->extra_tx_headroom);
4406 
4407 	lvif = VIF_TO_LVIF(vif);
4408 	vap = LVIF_TO_VAP(lvif);
4409 
4410 	nullf = skb_put_zero(skb, sizeof(*nullf));
4411 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
4412 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
4413 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
4414 
4415 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
4416 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
4417 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
4418 
4419 	return (skb);
4420 }
4421 
4422 struct wireless_dev *
4423 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
4424 {
4425 	struct lkpi_vif *lvif;
4426 
4427 	lvif = VIF_TO_LVIF(vif);
4428 	return (&lvif->wdev);
4429 }
4430 
4431 void
4432 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
4433 {
4434 	struct lkpi_vif *lvif;
4435 	struct ieee80211vap *vap;
4436 	enum ieee80211_state nstate;
4437 	int arg;
4438 
4439 	lvif = VIF_TO_LVIF(vif);
4440 	vap = LVIF_TO_VAP(lvif);
4441 
4442 	/*
4443 	 * Go to init; otherwise we need to elaborately check state and
4444 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4445 	 * Let the statemachine handle all neccessary changes.
4446 	 */
4447 	nstate = IEEE80211_S_INIT;
4448 	arg = 0;	/* Not a valid reason. */
4449 
4450 #ifdef LINUXKPI_DEBUG_80211
4451 	if (linuxkpi_debug_80211 & D80211_TRACE)
4452 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
4453 #endif
4454 	ieee80211_new_state(vap, nstate, arg);
4455 }
4456 
4457 void
4458 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4459 {
4460 	struct lkpi_vif *lvif;
4461 	struct ieee80211vap *vap;
4462 
4463 	lvif = VIF_TO_LVIF(vif);
4464 	vap = LVIF_TO_VAP(lvif);
4465 
4466 #ifdef LINUXKPI_DEBUG_80211
4467 	if (linuxkpi_debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4468 		ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4469 		    vif, vap, ieee80211_state_name[vap->iv_state]);
4470 #endif
4471 	ieee80211_beacon_miss(vap->iv_ic);
4472 }
4473 
4474 MODULE_VERSION(linuxkpi_wlan, 1);
4475 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
4476 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
4477