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