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