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