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