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