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