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