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