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