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 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1430 	/*
1431 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
1432 	 * as otherwise drivers (iwlwifi at least) will silently not remove
1433 	 * the sta from the firmware and when we will add a new one trigger
1434 	 * a fw assert.
1435 	 */
1436 	lkpi_disassoc(sta, vif, lhw);
1437 
1438 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1439 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1440 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1441 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1442 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1443 	if (error != 0) {
1444 		IMPROVE("do we need to undo the chan ctx?");
1445 		goto out;
1446 	}
1447 
1448 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1449 
1450 	IMPROVE("Any bss_info changes to announce?");
1451 	bss_changed = 0;
1452 	vif->bss_conf.qos = 0;
1453 	bss_changed |= BSS_CHANGED_QOS;
1454 	vif->cfg.ssid_len = 0;
1455 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1456 	bss_changed |= BSS_CHANGED_BSSID;
1457 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1458 
1459 	lkpi_lsta_remove(lsta, lvif);
1460 
1461 	/* conf_tx */
1462 
1463 	/* Take the chan ctx down. */
1464 	if (vif->chanctx_conf != NULL) {
1465 		struct lkpi_chanctx *lchanctx;
1466 		struct ieee80211_chanctx_conf *conf;
1467 
1468 		conf = vif->chanctx_conf;
1469 		/* Remove vif context. */
1470 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1471 		/* NB: vif->chanctx_conf is NULL now. */
1472 
1473 		/* Remove chan ctx. */
1474 		lkpi_80211_mo_remove_chanctx(hw, conf);
1475 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1476 		free(lchanctx, M_LKPI80211);
1477 	}
1478 
1479 	error = EALREADY;
1480 out:
1481 	LKPI_80211_LHW_UNLOCK(lhw);
1482 	IEEE80211_LOCK(vap->iv_ic);
1483 outni:
1484 	if (ni != NULL)
1485 		ieee80211_free_node(ni);
1486 	return (error);
1487 }
1488 
1489 static int
1490 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1491 {
1492 	int error;
1493 
1494 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1495 	if (error != 0 && error != EALREADY)
1496 		return (error);
1497 
1498 	/* At this point iv_bss is long a new node! */
1499 
1500 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1501 	return (error);
1502 }
1503 
1504 static int
1505 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1506 {
1507 	int error;
1508 
1509 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1510 	return (error);
1511 }
1512 
1513 static int
1514 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1515 {
1516 	int error;
1517 
1518 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1519 	return (error);
1520 }
1521 
1522 static int
1523 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1524 {
1525 	struct lkpi_hw *lhw;
1526 	struct ieee80211_hw *hw;
1527 	struct lkpi_vif *lvif;
1528 	struct ieee80211_vif *vif;
1529 	struct ieee80211_node *ni;
1530 	struct lkpi_sta *lsta;
1531 	struct ieee80211_sta *sta;
1532 	struct ieee80211_prep_tx_info prep_tx_info;
1533 	enum ieee80211_bss_changed bss_changed;
1534 	int error;
1535 
1536 	lhw = vap->iv_ic->ic_softc;
1537 	hw = LHW_TO_HW(lhw);
1538 	lvif = VAP_TO_LVIF(vap);
1539 	vif = LVIF_TO_VIF(lvif);
1540 
1541 	IEEE80211_UNLOCK(vap->iv_ic);
1542 	LKPI_80211_LHW_LOCK(lhw);
1543 	ni = NULL;
1544 
1545 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1546 	    "and to lesser extend ieee80211_notify_node_join");
1547 
1548 	/* Finish assoc. */
1549 	/* Update sta_state (AUTH to ASSOC) and set aid. */
1550 	ni = ieee80211_ref_node(vap->iv_bss);
1551 	lsta = ni->ni_drv_data;
1552 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1553 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1554 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1555 	sta = LSTA_TO_STA(lsta);
1556 	sta->aid = IEEE80211_NODE_AID(ni);
1557 #ifdef LKPI_80211_WME
1558 	if (vap->iv_flags & IEEE80211_F_WME)
1559 		sta->wme = true;
1560 #endif
1561 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1562 	if (error != 0)
1563 		goto out;
1564 
1565 	IMPROVE("wme / conf_tx [all]");
1566 
1567 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1568 	bss_changed = 0;
1569 #ifdef LKPI_80211_WME
1570 	bss_changed |= lkpi_wme_update(lhw, vap, true);
1571 #endif
1572 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
1573 		vif->cfg.assoc = true;
1574 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
1575 		bss_changed |= BSS_CHANGED_ASSOC;
1576 	}
1577 	/* We set SSID but this is not BSSID! */
1578 	vif->cfg.ssid_len = ni->ni_esslen;
1579 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
1580 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
1581 	    vif->bss_conf.use_short_preamble) {
1582 		vif->bss_conf.use_short_preamble ^= 1;
1583 		/* bss_changed |= BSS_CHANGED_??? */
1584 	}
1585 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
1586 	    vif->bss_conf.use_short_slot) {
1587 		vif->bss_conf.use_short_slot ^= 1;
1588 		/* bss_changed |= BSS_CHANGED_??? */
1589 	}
1590 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
1591 	    vif->bss_conf.qos) {
1592 		vif->bss_conf.qos ^= 1;
1593 		bss_changed |= BSS_CHANGED_QOS;
1594 	}
1595 
1596 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1597 
1598 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1599 
1600 	/* - change_chanctx (if needed)
1601 	 * - event_callback
1602 	 */
1603 
1604 	/* End mgd_complete_tx. */
1605 	if (lsta->in_mgd) {
1606 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1607 		prep_tx_info.success = true;
1608 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1609 		lsta->in_mgd = false;
1610 	}
1611 
1612 	lkpi_hw_conf_idle(hw, false);
1613 
1614 	/*
1615 	 * And then:
1616 	 * - (more packets)?
1617 	 * - set_key
1618 	 * - set_default_unicast_key
1619 	 * - set_key (?)
1620 	 * - ipv6_addr_change (?)
1621 	 */
1622 	/* Prepare_multicast && configure_filter. */
1623 	lhw->update_mc = true;
1624 	lkpi_update_mcast_filter(vap->iv_ic, true);
1625 
1626 	if (!ieee80211_node_is_authorized(ni)) {
1627 		IMPROVE("net80211 does not consider node authorized");
1628 	}
1629 
1630 	/* Update sta_state (ASSOC to AUTHORIZED). */
1631 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1632 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1633 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1634 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
1635 	if (error != 0) {
1636 		IMPROVE("undo some changes?");
1637 		goto out;
1638 	}
1639 
1640 	/* - drv_config (?)
1641 	 * - bss_info_changed
1642 	 * - set_rekey_data (?)
1643 	 *
1644 	 * And now we should be passing packets.
1645 	 */
1646 	IMPROVE("Need that bssid setting, and the keys");
1647 
1648 	bss_changed = 0;
1649 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1650 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1651 
1652 out:
1653 	LKPI_80211_LHW_UNLOCK(lhw);
1654 	IEEE80211_LOCK(vap->iv_ic);
1655 	if (ni != NULL)
1656 		ieee80211_free_node(ni);
1657 	return (error);
1658 }
1659 
1660 static int
1661 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1662 {
1663 	int error;
1664 
1665 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
1666 	if (error == 0)
1667 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
1668 	return (error);
1669 }
1670 
1671 static int
1672 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1673 {
1674 	struct lkpi_hw *lhw;
1675 	struct ieee80211_hw *hw;
1676 	struct lkpi_vif *lvif;
1677 	struct ieee80211_vif *vif;
1678 	struct ieee80211_node *ni;
1679 	struct lkpi_sta *lsta;
1680 	struct ieee80211_sta *sta;
1681 	struct ieee80211_prep_tx_info prep_tx_info;
1682 #if 0
1683 	enum ieee80211_bss_changed bss_changed;
1684 #endif
1685 	int error;
1686 
1687 	lhw = vap->iv_ic->ic_softc;
1688 	hw = LHW_TO_HW(lhw);
1689 	lvif = VAP_TO_LVIF(vap);
1690 	vif = LVIF_TO_VIF(lvif);
1691 
1692 	/* Keep ni around. */
1693 	ni = ieee80211_ref_node(vap->iv_bss);
1694 	lsta = ni->ni_drv_data;
1695 	sta = LSTA_TO_STA(lsta);
1696 
1697 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1698 
1699 	IEEE80211_UNLOCK(vap->iv_ic);
1700 	LKPI_80211_LHW_LOCK(lhw);
1701 
1702 	/* flush, drop. */
1703 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1704 
1705 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1706 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1707 	    !lsta->in_mgd) {
1708 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1709 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1710 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1711 		lsta->in_mgd = true;
1712 	}
1713 
1714 	LKPI_80211_LHW_UNLOCK(lhw);
1715 	IEEE80211_LOCK(vap->iv_ic);
1716 
1717 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1718 	error = lvif->iv_newstate(vap, nstate, arg);
1719 	if (error != 0)
1720 		goto outni;
1721 
1722 	IEEE80211_UNLOCK(vap->iv_ic);
1723 	LKPI_80211_LHW_LOCK(lhw);
1724 
1725 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1726 
1727 	/* Wake tx queues to get packet(s) out. */
1728 	lkpi_wake_tx_queues(hw, sta, true, true);
1729 
1730 	/* flush, no drop */
1731 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1732 
1733 	/* End mgd_complete_tx. */
1734 	if (lsta->in_mgd) {
1735 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1736 		prep_tx_info.success = false;
1737 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1738 		lsta->in_mgd = false;
1739 	}
1740 
1741 #if 0
1742 	/* sync_rx_queues */
1743 	lkpi_80211_mo_sync_rx_queues(hw);
1744 
1745 	/* sta_pre_rcu_remove */
1746         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1747 #endif
1748 
1749 	/* Take the station down. */
1750 
1751 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1752 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1753 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1754 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1755 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1756 	if (error != 0)
1757 		goto out;
1758 
1759 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1760 
1761 	/* Update sta_state (ASSOC to AUTH). */
1762 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1763 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1764 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1765 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1766 	if (error != 0)
1767 		goto out;
1768 
1769 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1770 
1771 #if 0
1772 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1773 	lkpi_disassoc(sta, vif, lhw);
1774 #endif
1775 
1776 	error = EALREADY;
1777 out:
1778 	LKPI_80211_LHW_UNLOCK(lhw);
1779 	IEEE80211_LOCK(vap->iv_ic);
1780 outni:
1781 	if (ni != NULL)
1782 		ieee80211_free_node(ni);
1783 	return (error);
1784 }
1785 
1786 static int
1787 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1788 {
1789 	struct lkpi_hw *lhw;
1790 	struct ieee80211_hw *hw;
1791 	struct lkpi_vif *lvif;
1792 	struct ieee80211_vif *vif;
1793 	struct ieee80211_node *ni;
1794 	struct lkpi_sta *lsta;
1795 	struct ieee80211_sta *sta;
1796 	struct ieee80211_prep_tx_info prep_tx_info;
1797 	enum ieee80211_bss_changed bss_changed;
1798 	int error;
1799 
1800 	lhw = vap->iv_ic->ic_softc;
1801 	hw = LHW_TO_HW(lhw);
1802 	lvif = VAP_TO_LVIF(vap);
1803 	vif = LVIF_TO_VIF(lvif);
1804 
1805 	/* Keep ni around. */
1806 	ni = ieee80211_ref_node(vap->iv_bss);
1807 	lsta = ni->ni_drv_data;
1808 	sta = LSTA_TO_STA(lsta);
1809 
1810 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1811 
1812 	IEEE80211_UNLOCK(vap->iv_ic);
1813 	LKPI_80211_LHW_LOCK(lhw);
1814 
1815 	/* flush, drop. */
1816 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1817 
1818 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1819 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1820 	    !lsta->in_mgd) {
1821 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1822 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1823 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1824 		lsta->in_mgd = true;
1825 	}
1826 
1827 	LKPI_80211_LHW_UNLOCK(lhw);
1828 	IEEE80211_LOCK(vap->iv_ic);
1829 
1830 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1831 	error = lvif->iv_newstate(vap, nstate, arg);
1832 	if (error != 0)
1833 		goto outni;
1834 
1835 	IEEE80211_UNLOCK(vap->iv_ic);
1836 	LKPI_80211_LHW_LOCK(lhw);
1837 
1838 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1839 
1840 	/* Wake tx queues to get packet(s) out. */
1841 	lkpi_wake_tx_queues(hw, sta, true, true);
1842 
1843 	/* flush, no drop */
1844 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1845 
1846 	/* End mgd_complete_tx. */
1847 	if (lsta->in_mgd) {
1848 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1849 		prep_tx_info.success = false;
1850 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1851 		lsta->in_mgd = false;
1852 	}
1853 
1854 	/* sync_rx_queues */
1855 	lkpi_80211_mo_sync_rx_queues(hw);
1856 
1857 	/* sta_pre_rcu_remove */
1858         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1859 
1860 	/* Take the station down. */
1861 
1862 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1863 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1864 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1865 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1866 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1867 	if (error != 0)
1868 		goto out;
1869 
1870 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1871 
1872 	/* Update sta_state (ASSOC to AUTH). */
1873 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1874 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1875 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1876 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1877 	if (error != 0)
1878 		goto out;
1879 
1880 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1881 
1882 	/* Update sta and change state (from AUTH) to NONE. */
1883 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1884 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1885 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1886 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1887 	if (error != 0)
1888 		goto out;
1889 
1890 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1891 
1892 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1893 	/*
1894 	 * One would expect this to happen when going off AUTHORIZED.
1895 	 * See comment there; removes the sta from fw.
1896 	 */
1897 	lkpi_disassoc(sta, vif, lhw);
1898 
1899 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1900 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1901 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1902 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1903 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1904 	if (error != 0) {
1905 		IMPROVE("do we need to undo the chan ctx?");
1906 		goto out;
1907 	}
1908 
1909 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1910 
1911 	IMPROVE("Any bss_info changes to announce?");
1912 	bss_changed = 0;
1913 	vif->bss_conf.qos = 0;
1914 	bss_changed |= BSS_CHANGED_QOS;
1915 	vif->cfg.ssid_len = 0;
1916 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1917 	bss_changed |= BSS_CHANGED_BSSID;
1918 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1919 
1920 	lkpi_lsta_remove(lsta, lvif);
1921 
1922 	/* conf_tx */
1923 
1924 	/* Take the chan ctx down. */
1925 	if (vif->chanctx_conf != NULL) {
1926 		struct lkpi_chanctx *lchanctx;
1927 		struct ieee80211_chanctx_conf *conf;
1928 
1929 		conf = vif->chanctx_conf;
1930 		/* Remove vif context. */
1931 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1932 		/* NB: vif->chanctx_conf is NULL now. */
1933 
1934 		/* Remove chan ctx. */
1935 		lkpi_80211_mo_remove_chanctx(hw, conf);
1936 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1937 		free(lchanctx, M_LKPI80211);
1938 	}
1939 
1940 	error = EALREADY;
1941 out:
1942 	LKPI_80211_LHW_UNLOCK(lhw);
1943 	IEEE80211_LOCK(vap->iv_ic);
1944 outni:
1945 	if (ni != NULL)
1946 		ieee80211_free_node(ni);
1947 	return (error);
1948 }
1949 
1950 static int
1951 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1952 {
1953 
1954 	return (lkpi_sta_run_to_init(vap, nstate, arg));
1955 }
1956 
1957 static int
1958 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1959 {
1960 	int error;
1961 
1962 	error = lkpi_sta_run_to_init(vap, nstate, arg);
1963 	if (error != 0 && error != EALREADY)
1964 		return (error);
1965 
1966 	/* At this point iv_bss is long a new node! */
1967 
1968 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1969 	return (error);
1970 }
1971 
1972 /* -------------------------------------------------------------------------- */
1973 
1974 /*
1975  * The matches the documented state changes in net80211::sta_newstate().
1976  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
1977  * there are "invalid" (so there is a room for failure here).
1978  */
1979 struct fsm_state {
1980 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
1981 	enum ieee80211_state ostate;
1982 	enum ieee80211_state nstate;
1983 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
1984 } sta_state_fsm[] = {
1985 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
1986 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
1987 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
1988 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
1989 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
1990 
1991 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1992 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1993 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
1994 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
1995 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
1996 
1997 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1998 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1999 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2000 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2001 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
2002 
2003 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2004 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2005 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
2006 
2007 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
2008 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
2009 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
2010 
2011 	/* Dummy at the end without handler. */
2012 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
2013 };
2014 
2015 static int
2016 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2017 {
2018 	struct ieee80211com *ic;
2019 	struct lkpi_hw *lhw;
2020 	struct lkpi_vif *lvif;
2021 	struct ieee80211_vif *vif;
2022 	struct fsm_state *s;
2023 	enum ieee80211_state ostate;
2024 	int error;
2025 
2026 	ic = vap->iv_ic;
2027 	IEEE80211_LOCK_ASSERT(ic);
2028 	ostate = vap->iv_state;
2029 
2030 #ifdef LINUXKPI_DEBUG_80211
2031 	if (linuxkpi_debug_80211 & D80211_TRACE)
2032 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
2033 		    __func__, __LINE__, vap, nstate, arg);
2034 #endif
2035 
2036 	if (vap->iv_opmode == IEEE80211_M_STA) {
2037 
2038 		lhw = ic->ic_softc;
2039 		lvif = VAP_TO_LVIF(vap);
2040 		vif = LVIF_TO_VIF(lvif);
2041 
2042 		/* No need to replicate this in most state handlers. */
2043 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2044 			lkpi_stop_hw_scan(lhw, vif);
2045 
2046 		s = sta_state_fsm;
2047 
2048 	} else {
2049 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2050 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2051 		return (ENOSYS);
2052 	}
2053 
2054 	error = 0;
2055 	for (; s->handler != NULL; s++) {
2056 		if (ostate == s->ostate && nstate == s->nstate) {
2057 #ifdef LINUXKPI_DEBUG_80211
2058 			if (linuxkpi_debug_80211 & D80211_TRACE)
2059 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2060 				    " %d (%s): arg %d.\n", __func__,
2061 				    ostate, ieee80211_state_name[ostate],
2062 				    nstate, ieee80211_state_name[nstate], arg);
2063 #endif
2064 			error = s->handler(vap, nstate, arg);
2065 			break;
2066 		}
2067 	}
2068 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2069 
2070 	if (s->handler == NULL) {
2071 		IMPROVE("turn this into a KASSERT\n");
2072 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
2073 		    "%d (%s) -> %d (%s)\n", __func__,
2074 		    ostate, ieee80211_state_name[ostate],
2075 		    nstate, ieee80211_state_name[nstate]);
2076 		return (ENOSYS);
2077 	}
2078 
2079 	if (error == EALREADY) {
2080 #ifdef LINUXKPI_DEBUG_80211
2081 		if (linuxkpi_debug_80211 & D80211_TRACE)
2082 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2083 			    "%d (%s): iv_newstate already handled: %d.\n",
2084 			    __func__, ostate, ieee80211_state_name[ostate],
2085 			    nstate, ieee80211_state_name[nstate], error);
2086 #endif
2087 		return (0);
2088 	}
2089 
2090 	if (error != 0) {
2091 		/* XXX-BZ currently expected so ignore. */
2092 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
2093 		    "%d (%s) -> %d (%s)\n", __func__, error,
2094 		    ostate, ieee80211_state_name[ostate],
2095 		    nstate, ieee80211_state_name[nstate]);
2096 		/* return (error); */
2097 	}
2098 
2099 #ifdef LINUXKPI_DEBUG_80211
2100 	if (linuxkpi_debug_80211 & D80211_TRACE)
2101 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2102 		    "calling net80211 parent\n",
2103 		    __func__, __LINE__, vap, nstate, arg);
2104 #endif
2105 
2106 	return (lvif->iv_newstate(vap, nstate, arg));
2107 }
2108 
2109 /* -------------------------------------------------------------------------- */
2110 
2111 /*
2112  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2113  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2114  * new node without us knowing and thus our ni/lsta are out of sync.
2115  */
2116 static struct ieee80211_node *
2117 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2118 {
2119 	struct lkpi_vif *lvif;
2120 	struct ieee80211_node *obss;
2121 	struct lkpi_sta *lsta;
2122 	struct ieee80211_sta *sta;
2123 
2124 	obss = vap->iv_bss;
2125 
2126 #ifdef LINUXKPI_DEBUG_80211
2127 	if (linuxkpi_debug_80211 & D80211_TRACE)
2128 		ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
2129 		    "ni %p ni_drv_data %p\n", __func__,
2130 		    obss, (obss != NULL) ? obss->ni_drv_data : NULL,
2131 		    ni, (ni != NULL) ? ni->ni_drv_data : NULL);
2132 #endif
2133 
2134 	/* Nothing to copy from.  Just return. */
2135 	if (obss == NULL || obss->ni_drv_data == NULL)
2136 		goto out;
2137 
2138 	/* Nothing to copy to.  Just return. */
2139 	IMPROVE("clearing the obss might still be needed?");
2140 	if (ni == NULL)
2141 		goto out;
2142 
2143 	/* Nothing changed? panic? */
2144 	if (obss == ni)
2145 		goto out;
2146 
2147 	lsta = obss->ni_drv_data;
2148 	obss->ni_drv_data = ni->ni_drv_data;
2149 	ni->ni_drv_data = lsta;
2150 	if (lsta != NULL) {
2151 		lsta->ni = ni;
2152 		sta = LSTA_TO_STA(lsta);
2153 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2154 		IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
2155 	}
2156 	lsta = obss->ni_drv_data;
2157 	if (lsta != NULL) {
2158 		lsta->ni = obss;
2159 		sta = LSTA_TO_STA(lsta);
2160 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2161 		IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
2162 	}
2163 
2164 out:
2165 	lvif = VAP_TO_LVIF(vap);
2166 	return (lvif->iv_update_bss(vap, ni));
2167 }
2168 
2169 #ifdef LKPI_80211_WME
2170 static int
2171 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
2172 {
2173 	struct ieee80211com *ic;
2174 	struct ieee80211_hw *hw;
2175 	struct lkpi_vif *lvif;
2176 	struct ieee80211_vif *vif;
2177 	struct chanAccParams chp;
2178 	struct wmeParams wmeparr[WME_NUM_AC];
2179 	struct ieee80211_tx_queue_params txqp;
2180 	enum ieee80211_bss_changed changed;
2181 	int error;
2182 	uint16_t ac;
2183 
2184 	IMPROVE();
2185 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2186 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2187 
2188 	if (vap == NULL)
2189 		return (0);
2190 
2191 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
2192 		return (0);
2193 
2194 	if (lhw->ops->conf_tx == NULL)
2195 		return (0);
2196 
2197 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
2198 		lhw->update_wme = true;
2199 		return (0);
2200 	}
2201 	lhw->update_wme = false;
2202 
2203 	ic = lhw->ic;
2204 	ieee80211_wme_ic_getparams(ic, &chp);
2205 	IEEE80211_LOCK(ic);
2206 	for (ac = 0; ac < WME_NUM_AC; ac++)
2207 		wmeparr[ac] = chp.cap_wmeParams[ac];
2208 	IEEE80211_UNLOCK(ic);
2209 
2210 	hw = LHW_TO_HW(lhw);
2211 	lvif = VAP_TO_LVIF(vap);
2212 	vif = LVIF_TO_VIF(lvif);
2213 
2214 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2215 	LKPI_80211_LHW_LOCK(lhw);
2216 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2217 		struct wmeParams *wmep;
2218 
2219 		wmep = &wmeparr[ac];
2220 		bzero(&txqp, sizeof(txqp));
2221 		txqp.cw_min = wmep->wmep_logcwmin;
2222 		txqp.cw_max = wmep->wmep_logcwmax;
2223 		txqp.txop = wmep->wmep_txopLimit;
2224 		txqp.aifs = wmep->wmep_aifsn;
2225 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2226 		if (error != 0)
2227 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2228 			    __func__, ac, error);
2229 	}
2230 	LKPI_80211_LHW_UNLOCK(lhw);
2231 	changed = BSS_CHANGED_QOS;
2232 	if (!planned)
2233 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2234 
2235 	return (changed);
2236 }
2237 #endif
2238 
2239 static int
2240 lkpi_ic_wme_update(struct ieee80211com *ic)
2241 {
2242 #ifdef LKPI_80211_WME
2243 	struct ieee80211vap *vap;
2244 	struct lkpi_hw *lhw;
2245 
2246 	IMPROVE("Use the per-VAP callback in net80211.");
2247 	vap = TAILQ_FIRST(&ic->ic_vaps);
2248 	if (vap == NULL)
2249 		return (0);
2250 
2251 	lhw = ic->ic_softc;
2252 
2253 	lkpi_wme_update(lhw, vap, false);
2254 #endif
2255 	return (0);	/* unused */
2256 }
2257 
2258 static struct ieee80211vap *
2259 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2260     int unit, enum ieee80211_opmode opmode, int flags,
2261     const uint8_t bssid[IEEE80211_ADDR_LEN],
2262     const uint8_t mac[IEEE80211_ADDR_LEN])
2263 {
2264 	struct lkpi_hw *lhw;
2265 	struct ieee80211_hw *hw;
2266 	struct lkpi_vif *lvif;
2267 	struct ieee80211vap *vap;
2268 	struct ieee80211_vif *vif;
2269 	struct ieee80211_tx_queue_params txqp;
2270 	enum ieee80211_bss_changed changed;
2271 	size_t len;
2272 	int error, i;
2273 	uint16_t ac;
2274 
2275 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
2276 		return (NULL);
2277 
2278 	lhw = ic->ic_softc;
2279 	hw = LHW_TO_HW(lhw);
2280 
2281 	len = sizeof(*lvif);
2282 	len += hw->vif_data_size;	/* vif->drv_priv */
2283 
2284 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2285 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2286 	TAILQ_INIT(&lvif->lsta_head);
2287 	vap = LVIF_TO_VAP(lvif);
2288 
2289 	vif = LVIF_TO_VIF(lvif);
2290 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2291 	vif->p2p = false;
2292 	vif->probe_req_reg = false;
2293 	vif->type = lkpi_opmode_to_vif_type(opmode);
2294 	lvif->wdev.iftype = vif->type;
2295 	/* Need to fill in other fields as well. */
2296 	IMPROVE();
2297 
2298 	/* XXX-BZ hardcoded for now! */
2299 #if 1
2300 	vif->chanctx_conf = NULL;
2301 	vif->bss_conf.vif = vif;
2302 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
2303 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
2304 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
2305 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2306 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2307 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
2308 	vif->bss_conf.qos = false;
2309 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
2310 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2311 	vif->cfg.aid = 0;
2312 	vif->cfg.assoc = false;
2313 	vif->cfg.idle = true;
2314 	vif->cfg.ps = false;
2315 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2316 	/*
2317 	 * We need to initialize it to something as the bss_info_changed call
2318 	 * will try to copy from it in iwlwifi and NULL is a panic.
2319 	 * We will set the proper one in scan_to_auth() before being assoc.
2320 	 */
2321 	vif->bss_conf.bssid = ieee80211broadcastaddr;
2322 #endif
2323 #if 0
2324 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2325 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2326 	vif->bss_conf.beacon_int = ic->ic_bintval;
2327 	/* iwlwifi bug. */
2328 	if (vif->bss_conf.beacon_int < 16)
2329 		vif->bss_conf.beacon_int = 16;
2330 #endif
2331 
2332 	/* Link Config */
2333 	vif->link_conf[0] = &vif->bss_conf;
2334 	for (i = 0; i < nitems(vif->link_conf); i++) {
2335 		IMPROVE("more than 1 link one day");
2336 	}
2337 
2338 	/* Setup queue defaults; driver may override in (*add_interface). */
2339 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2340 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2341 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2342 		else if (hw->queues >= IEEE80211_NUM_ACS)
2343 			vif->hw_queue[i] = i;
2344 		else
2345 			vif->hw_queue[i] = 0;
2346 
2347 		/* Initialize the queue to running. Stopped? */
2348 		lvif->hw_queue_stopped[i] = false;
2349 	}
2350 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2351 
2352 	IMPROVE();
2353 
2354 	error = lkpi_80211_mo_start(hw);
2355 	if (error != 0) {
2356 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
2357 		mtx_destroy(&lvif->mtx);
2358 		free(lvif, M_80211_VAP);
2359 		return (NULL);
2360 	}
2361 
2362 	error = lkpi_80211_mo_add_interface(hw, vif);
2363 	if (error != 0) {
2364 		IMPROVE();	/* XXX-BZ mo_stop()? */
2365 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
2366 		mtx_destroy(&lvif->mtx);
2367 		free(lvif, M_80211_VAP);
2368 		return (NULL);
2369 	}
2370 
2371 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2372 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2373 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2374 
2375 	/* Set bss_info. */
2376 	changed = 0;
2377 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2378 
2379 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2380 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2381 	LKPI_80211_LHW_LOCK(lhw);
2382 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2383 
2384 		bzero(&txqp, sizeof(txqp));
2385 		txqp.cw_min = 15;
2386 		txqp.cw_max = 1023;
2387 		txqp.txop = 0;
2388 		txqp.aifs = 2;
2389 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2390 		if (error != 0)
2391 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2392 			    __func__, ac, error);
2393 	}
2394 	LKPI_80211_LHW_UNLOCK(lhw);
2395 	changed = BSS_CHANGED_QOS;
2396 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2397 
2398 	/* Force MC init. */
2399 	lkpi_update_mcast_filter(ic, true);
2400 
2401 	IMPROVE();
2402 
2403 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2404 
2405 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2406 	lvif->iv_newstate = vap->iv_newstate;
2407 	vap->iv_newstate = lkpi_iv_newstate;
2408 	lvif->iv_update_bss = vap->iv_update_bss;
2409 	vap->iv_update_bss = lkpi_iv_update_bss;
2410 
2411 	/* Key management. */
2412 	if (lhw->ops->set_key != NULL) {
2413 #ifdef LKPI_80211_HW_CRYPTO
2414 		vap->iv_key_set = lkpi_iv_key_set;
2415 		vap->iv_key_delete = lkpi_iv_key_delete;
2416 #endif
2417 	}
2418 
2419 	ieee80211_ratectl_init(vap);
2420 
2421 	/* Complete setup. */
2422 	ieee80211_vap_attach(vap, ieee80211_media_change,
2423 	    ieee80211_media_status, mac);
2424 
2425 	if (hw->max_listen_interval == 0)
2426 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2427 	hw->conf.listen_interval = hw->max_listen_interval;
2428 	ic->ic_set_channel(ic);
2429 
2430 	/* XXX-BZ do we need to be able to update these? */
2431 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
2432 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2433 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
2434 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2435 	/* any others? */
2436 	IMPROVE();
2437 
2438 	return (vap);
2439 }
2440 
2441 void
2442 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
2443 {
2444 
2445 	wiphy_unregister(hw->wiphy);
2446 	linuxkpi_ieee80211_ifdetach(hw);
2447 
2448 	IMPROVE();
2449 }
2450 
2451 void
2452 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
2453 {
2454 
2455 	TODO();
2456 }
2457 
2458 static void
2459 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2460 {
2461 	struct ieee80211com *ic;
2462 	struct lkpi_hw *lhw;
2463 	struct ieee80211_hw *hw;
2464 	struct lkpi_vif *lvif;
2465 	struct ieee80211_vif *vif;
2466 
2467 	lvif = VAP_TO_LVIF(vap);
2468 	vif = LVIF_TO_VIF(lvif);
2469 	ic = vap->iv_ic;
2470 	lhw = ic->ic_softc;
2471 	hw = LHW_TO_HW(lhw);
2472 
2473 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2474 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
2475 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2476 
2477 	ieee80211_ratectl_deinit(vap);
2478 	ieee80211_vap_detach(vap);
2479 
2480 	IMPROVE("clear up other bits in this state");
2481 
2482 	lkpi_80211_mo_remove_interface(hw, vif);
2483 
2484 	mtx_destroy(&lvif->mtx);
2485 	free(lvif, M_80211_VAP);
2486 }
2487 
2488 static void
2489 lkpi_ic_update_mcast(struct ieee80211com *ic)
2490 {
2491 
2492 	lkpi_update_mcast_filter(ic, false);
2493 	TRACEOK();
2494 }
2495 
2496 static void
2497 lkpi_ic_update_promisc(struct ieee80211com *ic)
2498 {
2499 
2500 	UNIMPLEMENTED;
2501 }
2502 
2503 static void
2504 lkpi_ic_update_chw(struct ieee80211com *ic)
2505 {
2506 
2507 	UNIMPLEMENTED;
2508 }
2509 
2510 /* Start / stop device. */
2511 static void
2512 lkpi_ic_parent(struct ieee80211com *ic)
2513 {
2514 	struct lkpi_hw *lhw;
2515 #ifdef HW_START_STOP
2516 	struct ieee80211_hw *hw;
2517 	int error;
2518 #endif
2519 	bool start_all;
2520 
2521 	IMPROVE();
2522 
2523 	lhw = ic->ic_softc;
2524 #ifdef HW_START_STOP
2525 	hw = LHW_TO_HW(lhw);
2526 #endif
2527 	start_all = false;
2528 
2529 	/* IEEE80211_UNLOCK(ic); */
2530 	LKPI_80211_LHW_LOCK(lhw);
2531 	if (ic->ic_nrunning > 0) {
2532 #ifdef HW_START_STOP
2533 		error = lkpi_80211_mo_start(hw);
2534 		if (error == 0)
2535 #endif
2536 			start_all = true;
2537 	} else {
2538 #ifdef HW_START_STOP
2539 		lkpi_80211_mo_stop(hw);
2540 #endif
2541 	}
2542 	LKPI_80211_LHW_UNLOCK(lhw);
2543 	/* IEEE80211_LOCK(ic); */
2544 
2545 	if (start_all)
2546 		ieee80211_start_all(ic);
2547 }
2548 
2549 bool
2550 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
2551     size_t ie_ids_len)
2552 {
2553 	int i;
2554 
2555 	for (i = 0; i < ie_ids_len; i++) {
2556 		if (ie == *ie_ids)
2557 			return (true);
2558 	}
2559 
2560 	return (false);
2561 }
2562 
2563 /* Return true if skipped; false if error. */
2564 bool
2565 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
2566 {
2567 	size_t x;
2568 	uint8_t l;
2569 
2570 	x = *xp;
2571 
2572 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
2573 	    __func__, x, ies_len, ies));
2574 	l = ies[x + 1];
2575 	x += 2 + l;
2576 
2577 	if (x > ies_len)
2578 		return (false);
2579 
2580 	*xp = x;
2581 	return (true);
2582 }
2583 
2584 static uint8_t *
2585 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2586     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
2587 {
2588 	struct ieee80211_supported_band *supband;
2589 	struct linuxkpi_ieee80211_channel *channels;
2590 	struct ieee80211com *ic;
2591 	const struct ieee80211_channel *chan;
2592 	const struct ieee80211_rateset *rs;
2593 	uint8_t *pb;
2594 	int band, i;
2595 
2596 	ic = vap->iv_ic;
2597 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2598 		if ((band_mask & (1 << band)) == 0)
2599 			continue;
2600 
2601 		supband = hw->wiphy->bands[band];
2602 		/*
2603 		 * This should not happen;
2604 		 * band_mask is a bitmask of valid bands to scan on.
2605 		 */
2606 		if (supband == NULL || supband->n_channels == 0)
2607 			continue;
2608 
2609 		/* Find a first channel to get the mode and rates from. */
2610 		channels = supband->channels;
2611 		chan = NULL;
2612 		for (i = 0; i < supband->n_channels; i++) {
2613 
2614 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2615 				continue;
2616 
2617 			chan = ieee80211_find_channel(ic,
2618 			    channels[i].center_freq, 0);
2619 			if (chan != NULL)
2620 				break;
2621 		}
2622 
2623 		/* This really should not happen. */
2624 		if (chan == NULL)
2625 			continue;
2626 
2627 		pb = p;
2628 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
2629 		p = ieee80211_add_rates(p, rs);
2630 		p = ieee80211_add_xrates(p, rs);
2631 
2632 #if defined(LKPI_80211_HT)
2633 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
2634 			struct ieee80211_channel *c;
2635 
2636 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2637 			    vap->iv_flags_ht);
2638 			p = ieee80211_add_htcap_ch(p, vap, c);
2639 		}
2640 #endif
2641 #if defined(LKPI_80211_VHT)
2642 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
2643 			struct ieee80211_channel *c;
2644 
2645 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2646 			    vap->iv_flags_ht);
2647 			c = ieee80211_vht_adjust_channel(ic, c,
2648 			    vap->iv_vht_flags);
2649 			p = ieee80211_add_vhtcap_ch(p, vap, c);
2650 		}
2651 #endif
2652 
2653 		scan_ies->ies[band] = pb;
2654 		scan_ies->len[band] = p - pb;
2655 	}
2656 
2657 	/* Add common_ies */
2658 	pb = p;
2659 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2660 	    vap->iv_wpa_ie != NULL) {
2661 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2662 		p += 2 + vap->iv_wpa_ie[1];
2663 	}
2664 	if (vap->iv_appie_probereq != NULL) {
2665 		memcpy(p, vap->iv_appie_probereq->ie_data,
2666 		    vap->iv_appie_probereq->ie_len);
2667 		p += vap->iv_appie_probereq->ie_len;
2668 	}
2669 	scan_ies->common_ies = pb;
2670 	scan_ies->common_ie_len = p - pb;
2671 
2672 	return (p);
2673 }
2674 
2675 static void
2676 lkpi_ic_scan_start(struct ieee80211com *ic)
2677 {
2678 	struct lkpi_hw *lhw;
2679 	struct ieee80211_hw *hw;
2680 	struct lkpi_vif *lvif;
2681 	struct ieee80211_vif *vif;
2682 	struct ieee80211_scan_state *ss;
2683 	struct ieee80211vap *vap;
2684 	int error;
2685 	bool is_hw_scan;
2686 
2687 	lhw = ic->ic_softc;
2688 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2689 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2690 		/* A scan is still running. */
2691 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2692 		return;
2693 	}
2694 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2695 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2696 
2697 	ss = ic->ic_scan;
2698 	vap = ss->ss_vap;
2699 	if (vap->iv_state != IEEE80211_S_SCAN) {
2700 		IMPROVE("We need to be able to scan if not in S_SCAN");
2701 		return;
2702 	}
2703 
2704 	hw = LHW_TO_HW(lhw);
2705 	if (!is_hw_scan) {
2706 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
2707 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2708 sw_scan:
2709 		lvif = VAP_TO_LVIF(vap);
2710 		vif = LVIF_TO_VIF(lvif);
2711 
2712 		if (vap->iv_state == IEEE80211_S_SCAN)
2713 			lkpi_hw_conf_idle(hw, false);
2714 
2715 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
2716 		/* net80211::scan_start() handled PS for us. */
2717 		IMPROVE();
2718 		/* XXX Also means it is too late to flush queues?
2719 		 * need to check iv_sta_ps or overload? */
2720 		/* XXX want to adjust ss end time/ maxdwell? */
2721 
2722 	} else {
2723 		struct ieee80211_channel *c;
2724 		struct ieee80211_scan_request *hw_req;
2725 		struct linuxkpi_ieee80211_channel *lc, **cpp;
2726 		struct cfg80211_ssid *ssids;
2727 		struct cfg80211_scan_6ghz_params *s6gp;
2728 		size_t chan_len, nchan, ssids_len, s6ghzlen;
2729 		int band, i, ssid_count, common_ie_len;
2730 		uint32_t band_mask;
2731 		uint8_t *ie, *ieend;
2732 		bool running;
2733 
2734 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
2735 		ssids_len = ssid_count * sizeof(*ssids);
2736 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
2737 
2738 		band_mask = 0;
2739 		nchan = 0;
2740 		for (i = ss->ss_next; i < ss->ss_last; i++) {
2741 			nchan++;
2742 			band = lkpi_net80211_chan_to_nl80211_band(
2743 			    ss->ss_chans[ss->ss_next + i]);
2744 			band_mask |= (1 << band);
2745 		}
2746 
2747 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2748 			IMPROVE("individual band scans not yet supported, only scanning first band");
2749 			/* In theory net80211 should drive this. */
2750 			/* Probably we need to add local logic for now;
2751 			 * need to deal with scan_complete
2752 			 * and cancel_scan and keep local state.
2753 			 * Also cut the nchan down above.
2754 			 */
2755 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
2756 		}
2757 
2758 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
2759 
2760 		common_ie_len = 0;
2761 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2762 		    vap->iv_wpa_ie != NULL)
2763 			common_ie_len += vap->iv_wpa_ie[1];
2764 		if (vap->iv_appie_probereq != NULL)
2765 			common_ie_len += vap->iv_appie_probereq->ie_len;
2766 
2767 		/* We would love to check this at an earlier stage... */
2768 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
2769 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
2770 			    "wiphy->max_scan_ie_len %d\n", __func__,
2771 			    common_ie_len, hw->wiphy->max_scan_ie_len);
2772 		}
2773 
2774 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
2775 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
2776 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
2777 
2778 		hw_req->req.flags = 0;			/* XXX ??? */
2779 		/* hw_req->req.wdev */
2780 		hw_req->req.wiphy = hw->wiphy;
2781 		hw_req->req.no_cck = false;		/* XXX */
2782 #if 0
2783 		/* This seems to pessimise default scanning behaviour. */
2784 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
2785 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
2786 #endif
2787 #ifdef __notyet__
2788 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
2789 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
2790 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
2791 #endif
2792 		eth_broadcast_addr(hw_req->req.bssid);
2793 
2794 		hw_req->req.n_channels = nchan;
2795 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
2796 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
2797 		for (i = 0; i < nchan; i++) {
2798 			*(cpp + i) =
2799 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
2800 		}
2801 		for (i = 0; i < nchan; i++) {
2802 			c = ss->ss_chans[ss->ss_next + i];
2803 
2804 			lc->hw_value = c->ic_ieee;
2805 			lc->center_freq = c->ic_freq;	/* XXX */
2806 			/* lc->flags */
2807 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
2808 			lc->max_power = c->ic_maxpower;
2809 			/* lc-> ... */
2810 			lc++;
2811 		}
2812 
2813 		hw_req->req.n_ssids = ssid_count;
2814 		if (hw_req->req.n_ssids > 0) {
2815 			ssids = (struct cfg80211_ssid *)lc;
2816 			hw_req->req.ssids = ssids;
2817 			for (i = 0; i < ssid_count; i++) {
2818 				ssids->ssid_len = ss->ss_ssid[i].len;
2819 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
2820 				    ss->ss_ssid[i].len);
2821 				ssids++;
2822 			}
2823 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
2824 		} else {
2825 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
2826 		}
2827 
2828 		/* 6GHz one day. */
2829 		hw_req->req.n_6ghz_params = 0;
2830 		hw_req->req.scan_6ghz_params = NULL;
2831 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
2832 		/* s6gp->... */
2833 
2834 		ie = ieend = (uint8_t *)s6gp;
2835 		/* Copy per-band IEs, copy common IEs */
2836 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
2837 		hw_req->req.ie = ie;
2838 		hw_req->req.ie_len = ieend - ie;
2839 
2840 		lvif = VAP_TO_LVIF(vap);
2841 		vif = LVIF_TO_VIF(lvif);
2842 
2843 		LKPI_80211_LHW_SCAN_LOCK(lhw);
2844 		/* Re-check under lock. */
2845 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2846 		if (!running) {
2847 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
2848 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
2849 
2850 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
2851 			lhw->hw_req = hw_req;
2852 		}
2853 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2854 		if (running) {
2855 			free(hw_req, M_LKPI80211);
2856 			return;
2857 		}
2858 
2859 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
2860 		if (error != 0) {
2861 			ieee80211_cancel_scan(vap);
2862 
2863 			/*
2864 			 * ieee80211_scan_completed must be called in either
2865 			 * case of error or none.  So let the free happen there
2866 			 * and only there.
2867 			 * That would be fine in theory but in practice drivers
2868 			 * behave differently:
2869 			 * ath10k does not return hw_scan until after scan_complete
2870 			 *        and can then still return an error.
2871 			 * rtw88 can return 1 or -EBUSY without scan_complete
2872 			 * iwlwifi can return various errors before scan starts
2873 			 * ...
2874 			 * So we cannot rely on that behaviour and have to check
2875 			 * and balance between both code paths.
2876 			 */
2877 			LKPI_80211_LHW_SCAN_LOCK(lhw);
2878 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2879 				free(lhw->hw_req, M_LKPI80211);
2880 				lhw->hw_req = NULL;
2881 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
2882 			}
2883 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2884 
2885 			/*
2886 			 * XXX-SIGH magic number.
2887 			 * rtw88 has a magic "return 1" if offloading scan is
2888 			 * not possible.  Fall back to sw scan in that case.
2889 			 */
2890 			if (error == 1) {
2891 				LKPI_80211_LHW_SCAN_LOCK(lhw);
2892 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
2893 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2894 				/*
2895 				 * XXX If we clear this now and later a driver
2896 				 * thinks it * can do a hw_scan again, we will
2897 				 * currently not re-enable it?
2898 				 */
2899 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2900 				ieee80211_start_scan(vap,
2901 				    IEEE80211_SCAN_ACTIVE |
2902 				    IEEE80211_SCAN_NOPICK |
2903 				    IEEE80211_SCAN_ONCE,
2904 				    IEEE80211_SCAN_FOREVER,
2905 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2906 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2907 				    vap->iv_des_nssid, vap->iv_des_ssid);
2908 				goto sw_scan;
2909 			}
2910 
2911 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2912 			    __func__, error);
2913 		}
2914 	}
2915 }
2916 
2917 static void
2918 lkpi_ic_scan_end(struct ieee80211com *ic)
2919 {
2920 	struct lkpi_hw *lhw;
2921 	bool is_hw_scan;
2922 
2923 	lhw = ic->ic_softc;
2924 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2925 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
2926 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2927 		return;
2928 	}
2929 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2930 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2931 
2932 	if (!is_hw_scan) {
2933 		struct ieee80211_scan_state *ss;
2934 		struct ieee80211vap *vap;
2935 		struct ieee80211_hw *hw;
2936 		struct lkpi_vif *lvif;
2937 		struct ieee80211_vif *vif;
2938 
2939 		ss = ic->ic_scan;
2940 		vap = ss->ss_vap;
2941 		hw = LHW_TO_HW(lhw);
2942 		lvif = VAP_TO_LVIF(vap);
2943 		vif = LVIF_TO_VIF(lvif);
2944 
2945 		lkpi_80211_mo_sw_scan_complete(hw, vif);
2946 
2947 		/* Send PS to stop buffering if n80211 does not for us? */
2948 
2949 		if (vap->iv_state == IEEE80211_S_SCAN)
2950 			lkpi_hw_conf_idle(hw, true);
2951 	}
2952 }
2953 
2954 static void
2955 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
2956     unsigned long maxdwell)
2957 {
2958 	struct lkpi_hw *lhw;
2959 	bool is_hw_scan;
2960 
2961 	lhw = ss->ss_ic->ic_softc;
2962 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2963 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2964 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2965 	if (!is_hw_scan)
2966 		lhw->ic_scan_curchan(ss, maxdwell);
2967 }
2968 
2969 static void
2970 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
2971 {
2972 	struct lkpi_hw *lhw;
2973 	bool is_hw_scan;
2974 
2975 	lhw = ss->ss_ic->ic_softc;
2976 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2977 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2978 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2979 	if (!is_hw_scan)
2980 		lhw->ic_scan_mindwell(ss);
2981 }
2982 
2983 static void
2984 lkpi_ic_set_channel(struct ieee80211com *ic)
2985 {
2986 	struct lkpi_hw *lhw;
2987 	struct ieee80211_hw *hw;
2988 	struct ieee80211_channel *c;
2989 	struct linuxkpi_ieee80211_channel *chan;
2990 	int error;
2991 	bool hw_scan_running;
2992 
2993 	lhw = ic->ic_softc;
2994 
2995 	/* If we do not support (*config)() save us the work. */
2996 	if (lhw->ops->config == NULL)
2997 		return;
2998 
2999 	/* If we have a hw_scan running do not switch channels. */
3000 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3001 	hw_scan_running =
3002 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
3003 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
3004 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3005 	if (hw_scan_running)
3006 		return;
3007 
3008 	c = ic->ic_curchan;
3009 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
3010 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
3011 		    c, lhw->ops->config);
3012 		return;
3013 	}
3014 
3015 	chan = lkpi_find_lkpi80211_chan(lhw, c);
3016 	if (chan == NULL) {
3017 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
3018 		    c, chan);
3019 		return;
3020 	}
3021 
3022 	/* XXX max power for scanning? */
3023 	IMPROVE();
3024 
3025 	hw = LHW_TO_HW(lhw);
3026 	cfg80211_chandef_create(&hw->conf.chandef, chan,
3027 	    NL80211_CHAN_NO_HT);
3028 
3029 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
3030 	if (error != 0 && error != EOPNOTSUPP) {
3031 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
3032 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
3033 		/* XXX should we unroll to the previous chandef? */
3034 		IMPROVE();
3035 	} else {
3036 		/* Update radiotap channels as well. */
3037 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
3038 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
3039 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
3040 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
3041 	}
3042 
3043 	/* Currently PS is hard coded off! Not sure it belongs here. */
3044 	IMPROVE();
3045 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
3046 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
3047 		hw->conf.flags &= ~IEEE80211_CONF_PS;
3048 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
3049 		if (error != 0 && error != EOPNOTSUPP)
3050 			ic_printf(ic, "ERROR: %s: config %#0x returned "
3051 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
3052 			    error);
3053 	}
3054 }
3055 
3056 static struct ieee80211_node *
3057 lkpi_ic_node_alloc(struct ieee80211vap *vap,
3058     const uint8_t mac[IEEE80211_ADDR_LEN])
3059 {
3060 	struct ieee80211com *ic;
3061 	struct lkpi_hw *lhw;
3062 	struct ieee80211_node *ni;
3063 	struct ieee80211_hw *hw;
3064 	struct lkpi_sta *lsta;
3065 
3066 	ic = vap->iv_ic;
3067 	lhw = ic->ic_softc;
3068 
3069 	/* We keep allocations de-coupled so we can deal with the two worlds. */
3070 	if (lhw->ic_node_alloc == NULL)
3071 		return (NULL);
3072 
3073 	ni = lhw->ic_node_alloc(vap, mac);
3074 	if (ni == NULL)
3075 		return (NULL);
3076 
3077 	hw = LHW_TO_HW(lhw);
3078 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
3079 	if (lsta == NULL) {
3080 		if (lhw->ic_node_free != NULL)
3081 			lhw->ic_node_free(ni);
3082 		return (NULL);
3083 	}
3084 
3085 	return (ni);
3086 }
3087 
3088 static int
3089 lkpi_ic_node_init(struct ieee80211_node *ni)
3090 {
3091 	struct ieee80211com *ic;
3092 	struct lkpi_hw *lhw;
3093 	struct lkpi_sta *lsta;
3094 	int error;
3095 
3096 	ic = ni->ni_ic;
3097 	lhw = ic->ic_softc;
3098 
3099 	if (lhw->ic_node_init != NULL) {
3100 		error = lhw->ic_node_init(ni);
3101 		if (error != 0)
3102 			return (error);
3103 	}
3104 
3105 	lsta = ni->ni_drv_data;
3106 
3107 	/* Now take the reference before linking it to the table. */
3108 	lsta->ni = ieee80211_ref_node(ni);
3109 
3110 	/* XXX-BZ Sync other state over. */
3111 	IMPROVE();
3112 
3113 	return (0);
3114 }
3115 
3116 static void
3117 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
3118 {
3119 	struct ieee80211com *ic;
3120 	struct lkpi_hw *lhw;
3121 
3122 	ic = ni->ni_ic;
3123 	lhw = ic->ic_softc;
3124 
3125 	/* XXX-BZ remove from driver, ... */
3126 	IMPROVE();
3127 
3128 	if (lhw->ic_node_cleanup != NULL)
3129 		lhw->ic_node_cleanup(ni);
3130 }
3131 
3132 static void
3133 lkpi_ic_node_free(struct ieee80211_node *ni)
3134 {
3135 	struct ieee80211com *ic;
3136 	struct lkpi_hw *lhw;
3137 	struct lkpi_sta *lsta;
3138 
3139 	ic = ni->ni_ic;
3140 	lhw = ic->ic_softc;
3141 	lsta = ni->ni_drv_data;
3142 	if (lsta == NULL)
3143 		goto out;
3144 
3145 	/* XXX-BZ free resources, ... */
3146 	IMPROVE();
3147 
3148 	/* Flush mbufq (make sure to release ni refs!). */
3149 #ifdef __notyet__
3150 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
3151 	    __func__, lsta, mbufq_len(&lsta->txq)));
3152 #endif
3153 	/* Drain taskq. */
3154 
3155 	/* Drain sta->txq[] */
3156 	mtx_destroy(&lsta->txq_mtx);
3157 
3158 	/* Remove lsta if added_to_drv. */
3159 
3160 	/* Remove lsta from vif */
3161 	/* Remove ref from lsta node... */
3162 	/* Free lsta. */
3163 	lkpi_lsta_remove(lsta, VAP_TO_LVIF(ni->ni_vap));
3164 
3165 out:
3166 	if (lhw->ic_node_free != NULL)
3167 		lhw->ic_node_free(ni);
3168 }
3169 
3170 static int
3171 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3172         const struct ieee80211_bpf_params *params __unused)
3173 {
3174 	struct lkpi_sta *lsta;
3175 
3176 	lsta = ni->ni_drv_data;
3177 
3178 	/* Queue the packet and enqueue the task to handle it. */
3179 	LKPI_80211_LSTA_LOCK(lsta);
3180 	mbufq_enqueue(&lsta->txq, m);
3181 	LKPI_80211_LSTA_UNLOCK(lsta);
3182 
3183 #ifdef LINUXKPI_DEBUG_80211
3184 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3185 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
3186 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
3187 		    mbufq_len(&lsta->txq));
3188 #endif
3189 
3190 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3191 	return (0);
3192 }
3193 
3194 static void
3195 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
3196 {
3197 	struct ieee80211_node *ni;
3198 #ifndef LKPI_80211_HW_CRYPTO
3199 	struct ieee80211_frame *wh;
3200 #endif
3201 	struct ieee80211_key *k;
3202 	struct sk_buff *skb;
3203 	struct ieee80211com *ic;
3204 	struct lkpi_hw *lhw;
3205 	struct ieee80211_hw *hw;
3206 	struct lkpi_vif *lvif;
3207 	struct ieee80211_vif *vif;
3208 	struct ieee80211_channel *c;
3209 	struct ieee80211_tx_control control;
3210 	struct ieee80211_tx_info *info;
3211 	struct ieee80211_sta *sta;
3212 	struct ieee80211_hdr *hdr;
3213 	void *buf;
3214 	uint8_t ac, tid;
3215 
3216 	M_ASSERTPKTHDR(m);
3217 #ifdef LINUXKPI_DEBUG_80211
3218 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
3219 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
3220 #endif
3221 
3222 	ni = lsta->ni;
3223 	k = NULL;
3224 #ifndef LKPI_80211_HW_CRYPTO
3225 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
3226 	wh = mtod(m, struct ieee80211_frame *);
3227 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
3228 		/* Retrieve key for TX && do software encryption. */
3229 		k = ieee80211_crypto_encap(ni, m);
3230 		if (k == NULL) {
3231 			ieee80211_free_node(ni);
3232 			m_freem(m);
3233 			return;
3234 		}
3235 	}
3236 #endif
3237 
3238 	ic = ni->ni_ic;
3239 	lhw = ic->ic_softc;
3240 	hw = LHW_TO_HW(lhw);
3241 	c = ni->ni_chan;
3242 
3243 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
3244 		struct lkpi_radiotap_tx_hdr *rtap;
3245 
3246 		rtap = &lhw->rtap_tx;
3247 		rtap->wt_flags = 0;
3248 		if (k != NULL)
3249 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3250 		if (m->m_flags & M_FRAG)
3251 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
3252 		IMPROVE();
3253 		rtap->wt_rate = 0;
3254 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3255 			rtap->wt_chan_freq = htole16(c->ic_freq);
3256 			rtap->wt_chan_flags = htole16(c->ic_flags);
3257 		}
3258 
3259 		ieee80211_radiotap_tx(ni->ni_vap, m);
3260 	}
3261 
3262 	/*
3263 	 * net80211 should handle hw->extra_tx_headroom.
3264 	 * Though for as long as we are copying we don't mind.
3265 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
3266 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
3267 	 */
3268 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
3269 	if (skb == NULL) {
3270 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
3271 		ieee80211_free_node(ni);
3272 		m_freem(m);
3273 		return;
3274 	}
3275 	skb_reserve(skb, hw->extra_tx_headroom);
3276 
3277 	/* XXX-BZ we need a SKB version understanding mbuf. */
3278 	/* Save the mbuf for ieee80211_tx_complete(). */
3279 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3280 	skb->m = m;
3281 #if 0
3282 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3283 #else
3284 	buf = skb_put(skb, m->m_pkthdr.len);
3285 	m_copydata(m, 0, m->m_pkthdr.len, buf);
3286 #endif
3287 	/* Save the ni. */
3288 	m->m_pkthdr.PH_loc.ptr = ni;
3289 
3290 	lvif = VAP_TO_LVIF(ni->ni_vap);
3291 	vif = LVIF_TO_VIF(lvif);
3292 
3293 	hdr = (void *)skb->data;
3294 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3295 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3296 		skb->priority = 0;
3297 		ac = IEEE80211_AC_BE;
3298 	} else {
3299 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3300 		ac = ieee80211e_up_to_ac[tid & 7];
3301 	}
3302 	skb_set_queue_mapping(skb, ac);
3303 
3304 	info = IEEE80211_SKB_CB(skb);
3305 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3306 	/* Slight delay; probably only happens on scanning so fine? */
3307 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
3308 		c = ic->ic_curchan;
3309 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3310 	info->hw_queue = vif->hw_queue[ac];
3311 	if (m->m_flags & M_EAPOL)
3312 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3313 	info->control.vif = vif;
3314 	/* XXX-BZ info->control.rates */
3315 
3316 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
3317 	if (lsta != NULL) {
3318 		sta = LSTA_TO_STA(lsta);
3319 #ifdef LKPI_80211_HW_CRYPTO
3320 		info->control.hw_key = lsta->kc;
3321 #endif
3322 	} else {
3323 		sta = NULL;
3324 	}
3325 
3326 	IMPROVE();
3327 
3328 	if (sta != NULL) {
3329 		struct lkpi_txq *ltxq;
3330 
3331 		ltxq = NULL;
3332 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3333 			if (vif->type == NL80211_IFTYPE_STATION &&
3334 			    lsta->added_to_drv &&
3335 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3336 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3337 		} else if (lsta->added_to_drv &&
3338 		    sta->txq[skb->priority] != NULL) {
3339 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3340 		}
3341 		if (ltxq == NULL)
3342 			goto ops_tx;
3343 
3344 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3345 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3346 
3347 		skb_queue_tail(&ltxq->skbq, skb);
3348 #ifdef LINUXKPI_DEBUG_80211
3349 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3350 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3351 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3352 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3353 			    __func__, __LINE__,
3354 			    curthread->td_tid, (unsigned int)ticks,
3355 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3356 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3357 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
3358 #endif
3359 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
3360 		return;
3361 	}
3362 
3363 ops_tx:
3364 #ifdef LINUXKPI_DEBUG_80211
3365 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3366 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3367 		    "TX ac %d prio %u qmap %u\n",
3368 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3369 		    skb, ac, skb->priority, skb->qmap);
3370 #endif
3371 	memset(&control, 0, sizeof(control));
3372 	control.sta = sta;
3373 
3374 	lkpi_80211_mo_tx(hw, &control, skb);
3375 	return;
3376 }
3377 
3378 static void
3379 lkpi_80211_txq_task(void *ctx, int pending)
3380 {
3381 	struct lkpi_sta *lsta;
3382 	struct mbufq mq;
3383 	struct mbuf *m;
3384 
3385 	lsta = ctx;
3386 
3387 #ifdef LINUXKPI_DEBUG_80211
3388 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3389 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3390 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3391 		    pending, mbufq_len(&lsta->txq));
3392 #endif
3393 
3394 	mbufq_init(&mq, IFQ_MAXLEN);
3395 
3396 	LKPI_80211_LSTA_LOCK(lsta);
3397 	mbufq_concat(&mq, &lsta->txq);
3398 	LKPI_80211_LSTA_UNLOCK(lsta);
3399 
3400 	m = mbufq_dequeue(&mq);
3401 	while (m != NULL) {
3402 		lkpi_80211_txq_tx_one(lsta, m);
3403 		m = mbufq_dequeue(&mq);
3404 	}
3405 }
3406 
3407 static int
3408 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3409 {
3410 
3411 	/* XXX TODO */
3412 	IMPROVE();
3413 
3414 	/* Quick and dirty cheating hack. */
3415 	struct ieee80211_node *ni;
3416 
3417 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3418 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3419 }
3420 
3421 static void
3422 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
3423     int *n, struct ieee80211_channel *c)
3424 {
3425 	struct lkpi_hw *lhw;
3426 	struct ieee80211_hw *hw;
3427 	struct linuxkpi_ieee80211_channel *channels;
3428 	uint8_t bands[IEEE80211_MODE_BYTES];
3429 	int chan_flags, error, i, nchans;
3430 
3431 	/* Channels */
3432 	lhw = ic->ic_softc;
3433 	hw = LHW_TO_HW(lhw);
3434 
3435 	/* NL80211_BAND_2GHZ */
3436 	nchans = 0;
3437 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
3438 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
3439 	if (nchans > 0) {
3440 		memset(bands, 0, sizeof(bands));
3441 		chan_flags = 0;
3442 		setbit(bands, IEEE80211_MODE_11B);
3443 		/* XXX-BZ unclear how to check for 11g. */
3444 		setbit(bands, IEEE80211_MODE_11G);
3445 #ifdef __notyet__
3446 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
3447 			setbit(bands, IEEE80211_MODE_11NG);
3448 			chan_flags |= NET80211_CBW_FLAG_HT40;
3449 		}
3450 #endif
3451 
3452 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3453 		for (i = 0; i < nchans && *n < maxchan; i++) {
3454 			uint32_t nflags = 0;
3455 			int cflags = chan_flags;
3456 
3457 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3458 				ic_printf(ic, "%s: Skipping disabled chan "
3459 				    "[%u/%u/%#x]\n", __func__,
3460 				    channels[i].hw_value,
3461 				    channels[i].center_freq, channels[i].flags);
3462 				continue;
3463 			}
3464 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3465 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3466 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3467 				nflags |= IEEE80211_CHAN_DFS;
3468 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3469 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3470 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3471 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3472 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
3473 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3474 				cflags &= ~NET80211_CBW_FLAG_HT40;
3475 
3476 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3477 			    channels[i].hw_value, channels[i].center_freq,
3478 			    channels[i].max_power,
3479 			    nflags, bands, chan_flags);
3480 			/* net80211::ENOBUFS: *n >= maxchans */
3481 			if (error != 0 && error != ENOBUFS)
3482 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3483 				    "returned error %d\n",
3484 				    __func__, channels[i].hw_value,
3485 				    channels[i].center_freq, channels[i].flags,
3486 				    nflags, chan_flags, cflags, error);
3487 			if (error != 0)
3488 				break;
3489 		}
3490 	}
3491 
3492 	/* NL80211_BAND_5GHZ */
3493 	nchans = 0;
3494 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
3495 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
3496 	if (nchans > 0) {
3497 		memset(bands, 0, sizeof(bands));
3498 		chan_flags = 0;
3499 		setbit(bands, IEEE80211_MODE_11A);
3500 #ifdef __not_yet__
3501 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
3502 			setbit(bands, IEEE80211_MODE_11NA);
3503 			chan_flags |= NET80211_CBW_FLAG_HT40;
3504 		}
3505 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
3506 
3507 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
3508 			ic->ic_vhtcaps =
3509 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
3510 
3511 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
3512 			chan_flags |= NET80211_CBW_FLAG_VHT80;
3513 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
3514 			    ic->ic_vhtcaps))
3515 				chan_flags |= NET80211_CBW_FLAG_VHT160;
3516 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
3517 			    ic->ic_vhtcaps))
3518 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
3519 		}
3520 #endif
3521 
3522 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3523 		for (i = 0; i < nchans && *n < maxchan; i++) {
3524 			uint32_t nflags = 0;
3525 			int cflags = chan_flags;
3526 
3527 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3528 				ic_printf(ic, "%s: Skipping disabled chan "
3529 				    "[%u/%u/%#x]\n", __func__,
3530 				    channels[i].hw_value,
3531 				    channels[i].center_freq, channels[i].flags);
3532 				continue;
3533 			}
3534 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3535 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3536 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3537 				nflags |= IEEE80211_CHAN_DFS;
3538 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3539 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3540 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3541 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3542 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
3543 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3544 				cflags &= ~NET80211_CBW_FLAG_HT40;
3545 
3546 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3547 			    channels[i].hw_value, channels[i].center_freq,
3548 			    channels[i].max_power,
3549 			    nflags, bands, chan_flags);
3550 			/* net80211::ENOBUFS: *n >= maxchans */
3551 			if (error != 0 && error != ENOBUFS)
3552 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3553 				    "returned error %d\n",
3554 				    __func__, channels[i].hw_value,
3555 				    channels[i].center_freq, channels[i].flags,
3556 				    nflags, chan_flags, cflags, error);
3557 			if (error != 0)
3558 				break;
3559 		}
3560 	}
3561 }
3562 
3563 static void *
3564 lkpi_ieee80211_ifalloc(void)
3565 {
3566 	struct ieee80211com *ic;
3567 
3568 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
3569 	if (ic == NULL)
3570 		return (NULL);
3571 
3572 	/* Setting these happens later when we have device information. */
3573 	ic->ic_softc = NULL;
3574 	ic->ic_name = "linuxkpi";
3575 
3576 	return (ic);
3577 }
3578 
3579 struct ieee80211_hw *
3580 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
3581 {
3582 	struct ieee80211_hw *hw;
3583 	struct lkpi_hw *lhw;
3584 	struct wiphy *wiphy;
3585 	int ac;
3586 
3587 	/* Get us and the driver data also allocated. */
3588 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
3589 	if (wiphy == NULL)
3590 		return (NULL);
3591 
3592 	lhw = wiphy_priv(wiphy);
3593 	lhw->ops = ops;
3594 
3595 	LKPI_80211_LHW_LOCK_INIT(lhw);
3596 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
3597 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
3598 	TAILQ_INIT(&lhw->lvif_head);
3599 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3600 		lhw->txq_generation[ac] = 1;
3601 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
3602 	}
3603 
3604 	/*
3605 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
3606 	 * not initialized.
3607 	 */
3608 	hw = LHW_TO_HW(lhw);
3609 	hw->wiphy = wiphy;
3610 	hw->conf.flags |= IEEE80211_CONF_IDLE;
3611 	hw->priv = (void *)(lhw + 1);
3612 
3613 	/* BSD Specific. */
3614 	lhw->ic = lkpi_ieee80211_ifalloc();
3615 	if (lhw->ic == NULL) {
3616 		ieee80211_free_hw(hw);
3617 		return (NULL);
3618 	}
3619 
3620 	IMPROVE();
3621 
3622 	return (hw);
3623 }
3624 
3625 void
3626 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
3627 {
3628 	struct lkpi_hw *lhw;
3629 
3630 	lhw = HW_TO_LHW(hw);
3631 	free(lhw->ic, M_LKPI80211);
3632 	lhw->ic = NULL;
3633 
3634 	/* Cleanup more of lhw here or in wiphy_free()? */
3635 	sx_destroy(&lhw->lvif_sx);
3636 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
3637 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
3638 	IMPROVE();
3639 }
3640 
3641 void
3642 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
3643 {
3644 	struct lkpi_hw *lhw;
3645 	struct ieee80211com *ic;
3646 
3647 	lhw = HW_TO_LHW(hw);
3648 	ic = lhw->ic;
3649 
3650 	/* Now set a proper name before ieee80211_ifattach(). */
3651 	ic->ic_softc = lhw;
3652 	ic->ic_name = name;
3653 
3654 	/* XXX-BZ do we also need to set wiphy name? */
3655 }
3656 
3657 struct ieee80211_hw *
3658 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
3659 {
3660 	struct lkpi_hw *lhw;
3661 
3662 	lhw = wiphy_priv(wiphy);
3663 	return (LHW_TO_HW(lhw));
3664 }
3665 
3666 static void
3667 lkpi_radiotap_attach(struct lkpi_hw *lhw)
3668 {
3669 	struct ieee80211com *ic;
3670 
3671 	ic = lhw->ic;
3672 	ieee80211_radiotap_attach(ic,
3673 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
3674 	    LKPI_RTAP_TX_FLAGS_PRESENT,
3675 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
3676 	    LKPI_RTAP_RX_FLAGS_PRESENT);
3677 }
3678 
3679 int
3680 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
3681 {
3682 	struct ieee80211com *ic;
3683 	struct lkpi_hw *lhw;
3684 	int band, i;
3685 
3686 	lhw = HW_TO_LHW(hw);
3687 	ic = lhw->ic;
3688 
3689 	/* We do it this late as wiphy->dev should be set for the name. */
3690 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3691 	if (lhw->workq == NULL)
3692 		return (-EAGAIN);
3693 
3694 	/* XXX-BZ figure this out how they count his... */
3695 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
3696 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3697 		    hw->wiphy->perm_addr);
3698 	} else if (hw->wiphy->n_addresses > 0) {
3699 		/* We take the first one. */
3700 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3701 		    hw->wiphy->addresses[0].addr);
3702 	} else {
3703 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
3704 	}
3705 
3706 #ifdef __not_yet__
3707 	/* See comment in lkpi_80211_txq_tx_one(). */
3708 	ic->ic_headroom = hw->extra_tx_headroom;
3709 #endif
3710 
3711 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
3712 	ic->ic_opmode = IEEE80211_M_STA;
3713 
3714 	/* Set device capabilities. */
3715 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
3716 	ic->ic_caps =
3717 	    IEEE80211_C_STA |
3718 	    IEEE80211_C_MONITOR |
3719 	    IEEE80211_C_WPA |		/* WPA/RSN */
3720 #ifdef LKPI_80211_WME
3721 	    IEEE80211_C_WME |
3722 #endif
3723 #if 0
3724 	    IEEE80211_C_PMGT |
3725 #endif
3726 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
3727 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
3728 	    ;
3729 #if 0
3730 	/* Scanning is a different kind of beast to re-work. */
3731 	ic->ic_caps |= IEEE80211_C_BGSCAN;
3732 #endif
3733 	if (lhw->ops->hw_scan) {
3734 		/*
3735 		 * Advertise full-offload scanning.
3736 		 *
3737 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3738 		 * we essentially disable hw_scan for all drivers not setting
3739 		 * the flag.
3740 		 */
3741 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3742 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
3743 	}
3744 
3745 #ifdef __notyet__
3746 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
3747 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
3748 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
3749 		    | IEEE80211_HTCAP_MAXAMSDU_3839
3750 						/* max A-MSDU length */
3751 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
3752 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
3753 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
3754 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
3755 #endif
3756 
3757 	/*
3758 	 * The wiphy variables report bitmasks of avail antennas.
3759 	 * (*get_antenna) get the current bitmask sets which can be
3760 	 * altered by (*set_antenna) for some drivers.
3761 	 * XXX-BZ will the count alone do us much good long-term in net80211?
3762 	 */
3763 	if (hw->wiphy->available_antennas_rx ||
3764 	    hw->wiphy->available_antennas_tx) {
3765 		uint32_t rxs, txs;
3766 
3767 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
3768 			ic->ic_rxstream = bitcount32(rxs);
3769 			ic->ic_txstream = bitcount32(txs);
3770 		}
3771 	}
3772 
3773 	ic->ic_cryptocaps = 0;
3774 #ifdef LKPI_80211_HW_CRYPTO
3775 	if (hw->wiphy->n_cipher_suites > 0) {
3776 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
3777 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
3778 			    hw->wiphy->cipher_suites[i]);
3779 	}
3780 #endif
3781 
3782 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3783 	    ic->ic_channels);
3784 
3785 	ieee80211_ifattach(ic);
3786 
3787 	ic->ic_update_mcast = lkpi_ic_update_mcast;
3788 	ic->ic_update_promisc = lkpi_ic_update_promisc;
3789 	ic->ic_update_chw = lkpi_ic_update_chw;
3790 	ic->ic_parent = lkpi_ic_parent;
3791 	ic->ic_scan_start = lkpi_ic_scan_start;
3792 	ic->ic_scan_end = lkpi_ic_scan_end;
3793 	ic->ic_set_channel = lkpi_ic_set_channel;
3794 	ic->ic_transmit = lkpi_ic_transmit;
3795 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
3796 	ic->ic_vap_create = lkpi_ic_vap_create;
3797 	ic->ic_vap_delete = lkpi_ic_vap_delete;
3798 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
3799 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
3800 
3801 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
3802 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
3803 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
3804 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
3805 
3806 	lhw->ic_node_alloc = ic->ic_node_alloc;
3807 	ic->ic_node_alloc = lkpi_ic_node_alloc;
3808 	lhw->ic_node_init = ic->ic_node_init;
3809 	ic->ic_node_init = lkpi_ic_node_init;
3810 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
3811 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
3812 	lhw->ic_node_free = ic->ic_node_free;
3813 	ic->ic_node_free = lkpi_ic_node_free;
3814 
3815 	lkpi_radiotap_attach(lhw);
3816 
3817 	/*
3818 	 * Assign the first possible channel for now;  seems Realtek drivers
3819 	 * expect one.
3820 	 * Also remember the amount of bands we support and the most rates
3821 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
3822 	 */
3823 	lhw->supbands = lhw->max_rates = 0;
3824 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3825 		struct ieee80211_supported_band *supband;
3826 		struct linuxkpi_ieee80211_channel *channels;
3827 
3828 		supband = hw->wiphy->bands[band];
3829 		if (supband == NULL || supband->n_channels == 0)
3830 			continue;
3831 
3832 		lhw->supbands++;
3833 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
3834 
3835 		/* If we have a channel, we need to keep counting supbands. */
3836 		if (hw->conf.chandef.chan != NULL)
3837 			continue;
3838 
3839 		channels = supband->channels;
3840 		for (i = 0; i < supband->n_channels; i++) {
3841 
3842 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3843 				continue;
3844 
3845 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3846 			    NL80211_CHAN_NO_HT);
3847 			break;
3848 		}
3849 	}
3850 
3851 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
3852 
3853 	/* Make sure we do not support more than net80211 is willing to take. */
3854 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
3855 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
3856 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
3857 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
3858 	}
3859 
3860 	/*
3861 	 * The maximum supported bitrates on any band + size for
3862 	 * DSSS Parameter Set give our per-band IE size.
3863 	 * XXX-BZ FIXME add HT VHT ... later
3864 	 * SSID is the responsibility of the driver and goes on the side.
3865 	 * The user specified bits coming from the vap go into the
3866 	 * "common ies" fields.
3867 	 */
3868 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
3869 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
3870 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
3871 	/*
3872 	 * net80211 does not seem to support the DSSS Parameter Set but some of
3873 	 * the drivers insert it so calculate the extra fixed space in.
3874 	 */
3875 	lhw->scan_ie_len += 2 + 1;
3876 
3877 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
3878 	if (hw->wiphy->max_scan_ie_len > 0)
3879 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
3880 
3881 	if (bootverbose)
3882 		ieee80211_announce(ic);
3883 
3884 	return (0);
3885 }
3886 
3887 void
3888 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
3889 {
3890 	struct lkpi_hw *lhw;
3891 	struct ieee80211com *ic;
3892 
3893 	lhw = HW_TO_LHW(hw);
3894 	ic = lhw->ic;
3895 	ieee80211_ifdetach(ic);
3896 }
3897 
3898 void
3899 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
3900     enum ieee80211_iface_iter flags,
3901     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
3902     void *arg)
3903 {
3904 	struct lkpi_hw *lhw;
3905 	struct lkpi_vif *lvif;
3906 	struct ieee80211_vif *vif;
3907 	bool active, atomic, nin_drv;
3908 
3909 	lhw = HW_TO_LHW(hw);
3910 
3911 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
3912 	    IEEE80211_IFACE_ITER_RESUME_ALL|
3913 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
3914 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
3915 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
3916 		    __func__, flags);
3917 	}
3918 
3919 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
3920 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
3921 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
3922 
3923 	if (atomic)
3924 		LKPI_80211_LHW_LVIF_LOCK(lhw);
3925 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3926 		struct ieee80211vap *vap;
3927 
3928 		vif = LVIF_TO_VIF(lvif);
3929 
3930 		/*
3931 		 * If we want "active" interfaces, we need to distinguish on
3932 		 * whether the driver knows about them or not to be able to
3933 		 * handle the "resume" case correctly.  Skip the ones the
3934 		 * driver does not know about.
3935 		 */
3936 		if (active && !lvif->added_to_drv &&
3937 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
3938 			continue;
3939 
3940 		/*
3941 		 * If we shall skip interfaces not added to the driver do so
3942 		 * if we haven't yet.
3943 		 */
3944 		if (nin_drv && !lvif->added_to_drv)
3945 			continue;
3946 
3947 		/*
3948 		 * Run the iterator function if we are either not asking
3949 		 * asking for active only or if the VAP is "running".
3950 		 */
3951 		/* XXX-BZ probably should have state in the lvif as well. */
3952 		vap = LVIF_TO_VAP(lvif);
3953 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
3954 			iterfunc(arg, vif->addr, vif);
3955 	}
3956 	if (atomic)
3957 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3958 }
3959 
3960 void
3961 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
3962     struct ieee80211_vif *vif,
3963     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
3964         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
3965     void *arg)
3966 {
3967 
3968 	UNIMPLEMENTED;
3969 }
3970 
3971 void
3972 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
3973     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
3974 	void *),
3975     void *arg)
3976 {
3977 	struct lkpi_hw *lhw;
3978 	struct lkpi_vif *lvif;
3979 	struct ieee80211_vif *vif;
3980 	struct lkpi_chanctx *lchanctx;
3981 
3982 	KASSERT(hw != NULL && iterfunc != NULL,
3983 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
3984 
3985 	lhw = HW_TO_LHW(hw);
3986 
3987 	IMPROVE("lchanctx should be its own list somewhere");
3988 
3989 	LKPI_80211_LHW_LVIF_LOCK(lhw);
3990 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3991 
3992 		vif = LVIF_TO_VIF(lvif);
3993 		if (vif->chanctx_conf == NULL)
3994 			continue;
3995 
3996 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
3997 		if (!lchanctx->added_to_drv)
3998 			continue;
3999 
4000 		iterfunc(hw, &lchanctx->conf, arg);
4001 	}
4002 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4003 }
4004 
4005 void
4006 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
4007    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
4008 {
4009 	struct lkpi_hw *lhw;
4010 	struct lkpi_vif *lvif;
4011 	struct lkpi_sta *lsta;
4012 	struct ieee80211_sta *sta;
4013 
4014 	KASSERT(hw != NULL && iterfunc != NULL,
4015 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4016 
4017 	lhw = HW_TO_LHW(hw);
4018 
4019 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4020 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4021 
4022 		LKPI_80211_LVIF_LOCK(lvif);
4023 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4024 			if (!lsta->added_to_drv)
4025 				continue;
4026 			sta = LSTA_TO_STA(lsta);
4027 			iterfunc(arg, sta);
4028 		}
4029 		LKPI_80211_LVIF_UNLOCK(lvif);
4030 	}
4031 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4032 }
4033 
4034 struct linuxkpi_ieee80211_regdomain *
4035 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4036 {
4037 	struct linuxkpi_ieee80211_regdomain *regd;
4038 
4039 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4040 	    GFP_KERNEL);
4041 	return (regd);
4042 }
4043 
4044 int
4045 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
4046     struct linuxkpi_ieee80211_regdomain *regd)
4047 {
4048 	struct lkpi_hw *lhw;
4049 	struct ieee80211com *ic;
4050 	struct ieee80211_regdomain *rd;
4051 
4052 	lhw = wiphy_priv(wiphy);
4053 	ic = lhw->ic;
4054 
4055 	rd = &ic->ic_regdomain;
4056 	if (rd->isocc[0] == '\0') {
4057 		rd->isocc[0] = regd->alpha2[0];
4058 		rd->isocc[1] = regd->alpha2[1];
4059 	}
4060 
4061 	TODO();
4062 	/* XXX-BZ finish the rest. */
4063 
4064 	return (0);
4065 }
4066 
4067 void
4068 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
4069     struct cfg80211_scan_info *info)
4070 {
4071 	struct lkpi_hw *lhw;
4072 	struct ieee80211com *ic;
4073 	struct ieee80211_scan_state *ss;
4074 
4075 	lhw = wiphy_priv(hw->wiphy);
4076 	ic = lhw->ic;
4077 	ss = ic->ic_scan;
4078 
4079 	ieee80211_scan_done(ss->ss_vap);
4080 
4081 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4082 	free(lhw->hw_req, M_LKPI80211);
4083 	lhw->hw_req = NULL;
4084 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4085 	wakeup(lhw);
4086 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4087 
4088 	return;
4089 }
4090 
4091 /* For %list see comment towards the end of the function. */
4092 void
4093 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4094     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4095     struct list_head *list __unused)
4096 {
4097 	struct epoch_tracker et;
4098 	struct lkpi_hw *lhw;
4099 	struct ieee80211com *ic;
4100 	struct mbuf *m;
4101 	struct skb_shared_info *shinfo;
4102 	struct ieee80211_rx_status *rx_status;
4103 	struct ieee80211_rx_stats rx_stats;
4104 	struct ieee80211_node *ni;
4105 	struct ieee80211vap *vap;
4106 	struct ieee80211_hdr *hdr;
4107 	struct lkpi_sta *lsta;
4108 	int i, offset, ok;
4109 	int8_t rssi;
4110 	bool is_beacon;
4111 
4112 	if (skb->len < 2) {
4113 		/* Need 80211 stats here. */
4114 		IMPROVE();
4115 		goto err;
4116 	}
4117 
4118 	/*
4119 	 * For now do the data copy; we can later improve things. Might even
4120 	 * have an mbuf backing the skb data then?
4121 	 */
4122 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
4123 	if (m == NULL)
4124 		goto err;
4125 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
4126 
4127 	shinfo = skb_shinfo(skb);
4128 	offset = m->m_len;
4129 	for (i = 0; i < shinfo->nr_frags; i++) {
4130 		m_copyback(m, offset, shinfo->frags[i].size,
4131 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
4132 		    shinfo->frags[i].offset);
4133 		offset += shinfo->frags[i].size;
4134 	}
4135 
4136 	rx_status = IEEE80211_SKB_RXCB(skb);
4137 
4138 	hdr = (void *)skb->data;
4139 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4140 
4141 #ifdef LINUXKPI_DEBUG_80211
4142 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
4143 		goto no_trace_beacons;
4144 
4145 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4146 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4147 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
4148 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
4149 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
4150 		    shinfo, shinfo->nr_frags,
4151 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
4152 
4153 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
4154 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
4155 
4156 	/* Implement a dump_rxcb() !!! */
4157 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4158 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
4159 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
4160 			__func__,
4161 			(uintmax_t)rx_status->boottime_ns,
4162 			(uintmax_t)rx_status->mactime,
4163 			rx_status->device_timestamp,
4164 			rx_status->flag,
4165 			rx_status->freq,
4166 			rx_status->bw,
4167 			rx_status->encoding,
4168 			rx_status->ampdu_reference,
4169 			rx_status->band,
4170 			rx_status->chains,
4171 			rx_status->chain_signal[0],
4172 			rx_status->chain_signal[1],
4173 			rx_status->chain_signal[2],
4174 			rx_status->chain_signal[3],
4175 			rx_status->signal,
4176 			rx_status->enc_flags,
4177 			rx_status->he_dcm,
4178 			rx_status->he_gi,
4179 			rx_status->he_ru,
4180 			rx_status->zero_length_psdu_type,
4181 			rx_status->nss,
4182 			rx_status->rate_idx);
4183 no_trace_beacons:
4184 #endif
4185 
4186 	memset(&rx_stats, 0, sizeof(rx_stats));
4187 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
4188 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
4189 	rx_stats.c_nf = -96;
4190 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
4191 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4192 		rssi = rx_status->signal;
4193 	else
4194 		rssi = rx_stats.c_nf;
4195 	/*
4196 	 * net80211 signal strength data are in .5 dBm units relative to
4197 	 * the current noise floor (see comment in ieee80211_node.h).
4198 	 */
4199 	rssi -= rx_stats.c_nf;
4200 	rx_stats.c_rssi = rssi * 2;
4201 	rx_stats.r_flags |= IEEE80211_R_BAND;
4202 	rx_stats.c_band =
4203 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
4204 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
4205 	rx_stats.c_freq = rx_status->freq;
4206 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
4207 
4208 	/* XXX (*sta_statistics)() to get to some of that? */
4209 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
4210 
4211 	lhw = HW_TO_LHW(hw);
4212 	ic = lhw->ic;
4213 
4214 	ok = ieee80211_add_rx_params(m, &rx_stats);
4215 	if (ok == 0) {
4216 		m_freem(m);
4217 		counter_u64_add(ic->ic_ierrors, 1);
4218 		goto err;
4219 	}
4220 
4221 	if (sta != NULL) {
4222 		lsta = STA_TO_LSTA(sta);
4223 		ni = ieee80211_ref_node(lsta->ni);
4224 	} else {
4225 		struct ieee80211_frame_min *wh;
4226 
4227 		wh = mtod(m, struct ieee80211_frame_min *);
4228 		ni = ieee80211_find_rxnode(ic, wh);
4229 		if (ni != NULL)
4230 			lsta = ni->ni_drv_data;
4231 	}
4232 
4233 	if (ni != NULL)
4234 		vap = ni->ni_vap;
4235 	else
4236 		/*
4237 		 * XXX-BZ can we improve this by looking at the frame hdr
4238 		 * or other meta-data passed up?
4239 		 */
4240 		vap = TAILQ_FIRST(&ic->ic_vaps);
4241 
4242 #ifdef LINUXKPI_DEBUG_80211
4243 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4244 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
4245 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
4246 		    ni, vap, is_beacon ? " beacon" : "");
4247 #endif
4248 
4249 	if (ni != NULL && vap != NULL && is_beacon &&
4250 	    rx_status->device_timestamp > 0 &&
4251 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4252 		struct lkpi_vif *lvif;
4253 		struct ieee80211_vif *vif;
4254 		struct ieee80211_frame *wh;
4255 
4256 		wh = mtod(m, struct ieee80211_frame *);
4257 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4258 			goto skip_device_ts;
4259 
4260 		lvif = VAP_TO_LVIF(vap);
4261 		vif = LVIF_TO_VIF(lvif);
4262 
4263 		IMPROVE("TIMING_BEACON_ONLY?");
4264 		/* mac80211 specific (not net80211) so keep it here. */
4265 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4266 		/*
4267 		 * net80211 should take care of the other information (sync_tsf,
4268 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4269 		 */
4270 	}
4271 skip_device_ts:
4272 
4273 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
4274 	    ieee80211_radiotap_active_vap(vap)) {
4275 		struct lkpi_radiotap_rx_hdr *rtap;
4276 
4277 		rtap = &lhw->rtap_rx;
4278 		rtap->wr_tsft = rx_status->device_timestamp;
4279 		rtap->wr_flags = 0;
4280 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
4281 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4282 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4283 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4284 #if 0	/* .. or it does not given we strip it below. */
4285 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4286 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
4287 #endif
4288 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
4289 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
4290 		rtap->wr_rate = 0;
4291 		IMPROVE();
4292 		/* XXX TODO status->encoding / rate_index / bw */
4293 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
4294 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
4295 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4296 		rtap->wr_dbm_antsignal = rssi;
4297 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
4298 	}
4299 
4300 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4301 		m_adj(m, -IEEE80211_CRC_LEN);
4302 
4303 #if 0
4304 	if (list != NULL) {
4305 		/*
4306 		* Normally this would be queued up and delivered by
4307 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
4308 		* See mt76::mac80211.c as only current possible consumer.
4309 		*/
4310 		IMPROVE("we simply pass the packet to net80211 to deal with.");
4311 	}
4312 #endif
4313 
4314 	NET_EPOCH_ENTER(et);
4315 	if (ni != NULL) {
4316 		ok = ieee80211_input_mimo(ni, m);
4317 		ieee80211_free_node(ni);
4318 		if (ok < 0)
4319 			m_freem(m);
4320 	} else {
4321 		ok = ieee80211_input_mimo_all(ic, m);
4322 		/* mbuf got consumed. */
4323 	}
4324 	NET_EPOCH_EXIT(et);
4325 
4326 #ifdef LINUXKPI_DEBUG_80211
4327 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4328 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4329 #endif
4330 
4331 	IMPROVE();
4332 
4333 err:
4334 	/* The skb is ours so we can free it :-) */
4335 	kfree_skb(skb);
4336 }
4337 
4338 uint8_t
4339 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
4340 {
4341 	const struct ieee80211_frame *wh;
4342 	uint8_t tid;
4343 
4344 	/* Linux seems to assume this is a QOS-Data-Frame */
4345 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
4346 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
4347 	   hdr->frame_control));
4348 
4349 	wh = (const struct ieee80211_frame *)hdr;
4350 	tid = ieee80211_gettid(wh);
4351 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
4352 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
4353 
4354 	return (tid);
4355 }
4356 
4357 struct wiphy *
4358 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
4359 {
4360 	struct lkpi_wiphy *lwiphy;
4361 
4362 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
4363 	if (lwiphy == NULL)
4364 		return (NULL);
4365 	lwiphy->ops = ops;
4366 
4367 	/* XXX TODO */
4368 	return (LWIPHY_TO_WIPHY(lwiphy));
4369 }
4370 
4371 void
4372 linuxkpi_wiphy_free(struct wiphy *wiphy)
4373 {
4374 	struct lkpi_wiphy *lwiphy;
4375 
4376 	if (wiphy == NULL)
4377 		return;
4378 
4379 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
4380 	kfree(lwiphy);
4381 }
4382 
4383 uint32_t
4384 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
4385     enum nl80211_band band)
4386 {
4387 
4388 	switch (band) {
4389 	case NL80211_BAND_2GHZ:
4390 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
4391 		break;
4392 	case NL80211_BAND_5GHZ:
4393 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
4394 		break;
4395 	default:
4396 		/* XXX abort, retry, error, panic? */
4397 		break;
4398 	}
4399 
4400 	return (0);
4401 }
4402 
4403 uint32_t
4404 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
4405 {
4406 
4407 	return (ieee80211_mhz2ieee(freq, 0));
4408 }
4409 
4410 static struct lkpi_sta *
4411 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
4412 {
4413 	struct lkpi_sta *lsta, *temp;
4414 
4415 	LKPI_80211_LVIF_LOCK(lvif);
4416 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4417 		if (lsta->ni == ni) {
4418 			LKPI_80211_LVIF_UNLOCK(lvif);
4419 			return (lsta);
4420 		}
4421 	}
4422 	LKPI_80211_LVIF_UNLOCK(lvif);
4423 
4424 	return (NULL);
4425 }
4426 
4427 struct ieee80211_sta *
4428 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
4429 {
4430 	struct lkpi_vif *lvif;
4431 	struct lkpi_sta *lsta, *temp;
4432 	struct ieee80211_sta *sta;
4433 
4434 	lvif = VIF_TO_LVIF(vif);
4435 
4436 	LKPI_80211_LVIF_LOCK(lvif);
4437 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4438 		sta = LSTA_TO_STA(lsta);
4439 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
4440 			LKPI_80211_LVIF_UNLOCK(lvif);
4441 			return (sta);
4442 		}
4443 	}
4444 	LKPI_80211_LVIF_UNLOCK(lvif);
4445 	return (NULL);
4446 }
4447 
4448 struct ieee80211_sta *
4449 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
4450     const uint8_t *addr, const uint8_t *ourvifaddr)
4451 {
4452 	struct lkpi_hw *lhw;
4453 	struct lkpi_vif *lvif;
4454 	struct lkpi_sta *lsta;
4455 	struct ieee80211_vif *vif;
4456 	struct ieee80211_sta *sta;
4457 
4458 	lhw = wiphy_priv(hw->wiphy);
4459 	sta = NULL;
4460 
4461 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4462 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4463 
4464 		/* XXX-BZ check our address from the vif. */
4465 
4466 		vif = LVIF_TO_VIF(lvif);
4467 		if (ourvifaddr != NULL &&
4468 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
4469 			continue;
4470 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
4471 		if (sta != NULL)
4472 			break;
4473 	}
4474 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4475 
4476 	if (sta != NULL) {
4477 		lsta = STA_TO_LSTA(sta);
4478 		if (!lsta->added_to_drv)
4479 			return (NULL);
4480 	}
4481 
4482 	return (sta);
4483 }
4484 
4485 struct sk_buff *
4486 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
4487     struct ieee80211_txq *txq)
4488 {
4489 	struct lkpi_txq *ltxq;
4490 	struct lkpi_vif *lvif;
4491 	struct sk_buff *skb;
4492 
4493 	skb = NULL;
4494 	ltxq = TXQ_TO_LTXQ(txq);
4495 	ltxq->seen_dequeue = true;
4496 
4497 	if (ltxq->stopped)
4498 		goto stopped;
4499 
4500 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
4501 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
4502 		ltxq->stopped = true;
4503 		goto stopped;
4504 	}
4505 
4506 	skb = skb_dequeue(&ltxq->skbq);
4507 
4508 stopped:
4509 	return (skb);
4510 }
4511 
4512 void
4513 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4514     unsigned long *frame_cnt, unsigned long *byte_cnt)
4515 {
4516 	struct lkpi_txq *ltxq;
4517 	struct sk_buff *skb;
4518 	unsigned long fc, bc;
4519 
4520 	ltxq = TXQ_TO_LTXQ(txq);
4521 
4522 	fc = bc = 0;
4523 	skb_queue_walk(&ltxq->skbq, skb) {
4524 		fc++;
4525 		bc += skb->len;
4526 	}
4527 	if (frame_cnt)
4528 		*frame_cnt = fc;
4529 	if (byte_cnt)
4530 		*byte_cnt = bc;
4531 
4532 	/* Validate that this is doing the correct thing. */
4533 	/* Should we keep track on en/dequeue? */
4534 	IMPROVE();
4535 }
4536 
4537 /*
4538  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
4539  * The latter tries to derive the success status from the info flags
4540  * passed back from the driver.  rawx_mit() saves the ni on the m and the
4541  * m on the skb for us to be able to give feedback to net80211.
4542  */
4543 static void
4544 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4545     int status)
4546 {
4547 	struct ieee80211_node *ni;
4548 	struct mbuf *m;
4549 
4550 	m = skb->m;
4551 	skb->m = NULL;
4552 
4553 	if (m != NULL) {
4554 		ni = m->m_pkthdr.PH_loc.ptr;
4555 		/* Status: 0 is ok, != 0 is error. */
4556 		ieee80211_tx_complete(ni, m, status);
4557 		/* ni & mbuf were consumed. */
4558 	}
4559 }
4560 
4561 void
4562 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4563     int status)
4564 {
4565 
4566 	_lkpi_ieee80211_free_txskb(hw, skb, status);
4567 	kfree_skb(skb);
4568 }
4569 
4570 void
4571 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
4572     struct ieee80211_tx_status *txstat)
4573 {
4574 	struct sk_buff *skb;
4575 	struct ieee80211_tx_info *info;
4576 	struct ieee80211_ratectl_tx_status txs;
4577 	struct ieee80211_node *ni;
4578 	int status;
4579 
4580 	skb = txstat->skb;
4581 	if (skb->m != NULL) {
4582 		struct mbuf *m;
4583 
4584 		m = skb->m;
4585 		ni = m->m_pkthdr.PH_loc.ptr;
4586 		memset(&txs, 0, sizeof(txs));
4587 	} else {
4588 		ni = NULL;
4589 	}
4590 
4591 	info = txstat->info;
4592 	if (info->flags & IEEE80211_TX_STAT_ACK) {
4593 		status = 0;	/* No error. */
4594 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
4595 	} else {
4596 		status = 1;
4597 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
4598 	}
4599 
4600 	if (ni != NULL) {
4601 		int ridx __unused;
4602 #ifdef LINUXKPI_DEBUG_80211
4603 		int old_rate;
4604 
4605 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
4606 #endif
4607 		txs.pktlen = skb->len;
4608 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
4609 		if (info->status.rates[0].count > 1) {
4610 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
4611 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
4612 		}
4613 #if 0		/* Unused in net80211 currently. */
4614 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
4615 		txs.final_rate = info->status.rates[0].idx;
4616 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
4617 #endif
4618 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
4619 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
4620 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
4621 		}
4622 
4623 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
4624 		ieee80211_ratectl_tx_complete(ni, &txs);
4625 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
4626 
4627 #ifdef LINUXKPI_DEBUG_80211
4628 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
4629 			printf("TX-RATE: %s: old %d new %d ridx %d, "
4630 			    "long_retries %d\n", __func__,
4631 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
4632 			    ridx, txs.long_retries);
4633 		}
4634 #endif
4635 	}
4636 
4637 #ifdef LINUXKPI_DEBUG_80211
4638 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4639 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
4640 		    "band %u hw_queue %u tx_time_est %d : "
4641 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
4642 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
4643 		    "tx_time %u flags %#x "
4644 		    "status_driver_data [ %p %p ]\n",
4645 		    __func__, hw, skb, status, info->flags,
4646 		    info->band, info->hw_queue, info->tx_time_est,
4647 		    info->status.rates[0].idx, info->status.rates[0].count,
4648 		    info->status.rates[0].flags,
4649 		    info->status.rates[1].idx, info->status.rates[1].count,
4650 		    info->status.rates[1].flags,
4651 		    info->status.rates[2].idx, info->status.rates[2].count,
4652 		    info->status.rates[2].flags,
4653 		    info->status.rates[3].idx, info->status.rates[3].count,
4654 		    info->status.rates[3].flags,
4655 		    info->status.ack_signal, info->status.ampdu_ack_len,
4656 		    info->status.ampdu_len, info->status.antenna,
4657 		    info->status.tx_time, info->status.flags,
4658 		    info->status.status_driver_data[0],
4659 		    info->status.status_driver_data[1]);
4660 #endif
4661 
4662 	if (txstat->free_list) {
4663 		_lkpi_ieee80211_free_txskb(hw, skb, status);
4664 		list_add_tail(&skb->list, txstat->free_list);
4665 	} else {
4666 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
4667 	}
4668 }
4669 
4670 void
4671 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
4672 {
4673 	struct ieee80211_tx_status status;
4674 
4675 	memset(&status, 0, sizeof(status));
4676 	status.info = IEEE80211_SKB_CB(skb);
4677 	status.skb = skb;
4678 	/* sta, n_rates, rates, free_list? */
4679 
4680 	ieee80211_tx_status_ext(hw, &status);
4681 }
4682 
4683 /*
4684  * This is an internal bandaid for the moment for the way we glue
4685  * skbs and mbufs together for TX.  Once we have skbs backed by
4686  * mbufs this should go away.
4687  * This is a public function but kept on the private KPI (lkpi_)
4688  * and is not exposed by a header file.
4689  */
4690 static void
4691 lkpi_ieee80211_free_skb_mbuf(void *p)
4692 {
4693 	struct ieee80211_node *ni;
4694 	struct mbuf *m;
4695 
4696 	if (p == NULL)
4697 		return;
4698 
4699 	m = (struct mbuf *)p;
4700 	M_ASSERTPKTHDR(m);
4701 
4702 	ni = m->m_pkthdr.PH_loc.ptr;
4703 	m->m_pkthdr.PH_loc.ptr = NULL;
4704 	if (ni != NULL)
4705 		ieee80211_free_node(ni);
4706 	m_freem(m);
4707 }
4708 
4709 void
4710 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
4711     struct delayed_work *w, int delay)
4712 {
4713 	struct lkpi_hw *lhw;
4714 
4715 	/* Need to make sure hw is in a stable (non-suspended) state. */
4716 	IMPROVE();
4717 
4718 	lhw = HW_TO_LHW(hw);
4719 	queue_delayed_work(lhw->workq, w, delay);
4720 }
4721 
4722 void
4723 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
4724     struct work_struct *w)
4725 {
4726 	struct lkpi_hw *lhw;
4727 
4728 	/* Need to make sure hw is in a stable (non-suspended) state. */
4729 	IMPROVE();
4730 
4731 	lhw = HW_TO_LHW(hw);
4732 	queue_work(lhw->workq, w);
4733 }
4734 
4735 struct sk_buff *
4736 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
4737     uint8_t *ssid, size_t ssid_len, size_t tailroom)
4738 {
4739 	struct sk_buff *skb;
4740 	struct ieee80211_frame *wh;
4741 	uint8_t *p;
4742 	size_t len;
4743 
4744 	len = sizeof(*wh);
4745 	len += 2 + ssid_len;
4746 
4747 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
4748 	if (skb == NULL)
4749 		return (NULL);
4750 
4751 	skb_reserve(skb, hw->extra_tx_headroom);
4752 
4753 	wh = skb_put_zero(skb, sizeof(*wh));
4754 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
4755 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
4756 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4757 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
4758 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4759 
4760 	p = skb_put(skb, 2 + ssid_len);
4761 	*p++ = IEEE80211_ELEMID_SSID;
4762 	*p++ = ssid_len;
4763 	if (ssid_len > 0)
4764 		memcpy(p, ssid, ssid_len);
4765 
4766 	return (skb);
4767 }
4768 
4769 struct sk_buff *
4770 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
4771     struct ieee80211_vif *vif)
4772 {
4773 	struct lkpi_vif *lvif;
4774 	struct ieee80211vap *vap;
4775 	struct sk_buff *skb;
4776 	struct ieee80211_frame_pspoll *psp;
4777 	uint16_t v;
4778 
4779 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
4780 	if (skb == NULL)
4781 		return (NULL);
4782 
4783 	skb_reserve(skb, hw->extra_tx_headroom);
4784 
4785 	lvif = VIF_TO_LVIF(vif);
4786 	vap = LVIF_TO_VAP(lvif);
4787 
4788 	psp = skb_put_zero(skb, sizeof(*psp));
4789 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
4790 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
4791 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
4792 	memcpy(&psp->i_aid, &v, sizeof(v));
4793 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
4794 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
4795 
4796 	return (skb);
4797 }
4798 
4799 struct sk_buff *
4800 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4801     struct ieee80211_vif *vif, int linkid, bool qos)
4802 {
4803 	struct lkpi_vif *lvif;
4804 	struct ieee80211vap *vap;
4805 	struct sk_buff *skb;
4806 	struct ieee80211_frame *nullf;
4807 
4808 	IMPROVE("linkid");
4809 
4810 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
4811 	if (skb == NULL)
4812 		return (NULL);
4813 
4814 	skb_reserve(skb, hw->extra_tx_headroom);
4815 
4816 	lvif = VIF_TO_LVIF(vif);
4817 	vap = LVIF_TO_VAP(lvif);
4818 
4819 	nullf = skb_put_zero(skb, sizeof(*nullf));
4820 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
4821 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
4822 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
4823 
4824 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
4825 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
4826 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
4827 
4828 	return (skb);
4829 }
4830 
4831 struct wireless_dev *
4832 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
4833 {
4834 	struct lkpi_vif *lvif;
4835 
4836 	lvif = VIF_TO_LVIF(vif);
4837 	return (&lvif->wdev);
4838 }
4839 
4840 void
4841 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
4842 {
4843 	struct lkpi_vif *lvif;
4844 	struct ieee80211vap *vap;
4845 	enum ieee80211_state nstate;
4846 	int arg;
4847 
4848 	lvif = VIF_TO_LVIF(vif);
4849 	vap = LVIF_TO_VAP(lvif);
4850 
4851 	/*
4852 	 * Go to init; otherwise we need to elaborately check state and
4853 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4854 	 * Let the statemachine handle all neccessary changes.
4855 	 */
4856 	nstate = IEEE80211_S_INIT;
4857 	arg = 0;	/* Not a valid reason. */
4858 
4859 #ifdef LINUXKPI_DEBUG_80211
4860 	if (linuxkpi_debug_80211 & D80211_TRACE)
4861 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
4862 #endif
4863 	ieee80211_new_state(vap, nstate, arg);
4864 }
4865 
4866 void
4867 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4868 {
4869 	struct lkpi_vif *lvif;
4870 	struct ieee80211vap *vap;
4871 
4872 	lvif = VIF_TO_LVIF(vif);
4873 	vap = LVIF_TO_VAP(lvif);
4874 
4875 #ifdef LINUXKPI_DEBUG_80211
4876 	if (linuxkpi_debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4877 		ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4878 		    vif, vap, ieee80211_state_name[vap->iv_state]);
4879 #endif
4880 	ieee80211_beacon_miss(vap->iv_ic);
4881 }
4882 
4883 /* -------------------------------------------------------------------------- */
4884 
4885 void
4886 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
4887 {
4888 	struct lkpi_hw *lhw;
4889 	struct lkpi_vif *lvif;
4890 	struct ieee80211_vif *vif;
4891 	int ac_count, ac;
4892 
4893 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
4894 	    __func__, qnum, hw->queues, hw));
4895 
4896 	lhw = wiphy_priv(hw->wiphy);
4897 
4898 	/* See lkpi_ic_vap_create(). */
4899 	if (hw->queues >= IEEE80211_NUM_ACS)
4900 		ac_count = IEEE80211_NUM_ACS;
4901 	else
4902 		ac_count = 1;
4903 
4904 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4905 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4906 
4907 		vif = LVIF_TO_VIF(lvif);
4908 		for (ac = 0; ac < ac_count; ac++) {
4909 			IMPROVE_TXQ("LOCKING");
4910 			if (qnum == vif->hw_queue[ac]) {
4911 				/*
4912 				 * For now log this to better understand
4913 				 * how this is supposed to work.
4914 				 */
4915 				if (lvif->hw_queue_stopped[ac])
4916 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
4917 					    "lvif %p vif %p ac %d qnum %d already "
4918 					    "stopped\n", __func__, __LINE__,
4919 					    lhw, hw, lvif, vif, ac, qnum);
4920 				lvif->hw_queue_stopped[ac] = true;
4921 			}
4922 		}
4923 	}
4924 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4925 }
4926 
4927 void
4928 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
4929 {
4930 	int i;
4931 
4932 	IMPROVE_TXQ("Locking; do we need further info?");
4933 	for (i = 0; i < hw->queues; i++)
4934 		linuxkpi_ieee80211_stop_queue(hw, i);
4935 }
4936 
4937 
4938 static void
4939 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
4940 {
4941 	struct lkpi_hw *lhw;
4942 	struct lkpi_vif *lvif;
4943 	struct lkpi_sta *lsta;
4944 	int ac_count, ac, tid;
4945 
4946 	/* See lkpi_ic_vap_create(). */
4947 	if (hw->queues >= IEEE80211_NUM_ACS)
4948 		ac_count = IEEE80211_NUM_ACS;
4949 	else
4950 		ac_count = 1;
4951 
4952 	lhw = wiphy_priv(hw->wiphy);
4953 
4954 	IMPROVE_TXQ("Locking");
4955 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4956 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4957 		struct ieee80211_vif *vif;
4958 
4959 		vif = LVIF_TO_VIF(lvif);
4960 		for (ac = 0; ac < ac_count; ac++) {
4961 
4962 			if (hwq == vif->hw_queue[ac]) {
4963 
4964 				/* XXX-BZ what about software scan? */
4965 
4966 				/*
4967 				 * For now log this to better understand
4968 				 * how this is supposed to work.
4969 				 */
4970 				if (!lvif->hw_queue_stopped[ac])
4971 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
4972 					    "lvif %p vif %p ac %d hw_q not stopped\n",
4973 					    __func__, __LINE__,
4974 					    lhw, hw, lvif, vif, ac);
4975 				lvif->hw_queue_stopped[ac] = false;
4976 
4977 				LKPI_80211_LVIF_LOCK(lvif);
4978 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4979 					struct ieee80211_sta *sta;
4980 
4981 					sta = LSTA_TO_STA(lsta);
4982 					for (tid = 0; tid < nitems(sta->txq); tid++) {
4983 						struct lkpi_txq *ltxq;
4984 
4985 						if (sta->txq[tid] == NULL)
4986 							continue;
4987 
4988 						if (sta->txq[tid]->ac != ac)
4989 							continue;
4990 
4991 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4992 						if (!ltxq->stopped)
4993 							continue;
4994 
4995 						ltxq->stopped = false;
4996 
4997 						/* XXX-BZ see when this explodes with all the locking. taskq? */
4998 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
4999 					}
5000 				}
5001 				LKPI_80211_LVIF_UNLOCK(lvif);
5002 			}
5003 		}
5004 	}
5005 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5006 }
5007 
5008 void
5009 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
5010 {
5011 	int i;
5012 
5013 	IMPROVE_TXQ("Is this all/enough here?");
5014 	for (i = 0; i < hw->queues; i++)
5015 		lkpi_ieee80211_wake_queues(hw, i);
5016 }
5017 
5018 void
5019 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
5020 {
5021 
5022 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5023 	    __func__, qnum, hw->queues, hw));
5024 
5025 	lkpi_ieee80211_wake_queues(hw, qnum);
5026 }
5027 
5028 /* This is just hardware queues. */
5029 void
5030 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
5031 {
5032 	struct lkpi_hw *lhw;
5033 
5034 	lhw = HW_TO_LHW(hw);
5035 
5036 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
5037 	IMPROVE_TXQ("LOCKING");
5038 	if (++lhw->txq_generation[ac] == 0)
5039 		lhw->txq_generation[ac]++;
5040 }
5041 
5042 struct ieee80211_txq *
5043 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
5044 {
5045 	struct lkpi_hw *lhw;
5046 	struct ieee80211_txq *txq;
5047 	struct lkpi_txq *ltxq;
5048 
5049 	lhw = HW_TO_LHW(hw);
5050 	txq = NULL;
5051 
5052 	IMPROVE_TXQ("LOCKING");
5053 
5054 	/* Check that we are scheduled. */
5055 	if (lhw->txq_generation[ac] == 0)
5056 		goto out;
5057 
5058 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
5059 	if (ltxq == NULL)
5060 		goto out;
5061 	if (ltxq->txq_generation == lhw->txq_generation[ac])
5062 		goto out;
5063 
5064 	ltxq->txq_generation = lhw->txq_generation[ac];
5065 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
5066 	txq = &ltxq->txq;
5067 	TAILQ_ELEM_INIT(ltxq, txq_entry);
5068 
5069 out:
5070 	return (txq);
5071 }
5072 
5073 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
5074     struct ieee80211_txq *txq, bool withoutpkts)
5075 {
5076 	struct lkpi_hw *lhw;
5077 	struct lkpi_txq *ltxq;
5078 
5079 	ltxq = TXQ_TO_LTXQ(txq);
5080 
5081 	IMPROVE_TXQ("LOCKING");
5082 
5083 	/* Only schedule if work to do or asked to anyway. */
5084 	if (!withoutpkts && skb_queue_empty(&ltxq->skbq))
5085 		goto out;
5086 
5087 	/* Make sure we do not double-schedule. */
5088 	if (ltxq->txq_entry.tqe_next != NULL)
5089 		goto out;
5090 
5091 	lhw = HW_TO_LHW(hw);
5092 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
5093 out:
5094 	return;
5095 }
5096 
5097 /* -------------------------------------------------------------------------- */
5098 
5099 struct lkpi_cfg80211_bss {
5100 	u_int refcnt;
5101 	struct cfg80211_bss bss;
5102 };
5103 
5104 struct lkpi_cfg80211_get_bss_iter_lookup {
5105 	struct wiphy *wiphy;
5106 	struct linuxkpi_ieee80211_channel *chan;
5107 	const uint8_t *bssid;
5108 	const uint8_t *ssid;
5109 	size_t ssid_len;
5110 	enum ieee80211_bss_type bss_type;
5111 	enum ieee80211_privacy privacy;
5112 
5113 	/*
5114 	 * Something to store a copy of the result as the net80211 scan cache
5115 	 * is not refoucnted so a scan entry might go away any time.
5116 	 */
5117 	bool match;
5118 	struct cfg80211_bss *bss;
5119 };
5120 
5121 static void
5122 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
5123 {
5124 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
5125 	size_t ielen;
5126 
5127 	lookup = arg;
5128 
5129 	/* Do not try to find another match. */
5130 	if (lookup->match)
5131 		return;
5132 
5133 	/* Nothing to store result. */
5134 	if (lookup->bss == NULL)
5135 		return;
5136 
5137 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
5138 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
5139 		/* We have no idea what to compare to as the drivers only request ANY */
5140 		return;
5141 	}
5142 
5143 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
5144 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
5145 		/* We have no idea what to compare to as the drivers only request ANY */
5146 		return;
5147 	}
5148 
5149 	if (lookup->chan != NULL) {
5150 		struct linuxkpi_ieee80211_channel *chan;
5151 
5152 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
5153 		    se->se_chan->ic_freq);
5154 		if (chan == NULL || chan != lookup->chan)
5155 			return;
5156 	}
5157 
5158 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
5159 		return;
5160 
5161 	if (lookup->ssid) {
5162 		if (lookup->ssid_len != se->se_ssid[1] ||
5163 		    se->se_ssid[1] == 0)
5164 			return;
5165 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
5166 			return;
5167 	}
5168 
5169 	ielen = se->se_ies.len;
5170 
5171 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
5172 	    M_LKPI80211, M_NOWAIT | M_ZERO);
5173 	if (lookup->bss->ies == NULL)
5174 		return;
5175 
5176 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
5177 	lookup->bss->ies->len = ielen;
5178 	if (ielen)
5179 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
5180 
5181 	lookup->match = true;
5182 }
5183 
5184 struct cfg80211_bss *
5185 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
5186     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
5187     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
5188 {
5189 	struct lkpi_cfg80211_bss *lbss;
5190 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
5191 	struct lkpi_hw *lhw;
5192 	struct ieee80211vap *vap;
5193 
5194 	lhw = wiphy_priv(wiphy);
5195 
5196 	/* Let's hope we can alloc. */
5197 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
5198 	if (lbss == NULL) {
5199 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
5200 		return (NULL);
5201 	}
5202 
5203 	lookup.wiphy = wiphy;
5204 	lookup.chan = chan;
5205 	lookup.bssid = bssid;
5206 	lookup.ssid = ssid;
5207 	lookup.ssid_len = ssid_len;
5208 	lookup.bss_type = bss_type;
5209 	lookup.privacy = privacy;
5210 	lookup.match = false;
5211 	lookup.bss = &lbss->bss;
5212 
5213 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
5214 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
5215 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
5216 	if (!lookup.match) {
5217 		free(lbss, M_LKPI80211);
5218 		return (NULL);
5219 	}
5220 
5221 	refcount_init(&lbss->refcnt, 1);
5222 	return (&lbss->bss);
5223 }
5224 
5225 void
5226 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
5227 {
5228 	struct lkpi_cfg80211_bss *lbss;
5229 
5230 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
5231 
5232 	/* Free everything again on refcount ... */
5233 	if (refcount_release(&lbss->refcnt)) {
5234 		free(lbss->bss.ies, M_LKPI80211);
5235 		free(lbss, M_LKPI80211);
5236 	}
5237 }
5238 
5239 void
5240 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
5241 {
5242 	struct lkpi_hw *lhw;
5243 	struct ieee80211com *ic;
5244 	struct ieee80211vap *vap;
5245 
5246 	lhw = wiphy_priv(wiphy);
5247 	ic = lhw->ic;
5248 
5249 	/*
5250 	 * If we haven't called ieee80211_ifattach() yet
5251 	 * or there is no VAP, there are no scans to flush.
5252 	 */
5253 	if (ic == NULL ||
5254 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
5255 		return;
5256 
5257 	/* Should only happen on the current one? Not seen it late enough. */
5258 	IEEE80211_LOCK(ic);
5259 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
5260 		ieee80211_scan_flush(vap);
5261 	IEEE80211_UNLOCK(ic);
5262 }
5263 
5264 /* -------------------------------------------------------------------------- */
5265 
5266 MODULE_VERSION(linuxkpi_wlan, 1);
5267 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
5268 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
5269