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