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