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