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