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