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