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