1 /*-
2  * Copyright (c) 2020-2023 The FreeBSD Foundation
3  * Copyright (c) 2020-2022 Bjoern A. Zeeb
4  *
5  * This software was developed by Björn Zeeb under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Public functions are called linuxkpi_*().
32  * Internal (static) functions are called lkpi_*().
33  *
34  * The internal structures holding metadata over public structures are also
35  * called lkpi_xxx (usually with a member at the end called xxx).
36  * Note: we do not replicate the structure names but the general variable names
37  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
38  * There are macros to access one from the other.
39  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
40  */
41 
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/queue.h>
53 #include <sys/taskqueue.h>
54 #include <sys/libkern.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_proto.h>
63 #include <net80211/ieee80211_ratectl.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_vht.h>
66 
67 #define	LINUXKPI_NET80211
68 #include <net/mac80211.h>
69 
70 #include <linux/workqueue.h>
71 #include "linux_80211.h"
72 
73 #define	LKPI_80211_WME
74 /* #define	LKPI_80211_HW_CRYPTO */
75 /* #define	LKPI_80211_VHT */
76 /* #define	LKPI_80211_HT */
77 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
78 #define	LKPI_80211_HT
79 #endif
80 
81 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
82 
83 /* XXX-BZ really want this and others in queue.h */
84 #define	TAILQ_ELEM_INIT(elm, field) do {				\
85 	(elm)->field.tqe_next = NULL;					\
86 	(elm)->field.tqe_prev = NULL;					\
87 } while (0)
88 
89 /* -------------------------------------------------------------------------- */
90 
91 /* Keep public for as long as header files are using it too. */
92 int linuxkpi_debug_80211;
93 
94 #ifdef LINUXKPI_DEBUG_80211
95 SYSCTL_DECL(_compat_linuxkpi);
96 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
97     "LinuxKPI 802.11 compatibility layer");
98 
99 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
100     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
101 
102 #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
103     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
104 #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
105     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
106 #else
107 #define	UNIMPLEMENTED		do { } while (0)
108 #define	TRACEOK()		do { } while (0)
109 #endif
110 
111 /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
112 #ifndef PREP_TX_INFO_DURATION
113 #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
114 #endif
115 
116 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
117 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
118 
119 /* IEEE 802.11-05/0257r1 */
120 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
121 
122 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
123 static const uint8_t ieee80211e_up_to_ac[] = {
124 	IEEE80211_AC_BE,
125 	IEEE80211_AC_BK,
126 	IEEE80211_AC_BK,
127 	IEEE80211_AC_BE,
128 	IEEE80211_AC_VI,
129 	IEEE80211_AC_VI,
130 	IEEE80211_AC_VO,
131 	IEEE80211_AC_VO,
132 #if 0
133 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
134 #endif
135 };
136 
137 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
138 	/*
139 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
140 	 * mac80211 and to the driver or net80211.
141 	 * Can we pass some on 1:1? Need to compare the (*f)().
142 	 */
143 };
144 
145 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
146     struct ieee80211_node *);
147 static void lkpi_80211_txq_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_CIPHER_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 	/*
4276 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
4277 	 * not initialized.
4278 	 */
4279 	hw = LHW_TO_HW(lhw);
4280 	hw->wiphy = wiphy;
4281 	hw->conf.flags |= IEEE80211_CONF_IDLE;
4282 	hw->priv = (void *)(lhw + 1);
4283 
4284 	/* BSD Specific. */
4285 	lhw->ic = lkpi_ieee80211_ifalloc();
4286 	if (lhw->ic == NULL) {
4287 		ieee80211_free_hw(hw);
4288 		return (NULL);
4289 	}
4290 
4291 	IMPROVE();
4292 
4293 	return (hw);
4294 }
4295 
4296 void
4297 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
4298 {
4299 	struct lkpi_hw *lhw;
4300 
4301 	lhw = HW_TO_LHW(hw);
4302 	free(lhw->ic, M_LKPI80211);
4303 	lhw->ic = NULL;
4304 
4305 	/* Cleanup more of lhw here or in wiphy_free()? */
4306 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
4307 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4308 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4309 	sx_destroy(&lhw->lvif_sx);
4310 	IMPROVE();
4311 }
4312 
4313 void
4314 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
4315 {
4316 	struct lkpi_hw *lhw;
4317 	struct ieee80211com *ic;
4318 
4319 	lhw = HW_TO_LHW(hw);
4320 	ic = lhw->ic;
4321 
4322 	/* Now set a proper name before ieee80211_ifattach(). */
4323 	ic->ic_softc = lhw;
4324 	ic->ic_name = name;
4325 
4326 	/* XXX-BZ do we also need to set wiphy name? */
4327 }
4328 
4329 struct ieee80211_hw *
4330 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
4331 {
4332 	struct lkpi_hw *lhw;
4333 
4334 	lhw = wiphy_priv(wiphy);
4335 	return (LHW_TO_HW(lhw));
4336 }
4337 
4338 static void
4339 lkpi_radiotap_attach(struct lkpi_hw *lhw)
4340 {
4341 	struct ieee80211com *ic;
4342 
4343 	ic = lhw->ic;
4344 	ieee80211_radiotap_attach(ic,
4345 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
4346 	    LKPI_RTAP_TX_FLAGS_PRESENT,
4347 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
4348 	    LKPI_RTAP_RX_FLAGS_PRESENT);
4349 }
4350 
4351 int
4352 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
4353 {
4354 	struct ieee80211com *ic;
4355 	struct lkpi_hw *lhw;
4356 	int band, i;
4357 
4358 	lhw = HW_TO_LHW(hw);
4359 	ic = lhw->ic;
4360 
4361 	/* We do it this late as wiphy->dev should be set for the name. */
4362 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4363 	if (lhw->workq == NULL)
4364 		return (-EAGAIN);
4365 
4366 	/* XXX-BZ figure this out how they count his... */
4367 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
4368 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4369 		    hw->wiphy->perm_addr);
4370 	} else if (hw->wiphy->n_addresses > 0) {
4371 		/* We take the first one. */
4372 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4373 		    hw->wiphy->addresses[0].addr);
4374 	} else {
4375 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
4376 	}
4377 
4378 #ifdef __not_yet__
4379 	/* See comment in lkpi_80211_txq_tx_one(). */
4380 	ic->ic_headroom = hw->extra_tx_headroom;
4381 #endif
4382 
4383 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
4384 	ic->ic_opmode = IEEE80211_M_STA;
4385 
4386 	/* Set device capabilities. */
4387 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
4388 	ic->ic_caps =
4389 	    IEEE80211_C_STA |
4390 	    IEEE80211_C_MONITOR |
4391 	    IEEE80211_C_WPA |		/* WPA/RSN */
4392 #ifdef LKPI_80211_WME
4393 	    IEEE80211_C_WME |
4394 #endif
4395 #if 0
4396 	    IEEE80211_C_PMGT |
4397 #endif
4398 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
4399 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
4400 	    ;
4401 #if 0
4402 	/* Scanning is a different kind of beast to re-work. */
4403 	ic->ic_caps |= IEEE80211_C_BGSCAN;
4404 #endif
4405 	if (lhw->ops->hw_scan) {
4406 		/*
4407 		 * Advertise full-offload scanning.
4408 		 *
4409 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4410 		 * we essentially disable hw_scan for all drivers not setting
4411 		 * the flag.
4412 		 */
4413 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4414 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4415 	}
4416 
4417 	/*
4418 	 * The wiphy variables report bitmasks of avail antennas.
4419 	 * (*get_antenna) get the current bitmask sets which can be
4420 	 * altered by (*set_antenna) for some drivers.
4421 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4422 	 */
4423 	if (hw->wiphy->available_antennas_rx ||
4424 	    hw->wiphy->available_antennas_tx) {
4425 		uint32_t rxs, txs;
4426 
4427 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4428 			ic->ic_rxstream = bitcount32(rxs);
4429 			ic->ic_txstream = bitcount32(txs);
4430 		}
4431 	}
4432 
4433 	ic->ic_cryptocaps = 0;
4434 #ifdef LKPI_80211_HW_CRYPTO
4435 	if (hw->wiphy->n_cipher_suites > 0) {
4436 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
4437 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
4438 			    hw->wiphy->cipher_suites[i]);
4439 	}
4440 #endif
4441 
4442 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
4443 	    ic->ic_channels);
4444 
4445 	ieee80211_ifattach(ic);
4446 
4447 	ic->ic_update_mcast = lkpi_ic_update_mcast;
4448 	ic->ic_update_promisc = lkpi_ic_update_promisc;
4449 	ic->ic_update_chw = lkpi_ic_update_chw;
4450 	ic->ic_parent = lkpi_ic_parent;
4451 	ic->ic_scan_start = lkpi_ic_scan_start;
4452 	ic->ic_scan_end = lkpi_ic_scan_end;
4453 	ic->ic_set_channel = lkpi_ic_set_channel;
4454 	ic->ic_transmit = lkpi_ic_transmit;
4455 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
4456 	ic->ic_vap_create = lkpi_ic_vap_create;
4457 	ic->ic_vap_delete = lkpi_ic_vap_delete;
4458 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
4459 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
4460 
4461 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4462 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4463 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4464 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4465 
4466 	lhw->ic_node_alloc = ic->ic_node_alloc;
4467 	ic->ic_node_alloc = lkpi_ic_node_alloc;
4468 	lhw->ic_node_init = ic->ic_node_init;
4469 	ic->ic_node_init = lkpi_ic_node_init;
4470 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
4471 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
4472 	lhw->ic_node_free = ic->ic_node_free;
4473 	ic->ic_node_free = lkpi_ic_node_free;
4474 
4475 #ifdef LKPI_80211_HT
4476 	lhw->ic_recv_action = ic->ic_recv_action;
4477 	ic->ic_recv_action = lkpi_ic_recv_action;
4478 	lhw->ic_send_action = ic->ic_send_action;
4479 	ic->ic_send_action = lkpi_ic_send_action;
4480 
4481 	lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
4482 	ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
4483 
4484 	lhw->ic_addba_request = ic->ic_addba_request;
4485 	ic->ic_addba_request = lkpi_ic_addba_request;
4486 	lhw->ic_addba_response = ic->ic_addba_response;
4487 	ic->ic_addba_response = lkpi_ic_addba_response;
4488 	lhw->ic_addba_stop = ic->ic_addba_stop;
4489 	ic->ic_addba_stop = lkpi_ic_addba_stop;
4490 	lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
4491 	ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
4492 
4493 	lhw->ic_bar_response = ic->ic_bar_response;
4494 	ic->ic_bar_response = lkpi_ic_bar_response;
4495 
4496 	lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
4497 	ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
4498 	lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
4499 	ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
4500 #endif
4501 
4502 	lkpi_radiotap_attach(lhw);
4503 
4504 	/*
4505 	 * Assign the first possible channel for now;  seems Realtek drivers
4506 	 * expect one.
4507 	 * Also remember the amount of bands we support and the most rates
4508 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4509 	 */
4510 	lhw->supbands = lhw->max_rates = 0;
4511 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4512 		struct ieee80211_supported_band *supband;
4513 		struct linuxkpi_ieee80211_channel *channels;
4514 
4515 		supband = hw->wiphy->bands[band];
4516 		if (supband == NULL || supband->n_channels == 0)
4517 			continue;
4518 
4519 		lhw->supbands++;
4520 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4521 
4522 		/* If we have a channel, we need to keep counting supbands. */
4523 		if (hw->conf.chandef.chan != NULL)
4524 			continue;
4525 
4526 		channels = supband->channels;
4527 		for (i = 0; i < supband->n_channels; i++) {
4528 
4529 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4530 				continue;
4531 
4532 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
4533 #ifdef LKPI_80211_HT
4534 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
4535 #endif
4536 			    NL80211_CHAN_NO_HT);
4537 			break;
4538 		}
4539 	}
4540 
4541 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4542 
4543 	/* Make sure we do not support more than net80211 is willing to take. */
4544 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4545 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4546 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4547 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4548 	}
4549 
4550 	/*
4551 	 * The maximum supported bitrates on any band + size for
4552 	 * DSSS Parameter Set give our per-band IE size.
4553 	 * SSID is the responsibility of the driver and goes on the side.
4554 	 * The user specified bits coming from the vap go into the
4555 	 * "common ies" fields.
4556 	 */
4557 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4558 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4559 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
4560 
4561 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4562 		/*
4563 		 * net80211 does not seem to support the DSSS Parameter Set but
4564 		 * some of the drivers insert it so calculate the extra fixed
4565 		 * space in.
4566 		 */
4567 		lhw->scan_ie_len += 2 + 1;
4568 	}
4569 
4570 #if defined(LKPI_80211_HT)
4571 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
4572 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
4573 #endif
4574 #if defined(LKPI_80211_VHT)
4575 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
4576 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
4577 #endif
4578 
4579 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
4580 	if (hw->wiphy->max_scan_ie_len > 0) {
4581 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
4582 			goto err;
4583 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
4584 	}
4585 
4586 	if (bootverbose)
4587 		ieee80211_announce(ic);
4588 
4589 	return (0);
4590 err:
4591 	IMPROVE("TODO FIXME CLEANUP");
4592 	return (-EAGAIN);
4593 }
4594 
4595 void
4596 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
4597 {
4598 	struct lkpi_hw *lhw;
4599 	struct ieee80211com *ic;
4600 
4601 	lhw = HW_TO_LHW(hw);
4602 	ic = lhw->ic;
4603 	ieee80211_ifdetach(ic);
4604 }
4605 
4606 void
4607 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
4608     enum ieee80211_iface_iter flags,
4609     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
4610     void *arg)
4611 {
4612 	struct lkpi_hw *lhw;
4613 	struct lkpi_vif *lvif;
4614 	struct ieee80211_vif *vif;
4615 	bool active, atomic, nin_drv;
4616 
4617 	lhw = HW_TO_LHW(hw);
4618 
4619 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
4620 	    IEEE80211_IFACE_ITER_RESUME_ALL|
4621 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4622 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
4623 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
4624 		    __func__, flags);
4625 	}
4626 
4627 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
4628 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
4629 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
4630 
4631 	if (atomic)
4632 		LKPI_80211_LHW_LVIF_LOCK(lhw);
4633 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4634 		struct ieee80211vap *vap;
4635 
4636 		vif = LVIF_TO_VIF(lvif);
4637 
4638 		/*
4639 		 * If we want "active" interfaces, we need to distinguish on
4640 		 * whether the driver knows about them or not to be able to
4641 		 * handle the "resume" case correctly.  Skip the ones the
4642 		 * driver does not know about.
4643 		 */
4644 		if (active && !lvif->added_to_drv &&
4645 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
4646 			continue;
4647 
4648 		/*
4649 		 * If we shall skip interfaces not added to the driver do so
4650 		 * if we haven't yet.
4651 		 */
4652 		if (nin_drv && !lvif->added_to_drv)
4653 			continue;
4654 
4655 		/*
4656 		 * Run the iterator function if we are either not asking
4657 		 * asking for active only or if the VAP is "running".
4658 		 */
4659 		/* XXX-BZ probably should have state in the lvif as well. */
4660 		vap = LVIF_TO_VAP(lvif);
4661 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
4662 			iterfunc(arg, vif->addr, vif);
4663 	}
4664 	if (atomic)
4665 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4666 }
4667 
4668 void
4669 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
4670     struct ieee80211_vif *vif,
4671     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
4672         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
4673     void *arg)
4674 {
4675 
4676 	UNIMPLEMENTED;
4677 }
4678 
4679 void
4680 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
4681     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
4682 	void *),
4683     void *arg)
4684 {
4685 	struct lkpi_hw *lhw;
4686 	struct lkpi_vif *lvif;
4687 	struct ieee80211_vif *vif;
4688 	struct lkpi_chanctx *lchanctx;
4689 
4690 	KASSERT(hw != NULL && iterfunc != NULL,
4691 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4692 
4693 	lhw = HW_TO_LHW(hw);
4694 
4695 	IMPROVE("lchanctx should be its own list somewhere");
4696 
4697 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4698 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4699 
4700 		vif = LVIF_TO_VIF(lvif);
4701 		if (vif->chanctx_conf == NULL)
4702 			continue;
4703 
4704 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4705 		if (!lchanctx->added_to_drv)
4706 			continue;
4707 
4708 		iterfunc(hw, &lchanctx->conf, arg);
4709 	}
4710 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4711 }
4712 
4713 void
4714 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
4715    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
4716 {
4717 	struct lkpi_hw *lhw;
4718 	struct lkpi_vif *lvif;
4719 	struct lkpi_sta *lsta;
4720 	struct ieee80211_sta *sta;
4721 
4722 	KASSERT(hw != NULL && iterfunc != NULL,
4723 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4724 
4725 	lhw = HW_TO_LHW(hw);
4726 
4727 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4728 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4729 
4730 		LKPI_80211_LVIF_LOCK(lvif);
4731 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4732 			if (!lsta->added_to_drv)
4733 				continue;
4734 			sta = LSTA_TO_STA(lsta);
4735 			iterfunc(arg, sta);
4736 		}
4737 		LKPI_80211_LVIF_UNLOCK(lvif);
4738 	}
4739 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4740 }
4741 
4742 struct linuxkpi_ieee80211_regdomain *
4743 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4744 {
4745 	struct linuxkpi_ieee80211_regdomain *regd;
4746 
4747 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4748 	    GFP_KERNEL);
4749 	return (regd);
4750 }
4751 
4752 int
4753 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
4754     struct linuxkpi_ieee80211_regdomain *regd)
4755 {
4756 	struct lkpi_hw *lhw;
4757 	struct ieee80211com *ic;
4758 	struct ieee80211_regdomain *rd;
4759 
4760 	lhw = wiphy_priv(wiphy);
4761 	ic = lhw->ic;
4762 
4763 	rd = &ic->ic_regdomain;
4764 	if (rd->isocc[0] == '\0') {
4765 		rd->isocc[0] = regd->alpha2[0];
4766 		rd->isocc[1] = regd->alpha2[1];
4767 	}
4768 
4769 	TODO();
4770 	/* XXX-BZ finish the rest. */
4771 
4772 	return (0);
4773 }
4774 
4775 void
4776 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
4777     struct cfg80211_scan_info *info)
4778 {
4779 	struct lkpi_hw *lhw;
4780 	struct ieee80211com *ic;
4781 	struct ieee80211_scan_state *ss;
4782 
4783 	lhw = wiphy_priv(hw->wiphy);
4784 	ic = lhw->ic;
4785 	ss = ic->ic_scan;
4786 
4787 	ieee80211_scan_done(ss->ss_vap);
4788 
4789 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4790 	free(lhw->hw_req, M_LKPI80211);
4791 	lhw->hw_req = NULL;
4792 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4793 	wakeup(lhw);
4794 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4795 
4796 	return;
4797 }
4798 
4799 /* For %list see comment towards the end of the function. */
4800 void
4801 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4802     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4803     struct list_head *list __unused)
4804 {
4805 	struct lkpi_hw *lhw;
4806 	struct ieee80211com *ic;
4807 	struct mbuf *m;
4808 	struct skb_shared_info *shinfo;
4809 	struct ieee80211_rx_status *rx_status;
4810 	struct ieee80211_rx_stats rx_stats;
4811 	struct ieee80211_node *ni;
4812 	struct ieee80211vap *vap;
4813 	struct ieee80211_hdr *hdr;
4814 	struct lkpi_sta *lsta;
4815 	int i, offset, ok;
4816 	int8_t rssi;
4817 	bool is_beacon;
4818 
4819 	if (skb->len < 2) {
4820 		/* Need 80211 stats here. */
4821 		IMPROVE();
4822 		goto err;
4823 	}
4824 
4825 	/*
4826 	 * For now do the data copy; we can later improve things. Might even
4827 	 * have an mbuf backing the skb data then?
4828 	 */
4829 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
4830 	if (m == NULL)
4831 		goto err;
4832 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
4833 
4834 	shinfo = skb_shinfo(skb);
4835 	offset = m->m_len;
4836 	for (i = 0; i < shinfo->nr_frags; i++) {
4837 		m_copyback(m, offset, shinfo->frags[i].size,
4838 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
4839 		    shinfo->frags[i].offset);
4840 		offset += shinfo->frags[i].size;
4841 	}
4842 
4843 	rx_status = IEEE80211_SKB_RXCB(skb);
4844 
4845 	hdr = (void *)skb->data;
4846 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4847 
4848 #ifdef LINUXKPI_DEBUG_80211
4849 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
4850 		goto no_trace_beacons;
4851 
4852 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4853 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4854 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
4855 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
4856 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
4857 		    shinfo, shinfo->nr_frags,
4858 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
4859 
4860 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
4861 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
4862 
4863 	/* Implement a dump_rxcb() !!! */
4864 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4865 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
4866 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
4867 			__func__,
4868 			(uintmax_t)rx_status->boottime_ns,
4869 			(uintmax_t)rx_status->mactime,
4870 			rx_status->device_timestamp,
4871 			rx_status->flag,
4872 			rx_status->freq,
4873 			rx_status->bw,
4874 			rx_status->encoding,
4875 			rx_status->ampdu_reference,
4876 			rx_status->band,
4877 			rx_status->chains,
4878 			rx_status->chain_signal[0],
4879 			rx_status->chain_signal[1],
4880 			rx_status->chain_signal[2],
4881 			rx_status->chain_signal[3],
4882 			rx_status->signal,
4883 			rx_status->enc_flags,
4884 			rx_status->he_dcm,
4885 			rx_status->he_gi,
4886 			rx_status->he_ru,
4887 			rx_status->zero_length_psdu_type,
4888 			rx_status->nss,
4889 			rx_status->rate_idx);
4890 no_trace_beacons:
4891 #endif
4892 
4893 	memset(&rx_stats, 0, sizeof(rx_stats));
4894 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
4895 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
4896 	rx_stats.c_nf = -96;
4897 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
4898 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4899 		rssi = rx_status->signal;
4900 	else
4901 		rssi = rx_stats.c_nf;
4902 	/*
4903 	 * net80211 signal strength data are in .5 dBm units relative to
4904 	 * the current noise floor (see comment in ieee80211_node.h).
4905 	 */
4906 	rssi -= rx_stats.c_nf;
4907 	rx_stats.c_rssi = rssi * 2;
4908 	rx_stats.r_flags |= IEEE80211_R_BAND;
4909 	rx_stats.c_band =
4910 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
4911 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
4912 	rx_stats.c_freq = rx_status->freq;
4913 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
4914 
4915 	/* XXX (*sta_statistics)() to get to some of that? */
4916 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
4917 
4918 	lhw = HW_TO_LHW(hw);
4919 	ic = lhw->ic;
4920 
4921 	ok = ieee80211_add_rx_params(m, &rx_stats);
4922 	if (ok == 0) {
4923 		m_freem(m);
4924 		counter_u64_add(ic->ic_ierrors, 1);
4925 		goto err;
4926 	}
4927 
4928 	if (sta != NULL) {
4929 		lsta = STA_TO_LSTA(sta);
4930 		ni = ieee80211_ref_node(lsta->ni);
4931 	} else {
4932 		struct ieee80211_frame_min *wh;
4933 
4934 		wh = mtod(m, struct ieee80211_frame_min *);
4935 		ni = ieee80211_find_rxnode(ic, wh);
4936 		if (ni != NULL)
4937 			lsta = ni->ni_drv_data;
4938 	}
4939 
4940 	if (ni != NULL)
4941 		vap = ni->ni_vap;
4942 	else
4943 		/*
4944 		 * XXX-BZ can we improve this by looking at the frame hdr
4945 		 * or other meta-data passed up?
4946 		 */
4947 		vap = TAILQ_FIRST(&ic->ic_vaps);
4948 
4949 #ifdef LINUXKPI_DEBUG_80211
4950 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4951 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
4952 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
4953 		    ni, vap, is_beacon ? " beacon" : "");
4954 #endif
4955 
4956 	if (ni != NULL && vap != NULL && is_beacon &&
4957 	    rx_status->device_timestamp > 0 &&
4958 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4959 		struct lkpi_vif *lvif;
4960 		struct ieee80211_vif *vif;
4961 		struct ieee80211_frame *wh;
4962 
4963 		wh = mtod(m, struct ieee80211_frame *);
4964 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4965 			goto skip_device_ts;
4966 
4967 		lvif = VAP_TO_LVIF(vap);
4968 		vif = LVIF_TO_VIF(lvif);
4969 
4970 		IMPROVE("TIMING_BEACON_ONLY?");
4971 		/* mac80211 specific (not net80211) so keep it here. */
4972 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4973 		/*
4974 		 * net80211 should take care of the other information (sync_tsf,
4975 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4976 		 */
4977 skip_device_ts:
4978 		;
4979 	}
4980 
4981 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
4982 	    ieee80211_radiotap_active_vap(vap)) {
4983 		struct lkpi_radiotap_rx_hdr *rtap;
4984 
4985 		rtap = &lhw->rtap_rx;
4986 		rtap->wr_tsft = rx_status->device_timestamp;
4987 		rtap->wr_flags = 0;
4988 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
4989 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4990 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4991 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4992 #if 0	/* .. or it does not given we strip it below. */
4993 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
4994 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
4995 #endif
4996 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
4997 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
4998 		rtap->wr_rate = 0;
4999 		IMPROVE();
5000 		/* XXX TODO status->encoding / rate_index / bw */
5001 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
5002 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
5003 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5004 		rtap->wr_dbm_antsignal = rssi;
5005 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
5006 	}
5007 
5008 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5009 		m_adj(m, -IEEE80211_CRC_LEN);
5010 
5011 #if 0
5012 	if (list != NULL) {
5013 		/*
5014 		* Normally this would be queued up and delivered by
5015 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5016 		* See mt76::mac80211.c as only current possible consumer.
5017 		*/
5018 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5019 	}
5020 #endif
5021 
5022 	if (ni != NULL) {
5023 		ok = ieee80211_input_mimo(ni, m);
5024 		ieee80211_free_node(ni);
5025 		if (ok < 0)
5026 			m_freem(m);
5027 	} else {
5028 		ok = ieee80211_input_mimo_all(ic, m);
5029 		/* mbuf got consumed. */
5030 	}
5031 
5032 #ifdef LINUXKPI_DEBUG_80211
5033 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5034 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
5035 #endif
5036 
5037 	IMPROVE();
5038 
5039 err:
5040 	/* The skb is ours so we can free it :-) */
5041 	kfree_skb(skb);
5042 }
5043 
5044 uint8_t
5045 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
5046 {
5047 	const struct ieee80211_frame *wh;
5048 	uint8_t tid;
5049 
5050 	/* Linux seems to assume this is a QOS-Data-Frame */
5051 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5052 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5053 	   hdr->frame_control));
5054 
5055 	wh = (const struct ieee80211_frame *)hdr;
5056 	tid = ieee80211_gettid(wh);
5057 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5058 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5059 
5060 	return (tid);
5061 }
5062 
5063 struct wiphy *
5064 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
5065 {
5066 	struct lkpi_wiphy *lwiphy;
5067 
5068 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
5069 	if (lwiphy == NULL)
5070 		return (NULL);
5071 	lwiphy->ops = ops;
5072 
5073 	/* XXX TODO */
5074 	return (LWIPHY_TO_WIPHY(lwiphy));
5075 }
5076 
5077 void
5078 linuxkpi_wiphy_free(struct wiphy *wiphy)
5079 {
5080 	struct lkpi_wiphy *lwiphy;
5081 
5082 	if (wiphy == NULL)
5083 		return;
5084 
5085 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5086 	kfree(lwiphy);
5087 }
5088 
5089 uint32_t
5090 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
5091     enum nl80211_band band)
5092 {
5093 
5094 	switch (band) {
5095 	case NL80211_BAND_2GHZ:
5096 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
5097 		break;
5098 	case NL80211_BAND_5GHZ:
5099 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
5100 		break;
5101 	default:
5102 		/* XXX abort, retry, error, panic? */
5103 		break;
5104 	}
5105 
5106 	return (0);
5107 }
5108 
5109 uint32_t
5110 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
5111 {
5112 
5113 	return (ieee80211_mhz2ieee(freq, 0));
5114 }
5115 
5116 static struct lkpi_sta *
5117 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
5118 {
5119 	struct lkpi_sta *lsta, *temp;
5120 
5121 	LKPI_80211_LVIF_LOCK(lvif);
5122 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5123 		if (lsta->ni == ni) {
5124 			LKPI_80211_LVIF_UNLOCK(lvif);
5125 			return (lsta);
5126 		}
5127 	}
5128 	LKPI_80211_LVIF_UNLOCK(lvif);
5129 
5130 	return (NULL);
5131 }
5132 
5133 struct ieee80211_sta *
5134 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
5135 {
5136 	struct lkpi_vif *lvif;
5137 	struct lkpi_sta *lsta, *temp;
5138 	struct ieee80211_sta *sta;
5139 
5140 	lvif = VIF_TO_LVIF(vif);
5141 
5142 	LKPI_80211_LVIF_LOCK(lvif);
5143 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5144 		sta = LSTA_TO_STA(lsta);
5145 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
5146 			LKPI_80211_LVIF_UNLOCK(lvif);
5147 			return (sta);
5148 		}
5149 	}
5150 	LKPI_80211_LVIF_UNLOCK(lvif);
5151 	return (NULL);
5152 }
5153 
5154 struct ieee80211_sta *
5155 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
5156     const uint8_t *addr, const uint8_t *ourvifaddr)
5157 {
5158 	struct lkpi_hw *lhw;
5159 	struct lkpi_vif *lvif;
5160 	struct lkpi_sta *lsta;
5161 	struct ieee80211_vif *vif;
5162 	struct ieee80211_sta *sta;
5163 
5164 	lhw = wiphy_priv(hw->wiphy);
5165 	sta = NULL;
5166 
5167 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5168 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5169 
5170 		/* XXX-BZ check our address from the vif. */
5171 
5172 		vif = LVIF_TO_VIF(lvif);
5173 		if (ourvifaddr != NULL &&
5174 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
5175 			continue;
5176 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
5177 		if (sta != NULL)
5178 			break;
5179 	}
5180 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5181 
5182 	if (sta != NULL) {
5183 		lsta = STA_TO_LSTA(sta);
5184 		if (!lsta->added_to_drv)
5185 			return (NULL);
5186 	}
5187 
5188 	return (sta);
5189 }
5190 
5191 struct sk_buff *
5192 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
5193     struct ieee80211_txq *txq)
5194 {
5195 	struct lkpi_txq *ltxq;
5196 	struct lkpi_vif *lvif;
5197 	struct sk_buff *skb;
5198 
5199 	skb = NULL;
5200 	ltxq = TXQ_TO_LTXQ(txq);
5201 	ltxq->seen_dequeue = true;
5202 
5203 	if (ltxq->stopped)
5204 		goto stopped;
5205 
5206 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
5207 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
5208 		ltxq->stopped = true;
5209 		goto stopped;
5210 	}
5211 
5212 	IMPROVE("hw(TX_FRAG_LIST)");
5213 
5214 	LKPI_80211_LTXQ_LOCK(ltxq);
5215 	skb = skb_dequeue(&ltxq->skbq);
5216 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5217 
5218 stopped:
5219 	return (skb);
5220 }
5221 
5222 void
5223 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
5224     unsigned long *frame_cnt, unsigned long *byte_cnt)
5225 {
5226 	struct lkpi_txq *ltxq;
5227 	struct sk_buff *skb;
5228 	unsigned long fc, bc;
5229 
5230 	ltxq = TXQ_TO_LTXQ(txq);
5231 
5232 	fc = bc = 0;
5233 	LKPI_80211_LTXQ_LOCK(ltxq);
5234 	skb_queue_walk(&ltxq->skbq, skb) {
5235 		fc++;
5236 		bc += skb->len;
5237 	}
5238 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5239 	if (frame_cnt)
5240 		*frame_cnt = fc;
5241 	if (byte_cnt)
5242 		*byte_cnt = bc;
5243 
5244 	/* Validate that this is doing the correct thing. */
5245 	/* Should we keep track on en/dequeue? */
5246 	IMPROVE();
5247 }
5248 
5249 /*
5250  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
5251  * The latter tries to derive the success status from the info flags
5252  * passed back from the driver.  rawx_mit() saves the ni on the m and the
5253  * m on the skb for us to be able to give feedback to net80211.
5254  */
5255 static void
5256 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5257     int status)
5258 {
5259 	struct ieee80211_node *ni;
5260 	struct mbuf *m;
5261 
5262 	m = skb->m;
5263 	skb->m = NULL;
5264 
5265 	if (m != NULL) {
5266 		ni = m->m_pkthdr.PH_loc.ptr;
5267 		/* Status: 0 is ok, != 0 is error. */
5268 		ieee80211_tx_complete(ni, m, status);
5269 		/* ni & mbuf were consumed. */
5270 	}
5271 }
5272 
5273 void
5274 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5275     int status)
5276 {
5277 
5278 	_lkpi_ieee80211_free_txskb(hw, skb, status);
5279 	kfree_skb(skb);
5280 }
5281 
5282 void
5283 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5284     struct ieee80211_tx_status *txstat)
5285 {
5286 	struct sk_buff *skb;
5287 	struct ieee80211_tx_info *info;
5288 	struct ieee80211_ratectl_tx_status txs;
5289 	struct ieee80211_node *ni;
5290 	int status;
5291 
5292 	skb = txstat->skb;
5293 	if (skb->m != NULL) {
5294 		struct mbuf *m;
5295 
5296 		m = skb->m;
5297 		ni = m->m_pkthdr.PH_loc.ptr;
5298 		memset(&txs, 0, sizeof(txs));
5299 	} else {
5300 		ni = NULL;
5301 	}
5302 
5303 	info = txstat->info;
5304 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5305 		status = 0;	/* No error. */
5306 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5307 	} else {
5308 		status = 1;
5309 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5310 	}
5311 
5312 	if (ni != NULL) {
5313 		int ridx __unused;
5314 #ifdef LINUXKPI_DEBUG_80211
5315 		int old_rate;
5316 
5317 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5318 #endif
5319 		txs.pktlen = skb->len;
5320 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5321 		if (info->status.rates[0].count > 1) {
5322 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5323 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5324 		}
5325 #if 0		/* Unused in net80211 currently. */
5326 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5327 		txs.final_rate = info->status.rates[0].idx;
5328 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5329 #endif
5330 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5331 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5332 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5333 		}
5334 
5335 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5336 		ieee80211_ratectl_tx_complete(ni, &txs);
5337 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5338 
5339 #ifdef LINUXKPI_DEBUG_80211
5340 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5341 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5342 			    "long_retries %d\n", __func__,
5343 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5344 			    ridx, txs.long_retries);
5345 		}
5346 #endif
5347 	}
5348 
5349 #ifdef LINUXKPI_DEBUG_80211
5350 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5351 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5352 		    "band %u hw_queue %u tx_time_est %d : "
5353 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5354 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5355 		    "tx_time %u flags %#x "
5356 		    "status_driver_data [ %p %p ]\n",
5357 		    __func__, hw, skb, status, info->flags,
5358 		    info->band, info->hw_queue, info->tx_time_est,
5359 		    info->status.rates[0].idx, info->status.rates[0].count,
5360 		    info->status.rates[0].flags,
5361 		    info->status.rates[1].idx, info->status.rates[1].count,
5362 		    info->status.rates[1].flags,
5363 		    info->status.rates[2].idx, info->status.rates[2].count,
5364 		    info->status.rates[2].flags,
5365 		    info->status.rates[3].idx, info->status.rates[3].count,
5366 		    info->status.rates[3].flags,
5367 		    info->status.ack_signal, info->status.ampdu_ack_len,
5368 		    info->status.ampdu_len, info->status.antenna,
5369 		    info->status.tx_time, info->status.flags,
5370 		    info->status.status_driver_data[0],
5371 		    info->status.status_driver_data[1]);
5372 #endif
5373 
5374 	if (txstat->free_list) {
5375 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5376 		list_add_tail(&skb->list, txstat->free_list);
5377 	} else {
5378 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5379 	}
5380 }
5381 
5382 void
5383 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5384 {
5385 	struct ieee80211_tx_status status;
5386 
5387 	memset(&status, 0, sizeof(status));
5388 	status.info = IEEE80211_SKB_CB(skb);
5389 	status.skb = skb;
5390 	/* sta, n_rates, rates, free_list? */
5391 
5392 	ieee80211_tx_status_ext(hw, &status);
5393 }
5394 
5395 /*
5396  * This is an internal bandaid for the moment for the way we glue
5397  * skbs and mbufs together for TX.  Once we have skbs backed by
5398  * mbufs this should go away.
5399  * This is a public function but kept on the private KPI (lkpi_)
5400  * and is not exposed by a header file.
5401  */
5402 static void
5403 lkpi_ieee80211_free_skb_mbuf(void *p)
5404 {
5405 	struct ieee80211_node *ni;
5406 	struct mbuf *m;
5407 
5408 	if (p == NULL)
5409 		return;
5410 
5411 	m = (struct mbuf *)p;
5412 	M_ASSERTPKTHDR(m);
5413 
5414 	ni = m->m_pkthdr.PH_loc.ptr;
5415 	m->m_pkthdr.PH_loc.ptr = NULL;
5416 	if (ni != NULL)
5417 		ieee80211_free_node(ni);
5418 	m_freem(m);
5419 }
5420 
5421 void
5422 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
5423     struct delayed_work *w, int delay)
5424 {
5425 	struct lkpi_hw *lhw;
5426 
5427 	/* Need to make sure hw is in a stable (non-suspended) state. */
5428 	IMPROVE();
5429 
5430 	lhw = HW_TO_LHW(hw);
5431 	queue_delayed_work(lhw->workq, w, delay);
5432 }
5433 
5434 void
5435 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
5436     struct work_struct *w)
5437 {
5438 	struct lkpi_hw *lhw;
5439 
5440 	/* Need to make sure hw is in a stable (non-suspended) state. */
5441 	IMPROVE();
5442 
5443 	lhw = HW_TO_LHW(hw);
5444 	queue_work(lhw->workq, w);
5445 }
5446 
5447 struct sk_buff *
5448 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5449     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5450 {
5451 	struct sk_buff *skb;
5452 	struct ieee80211_frame *wh;
5453 	uint8_t *p;
5454 	size_t len;
5455 
5456 	len = sizeof(*wh);
5457 	len += 2 + ssid_len;
5458 
5459 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5460 	if (skb == NULL)
5461 		return (NULL);
5462 
5463 	skb_reserve(skb, hw->extra_tx_headroom);
5464 
5465 	wh = skb_put_zero(skb, sizeof(*wh));
5466 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5467 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5468 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5469 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5470 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5471 
5472 	p = skb_put(skb, 2 + ssid_len);
5473 	*p++ = IEEE80211_ELEMID_SSID;
5474 	*p++ = ssid_len;
5475 	if (ssid_len > 0)
5476 		memcpy(p, ssid, ssid_len);
5477 
5478 	return (skb);
5479 }
5480 
5481 struct sk_buff *
5482 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
5483     struct ieee80211_vif *vif)
5484 {
5485 	struct lkpi_vif *lvif;
5486 	struct ieee80211vap *vap;
5487 	struct sk_buff *skb;
5488 	struct ieee80211_frame_pspoll *psp;
5489 	uint16_t v;
5490 
5491 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
5492 	if (skb == NULL)
5493 		return (NULL);
5494 
5495 	skb_reserve(skb, hw->extra_tx_headroom);
5496 
5497 	lvif = VIF_TO_LVIF(vif);
5498 	vap = LVIF_TO_VAP(lvif);
5499 
5500 	psp = skb_put_zero(skb, sizeof(*psp));
5501 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
5502 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5503 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
5504 	memcpy(&psp->i_aid, &v, sizeof(v));
5505 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
5506 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
5507 
5508 	return (skb);
5509 }
5510 
5511 struct sk_buff *
5512 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5513     struct ieee80211_vif *vif, int linkid, bool qos)
5514 {
5515 	struct lkpi_vif *lvif;
5516 	struct ieee80211vap *vap;
5517 	struct sk_buff *skb;
5518 	struct ieee80211_frame *nullf;
5519 
5520 	IMPROVE("linkid");
5521 
5522 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
5523 	if (skb == NULL)
5524 		return (NULL);
5525 
5526 	skb_reserve(skb, hw->extra_tx_headroom);
5527 
5528 	lvif = VIF_TO_LVIF(vif);
5529 	vap = LVIF_TO_VAP(lvif);
5530 
5531 	nullf = skb_put_zero(skb, sizeof(*nullf));
5532 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
5533 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
5534 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5535 
5536 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
5537 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
5538 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
5539 
5540 	return (skb);
5541 }
5542 
5543 struct wireless_dev *
5544 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
5545 {
5546 	struct lkpi_vif *lvif;
5547 
5548 	lvif = VIF_TO_LVIF(vif);
5549 	return (&lvif->wdev);
5550 }
5551 
5552 void
5553 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
5554 {
5555 	struct lkpi_vif *lvif;
5556 	struct ieee80211vap *vap;
5557 	enum ieee80211_state nstate;
5558 	int arg;
5559 
5560 	lvif = VIF_TO_LVIF(vif);
5561 	vap = LVIF_TO_VAP(lvif);
5562 
5563 	/*
5564 	 * Go to init; otherwise we need to elaborately check state and
5565 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
5566 	 * Let the statemachine handle all neccessary changes.
5567 	 */
5568 	nstate = IEEE80211_S_INIT;
5569 	arg = 0;	/* Not a valid reason. */
5570 
5571 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5572 	    vif, vap, ieee80211_state_name[vap->iv_state]);
5573 	ieee80211_new_state(vap, nstate, arg);
5574 }
5575 
5576 void
5577 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
5578 {
5579 	struct lkpi_vif *lvif;
5580 	struct ieee80211vap *vap;
5581 
5582 	lvif = VIF_TO_LVIF(vif);
5583 	vap = LVIF_TO_VAP(lvif);
5584 
5585 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5586 	    vif, vap, ieee80211_state_name[vap->iv_state]);
5587 	ieee80211_beacon_miss(vap->iv_ic);
5588 }
5589 
5590 /* -------------------------------------------------------------------------- */
5591 
5592 void
5593 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
5594 {
5595 	struct lkpi_hw *lhw;
5596 	struct lkpi_vif *lvif;
5597 	struct ieee80211_vif *vif;
5598 	int ac_count, ac;
5599 
5600 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5601 	    __func__, qnum, hw->queues, hw));
5602 
5603 	lhw = wiphy_priv(hw->wiphy);
5604 
5605 	/* See lkpi_ic_vap_create(). */
5606 	if (hw->queues >= IEEE80211_NUM_ACS)
5607 		ac_count = IEEE80211_NUM_ACS;
5608 	else
5609 		ac_count = 1;
5610 
5611 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5612 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5613 
5614 		vif = LVIF_TO_VIF(lvif);
5615 		for (ac = 0; ac < ac_count; ac++) {
5616 			IMPROVE_TXQ("LOCKING");
5617 			if (qnum == vif->hw_queue[ac]) {
5618 #ifdef LINUXKPI_DEBUG_80211
5619 				/*
5620 				 * For now log this to better understand
5621 				 * how this is supposed to work.
5622 				 */
5623 				if (lvif->hw_queue_stopped[ac] &&
5624 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
5625 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
5626 					    "lvif %p vif %p ac %d qnum %d already "
5627 					    "stopped\n", __func__, __LINE__,
5628 					    lhw, hw, lvif, vif, ac, qnum);
5629 #endif
5630 				lvif->hw_queue_stopped[ac] = true;
5631 			}
5632 		}
5633 	}
5634 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5635 }
5636 
5637 void
5638 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
5639 {
5640 	int i;
5641 
5642 	IMPROVE_TXQ("Locking; do we need further info?");
5643 	for (i = 0; i < hw->queues; i++)
5644 		linuxkpi_ieee80211_stop_queue(hw, i);
5645 }
5646 
5647 
5648 static void
5649 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
5650 {
5651 	struct lkpi_hw *lhw;
5652 	struct lkpi_vif *lvif;
5653 	struct lkpi_sta *lsta;
5654 	int ac_count, ac, tid;
5655 
5656 	/* See lkpi_ic_vap_create(). */
5657 	if (hw->queues >= IEEE80211_NUM_ACS)
5658 		ac_count = IEEE80211_NUM_ACS;
5659 	else
5660 		ac_count = 1;
5661 
5662 	lhw = wiphy_priv(hw->wiphy);
5663 
5664 	IMPROVE_TXQ("Locking");
5665 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5666 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5667 		struct ieee80211_vif *vif;
5668 
5669 		vif = LVIF_TO_VIF(lvif);
5670 		for (ac = 0; ac < ac_count; ac++) {
5671 
5672 			if (hwq == vif->hw_queue[ac]) {
5673 
5674 				/* XXX-BZ what about software scan? */
5675 
5676 #ifdef LINUXKPI_DEBUG_80211
5677 				/*
5678 				 * For now log this to better understand
5679 				 * how this is supposed to work.
5680 				 */
5681 				if (!lvif->hw_queue_stopped[ac] &&
5682 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
5683 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
5684 					    "lvif %p vif %p ac %d hw_q not stopped\n",
5685 					    __func__, __LINE__,
5686 					    lhw, hw, lvif, vif, ac);
5687 #endif
5688 				lvif->hw_queue_stopped[ac] = false;
5689 
5690 				LKPI_80211_LVIF_LOCK(lvif);
5691 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
5692 					struct ieee80211_sta *sta;
5693 
5694 					sta = LSTA_TO_STA(lsta);
5695 					for (tid = 0; tid < nitems(sta->txq); tid++) {
5696 						struct lkpi_txq *ltxq;
5697 
5698 						if (sta->txq[tid] == NULL)
5699 							continue;
5700 
5701 						if (sta->txq[tid]->ac != ac)
5702 							continue;
5703 
5704 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5705 						if (!ltxq->stopped)
5706 							continue;
5707 
5708 						ltxq->stopped = false;
5709 
5710 						/* XXX-BZ see when this explodes with all the locking. taskq? */
5711 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
5712 					}
5713 				}
5714 				LKPI_80211_LVIF_UNLOCK(lvif);
5715 			}
5716 		}
5717 	}
5718 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5719 }
5720 
5721 void
5722 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
5723 {
5724 	int i;
5725 
5726 	IMPROVE_TXQ("Is this all/enough here?");
5727 	for (i = 0; i < hw->queues; i++)
5728 		lkpi_ieee80211_wake_queues(hw, i);
5729 }
5730 
5731 void
5732 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
5733 {
5734 
5735 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5736 	    __func__, qnum, hw->queues, hw));
5737 
5738 	lkpi_ieee80211_wake_queues(hw, qnum);
5739 }
5740 
5741 /* This is just hardware queues. */
5742 void
5743 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
5744 {
5745 	struct lkpi_hw *lhw;
5746 
5747 	lhw = HW_TO_LHW(hw);
5748 
5749 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
5750 	IMPROVE_TXQ("LOCKING");
5751 	if (++lhw->txq_generation[ac] == 0)
5752 		lhw->txq_generation[ac]++;
5753 }
5754 
5755 struct ieee80211_txq *
5756 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
5757 {
5758 	struct lkpi_hw *lhw;
5759 	struct ieee80211_txq *txq;
5760 	struct lkpi_txq *ltxq;
5761 
5762 	lhw = HW_TO_LHW(hw);
5763 	txq = NULL;
5764 
5765 	IMPROVE_TXQ("LOCKING");
5766 
5767 	/* Check that we are scheduled. */
5768 	if (lhw->txq_generation[ac] == 0)
5769 		goto out;
5770 
5771 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
5772 	if (ltxq == NULL)
5773 		goto out;
5774 	if (ltxq->txq_generation == lhw->txq_generation[ac])
5775 		goto out;
5776 
5777 	ltxq->txq_generation = lhw->txq_generation[ac];
5778 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
5779 	txq = &ltxq->txq;
5780 	TAILQ_ELEM_INIT(ltxq, txq_entry);
5781 
5782 out:
5783 	return (txq);
5784 }
5785 
5786 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
5787     struct ieee80211_txq *txq, bool withoutpkts)
5788 {
5789 	struct lkpi_hw *lhw;
5790 	struct lkpi_txq *ltxq;
5791 	bool ltxq_empty;
5792 
5793 	ltxq = TXQ_TO_LTXQ(txq);
5794 
5795 	IMPROVE_TXQ("LOCKING");
5796 
5797 	/* Only schedule if work to do or asked to anyway. */
5798 	LKPI_80211_LTXQ_LOCK(ltxq);
5799 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
5800 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5801 	if (!withoutpkts && ltxq_empty)
5802 		goto out;
5803 
5804 	/* Make sure we do not double-schedule. */
5805 	if (ltxq->txq_entry.tqe_next != NULL)
5806 		goto out;
5807 
5808 	lhw = HW_TO_LHW(hw);
5809 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
5810 out:
5811 	return;
5812 }
5813 
5814 void
5815 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
5816     struct ieee80211_txq *txq)
5817 {
5818 	struct lkpi_hw *lhw;
5819 	struct ieee80211_txq *ntxq;
5820 	struct ieee80211_tx_control control;
5821         struct sk_buff *skb;
5822 
5823 	lhw = HW_TO_LHW(hw);
5824 
5825 	LKPI_80211_LHW_TXQ_LOCK(lhw);
5826 	ieee80211_txq_schedule_start(hw, txq->ac);
5827 	do {
5828 		ntxq = ieee80211_next_txq(hw, txq->ac);
5829 		if (ntxq == NULL)
5830 			break;
5831 
5832 		memset(&control, 0, sizeof(control));
5833 		control.sta = ntxq->sta;
5834 		do {
5835 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
5836 			if (skb == NULL)
5837 				break;
5838 			lkpi_80211_mo_tx(hw, &control, skb);
5839 		} while(1);
5840 
5841 		ieee80211_return_txq(hw, ntxq, false);
5842 	} while (1);
5843 	ieee80211_txq_schedule_end(hw, txq->ac);
5844 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
5845 }
5846 
5847 /* -------------------------------------------------------------------------- */
5848 
5849 struct lkpi_cfg80211_bss {
5850 	u_int refcnt;
5851 	struct cfg80211_bss bss;
5852 };
5853 
5854 struct lkpi_cfg80211_get_bss_iter_lookup {
5855 	struct wiphy *wiphy;
5856 	struct linuxkpi_ieee80211_channel *chan;
5857 	const uint8_t *bssid;
5858 	const uint8_t *ssid;
5859 	size_t ssid_len;
5860 	enum ieee80211_bss_type bss_type;
5861 	enum ieee80211_privacy privacy;
5862 
5863 	/*
5864 	 * Something to store a copy of the result as the net80211 scan cache
5865 	 * is not refoucnted so a scan entry might go away any time.
5866 	 */
5867 	bool match;
5868 	struct cfg80211_bss *bss;
5869 };
5870 
5871 static void
5872 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
5873 {
5874 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
5875 	size_t ielen;
5876 
5877 	lookup = arg;
5878 
5879 	/* Do not try to find another match. */
5880 	if (lookup->match)
5881 		return;
5882 
5883 	/* Nothing to store result. */
5884 	if (lookup->bss == NULL)
5885 		return;
5886 
5887 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
5888 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
5889 		/* We have no idea what to compare to as the drivers only request ANY */
5890 		return;
5891 	}
5892 
5893 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
5894 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
5895 		/* We have no idea what to compare to as the drivers only request ANY */
5896 		return;
5897 	}
5898 
5899 	if (lookup->chan != NULL) {
5900 		struct linuxkpi_ieee80211_channel *chan;
5901 
5902 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
5903 		    se->se_chan->ic_freq);
5904 		if (chan == NULL || chan != lookup->chan)
5905 			return;
5906 	}
5907 
5908 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
5909 		return;
5910 
5911 	if (lookup->ssid) {
5912 		if (lookup->ssid_len != se->se_ssid[1] ||
5913 		    se->se_ssid[1] == 0)
5914 			return;
5915 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
5916 			return;
5917 	}
5918 
5919 	ielen = se->se_ies.len;
5920 
5921 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
5922 	    M_LKPI80211, M_NOWAIT | M_ZERO);
5923 	if (lookup->bss->ies == NULL)
5924 		return;
5925 
5926 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
5927 	lookup->bss->ies->len = ielen;
5928 	if (ielen)
5929 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
5930 
5931 	lookup->match = true;
5932 }
5933 
5934 struct cfg80211_bss *
5935 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
5936     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
5937     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
5938 {
5939 	struct lkpi_cfg80211_bss *lbss;
5940 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
5941 	struct lkpi_hw *lhw;
5942 	struct ieee80211vap *vap;
5943 
5944 	lhw = wiphy_priv(wiphy);
5945 
5946 	/* Let's hope we can alloc. */
5947 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
5948 	if (lbss == NULL) {
5949 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
5950 		return (NULL);
5951 	}
5952 
5953 	lookup.wiphy = wiphy;
5954 	lookup.chan = chan;
5955 	lookup.bssid = bssid;
5956 	lookup.ssid = ssid;
5957 	lookup.ssid_len = ssid_len;
5958 	lookup.bss_type = bss_type;
5959 	lookup.privacy = privacy;
5960 	lookup.match = false;
5961 	lookup.bss = &lbss->bss;
5962 
5963 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
5964 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
5965 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
5966 	if (!lookup.match) {
5967 		free(lbss, M_LKPI80211);
5968 		return (NULL);
5969 	}
5970 
5971 	refcount_init(&lbss->refcnt, 1);
5972 	return (&lbss->bss);
5973 }
5974 
5975 void
5976 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
5977 {
5978 	struct lkpi_cfg80211_bss *lbss;
5979 
5980 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
5981 
5982 	/* Free everything again on refcount ... */
5983 	if (refcount_release(&lbss->refcnt)) {
5984 		free(lbss->bss.ies, M_LKPI80211);
5985 		free(lbss, M_LKPI80211);
5986 	}
5987 }
5988 
5989 void
5990 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
5991 {
5992 	struct lkpi_hw *lhw;
5993 	struct ieee80211com *ic;
5994 	struct ieee80211vap *vap;
5995 
5996 	lhw = wiphy_priv(wiphy);
5997 	ic = lhw->ic;
5998 
5999 	/*
6000 	 * If we haven't called ieee80211_ifattach() yet
6001 	 * or there is no VAP, there are no scans to flush.
6002 	 */
6003 	if (ic == NULL ||
6004 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
6005 		return;
6006 
6007 	/* Should only happen on the current one? Not seen it late enough. */
6008 	IEEE80211_LOCK(ic);
6009 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6010 		ieee80211_scan_flush(vap);
6011 	IEEE80211_UNLOCK(ic);
6012 }
6013 
6014 /* -------------------------------------------------------------------------- */
6015 
6016 MODULE_VERSION(linuxkpi_wlan, 1);
6017 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
6018 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6019