xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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.vif = vif;
2277 	vif->bss_conf.idle = true;
2278 	vif->bss_conf.ps = false;
2279 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2280 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2281 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
2282 	vif->bss_conf.qos = false;
2283 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
2284 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2285 	vif->bss_conf.assoc = false;
2286 	vif->bss_conf.aid = 0;
2287 	/*
2288 	 * We need to initialize it to something as the bss_info_changed call
2289 	 * will try to copy from it in iwlwifi and NULL is a panic.
2290 	 * We will set the proper one in scan_to_auth() before being assoc.
2291 	 * NB: the logic there with using an array as bssid_override and checking
2292 	 * for non-NULL later is flawed but in their workflow does not seem to
2293 	 * matter.
2294 	 */
2295 	vif->bss_conf.bssid = zerobssid;
2296 #endif
2297 #if 0
2298 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2299 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2300 	vif->bss_conf.beacon_int = ic->ic_bintval;
2301 	/* iwlwifi bug. */
2302 	if (vif->bss_conf.beacon_int < 16)
2303 		vif->bss_conf.beacon_int = 16;
2304 #endif
2305 
2306 	/* Setup queue defaults; driver may override in (*add_interface). */
2307 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2308 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2309 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2310 		else if (hw->queues >= IEEE80211_NUM_ACS)
2311 			vif->hw_queue[i] = i;
2312 		else
2313 			vif->hw_queue[i] = 0;
2314 
2315 		/* Initialize the queue to running. Stopped? */
2316 		lvif->hw_queue_stopped[i] = false;
2317 	}
2318 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2319 
2320 	IMPROVE();
2321 
2322 	error = lkpi_80211_mo_start(hw);
2323 	if (error != 0) {
2324 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
2325 		mtx_destroy(&lvif->mtx);
2326 		free(lvif, M_80211_VAP);
2327 		return (NULL);
2328 	}
2329 
2330 	error = lkpi_80211_mo_add_interface(hw, vif);
2331 	if (error != 0) {
2332 		IMPROVE();	/* XXX-BZ mo_stop()? */
2333 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
2334 		mtx_destroy(&lvif->mtx);
2335 		free(lvif, M_80211_VAP);
2336 		return (NULL);
2337 	}
2338 
2339 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2340 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2341 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2342 
2343 	/* Set bss_info. */
2344 	changed = 0;
2345 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2346 
2347 	/* conf_tx setup; default WME? */
2348 
2349 	/* Force MC init. */
2350 	lkpi_update_mcast_filter(ic, true);
2351 
2352 	IMPROVE();
2353 
2354 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2355 
2356 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2357 	lvif->iv_newstate = vap->iv_newstate;
2358 	vap->iv_newstate = lkpi_iv_newstate;
2359 	lvif->iv_update_bss = vap->iv_update_bss;
2360 	vap->iv_update_bss = lkpi_iv_update_bss;
2361 
2362 	/* Key management. */
2363 	if (lhw->ops->set_key != NULL) {
2364 #ifdef LKPI_80211_HW_CRYPTO
2365 		vap->iv_key_set = lkpi_iv_key_set;
2366 		vap->iv_key_delete = lkpi_iv_key_delete;
2367 #endif
2368 	}
2369 
2370 	ieee80211_ratectl_init(vap);
2371 
2372 	/* Complete setup. */
2373 	ieee80211_vap_attach(vap, ieee80211_media_change,
2374 	    ieee80211_media_status, mac);
2375 
2376 	if (hw->max_listen_interval == 0)
2377 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2378 	hw->conf.listen_interval = hw->max_listen_interval;
2379 	ic->ic_set_channel(ic);
2380 
2381 	/* XXX-BZ do we need to be able to update these? */
2382 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
2383 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2384 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
2385 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2386 	/* any others? */
2387 	IMPROVE();
2388 
2389 	return (vap);
2390 }
2391 
2392 void
2393 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
2394 {
2395 
2396 	wiphy_unregister(hw->wiphy);
2397 	linuxkpi_ieee80211_ifdetach(hw);
2398 
2399 	IMPROVE();
2400 }
2401 
2402 void
2403 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
2404 {
2405 
2406 	TODO();
2407 }
2408 
2409 static void
2410 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2411 {
2412 	struct ieee80211com *ic;
2413 	struct lkpi_hw *lhw;
2414 	struct ieee80211_hw *hw;
2415 	struct lkpi_vif *lvif;
2416 	struct ieee80211_vif *vif;
2417 
2418 	lvif = VAP_TO_LVIF(vap);
2419 	vif = LVIF_TO_VIF(lvif);
2420 	ic = vap->iv_ic;
2421 	lhw = ic->ic_softc;
2422 	hw = LHW_TO_HW(lhw);
2423 
2424 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2425 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
2426 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2427 	lkpi_80211_mo_remove_interface(hw, vif);
2428 
2429 	ieee80211_ratectl_deinit(vap);
2430 	ieee80211_vap_detach(vap);
2431 	mtx_destroy(&lvif->mtx);
2432 	free(lvif, M_80211_VAP);
2433 }
2434 
2435 static void
2436 lkpi_ic_update_mcast(struct ieee80211com *ic)
2437 {
2438 
2439 	lkpi_update_mcast_filter(ic, false);
2440 	TRACEOK();
2441 }
2442 
2443 static void
2444 lkpi_ic_update_promisc(struct ieee80211com *ic)
2445 {
2446 
2447 	UNIMPLEMENTED;
2448 }
2449 
2450 static void
2451 lkpi_ic_update_chw(struct ieee80211com *ic)
2452 {
2453 
2454 	UNIMPLEMENTED;
2455 }
2456 
2457 /* Start / stop device. */
2458 static void
2459 lkpi_ic_parent(struct ieee80211com *ic)
2460 {
2461 	struct lkpi_hw *lhw;
2462 	struct ieee80211_hw *hw;
2463 	int error;
2464 	bool start_all;
2465 
2466 	IMPROVE();
2467 
2468 	lhw = ic->ic_softc;
2469 	hw = LHW_TO_HW(lhw);
2470 	start_all = false;
2471 
2472 	/* IEEE80211_UNLOCK(ic); */
2473 	LKPI_80211_LHW_LOCK(lhw);
2474 	if (ic->ic_nrunning > 0) {
2475 		error = lkpi_80211_mo_start(hw);
2476 		if (error == 0)
2477 			start_all = true;
2478 	} else {
2479 		lkpi_80211_mo_stop(hw);
2480 	}
2481 	LKPI_80211_LHW_UNLOCK(lhw);
2482 	/* IEEE80211_LOCK(ic); */
2483 
2484 	if (start_all)
2485 		ieee80211_start_all(ic);
2486 }
2487 
2488 bool
2489 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
2490     size_t ie_ids_len)
2491 {
2492 	int i;
2493 
2494 	for (i = 0; i < ie_ids_len; i++) {
2495 		if (ie == *ie_ids)
2496 			return (true);
2497 	}
2498 
2499 	return (false);
2500 }
2501 
2502 /* Return true if skipped; false if error. */
2503 bool
2504 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
2505 {
2506 	size_t x;
2507 	uint8_t l;
2508 
2509 	x = *xp;
2510 
2511 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
2512 	    __func__, x, ies_len, ies));
2513 	l = ies[x + 1];
2514 	x += 2 + l;
2515 
2516 	if (x > ies_len)
2517 		return (false);
2518 
2519 	*xp = x;
2520 	return (true);
2521 }
2522 
2523 static uint8_t *
2524 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2525     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
2526 {
2527 	struct ieee80211_supported_band *supband;
2528 	struct linuxkpi_ieee80211_channel *channels;
2529 	struct ieee80211com *ic;
2530 	const struct ieee80211_channel *chan;
2531 	const struct ieee80211_rateset *rs;
2532 	uint8_t *pb;
2533 	int band, i;
2534 
2535 	ic = vap->iv_ic;
2536 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2537 		if ((band_mask & (1 << band)) == 0)
2538 			continue;
2539 
2540 		supband = hw->wiphy->bands[band];
2541 		/*
2542 		 * This should not happen;
2543 		 * band_mask is a bitmask of valid bands to scan on.
2544 		 */
2545 		if (supband == NULL || supband->n_channels == 0)
2546 			continue;
2547 
2548 		/* Find a first channel to get the mode and rates from. */
2549 		channels = supband->channels;
2550 		chan = NULL;
2551 		for (i = 0; i < supband->n_channels; i++) {
2552 
2553 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2554 				continue;
2555 
2556 			chan = ieee80211_find_channel(ic,
2557 			    channels[i].center_freq, 0);
2558 			if (chan != NULL)
2559 				break;
2560 		}
2561 
2562 		/* This really should not happen. */
2563 		if (chan == NULL)
2564 			continue;
2565 
2566 		pb = p;
2567 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
2568 		p = ieee80211_add_rates(p, rs);
2569 		p = ieee80211_add_xrates(p, rs);
2570 
2571 #if defined(LKPI_80211_HT)
2572 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
2573 			struct ieee80211_channel *c;
2574 
2575 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2576 			    vap->iv_flags_ht);
2577 			p = ieee80211_add_htcap_ch(p, vap, c);
2578 		}
2579 #endif
2580 #if defined(LKPI_80211_VHT)
2581 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
2582 			struct ieee80211_channel *c;
2583 
2584 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2585 			    vap->iv_flags_ht);
2586 			c = ieee80211_vht_adjust_channel(ic, c,
2587 			    vap->iv_vht_flags);
2588 			p = ieee80211_add_vhtcap_ch(p, vap, c);
2589 		}
2590 #endif
2591 
2592 		scan_ies->ies[band] = pb;
2593 		scan_ies->len[band] = p - pb;
2594 	}
2595 
2596 	/* Add common_ies */
2597 	pb = p;
2598 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2599 	    vap->iv_wpa_ie != NULL) {
2600 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2601 		p += 2 + vap->iv_wpa_ie[1];
2602 	}
2603 	if (vap->iv_appie_probereq != NULL) {
2604 		memcpy(p, vap->iv_appie_probereq->ie_data,
2605 		    vap->iv_appie_probereq->ie_len);
2606 		p += vap->iv_appie_probereq->ie_len;
2607 	}
2608 	scan_ies->common_ies = pb;
2609 	scan_ies->common_ie_len = p - pb;
2610 
2611 	return (p);
2612 }
2613 
2614 static void
2615 lkpi_ic_scan_start(struct ieee80211com *ic)
2616 {
2617 	struct lkpi_hw *lhw;
2618 	struct ieee80211_hw *hw;
2619 	struct lkpi_vif *lvif;
2620 	struct ieee80211_vif *vif;
2621 	struct ieee80211_scan_state *ss;
2622 	struct ieee80211vap *vap;
2623 	int error;
2624 	bool is_hw_scan;
2625 
2626 	lhw = ic->ic_softc;
2627 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2628 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2629 		/* A scan is still running. */
2630 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2631 		return;
2632 	}
2633 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2634 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2635 
2636 	ss = ic->ic_scan;
2637 	vap = ss->ss_vap;
2638 	if (vap->iv_state != IEEE80211_S_SCAN) {
2639 		IMPROVE("We need to be able to scan if not in S_SCAN");
2640 		return;
2641 	}
2642 
2643 	hw = LHW_TO_HW(lhw);
2644 	if (!is_hw_scan) {
2645 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
2646 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2647 sw_scan:
2648 		lvif = VAP_TO_LVIF(vap);
2649 		vif = LVIF_TO_VIF(lvif);
2650 
2651 		if (vap->iv_state == IEEE80211_S_SCAN)
2652 			lkpi_hw_conf_idle(hw, false);
2653 
2654 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
2655 		/* net80211::scan_start() handled PS for us. */
2656 		IMPROVE();
2657 		/* XXX Also means it is too late to flush queues?
2658 		 * need to check iv_sta_ps or overload? */
2659 		/* XXX want to adjust ss end time/ maxdwell? */
2660 
2661 	} else {
2662 		struct ieee80211_channel *c;
2663 		struct ieee80211_scan_request *hw_req;
2664 		struct linuxkpi_ieee80211_channel *lc, **cpp;
2665 		struct cfg80211_ssid *ssids;
2666 		struct cfg80211_scan_6ghz_params *s6gp;
2667 		size_t chan_len, nchan, ssids_len, s6ghzlen;
2668 		int band, i, ssid_count, common_ie_len;
2669 		uint32_t band_mask;
2670 		uint8_t *ie, *ieend;
2671 		bool running;
2672 
2673 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
2674 		ssids_len = ssid_count * sizeof(*ssids);
2675 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
2676 
2677 		band_mask = 0;
2678 		nchan = 0;
2679 		for (i = ss->ss_next; i < ss->ss_last; i++) {
2680 			nchan++;
2681 			band = lkpi_net80211_chan_to_nl80211_band(
2682 			    ss->ss_chans[ss->ss_next + i]);
2683 			band_mask |= (1 << band);
2684 		}
2685 
2686 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2687 			IMPROVE("individual band scans not yet supported, only scanning first band");
2688 			/* In theory net80211 should drive this. */
2689 			/* Probably we need to add local logic for now;
2690 			 * need to deal with scan_complete
2691 			 * and cancel_scan and keep local state.
2692 			 * Also cut the nchan down above.
2693 			 */
2694 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
2695 		}
2696 
2697 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
2698 
2699 		common_ie_len = 0;
2700 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2701 		    vap->iv_wpa_ie != NULL)
2702 			common_ie_len += vap->iv_wpa_ie[1];
2703 		if (vap->iv_appie_probereq != NULL)
2704 			common_ie_len += vap->iv_appie_probereq->ie_len;
2705 
2706 		/* We would love to check this at an earlier stage... */
2707 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
2708 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
2709 			    "wiphy->max_scan_ie_len %d\n", __func__,
2710 			    common_ie_len, hw->wiphy->max_scan_ie_len);
2711 		}
2712 
2713 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
2714 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
2715 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
2716 
2717 		hw_req->req.flags = 0;			/* XXX ??? */
2718 		/* hw_req->req.wdev */
2719 		hw_req->req.wiphy = hw->wiphy;
2720 		hw_req->req.no_cck = false;		/* XXX */
2721 #if 0
2722 		/* This seems to pessimise default scanning behaviour. */
2723 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
2724 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
2725 #endif
2726 #ifdef __notyet__
2727 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
2728 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
2729 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
2730 #endif
2731 
2732 		hw_req->req.n_channels = nchan;
2733 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
2734 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
2735 		for (i = 0; i < nchan; i++) {
2736 			*(cpp + i) =
2737 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
2738 		}
2739 		for (i = 0; i < nchan; i++) {
2740 			c = ss->ss_chans[ss->ss_next + i];
2741 
2742 			lc->hw_value = c->ic_ieee;
2743 			lc->center_freq = c->ic_freq;	/* XXX */
2744 			/* lc->flags */
2745 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
2746 			lc->max_power = c->ic_maxpower;
2747 			/* lc-> ... */
2748 			lc++;
2749 		}
2750 
2751 		hw_req->req.n_ssids = ssid_count;
2752 		if (hw_req->req.n_ssids > 0) {
2753 			ssids = (struct cfg80211_ssid *)lc;
2754 			hw_req->req.ssids = ssids;
2755 			for (i = 0; i < ssid_count; i++) {
2756 				ssids->ssid_len = ss->ss_ssid[i].len;
2757 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
2758 				    ss->ss_ssid[i].len);
2759 				ssids++;
2760 			}
2761 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
2762 		} else {
2763 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
2764 		}
2765 
2766 		/* 6GHz one day. */
2767 		hw_req->req.n_6ghz_params = 0;
2768 		hw_req->req.scan_6ghz_params = NULL;
2769 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
2770 		/* s6gp->... */
2771 
2772 		ie = ieend = (uint8_t *)s6gp;
2773 		/* Copy per-band IEs, copy common IEs */
2774 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
2775 		hw_req->req.ie = ie;
2776 		hw_req->req.ie_len = ieend - ie;
2777 
2778 		lvif = VAP_TO_LVIF(vap);
2779 		vif = LVIF_TO_VIF(lvif);
2780 
2781 		LKPI_80211_LHW_SCAN_LOCK(lhw);
2782 		/* Re-check under lock. */
2783 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2784 		if (!running) {
2785 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
2786 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
2787 
2788 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
2789 			lhw->hw_req = hw_req;
2790 		}
2791 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2792 		if (running) {
2793 			free(hw_req, M_LKPI80211);
2794 			return;
2795 		}
2796 
2797 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
2798 		if (error != 0) {
2799 			ieee80211_cancel_scan(vap);
2800 
2801 			/*
2802 			 * ieee80211_scan_completed must be called in either
2803 			 * case of error or none.  So let the free happen there
2804 			 * and only there.
2805 			 * That would be fine in theory but in practice drivers
2806 			 * behave differently:
2807 			 * ath10k does not return hw_scan until after scan_complete
2808 			 *        and can then still return an error.
2809 			 * rtw88 can return 1 or -EBUSY without scan_complete
2810 			 * iwlwifi can return various errors before scan starts
2811 			 * ...
2812 			 * So we cannot rely on that behaviour and have to check
2813 			 * and balance between both code paths.
2814 			 */
2815 			LKPI_80211_LHW_SCAN_LOCK(lhw);
2816 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
2817 				free(lhw->hw_req, M_LKPI80211);
2818 				lhw->hw_req = NULL;
2819 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
2820 			}
2821 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2822 
2823 			/*
2824 			 * XXX-SIGH magic number.
2825 			 * rtw88 has a magic "return 1" if offloading scan is
2826 			 * not possible.  Fall back to sw scan in that case.
2827 			 */
2828 			if (error == 1) {
2829 				LKPI_80211_LHW_SCAN_LOCK(lhw);
2830 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
2831 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2832 				/*
2833 				 * XXX If we clear this now and later a driver
2834 				 * thinks it * can do a hw_scan again, we will
2835 				 * currently not re-enable it?
2836 				 */
2837 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2838 				ieee80211_start_scan(vap,
2839 				    IEEE80211_SCAN_ACTIVE |
2840 				    IEEE80211_SCAN_NOPICK |
2841 				    IEEE80211_SCAN_ONCE,
2842 				    IEEE80211_SCAN_FOREVER,
2843 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2844 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2845 				    vap->iv_des_nssid, vap->iv_des_ssid);
2846 				goto sw_scan;
2847 			}
2848 
2849 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2850 			    __func__, error);
2851 		}
2852 	}
2853 }
2854 
2855 static void
2856 lkpi_ic_scan_end(struct ieee80211com *ic)
2857 {
2858 	struct lkpi_hw *lhw;
2859 	bool is_hw_scan;
2860 
2861 	lhw = ic->ic_softc;
2862 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2863 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
2864 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2865 		return;
2866 	}
2867 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2868 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2869 
2870 	if (!is_hw_scan) {
2871 		struct ieee80211_scan_state *ss;
2872 		struct ieee80211vap *vap;
2873 		struct ieee80211_hw *hw;
2874 		struct lkpi_vif *lvif;
2875 		struct ieee80211_vif *vif;
2876 
2877 		ss = ic->ic_scan;
2878 		vap = ss->ss_vap;
2879 		hw = LHW_TO_HW(lhw);
2880 		lvif = VAP_TO_LVIF(vap);
2881 		vif = LVIF_TO_VIF(lvif);
2882 
2883 		lkpi_80211_mo_sw_scan_complete(hw, vif);
2884 
2885 		/* Send PS to stop buffering if n80211 does not for us? */
2886 
2887 		if (vap->iv_state == IEEE80211_S_SCAN)
2888 			lkpi_hw_conf_idle(hw, true);
2889 	}
2890 }
2891 
2892 static void
2893 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
2894     unsigned long maxdwell)
2895 {
2896 	struct lkpi_hw *lhw;
2897 	bool is_hw_scan;
2898 
2899 	lhw = ss->ss_ic->ic_softc;
2900 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2901 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2902 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2903 	if (!is_hw_scan)
2904 		lhw->ic_scan_curchan(ss, maxdwell);
2905 }
2906 
2907 static void
2908 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
2909 {
2910 	struct lkpi_hw *lhw;
2911 	bool is_hw_scan;
2912 
2913 	lhw = ss->ss_ic->ic_softc;
2914 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2915 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
2916 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2917 	if (!is_hw_scan)
2918 		lhw->ic_scan_mindwell(ss);
2919 }
2920 
2921 static void
2922 lkpi_ic_set_channel(struct ieee80211com *ic)
2923 {
2924 	struct lkpi_hw *lhw;
2925 	struct ieee80211_hw *hw;
2926 	struct ieee80211_channel *c;
2927 	struct linuxkpi_ieee80211_channel *chan;
2928 	int error;
2929 	bool hw_scan_running;
2930 
2931 	lhw = ic->ic_softc;
2932 
2933 	/* If we do not support (*config)() save us the work. */
2934 	if (lhw->ops->config == NULL)
2935 		return;
2936 
2937 	/* If we have a hw_scan running do not switch channels. */
2938 	LKPI_80211_LHW_SCAN_LOCK(lhw);
2939 	hw_scan_running =
2940 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
2941 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
2942 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2943 	if (hw_scan_running)
2944 		return;
2945 
2946 	c = ic->ic_curchan;
2947 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
2948 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
2949 		    c, lhw->ops->config);
2950 		return;
2951 	}
2952 
2953 	chan = lkpi_find_lkpi80211_chan(lhw, c);
2954 	if (chan == NULL) {
2955 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
2956 		    c, chan);
2957 		return;
2958 	}
2959 
2960 	/* XXX max power for scanning? */
2961 	IMPROVE();
2962 
2963 	hw = LHW_TO_HW(lhw);
2964 	cfg80211_chandef_create(&hw->conf.chandef, chan,
2965 	    NL80211_CHAN_NO_HT);
2966 
2967 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
2968 	if (error != 0 && error != EOPNOTSUPP) {
2969 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
2970 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
2971 		/* XXX should we unroll to the previous chandef? */
2972 		IMPROVE();
2973 	} else {
2974 		/* Update radiotap channels as well. */
2975 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
2976 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
2977 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
2978 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
2979 	}
2980 
2981 	/* Currently PS is hard coded off! Not sure it belongs here. */
2982 	IMPROVE();
2983 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
2984 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
2985 		hw->conf.flags &= ~IEEE80211_CONF_PS;
2986 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
2987 		if (error != 0 && error != EOPNOTSUPP)
2988 			ic_printf(ic, "ERROR: %s: config %#0x returned "
2989 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
2990 			    error);
2991 	}
2992 }
2993 
2994 static struct ieee80211_node *
2995 lkpi_ic_node_alloc(struct ieee80211vap *vap,
2996     const uint8_t mac[IEEE80211_ADDR_LEN])
2997 {
2998 	struct ieee80211com *ic;
2999 	struct lkpi_hw *lhw;
3000 	struct ieee80211_node *ni;
3001 	struct ieee80211_hw *hw;
3002 	struct lkpi_sta *lsta;
3003 
3004 	ic = vap->iv_ic;
3005 	lhw = ic->ic_softc;
3006 
3007 	/* We keep allocations de-coupled so we can deal with the two worlds. */
3008 	if (lhw->ic_node_alloc == NULL)
3009 		return (NULL);
3010 
3011 	ni = lhw->ic_node_alloc(vap, mac);
3012 	if (ni == NULL)
3013 		return (NULL);
3014 
3015 	hw = LHW_TO_HW(lhw);
3016 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
3017 	if (lsta == NULL) {
3018 		if (lhw->ic_node_free != NULL)
3019 			lhw->ic_node_free(ni);
3020 		return (NULL);
3021 	}
3022 
3023 	return (ni);
3024 }
3025 
3026 static int
3027 lkpi_ic_node_init(struct ieee80211_node *ni)
3028 {
3029 	struct ieee80211com *ic;
3030 	struct lkpi_hw *lhw;
3031 	struct lkpi_sta *lsta;
3032 	int error;
3033 
3034 	ic = ni->ni_ic;
3035 	lhw = ic->ic_softc;
3036 
3037 	if (lhw->ic_node_init != NULL) {
3038 		error = lhw->ic_node_init(ni);
3039 		if (error != 0)
3040 			return (error);
3041 	}
3042 
3043 	lsta = ni->ni_drv_data;
3044 
3045 	/* Now take the reference before linking it to the table. */
3046 	lsta->ni = ieee80211_ref_node(ni);
3047 
3048 	/* XXX-BZ Sync other state over. */
3049 	IMPROVE();
3050 
3051 	return (0);
3052 }
3053 
3054 static void
3055 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
3056 {
3057 	struct ieee80211com *ic;
3058 	struct lkpi_hw *lhw;
3059 
3060 	ic = ni->ni_ic;
3061 	lhw = ic->ic_softc;
3062 
3063 	/* XXX-BZ remove from driver, ... */
3064 	IMPROVE();
3065 
3066 	if (lhw->ic_node_cleanup != NULL)
3067 		lhw->ic_node_cleanup(ni);
3068 }
3069 
3070 static void
3071 lkpi_ic_node_free(struct ieee80211_node *ni)
3072 {
3073 	struct ieee80211com *ic;
3074 	struct lkpi_hw *lhw;
3075 	struct lkpi_sta *lsta;
3076 
3077 	ic = ni->ni_ic;
3078 	lhw = ic->ic_softc;
3079 	lsta = ni->ni_drv_data;
3080 	if (lsta == NULL)
3081 		goto out;
3082 
3083 	/* XXX-BZ free resources, ... */
3084 	IMPROVE();
3085 
3086 	/* Flush mbufq (make sure to release ni refs!). */
3087 #ifdef __notyet__
3088 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
3089 	    __func__, lsta, mbufq_len(&lsta->txq)));
3090 #endif
3091 	/* Drain taskq. */
3092 
3093 	/* Drain sta->txq[] */
3094 	mtx_destroy(&lsta->txq_mtx);
3095 
3096 	/* Remove lsta if added_to_drv. */
3097 
3098 	/* Remove lsta from vif */
3099 	/* Remove ref from lsta node... */
3100 	/* Free lsta. */
3101 	lkpi_lsta_remove(lsta, VAP_TO_LVIF(ni->ni_vap));
3102 
3103 out:
3104 	if (lhw->ic_node_free != NULL)
3105 		lhw->ic_node_free(ni);
3106 }
3107 
3108 static int
3109 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3110         const struct ieee80211_bpf_params *params __unused)
3111 {
3112 	struct lkpi_sta *lsta;
3113 
3114 	lsta = ni->ni_drv_data;
3115 
3116 	/* Queue the packet and enqueue the task to handle it. */
3117 	LKPI_80211_LSTA_LOCK(lsta);
3118 	mbufq_enqueue(&lsta->txq, m);
3119 	LKPI_80211_LSTA_UNLOCK(lsta);
3120 
3121 #ifdef LINUXKPI_DEBUG_80211
3122 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3123 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
3124 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
3125 		    mbufq_len(&lsta->txq));
3126 #endif
3127 
3128 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3129 	return (0);
3130 }
3131 
3132 static void
3133 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
3134 {
3135 	struct ieee80211_node *ni;
3136 #ifndef LKPI_80211_HW_CRYPTO
3137 	struct ieee80211_frame *wh;
3138 #endif
3139 	struct ieee80211_key *k;
3140 	struct sk_buff *skb;
3141 	struct ieee80211com *ic;
3142 	struct lkpi_hw *lhw;
3143 	struct ieee80211_hw *hw;
3144 	struct lkpi_vif *lvif;
3145 	struct ieee80211_vif *vif;
3146 	struct ieee80211_channel *c;
3147 	struct ieee80211_tx_control control;
3148 	struct ieee80211_tx_info *info;
3149 	struct ieee80211_sta *sta;
3150 	struct ieee80211_hdr *hdr;
3151 	void *buf;
3152 	uint8_t ac, tid;
3153 
3154 	M_ASSERTPKTHDR(m);
3155 #ifdef LINUXKPI_DEBUG_80211
3156 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
3157 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
3158 #endif
3159 
3160 	ni = lsta->ni;
3161 	k = NULL;
3162 #ifndef LKPI_80211_HW_CRYPTO
3163 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
3164 	wh = mtod(m, struct ieee80211_frame *);
3165 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
3166 		/* Retrieve key for TX && do software encryption. */
3167 		k = ieee80211_crypto_encap(ni, m);
3168 		if (k == NULL) {
3169 			ieee80211_free_node(ni);
3170 			m_freem(m);
3171 			return;
3172 		}
3173 	}
3174 #endif
3175 
3176 	ic = ni->ni_ic;
3177 	lhw = ic->ic_softc;
3178 	hw = LHW_TO_HW(lhw);
3179 	c = ni->ni_chan;
3180 
3181 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
3182 		struct lkpi_radiotap_tx_hdr *rtap;
3183 
3184 		rtap = &lhw->rtap_tx;
3185 		rtap->wt_flags = 0;
3186 		if (k != NULL)
3187 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3188 		if (m->m_flags & M_FRAG)
3189 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
3190 		IMPROVE();
3191 		rtap->wt_rate = 0;
3192 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3193 			rtap->wt_chan_freq = htole16(c->ic_freq);
3194 			rtap->wt_chan_flags = htole16(c->ic_flags);
3195 		}
3196 
3197 		ieee80211_radiotap_tx(ni->ni_vap, m);
3198 	}
3199 
3200 	/*
3201 	 * net80211 should handle hw->extra_tx_headroom.
3202 	 * Though for as long as we are copying we don't mind.
3203 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
3204 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
3205 	 */
3206 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
3207 	if (skb == NULL) {
3208 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
3209 		ieee80211_free_node(ni);
3210 		m_freem(m);
3211 		return;
3212 	}
3213 	skb_reserve(skb, hw->extra_tx_headroom);
3214 
3215 	/* XXX-BZ we need a SKB version understanding mbuf. */
3216 	/* Save the mbuf for ieee80211_tx_complete(). */
3217 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3218 	skb->m = m;
3219 #if 0
3220 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3221 #else
3222 	buf = skb_put(skb, m->m_pkthdr.len);
3223 	m_copydata(m, 0, m->m_pkthdr.len, buf);
3224 #endif
3225 	/* Save the ni. */
3226 	m->m_pkthdr.PH_loc.ptr = ni;
3227 
3228 	lvif = VAP_TO_LVIF(ni->ni_vap);
3229 	vif = LVIF_TO_VIF(lvif);
3230 
3231 	hdr = (void *)skb->data;
3232 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3233 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3234 		skb->priority = 0;
3235 		ac = IEEE80211_AC_BE;
3236 	} else {
3237 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3238 		ac = tid_to_mac80211_ac[tid & 7];
3239 	}
3240 	skb_set_queue_mapping(skb, ac);
3241 
3242 	info = IEEE80211_SKB_CB(skb);
3243 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3244 	/* Slight delay; probably only happens on scanning so fine? */
3245 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
3246 		c = ic->ic_curchan;
3247 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3248 	info->hw_queue = vif->hw_queue[ac];
3249 	if (m->m_flags & M_EAPOL)
3250 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3251 	info->control.vif = vif;
3252 	/* XXX-BZ info->control.rates */
3253 
3254 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
3255 	if (lsta != NULL) {
3256 		sta = LSTA_TO_STA(lsta);
3257 #ifdef LKPI_80211_HW_CRYPTO
3258 		info->control.hw_key = lsta->kc;
3259 #endif
3260 	} else {
3261 		sta = NULL;
3262 	}
3263 
3264 	IMPROVE();
3265 
3266 	if (sta != NULL) {
3267 		struct lkpi_txq *ltxq;
3268 
3269 		ltxq = NULL;
3270 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3271 			if (vif->type == NL80211_IFTYPE_STATION &&
3272 			    lsta->added_to_drv &&
3273 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3274 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3275 		} else if (lsta->added_to_drv &&
3276 		    sta->txq[skb->priority] != NULL) {
3277 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3278 		}
3279 		if (ltxq == NULL)
3280 			goto ops_tx;
3281 
3282 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3283 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3284 
3285 		skb_queue_tail(&ltxq->skbq, skb);
3286 #ifdef LINUXKPI_DEBUG_80211
3287 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3288 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3289 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3290 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3291 			    __func__, __LINE__,
3292 			    curthread->td_tid, (unsigned int)ticks,
3293 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3294 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3295 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
3296 #endif
3297 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
3298 		return;
3299 	}
3300 
3301 ops_tx:
3302 #ifdef LINUXKPI_DEBUG_80211
3303 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3304 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3305 		    "TX ac %d prio %u qmap %u\n",
3306 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3307 		    skb, ac, skb->priority, skb->qmap);
3308 #endif
3309 	memset(&control, 0, sizeof(control));
3310 	control.sta = sta;
3311 
3312 	lkpi_80211_mo_tx(hw, &control, skb);
3313 	return;
3314 }
3315 
3316 static void
3317 lkpi_80211_txq_task(void *ctx, int pending)
3318 {
3319 	struct lkpi_sta *lsta;
3320 	struct mbufq mq;
3321 	struct mbuf *m;
3322 
3323 	lsta = ctx;
3324 
3325 #ifdef LINUXKPI_DEBUG_80211
3326 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3327 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3328 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3329 		    pending, mbufq_len(&lsta->txq));
3330 #endif
3331 
3332 	mbufq_init(&mq, IFQ_MAXLEN);
3333 
3334 	LKPI_80211_LSTA_LOCK(lsta);
3335 	mbufq_concat(&mq, &lsta->txq);
3336 	LKPI_80211_LSTA_UNLOCK(lsta);
3337 
3338 	m = mbufq_dequeue(&mq);
3339 	while (m != NULL) {
3340 		lkpi_80211_txq_tx_one(lsta, m);
3341 		m = mbufq_dequeue(&mq);
3342 	}
3343 }
3344 
3345 static int
3346 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3347 {
3348 
3349 	/* XXX TODO */
3350 	IMPROVE();
3351 
3352 	/* Quick and dirty cheating hack. */
3353 	struct ieee80211_node *ni;
3354 
3355 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3356 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3357 }
3358 
3359 static void
3360 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
3361     int *n, struct ieee80211_channel *c)
3362 {
3363 	struct lkpi_hw *lhw;
3364 	struct ieee80211_hw *hw;
3365 	struct linuxkpi_ieee80211_channel *channels;
3366 	uint8_t bands[IEEE80211_MODE_BYTES];
3367 	int chan_flags, error, i, nchans;
3368 
3369 	/* Channels */
3370 	lhw = ic->ic_softc;
3371 	hw = LHW_TO_HW(lhw);
3372 
3373 	/* NL80211_BAND_2GHZ */
3374 	nchans = 0;
3375 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
3376 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
3377 	if (nchans > 0) {
3378 		memset(bands, 0, sizeof(bands));
3379 		chan_flags = 0;
3380 		setbit(bands, IEEE80211_MODE_11B);
3381 		/* XXX-BZ unclear how to check for 11g. */
3382 		setbit(bands, IEEE80211_MODE_11G);
3383 #ifdef __notyet__
3384 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
3385 			setbit(bands, IEEE80211_MODE_11NG);
3386 			chan_flags |= NET80211_CBW_FLAG_HT40;
3387 		}
3388 #endif
3389 
3390 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3391 		for (i = 0; i < nchans && *n < maxchan; i++) {
3392 			uint32_t nflags = 0;
3393 			int cflags = chan_flags;
3394 
3395 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3396 				ic_printf(ic, "%s: Skipping disabled chan "
3397 				    "[%u/%u/%#x]\n", __func__,
3398 				    channels[i].hw_value,
3399 				    channels[i].center_freq, channels[i].flags);
3400 				continue;
3401 			}
3402 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3403 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3404 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3405 				nflags |= IEEE80211_CHAN_DFS;
3406 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3407 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3408 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3409 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3410 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
3411 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3412 				cflags &= ~NET80211_CBW_FLAG_HT40;
3413 
3414 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3415 			    channels[i].hw_value, channels[i].center_freq,
3416 			    channels[i].max_power,
3417 			    nflags, bands, chan_flags);
3418 			/* net80211::ENOBUFS: *n >= maxchans */
3419 			if (error != 0 && error != ENOBUFS)
3420 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3421 				    "returned error %d\n",
3422 				    __func__, channels[i].hw_value,
3423 				    channels[i].center_freq, channels[i].flags,
3424 				    nflags, chan_flags, cflags, error);
3425 			if (error != 0)
3426 				break;
3427 		}
3428 	}
3429 
3430 	/* NL80211_BAND_5GHZ */
3431 	nchans = 0;
3432 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
3433 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
3434 	if (nchans > 0) {
3435 		memset(bands, 0, sizeof(bands));
3436 		chan_flags = 0;
3437 		setbit(bands, IEEE80211_MODE_11A);
3438 #ifdef __not_yet__
3439 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
3440 			setbit(bands, IEEE80211_MODE_11NA);
3441 			chan_flags |= NET80211_CBW_FLAG_HT40;
3442 		}
3443 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
3444 
3445 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
3446 			ic->ic_vhtcaps =
3447 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
3448 
3449 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
3450 			chan_flags |= NET80211_CBW_FLAG_VHT80;
3451 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
3452 			    ic->ic_vhtcaps))
3453 				chan_flags |= NET80211_CBW_FLAG_VHT160;
3454 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
3455 			    ic->ic_vhtcaps))
3456 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
3457 		}
3458 #endif
3459 
3460 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3461 		for (i = 0; i < nchans && *n < maxchan; i++) {
3462 			uint32_t nflags = 0;
3463 			int cflags = chan_flags;
3464 
3465 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3466 				ic_printf(ic, "%s: Skipping disabled chan "
3467 				    "[%u/%u/%#x]\n", __func__,
3468 				    channels[i].hw_value,
3469 				    channels[i].center_freq, channels[i].flags);
3470 				continue;
3471 			}
3472 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3473 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3474 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
3475 				nflags |= IEEE80211_CHAN_DFS;
3476 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3477 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3478 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3479 				cflags &= ~NET80211_CBW_FLAG_VHT80;
3480 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
3481 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3482 				cflags &= ~NET80211_CBW_FLAG_HT40;
3483 
3484 			error = ieee80211_add_channel_cbw(c, maxchan, n,
3485 			    channels[i].hw_value, channels[i].center_freq,
3486 			    channels[i].max_power,
3487 			    nflags, bands, chan_flags);
3488 			/* net80211::ENOBUFS: *n >= maxchans */
3489 			if (error != 0 && error != ENOBUFS)
3490 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3491 				    "returned error %d\n",
3492 				    __func__, channels[i].hw_value,
3493 				    channels[i].center_freq, channels[i].flags,
3494 				    nflags, chan_flags, cflags, error);
3495 			if (error != 0)
3496 				break;
3497 		}
3498 	}
3499 }
3500 
3501 static void *
3502 lkpi_ieee80211_ifalloc(void)
3503 {
3504 	struct ieee80211com *ic;
3505 
3506 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
3507 	if (ic == NULL)
3508 		return (NULL);
3509 
3510 	/* Setting these happens later when we have device information. */
3511 	ic->ic_softc = NULL;
3512 	ic->ic_name = "linuxkpi";
3513 
3514 	return (ic);
3515 }
3516 
3517 struct ieee80211_hw *
3518 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
3519 {
3520 	struct ieee80211_hw *hw;
3521 	struct lkpi_hw *lhw;
3522 	struct wiphy *wiphy;
3523 	int ac;
3524 
3525 	/* Get us and the driver data also allocated. */
3526 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
3527 	if (wiphy == NULL)
3528 		return (NULL);
3529 
3530 	lhw = wiphy_priv(wiphy);
3531 	lhw->ops = ops;
3532 
3533 	LKPI_80211_LHW_LOCK_INIT(lhw);
3534 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
3535 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
3536 	TAILQ_INIT(&lhw->lvif_head);
3537 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3538 		lhw->txq_generation[ac] = 1;
3539 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
3540 	}
3541 
3542 	/*
3543 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
3544 	 * not initialized.
3545 	 */
3546 	hw = LHW_TO_HW(lhw);
3547 	hw->wiphy = wiphy;
3548 	hw->conf.flags |= IEEE80211_CONF_IDLE;
3549 	hw->priv = (void *)(lhw + 1);
3550 
3551 	/* BSD Specific. */
3552 	lhw->ic = lkpi_ieee80211_ifalloc();
3553 	if (lhw->ic == NULL) {
3554 		ieee80211_free_hw(hw);
3555 		return (NULL);
3556 	}
3557 
3558 	IMPROVE();
3559 
3560 	return (hw);
3561 }
3562 
3563 void
3564 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
3565 {
3566 	struct lkpi_hw *lhw;
3567 
3568 	lhw = HW_TO_LHW(hw);
3569 	free(lhw->ic, M_LKPI80211);
3570 	lhw->ic = NULL;
3571 
3572 	/* Cleanup more of lhw here or in wiphy_free()? */
3573 	sx_destroy(&lhw->lvif_sx);
3574 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
3575 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
3576 	IMPROVE();
3577 }
3578 
3579 void
3580 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
3581 {
3582 	struct lkpi_hw *lhw;
3583 	struct ieee80211com *ic;
3584 
3585 	lhw = HW_TO_LHW(hw);
3586 	ic = lhw->ic;
3587 
3588 	/* Now set a proper name before ieee80211_ifattach(). */
3589 	ic->ic_softc = lhw;
3590 	ic->ic_name = name;
3591 
3592 	/* XXX-BZ do we also need to set wiphy name? */
3593 }
3594 
3595 struct ieee80211_hw *
3596 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
3597 {
3598 	struct lkpi_hw *lhw;
3599 
3600 	lhw = wiphy_priv(wiphy);
3601 	return (LHW_TO_HW(lhw));
3602 }
3603 
3604 static void
3605 lkpi_radiotap_attach(struct lkpi_hw *lhw)
3606 {
3607 	struct ieee80211com *ic;
3608 
3609 	ic = lhw->ic;
3610 	ieee80211_radiotap_attach(ic,
3611 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
3612 	    LKPI_RTAP_TX_FLAGS_PRESENT,
3613 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
3614 	    LKPI_RTAP_RX_FLAGS_PRESENT);
3615 }
3616 
3617 int
3618 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
3619 {
3620 	struct ieee80211com *ic;
3621 	struct lkpi_hw *lhw;
3622 	int band, i;
3623 
3624 	lhw = HW_TO_LHW(hw);
3625 	ic = lhw->ic;
3626 
3627 	/* We do it this late as wiphy->dev should be set for the name. */
3628 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3629 	if (lhw->workq == NULL)
3630 		return (-EAGAIN);
3631 
3632 	/* XXX-BZ figure this out how they count his... */
3633 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
3634 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3635 		    hw->wiphy->perm_addr);
3636 	} else if (hw->wiphy->n_addresses > 0) {
3637 		/* We take the first one. */
3638 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
3639 		    hw->wiphy->addresses[0].addr);
3640 	} else {
3641 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
3642 	}
3643 
3644 #ifdef __not_yet__
3645 	/* See comment in lkpi_80211_txq_tx_one(). */
3646 	ic->ic_headroom = hw->extra_tx_headroom;
3647 #endif
3648 
3649 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
3650 	ic->ic_opmode = IEEE80211_M_STA;
3651 
3652 	/* Set device capabilities. */
3653 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
3654 	ic->ic_caps =
3655 	    IEEE80211_C_STA |
3656 	    IEEE80211_C_MONITOR |
3657 	    IEEE80211_C_WPA |		/* WPA/RSN */
3658 #ifdef LKPI_80211_WME
3659 	    IEEE80211_C_WME |
3660 #endif
3661 #if 0
3662 	    IEEE80211_C_PMGT |
3663 #endif
3664 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
3665 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
3666 	    ;
3667 #if 0
3668 	/* Scanning is a different kind of beast to re-work. */
3669 	ic->ic_caps |= IEEE80211_C_BGSCAN;
3670 #endif
3671 	if (lhw->ops->hw_scan) {
3672 		/*
3673 		 * Advertise full-offload scanning.
3674 		 *
3675 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3676 		 * we essentially disable hw_scan for all drivers not setting
3677 		 * the flag.
3678 		 */
3679 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3680 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
3681 	}
3682 
3683 #ifdef __notyet__
3684 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
3685 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
3686 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
3687 		    | IEEE80211_HTCAP_MAXAMSDU_3839
3688 						/* max A-MSDU length */
3689 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
3690 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
3691 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
3692 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
3693 #endif
3694 
3695 	/*
3696 	 * The wiphy variables report bitmasks of avail antennas.
3697 	 * (*get_antenna) get the current bitmask sets which can be
3698 	 * altered by (*set_antenna) for some drivers.
3699 	 * XXX-BZ will the count alone do us much good long-term in net80211?
3700 	 */
3701 	if (hw->wiphy->available_antennas_rx ||
3702 	    hw->wiphy->available_antennas_tx) {
3703 		uint32_t rxs, txs;
3704 
3705 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
3706 			ic->ic_rxstream = bitcount32(rxs);
3707 			ic->ic_txstream = bitcount32(txs);
3708 		}
3709 	}
3710 
3711 	ic->ic_cryptocaps = 0;
3712 #ifdef LKPI_80211_HW_CRYPTO
3713 	if (hw->wiphy->n_cipher_suites > 0) {
3714 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
3715 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
3716 			    hw->wiphy->cipher_suites[i]);
3717 	}
3718 #endif
3719 
3720 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3721 	    ic->ic_channels);
3722 
3723 	ieee80211_ifattach(ic);
3724 
3725 	ic->ic_update_mcast = lkpi_ic_update_mcast;
3726 	ic->ic_update_promisc = lkpi_ic_update_promisc;
3727 	ic->ic_update_chw = lkpi_ic_update_chw;
3728 	ic->ic_parent = lkpi_ic_parent;
3729 	ic->ic_scan_start = lkpi_ic_scan_start;
3730 	ic->ic_scan_end = lkpi_ic_scan_end;
3731 	ic->ic_set_channel = lkpi_ic_set_channel;
3732 	ic->ic_transmit = lkpi_ic_transmit;
3733 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
3734 	ic->ic_vap_create = lkpi_ic_vap_create;
3735 	ic->ic_vap_delete = lkpi_ic_vap_delete;
3736 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
3737 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
3738 
3739 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
3740 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
3741 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
3742 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
3743 
3744 	lhw->ic_node_alloc = ic->ic_node_alloc;
3745 	ic->ic_node_alloc = lkpi_ic_node_alloc;
3746 	lhw->ic_node_init = ic->ic_node_init;
3747 	ic->ic_node_init = lkpi_ic_node_init;
3748 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
3749 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
3750 	lhw->ic_node_free = ic->ic_node_free;
3751 	ic->ic_node_free = lkpi_ic_node_free;
3752 
3753 	lkpi_radiotap_attach(lhw);
3754 
3755 	/*
3756 	 * Assign the first possible channel for now;  seems Realtek drivers
3757 	 * expect one.
3758 	 * Also remember the amount of bands we support and the most rates
3759 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
3760 	 */
3761 	lhw->supbands = lhw->max_rates = 0;
3762 	for (band = 0; band < NUM_NL80211_BANDS &&
3763 	    hw->conf.chandef.chan == NULL; band++) {
3764 		struct ieee80211_supported_band *supband;
3765 		struct linuxkpi_ieee80211_channel *channels;
3766 
3767 		supband = hw->wiphy->bands[band];
3768 		if (supband == NULL || supband->n_channels == 0)
3769 			continue;
3770 
3771 		lhw->supbands++;
3772 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
3773 
3774 		channels = supband->channels;
3775 		for (i = 0; i < supband->n_channels; i++) {
3776 
3777 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3778 				continue;
3779 
3780 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3781 			    NL80211_CHAN_NO_HT);
3782 			break;
3783 		}
3784 	}
3785 
3786 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
3787 
3788 	/* Make sure we do not support more than net80211 is willing to take. */
3789 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
3790 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
3791 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
3792 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
3793 	}
3794 
3795 	/*
3796 	 * The maximum supported bitrates on any band + size for
3797 	 * DSSS Parameter Set give our per-band IE size.
3798 	 * XXX-BZ FIXME add HT VHT ... later
3799 	 * SSID is the responsibility of the driver and goes on the side.
3800 	 * The user specified bits coming from the vap go into the
3801 	 * "common ies" fields.
3802 	 */
3803 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
3804 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
3805 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
3806 	/*
3807 	 * net80211 does not seem to support the DSSS Parameter Set but some of
3808 	 * the drivers insert it so calculate the extra fixed space in.
3809 	 */
3810 	lhw->scan_ie_len += 2 + 1;
3811 
3812 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
3813 	if (hw->wiphy->max_scan_ie_len > 0)
3814 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
3815 
3816 	if (bootverbose)
3817 		ieee80211_announce(ic);
3818 
3819 	return (0);
3820 }
3821 
3822 void
3823 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
3824 {
3825 	struct lkpi_hw *lhw;
3826 	struct ieee80211com *ic;
3827 
3828 	lhw = HW_TO_LHW(hw);
3829 	ic = lhw->ic;
3830 	ieee80211_ifdetach(ic);
3831 }
3832 
3833 void
3834 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
3835     enum ieee80211_iface_iter flags,
3836     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
3837     void *arg)
3838 {
3839 	struct lkpi_hw *lhw;
3840 	struct lkpi_vif *lvif;
3841 	struct ieee80211_vif *vif;
3842 	bool active, atomic, nin_drv;
3843 
3844 	lhw = HW_TO_LHW(hw);
3845 
3846 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
3847 	    IEEE80211_IFACE_ITER_RESUME_ALL|
3848 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
3849 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
3850 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
3851 		    __func__, flags);
3852 	}
3853 
3854 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
3855 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
3856 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
3857 
3858 	if (atomic)
3859 		LKPI_80211_LHW_LVIF_LOCK(lhw);
3860 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3861 		struct ieee80211vap *vap;
3862 
3863 		vif = LVIF_TO_VIF(lvif);
3864 
3865 		/*
3866 		 * If we want "active" interfaces, we need to distinguish on
3867 		 * whether the driver knows about them or not to be able to
3868 		 * handle the "resume" case correctly.  Skip the ones the
3869 		 * driver does not know about.
3870 		 */
3871 		if (active && !lvif->added_to_drv &&
3872 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
3873 			continue;
3874 
3875 		/*
3876 		 * If we shall skip interfaces not added to the driver do so
3877 		 * if we haven't yet.
3878 		 */
3879 		if (nin_drv && !lvif->added_to_drv)
3880 			continue;
3881 
3882 		/*
3883 		 * Run the iterator function if we are either not asking
3884 		 * asking for active only or if the VAP is "running".
3885 		 */
3886 		/* XXX-BZ probably should have state in the lvif as well. */
3887 		vap = LVIF_TO_VAP(lvif);
3888 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
3889 			iterfunc(arg, vif->addr, vif);
3890 	}
3891 	if (atomic)
3892 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3893 }
3894 
3895 void
3896 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
3897     struct ieee80211_vif *vif,
3898     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
3899         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
3900     void *arg)
3901 {
3902 
3903 	UNIMPLEMENTED;
3904 }
3905 
3906 void
3907 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
3908     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
3909 	void *),
3910     void *arg)
3911 {
3912 
3913 	UNIMPLEMENTED;
3914 }
3915 
3916 void
3917 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
3918    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
3919 {
3920 	struct lkpi_hw *lhw;
3921 	struct lkpi_vif *lvif;
3922 	struct lkpi_sta *lsta;
3923 	struct ieee80211_sta *sta;
3924 
3925 	KASSERT(hw != NULL && iterfunc != NULL,
3926 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
3927 
3928 	lhw = HW_TO_LHW(hw);
3929 
3930 	LKPI_80211_LHW_LVIF_LOCK(lhw);
3931 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3932 
3933 		LKPI_80211_LVIF_LOCK(lvif);
3934 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
3935 			if (!lsta->added_to_drv)
3936 				continue;
3937 			sta = LSTA_TO_STA(lsta);
3938 			iterfunc(arg, sta);
3939 		}
3940 		LKPI_80211_LVIF_UNLOCK(lvif);
3941 	}
3942 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3943 }
3944 
3945 int
3946 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
3947     struct linuxkpi_ieee80211_regdomain *regd)
3948 {
3949 	struct lkpi_hw *lhw;
3950 	struct ieee80211com *ic;
3951 	struct ieee80211_regdomain *rd;
3952 
3953 	lhw = wiphy_priv(wiphy);
3954 	ic = lhw->ic;
3955 
3956 	rd = &ic->ic_regdomain;
3957 	if (rd->isocc[0] == '\0') {
3958 		rd->isocc[0] = regd->alpha2[0];
3959 		rd->isocc[1] = regd->alpha2[1];
3960 	}
3961 
3962 	TODO();
3963 	/* XXX-BZ finish the rest. */
3964 
3965 	return (0);
3966 }
3967 
3968 void
3969 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
3970     struct cfg80211_scan_info *info)
3971 {
3972 	struct lkpi_hw *lhw;
3973 	struct ieee80211com *ic;
3974 	struct ieee80211_scan_state *ss;
3975 
3976 	lhw = wiphy_priv(hw->wiphy);
3977 	ic = lhw->ic;
3978 	ss = ic->ic_scan;
3979 
3980 	ieee80211_scan_done(ss->ss_vap);
3981 
3982 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3983 	free(lhw->hw_req, M_LKPI80211);
3984 	lhw->hw_req = NULL;
3985 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
3986 	wakeup(lhw);
3987 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3988 
3989 	return;
3990 }
3991 
3992 /* For %list see comment towards the end of the function. */
3993 void
3994 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3995     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
3996     struct list_head *list __unused)
3997 {
3998 	struct epoch_tracker et;
3999 	struct lkpi_hw *lhw;
4000 	struct ieee80211com *ic;
4001 	struct mbuf *m;
4002 	struct skb_shared_info *shinfo;
4003 	struct ieee80211_rx_status *rx_status;
4004 	struct ieee80211_rx_stats rx_stats;
4005 	struct ieee80211_node *ni;
4006 	struct ieee80211vap *vap;
4007 	struct ieee80211_hdr *hdr;
4008 	struct lkpi_sta *lsta;
4009 	int i, offset, ok;
4010 	int8_t rssi;
4011 	bool is_beacon;
4012 
4013 	if (skb->len < 2) {
4014 		/* Need 80211 stats here. */
4015 		IMPROVE();
4016 		goto err;
4017 	}
4018 
4019 	/*
4020 	 * For now do the data copy; we can later improve things. Might even
4021 	 * have an mbuf backing the skb data then?
4022 	 */
4023 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
4024 	if (m == NULL)
4025 		goto err;
4026 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
4027 
4028 	shinfo = skb_shinfo(skb);
4029 	offset = m->m_len;
4030 	for (i = 0; i < shinfo->nr_frags; i++) {
4031 		m_copyback(m, offset, shinfo->frags[i].size,
4032 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
4033 		    shinfo->frags[i].offset);
4034 		offset += shinfo->frags[i].size;
4035 	}
4036 
4037 	rx_status = IEEE80211_SKB_RXCB(skb);
4038 
4039 	hdr = (void *)skb->data;
4040 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4041 
4042 #ifdef LINUXKPI_DEBUG_80211
4043 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
4044 		goto no_trace_beacons;
4045 
4046 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4047 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4048 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
4049 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
4050 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
4051 		    shinfo, shinfo->nr_frags,
4052 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
4053 
4054 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
4055 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
4056 
4057 	/* Implement a dump_rxcb() !!! */
4058 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4059 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
4060 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
4061 			__func__,
4062 			(uintmax_t)rx_status->boottime_ns,
4063 			(uintmax_t)rx_status->mactime,
4064 			rx_status->device_timestamp,
4065 			rx_status->flag,
4066 			rx_status->freq,
4067 			rx_status->bw,
4068 			rx_status->encoding,
4069 			rx_status->ampdu_reference,
4070 			rx_status->band,
4071 			rx_status->chains,
4072 			rx_status->chain_signal[0],
4073 			rx_status->chain_signal[1],
4074 			rx_status->chain_signal[2],
4075 			rx_status->chain_signal[3],
4076 			rx_status->signal,
4077 			rx_status->enc_flags,
4078 			rx_status->he_dcm,
4079 			rx_status->he_gi,
4080 			rx_status->he_ru,
4081 			rx_status->zero_length_psdu_type,
4082 			rx_status->nss,
4083 			rx_status->rate_idx);
4084 no_trace_beacons:
4085 #endif
4086 
4087 	memset(&rx_stats, 0, sizeof(rx_stats));
4088 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
4089 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
4090 	rx_stats.c_nf = -96;
4091 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
4092 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4093 		rssi = rx_status->signal;
4094 	else
4095 		rssi = rx_stats.c_nf;
4096 	/*
4097 	 * net80211 signal strength data are in .5 dBm units relative to
4098 	 * the current noise floor (see comment in ieee80211_node.h).
4099 	 */
4100 	rssi -= rx_stats.c_nf;
4101 	rx_stats.c_rssi = rssi * 2;
4102 	rx_stats.r_flags |= IEEE80211_R_BAND;
4103 	rx_stats.c_band =
4104 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
4105 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
4106 	rx_stats.c_freq = rx_status->freq;
4107 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
4108 
4109 	/* XXX (*sta_statistics)() to get to some of that? */
4110 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
4111 
4112 	lhw = HW_TO_LHW(hw);
4113 	ic = lhw->ic;
4114 
4115 	ok = ieee80211_add_rx_params(m, &rx_stats);
4116 	if (ok == 0) {
4117 		m_freem(m);
4118 		counter_u64_add(ic->ic_ierrors, 1);
4119 		goto err;
4120 	}
4121 
4122 	if (sta != NULL) {
4123 		lsta = STA_TO_LSTA(sta);
4124 		ni = ieee80211_ref_node(lsta->ni);
4125 	} else {
4126 		struct ieee80211_frame_min *wh;
4127 
4128 		wh = mtod(m, struct ieee80211_frame_min *);
4129 		ni = ieee80211_find_rxnode(ic, wh);
4130 		if (ni != NULL)
4131 			lsta = ni->ni_drv_data;
4132 	}
4133 
4134 	if (ni != NULL)
4135 		vap = ni->ni_vap;
4136 	else
4137 		/*
4138 		 * XXX-BZ can we improve this by looking at the frame hdr
4139 		 * or other meta-data passed up?
4140 		 */
4141 		vap = TAILQ_FIRST(&ic->ic_vaps);
4142 
4143 #ifdef LINUXKPI_DEBUG_80211
4144 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4145 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
4146 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
4147 		    ni, vap, is_beacon ? " beacon" : "");
4148 #endif
4149 
4150 	if (ni != NULL && vap != NULL && is_beacon &&
4151 	    rx_status->device_timestamp > 0 &&
4152 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4153 		struct lkpi_vif *lvif;
4154 		struct ieee80211_vif *vif;
4155 		struct ieee80211_frame *wh;
4156 
4157 		wh = mtod(m, struct ieee80211_frame *);
4158 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4159 			goto skip_device_ts;
4160 
4161 		lvif = VAP_TO_LVIF(vap);
4162 		vif = LVIF_TO_VIF(lvif);
4163 
4164 		IMPROVE("TIMING_BEACON_ONLY?");
4165 		/* mac80211 specific (not net80211) so keep it here. */
4166 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4167 		/*
4168 		 * net80211 should take care of the other information (sync_tsf,
4169 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4170 		 */
4171 	}
4172 skip_device_ts:
4173 
4174 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
4175 	    ieee80211_radiotap_active_vap(vap)) {
4176 		struct lkpi_radiotap_rx_hdr *rtap;
4177 
4178 		rtap = &lhw->rtap_rx;
4179 		rtap->wr_tsft = rx_status->device_timestamp;
4180 		rtap->wr_flags = 0;
4181 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
4182 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4183 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4184 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4185 #if 0	/* .. or it does not given we strip it below. */
4186 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4187 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
4188 #endif
4189 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
4190 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
4191 		rtap->wr_rate = 0;
4192 		IMPROVE();
4193 		/* XXX TODO status->encoding / rate_index / bw */
4194 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
4195 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
4196 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4197 		rtap->wr_dbm_antsignal = rssi;
4198 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
4199 	}
4200 
4201 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4202 		m_adj(m, -IEEE80211_CRC_LEN);
4203 
4204 #if 0
4205 	if (list != NULL) {
4206 		/*
4207 		* Normally this would be queued up and delivered by
4208 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
4209 		* See mt76::mac80211.c as only current possible consumer.
4210 		*/
4211 		IMPROVE("we simply pass the packet to net80211 to deal with.");
4212 	}
4213 #endif
4214 
4215 	NET_EPOCH_ENTER(et);
4216 	if (ni != NULL) {
4217 		ok = ieee80211_input_mimo(ni, m);
4218 		ieee80211_free_node(ni);
4219 		if (ok < 0)
4220 			m_freem(m);
4221 	} else {
4222 		ok = ieee80211_input_mimo_all(ic, m);
4223 		/* mbuf got consumed. */
4224 	}
4225 	NET_EPOCH_EXIT(et);
4226 
4227 #ifdef LINUXKPI_DEBUG_80211
4228 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4229 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4230 #endif
4231 
4232 	IMPROVE();
4233 
4234 err:
4235 	/* The skb is ours so we can free it :-) */
4236 	kfree_skb(skb);
4237 }
4238 
4239 uint8_t
4240 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
4241 {
4242 	const struct ieee80211_frame *wh;
4243 	uint8_t tid;
4244 
4245 	/* Linux seems to assume this is a QOS-Data-Frame */
4246 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
4247 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
4248 	   hdr->frame_control));
4249 
4250 	wh = (const struct ieee80211_frame *)hdr;
4251 	tid = ieee80211_gettid(wh);
4252 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
4253 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
4254 
4255 	return (tid);
4256 }
4257 
4258 struct wiphy *
4259 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
4260 {
4261 	struct lkpi_wiphy *lwiphy;
4262 
4263 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
4264 	if (lwiphy == NULL)
4265 		return (NULL);
4266 	lwiphy->ops = ops;
4267 
4268 	/* XXX TODO */
4269 	return (LWIPHY_TO_WIPHY(lwiphy));
4270 }
4271 
4272 void
4273 linuxkpi_wiphy_free(struct wiphy *wiphy)
4274 {
4275 	struct lkpi_wiphy *lwiphy;
4276 
4277 	if (wiphy == NULL)
4278 		return;
4279 
4280 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
4281 	kfree(lwiphy);
4282 }
4283 
4284 uint32_t
4285 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
4286     enum nl80211_band band)
4287 {
4288 
4289 	switch (band) {
4290 	case NL80211_BAND_2GHZ:
4291 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
4292 		break;
4293 	case NL80211_BAND_5GHZ:
4294 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
4295 		break;
4296 	default:
4297 		/* XXX abort, retry, error, panic? */
4298 		break;
4299 	}
4300 
4301 	return (0);
4302 }
4303 
4304 uint32_t
4305 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
4306 {
4307 
4308 	return (ieee80211_mhz2ieee(freq, 0));
4309 }
4310 
4311 static struct lkpi_sta *
4312 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
4313 {
4314 	struct lkpi_sta *lsta, *temp;
4315 
4316 	LKPI_80211_LVIF_LOCK(lvif);
4317 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4318 		if (lsta->ni == ni) {
4319 			LKPI_80211_LVIF_UNLOCK(lvif);
4320 			return (lsta);
4321 		}
4322 	}
4323 	LKPI_80211_LVIF_UNLOCK(lvif);
4324 
4325 	return (NULL);
4326 }
4327 
4328 struct ieee80211_sta *
4329 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
4330 {
4331 	struct lkpi_vif *lvif;
4332 	struct lkpi_sta *lsta, *temp;
4333 	struct ieee80211_sta *sta;
4334 
4335 	lvif = VIF_TO_LVIF(vif);
4336 
4337 	LKPI_80211_LVIF_LOCK(lvif);
4338 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
4339 		sta = LSTA_TO_STA(lsta);
4340 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
4341 			LKPI_80211_LVIF_UNLOCK(lvif);
4342 			return (sta);
4343 		}
4344 	}
4345 	LKPI_80211_LVIF_UNLOCK(lvif);
4346 	return (NULL);
4347 }
4348 
4349 struct ieee80211_sta *
4350 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
4351     const uint8_t *addr, const uint8_t *ourvifaddr)
4352 {
4353 	struct lkpi_hw *lhw;
4354 	struct lkpi_vif *lvif;
4355 	struct lkpi_sta *lsta;
4356 	struct ieee80211_vif *vif;
4357 	struct ieee80211_sta *sta;
4358 
4359 	lhw = wiphy_priv(hw->wiphy);
4360 	sta = NULL;
4361 
4362 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4363 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4364 
4365 		/* XXX-BZ check our address from the vif. */
4366 
4367 		vif = LVIF_TO_VIF(lvif);
4368 		if (ourvifaddr != NULL &&
4369 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
4370 			continue;
4371 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
4372 		if (sta != NULL)
4373 			break;
4374 	}
4375 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4376 
4377 	if (sta != NULL) {
4378 		lsta = STA_TO_LSTA(sta);
4379 		if (!lsta->added_to_drv)
4380 			return (NULL);
4381 	}
4382 
4383 	return (sta);
4384 }
4385 
4386 struct sk_buff *
4387 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
4388     struct ieee80211_txq *txq)
4389 {
4390 	struct lkpi_txq *ltxq;
4391 	struct lkpi_vif *lvif;
4392 	struct sk_buff *skb;
4393 
4394 	skb = NULL;
4395 	ltxq = TXQ_TO_LTXQ(txq);
4396 	ltxq->seen_dequeue = true;
4397 
4398 	if (ltxq->stopped)
4399 		goto stopped;
4400 
4401 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
4402 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
4403 		ltxq->stopped = true;
4404 		goto stopped;
4405 	}
4406 
4407 	skb = skb_dequeue(&ltxq->skbq);
4408 
4409 stopped:
4410 	return (skb);
4411 }
4412 
4413 void
4414 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4415     unsigned long *frame_cnt, unsigned long *byte_cnt)
4416 {
4417 	struct lkpi_txq *ltxq;
4418 	struct sk_buff *skb;
4419 	unsigned long fc, bc;
4420 
4421 	ltxq = TXQ_TO_LTXQ(txq);
4422 
4423 	fc = bc = 0;
4424 	skb_queue_walk(&ltxq->skbq, skb) {
4425 		fc++;
4426 		bc += skb->len;
4427 	}
4428 	if (frame_cnt)
4429 		*frame_cnt = fc;
4430 	if (byte_cnt)
4431 		*byte_cnt = bc;
4432 
4433 	/* Validate that this is doing the correct thing. */
4434 	/* Should we keep track on en/dequeue? */
4435 	IMPROVE();
4436 }
4437 
4438 /*
4439  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
4440  * The latter tries to derive the success status from the info flags
4441  * passed back from the driver.  rawx_mit() saves the ni on the m and the
4442  * m on the skb for us to be able to give feedback to net80211.
4443  */
4444 static void
4445 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4446     int status)
4447 {
4448 	struct ieee80211_node *ni;
4449 	struct mbuf *m;
4450 
4451 	m = skb->m;
4452 	skb->m = NULL;
4453 
4454 	if (m != NULL) {
4455 		ni = m->m_pkthdr.PH_loc.ptr;
4456 		/* Status: 0 is ok, != 0 is error. */
4457 		ieee80211_tx_complete(ni, m, status);
4458 		/* ni & mbuf were consumed. */
4459 	}
4460 }
4461 
4462 void
4463 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4464     int status)
4465 {
4466 
4467 	_lkpi_ieee80211_free_txskb(hw, skb, status);
4468 	kfree_skb(skb);
4469 }
4470 
4471 void
4472 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
4473     struct ieee80211_tx_status *txstat)
4474 {
4475 	struct sk_buff *skb;
4476 	struct ieee80211_tx_info *info;
4477 	struct ieee80211_ratectl_tx_status txs;
4478 	struct ieee80211_node *ni;
4479 	int status;
4480 
4481 	skb = txstat->skb;
4482 	if (skb->m != NULL) {
4483 		struct mbuf *m;
4484 
4485 		m = skb->m;
4486 		ni = m->m_pkthdr.PH_loc.ptr;
4487 		memset(&txs, 0, sizeof(txs));
4488 	} else {
4489 		ni = NULL;
4490 	}
4491 
4492 	info = txstat->info;
4493 	if (info->flags & IEEE80211_TX_STAT_ACK) {
4494 		status = 0;	/* No error. */
4495 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
4496 	} else {
4497 		status = 1;
4498 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
4499 	}
4500 
4501 	if (ni != NULL) {
4502 		int ridx __unused;
4503 #ifdef LINUXKPI_DEBUG_80211
4504 		int old_rate;
4505 
4506 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
4507 #endif
4508 		txs.pktlen = skb->len;
4509 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
4510 		if (info->status.rates[0].count > 1) {
4511 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
4512 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
4513 		}
4514 #if 0		/* Unused in net80211 currently. */
4515 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
4516 		txs.final_rate = info->status.rates[0].idx;
4517 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
4518 #endif
4519 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
4520 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
4521 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
4522 		}
4523 
4524 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
4525 		ieee80211_ratectl_tx_complete(ni, &txs);
4526 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
4527 
4528 #ifdef LINUXKPI_DEBUG_80211
4529 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
4530 			printf("TX-RATE: %s: old %d new %d ridx %d, "
4531 			    "long_retries %d\n", __func__,
4532 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
4533 			    ridx, txs.long_retries);
4534 		}
4535 #endif
4536 	}
4537 
4538 #ifdef LINUXKPI_DEBUG_80211
4539 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4540 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
4541 		    "band %u hw_queue %u tx_time_est %d : "
4542 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
4543 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
4544 		    "tx_time %u flags %#x "
4545 		    "status_driver_data [ %p %p ]\n",
4546 		    __func__, hw, skb, status, info->flags,
4547 		    info->band, info->hw_queue, info->tx_time_est,
4548 		    info->status.rates[0].idx, info->status.rates[0].count,
4549 		    info->status.rates[0].flags,
4550 		    info->status.rates[1].idx, info->status.rates[1].count,
4551 		    info->status.rates[1].flags,
4552 		    info->status.rates[2].idx, info->status.rates[2].count,
4553 		    info->status.rates[2].flags,
4554 		    info->status.rates[3].idx, info->status.rates[3].count,
4555 		    info->status.rates[3].flags,
4556 		    info->status.ack_signal, info->status.ampdu_ack_len,
4557 		    info->status.ampdu_len, info->status.antenna,
4558 		    info->status.tx_time, info->status.flags,
4559 		    info->status.status_driver_data[0],
4560 		    info->status.status_driver_data[1]);
4561 #endif
4562 
4563 	if (txstat->free_list) {
4564 		_lkpi_ieee80211_free_txskb(hw, skb, status);
4565 		list_add_tail(&skb->list, txstat->free_list);
4566 	} else {
4567 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
4568 	}
4569 }
4570 
4571 void
4572 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
4573 {
4574 	struct ieee80211_tx_status status;
4575 
4576 	memset(&status, 0, sizeof(status));
4577 	status.info = IEEE80211_SKB_CB(skb);
4578 	status.skb = skb;
4579 	/* sta, n_rates, rates, free_list? */
4580 
4581 	ieee80211_tx_status_ext(hw, &status);
4582 }
4583 
4584 /*
4585  * This is an internal bandaid for the moment for the way we glue
4586  * skbs and mbufs together for TX.  Once we have skbs backed by
4587  * mbufs this should go away.
4588  * This is a public function but kept on the private KPI (lkpi_)
4589  * and is not exposed by a header file.
4590  */
4591 static void
4592 lkpi_ieee80211_free_skb_mbuf(void *p)
4593 {
4594 	struct ieee80211_node *ni;
4595 	struct mbuf *m;
4596 
4597 	if (p == NULL)
4598 		return;
4599 
4600 	m = (struct mbuf *)p;
4601 	M_ASSERTPKTHDR(m);
4602 
4603 	ni = m->m_pkthdr.PH_loc.ptr;
4604 	m->m_pkthdr.PH_loc.ptr = NULL;
4605 	if (ni != NULL)
4606 		ieee80211_free_node(ni);
4607 	m_freem(m);
4608 }
4609 
4610 void
4611 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
4612     struct delayed_work *w, int delay)
4613 {
4614 	struct lkpi_hw *lhw;
4615 
4616 	/* Need to make sure hw is in a stable (non-suspended) state. */
4617 	IMPROVE();
4618 
4619 	lhw = HW_TO_LHW(hw);
4620 	queue_delayed_work(lhw->workq, w, delay);
4621 }
4622 
4623 void
4624 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
4625     struct work_struct *w)
4626 {
4627 	struct lkpi_hw *lhw;
4628 
4629 	/* Need to make sure hw is in a stable (non-suspended) state. */
4630 	IMPROVE();
4631 
4632 	lhw = HW_TO_LHW(hw);
4633 	queue_work(lhw->workq, w);
4634 }
4635 
4636 struct sk_buff *
4637 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
4638     uint8_t *ssid, size_t ssid_len, size_t tailroom)
4639 {
4640 	struct sk_buff *skb;
4641 	struct ieee80211_frame *wh;
4642 	uint8_t *p;
4643 	size_t len;
4644 
4645 	len = sizeof(*wh);
4646 	len += 2 + ssid_len;
4647 
4648 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
4649 	if (skb == NULL)
4650 		return (NULL);
4651 
4652 	skb_reserve(skb, hw->extra_tx_headroom);
4653 
4654 	wh = skb_put_zero(skb, sizeof(*wh));
4655 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
4656 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
4657 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4658 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
4659 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4660 
4661 	p = skb_put(skb, 2 + ssid_len);
4662 	*p++ = IEEE80211_ELEMID_SSID;
4663 	*p++ = ssid_len;
4664 	if (ssid_len > 0)
4665 		memcpy(p, ssid, ssid_len);
4666 
4667 	return (skb);
4668 }
4669 
4670 struct sk_buff *
4671 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
4672     struct ieee80211_vif *vif)
4673 {
4674 	struct lkpi_vif *lvif;
4675 	struct ieee80211vap *vap;
4676 	struct sk_buff *skb;
4677 	struct ieee80211_frame_pspoll *psp;
4678 	uint16_t v;
4679 
4680 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
4681 	if (skb == NULL)
4682 		return (NULL);
4683 
4684 	skb_reserve(skb, hw->extra_tx_headroom);
4685 
4686 	lvif = VIF_TO_LVIF(vif);
4687 	vap = LVIF_TO_VAP(lvif);
4688 
4689 	psp = skb_put_zero(skb, sizeof(*psp));
4690 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
4691 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
4692 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
4693 	memcpy(&psp->i_aid, &v, sizeof(v));
4694 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
4695 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
4696 
4697 	return (skb);
4698 }
4699 
4700 struct sk_buff *
4701 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4702     struct ieee80211_vif *vif, int linkid, bool qos)
4703 {
4704 	struct lkpi_vif *lvif;
4705 	struct ieee80211vap *vap;
4706 	struct sk_buff *skb;
4707 	struct ieee80211_frame *nullf;
4708 
4709 	IMPROVE("linkid");
4710 
4711 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
4712 	if (skb == NULL)
4713 		return (NULL);
4714 
4715 	skb_reserve(skb, hw->extra_tx_headroom);
4716 
4717 	lvif = VIF_TO_LVIF(vif);
4718 	vap = LVIF_TO_VAP(lvif);
4719 
4720 	nullf = skb_put_zero(skb, sizeof(*nullf));
4721 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
4722 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
4723 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
4724 
4725 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
4726 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
4727 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
4728 
4729 	return (skb);
4730 }
4731 
4732 struct wireless_dev *
4733 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
4734 {
4735 	struct lkpi_vif *lvif;
4736 
4737 	lvif = VIF_TO_LVIF(vif);
4738 	return (&lvif->wdev);
4739 }
4740 
4741 void
4742 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
4743 {
4744 	struct lkpi_vif *lvif;
4745 	struct ieee80211vap *vap;
4746 	enum ieee80211_state nstate;
4747 	int arg;
4748 
4749 	lvif = VIF_TO_LVIF(vif);
4750 	vap = LVIF_TO_VAP(lvif);
4751 
4752 	/*
4753 	 * Go to init; otherwise we need to elaborately check state and
4754 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4755 	 * Let the statemachine handle all neccessary changes.
4756 	 */
4757 	nstate = IEEE80211_S_INIT;
4758 	arg = 0;	/* Not a valid reason. */
4759 
4760 #ifdef LINUXKPI_DEBUG_80211
4761 	if (linuxkpi_debug_80211 & D80211_TRACE)
4762 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
4763 #endif
4764 	ieee80211_new_state(vap, nstate, arg);
4765 }
4766 
4767 void
4768 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4769 {
4770 	struct lkpi_vif *lvif;
4771 	struct ieee80211vap *vap;
4772 
4773 	lvif = VIF_TO_LVIF(vif);
4774 	vap = LVIF_TO_VAP(lvif);
4775 
4776 #ifdef LINUXKPI_DEBUG_80211
4777 	if (linuxkpi_debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4778 		ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4779 		    vif, vap, ieee80211_state_name[vap->iv_state]);
4780 #endif
4781 	ieee80211_beacon_miss(vap->iv_ic);
4782 }
4783 
4784 /* -------------------------------------------------------------------------- */
4785 
4786 void
4787 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
4788 {
4789 	struct lkpi_hw *lhw;
4790 	struct lkpi_vif *lvif;
4791 	struct ieee80211_vif *vif;
4792 	int ac_count, ac;
4793 
4794 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
4795 	    __func__, qnum, hw->queues, hw));
4796 
4797 	lhw = wiphy_priv(hw->wiphy);
4798 
4799 	/* See lkpi_ic_vap_create(). */
4800 	if (hw->queues >= IEEE80211_NUM_ACS)
4801 		ac_count = IEEE80211_NUM_ACS;
4802 	else
4803 		ac_count = 1;
4804 
4805 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4806 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4807 
4808 		vif = LVIF_TO_VIF(lvif);
4809 		for (ac = 0; ac < ac_count; ac++) {
4810 			IMPROVE_TXQ("LOCKING");
4811 			if (qnum == vif->hw_queue[ac]) {
4812 				/*
4813 				 * For now log this to better understand
4814 				 * how this is supposed to work.
4815 				 */
4816 				if (lvif->hw_queue_stopped[ac])
4817 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
4818 					    "lvif %p vif %p ac %d qnum %d already "
4819 					    "stopped\n", __func__, __LINE__,
4820 					    lhw, hw, lvif, vif, ac, qnum);
4821 				lvif->hw_queue_stopped[ac] = true;
4822 			}
4823 		}
4824 	}
4825 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4826 }
4827 
4828 void
4829 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
4830 {
4831 	int i;
4832 
4833 	IMPROVE_TXQ("Locking; do we need further info?");
4834 	for (i = 0; i < hw->queues; i++)
4835 		linuxkpi_ieee80211_stop_queue(hw, i);
4836 }
4837 
4838 
4839 static void
4840 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
4841 {
4842 	struct lkpi_hw *lhw;
4843 	struct lkpi_vif *lvif;
4844 	struct lkpi_sta *lsta;
4845 	int ac_count, ac, tid;
4846 
4847 	/* See lkpi_ic_vap_create(). */
4848 	if (hw->queues >= IEEE80211_NUM_ACS)
4849 		ac_count = IEEE80211_NUM_ACS;
4850 	else
4851 		ac_count = 1;
4852 
4853 	lhw = wiphy_priv(hw->wiphy);
4854 
4855 	IMPROVE_TXQ("Locking");
4856 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4857 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4858 		struct ieee80211_vif *vif;
4859 
4860 		vif = LVIF_TO_VIF(lvif);
4861 		for (ac = 0; ac < ac_count; ac++) {
4862 
4863 			if (hwq == vif->hw_queue[ac]) {
4864 
4865 				/* XXX-BZ what about software scan? */
4866 
4867 				/*
4868 				 * For now log this to better understand
4869 				 * how this is supposed to work.
4870 				 */
4871 				if (!lvif->hw_queue_stopped[ac])
4872 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
4873 					    "lvif %p vif %p ac %d hw_q not stopped\n",
4874 					    __func__, __LINE__,
4875 					    lhw, hw, lvif, vif, ac);
4876 				lvif->hw_queue_stopped[ac] = false;
4877 
4878 				LKPI_80211_LVIF_LOCK(lvif);
4879 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4880 					struct ieee80211_sta *sta;
4881 
4882 					sta = LSTA_TO_STA(lsta);
4883 					for (tid = 0; tid < nitems(sta->txq); tid++) {
4884 						struct lkpi_txq *ltxq;
4885 
4886 						if (sta->txq[tid] == NULL)
4887 							continue;
4888 
4889 						if (sta->txq[tid]->ac != ac)
4890 							continue;
4891 
4892 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4893 						if (!ltxq->stopped)
4894 							continue;
4895 
4896 						ltxq->stopped = false;
4897 
4898 						/* XXX-BZ see when this explodes with all the locking. taskq? */
4899 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
4900 					}
4901 				}
4902 				LKPI_80211_LVIF_UNLOCK(lvif);
4903 			}
4904 		}
4905 	}
4906 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4907 }
4908 
4909 void
4910 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
4911 {
4912 	int i;
4913 
4914 	IMPROVE_TXQ("Is this all/enough here?");
4915 	for (i = 0; i < hw->queues; i++)
4916 		lkpi_ieee80211_wake_queues(hw, i);
4917 }
4918 
4919 void
4920 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
4921 {
4922 
4923 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
4924 	    __func__, qnum, hw->queues, hw));
4925 
4926 	lkpi_ieee80211_wake_queues(hw, qnum);
4927 }
4928 
4929 /* This is just hardware queues. */
4930 void
4931 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
4932 {
4933 	struct lkpi_hw *lhw;
4934 
4935 	lhw = HW_TO_LHW(hw);
4936 
4937 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
4938 	IMPROVE_TXQ("LOCKING");
4939 	if (++lhw->txq_generation[ac] == 0)
4940 		lhw->txq_generation[ac]++;
4941 }
4942 
4943 struct ieee80211_txq *
4944 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
4945 {
4946 	struct lkpi_hw *lhw;
4947 	struct ieee80211_txq *txq;
4948 	struct lkpi_txq *ltxq;
4949 
4950 	lhw = HW_TO_LHW(hw);
4951 	txq = NULL;
4952 
4953 	IMPROVE_TXQ("LOCKING");
4954 
4955 	/* Check that we are scheduled. */
4956 	if (lhw->txq_generation[ac] == 0)
4957 		goto out;
4958 
4959 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
4960 	if (ltxq == NULL)
4961 		goto out;
4962 	if (ltxq->txq_generation == lhw->txq_generation[ac])
4963 		goto out;
4964 
4965 	ltxq->txq_generation = lhw->txq_generation[ac];
4966 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
4967 	txq = &ltxq->txq;
4968 	TAILQ_ELEM_INIT(ltxq, txq_entry);
4969 
4970 out:
4971 	return (txq);
4972 }
4973 
4974 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
4975     struct ieee80211_txq *txq, bool withoutpkts)
4976 {
4977 	struct lkpi_hw *lhw;
4978 	struct lkpi_txq *ltxq;
4979 
4980 	ltxq = TXQ_TO_LTXQ(txq);
4981 
4982 	IMPROVE_TXQ("LOCKING");
4983 
4984 	/* Only schedule if work to do or asked to anyway. */
4985 	if (!withoutpkts && skb_queue_empty(&ltxq->skbq))
4986 		goto out;
4987 
4988 	/* Make sure we do not double-schedule. */
4989 	if (ltxq->txq_entry.tqe_next != NULL)
4990 		goto out;
4991 
4992 	lhw = HW_TO_LHW(hw);
4993 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
4994 out:
4995 	return;
4996 }
4997 
4998 /* -------------------------------------------------------------------------- */
4999 
5000 struct lkpi_cfg80211_bss {
5001 	u_int refcnt;
5002 	struct cfg80211_bss bss;
5003 };
5004 
5005 struct lkpi_cfg80211_get_bss_iter_lookup {
5006 	struct wiphy *wiphy;
5007 	struct linuxkpi_ieee80211_channel *chan;
5008 	const uint8_t *bssid;
5009 	const uint8_t *ssid;
5010 	size_t ssid_len;
5011 	enum ieee80211_bss_type bss_type;
5012 	enum ieee80211_privacy privacy;
5013 
5014 	/*
5015 	 * Something to store a copy of the result as the net80211 scan cache
5016 	 * is not refoucnted so a scan entry might go away any time.
5017 	 */
5018 	bool match;
5019 	struct cfg80211_bss *bss;
5020 };
5021 
5022 static void
5023 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
5024 {
5025 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
5026 	size_t ielen;
5027 
5028 	lookup = arg;
5029 
5030 	/* Do not try to find another match. */
5031 	if (lookup->match)
5032 		return;
5033 
5034 	/* Nothing to store result. */
5035 	if (lookup->bss == NULL)
5036 		return;
5037 
5038 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
5039 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
5040 		/* We have no idea what to compare to as the drivers only request ANY */
5041 		return;
5042 	}
5043 
5044 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
5045 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
5046 		/* We have no idea what to compare to as the drivers only request ANY */
5047 		return;
5048 	}
5049 
5050 	if (lookup->chan != NULL) {
5051 		struct linuxkpi_ieee80211_channel *chan;
5052 
5053 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
5054 		    se->se_chan->ic_freq);
5055 		if (chan == NULL || chan != lookup->chan)
5056 			return;
5057 	}
5058 
5059 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
5060 		return;
5061 
5062 	if (lookup->ssid) {
5063 		if (lookup->ssid_len != se->se_ssid[1] ||
5064 		    se->se_ssid[1] == 0)
5065 			return;
5066 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
5067 			return;
5068 	}
5069 
5070 	ielen = se->se_ies.len;
5071 
5072 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
5073 	    M_LKPI80211, M_NOWAIT | M_ZERO);
5074 	if (lookup->bss->ies == NULL)
5075 		return;
5076 
5077 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
5078 	lookup->bss->ies->len = ielen;
5079 	if (ielen)
5080 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
5081 
5082 	lookup->match = true;
5083 }
5084 
5085 struct cfg80211_bss *
5086 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
5087     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
5088     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
5089 {
5090 	struct lkpi_cfg80211_bss *lbss;
5091 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
5092 	struct lkpi_hw *lhw;
5093 	struct ieee80211vap *vap;
5094 
5095 	lhw = wiphy_priv(wiphy);
5096 
5097 	/* Let's hope we can alloc. */
5098 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
5099 	if (lbss == NULL) {
5100 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
5101 		return (NULL);
5102 	}
5103 
5104 	lookup.wiphy = wiphy;
5105 	lookup.chan = chan;
5106 	lookup.bssid = bssid;
5107 	lookup.ssid = ssid;
5108 	lookup.ssid_len = ssid_len;
5109 	lookup.bss_type = bss_type;
5110 	lookup.privacy = privacy;
5111 	lookup.match = false;
5112 	lookup.bss = &lbss->bss;
5113 
5114 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
5115 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
5116 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
5117 	if (!lookup.match) {
5118 		free(lbss, M_LKPI80211);
5119 		return (NULL);
5120 	}
5121 
5122 	refcount_init(&lbss->refcnt, 1);
5123 	return (&lbss->bss);
5124 }
5125 
5126 void
5127 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
5128 {
5129 	struct lkpi_cfg80211_bss *lbss;
5130 
5131 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
5132 
5133 	/* Free everything again on refcount ... */
5134 	if (refcount_release(&lbss->refcnt)) {
5135 		free(lbss->bss.ies, M_LKPI80211);
5136 		free(lbss, M_LKPI80211);
5137 	}
5138 }
5139 
5140 void
5141 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
5142 {
5143 	struct lkpi_hw *lhw;
5144 	struct ieee80211com *ic;
5145 	struct ieee80211vap *vap;
5146 
5147 	lhw = wiphy_priv(wiphy);
5148 	ic = lhw->ic;
5149 
5150 	/*
5151 	 * If we haven't called ieee80211_ifattach() yet
5152 	 * or there is no VAP, there are no scans to flush.
5153 	 */
5154 	if (ic == NULL ||
5155 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
5156 		return;
5157 
5158 	/* Should only happen on the current one? Not seen it late enough. */
5159 	IEEE80211_LOCK(ic);
5160 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
5161 		ieee80211_scan_flush(vap);
5162 	IEEE80211_UNLOCK(ic);
5163 }
5164 
5165 /* -------------------------------------------------------------------------- */
5166 
5167 MODULE_VERSION(linuxkpi_wlan, 1);
5168 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
5169 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
5170