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