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