xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 4c053c17f2c8a715988f215d16284879857ca376)
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_LHW_LOCK(lhw);
3773 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
3774 	LKPI_80211_LHW_UNLOCK(lhw);
3775 	return;
3776 
3777 ops_tx:
3778 #ifdef LINUXKPI_DEBUG_80211
3779 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3780 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3781 		    "TX ac %d prio %u qmap %u\n",
3782 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3783 		    skb, ac, skb->priority, skb->qmap);
3784 #endif
3785 	memset(&control, 0, sizeof(control));
3786 	control.sta = sta;
3787 	LKPI_80211_LHW_LOCK(lhw);
3788 	lkpi_80211_mo_tx(hw, &control, skb);
3789 	LKPI_80211_LHW_UNLOCK(lhw);
3790 }
3791 
3792 static void
3793 lkpi_80211_txq_task(void *ctx, int pending)
3794 {
3795 	struct lkpi_sta *lsta;
3796 	struct mbufq mq;
3797 	struct mbuf *m;
3798 
3799 	lsta = ctx;
3800 
3801 #ifdef LINUXKPI_DEBUG_80211
3802 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3803 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3804 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3805 		    pending, mbufq_len(&lsta->txq));
3806 #endif
3807 
3808 	mbufq_init(&mq, IFQ_MAXLEN);
3809 
3810 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3811 	/*
3812 	 * Do not re-check lsta->txq_ready here; we may have a pending
3813 	 * disassoc frame still.
3814 	 */
3815 	mbufq_concat(&mq, &lsta->txq);
3816 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3817 
3818 	m = mbufq_dequeue(&mq);
3819 	while (m != NULL) {
3820 		lkpi_80211_txq_tx_one(lsta, m);
3821 		m = mbufq_dequeue(&mq);
3822 	}
3823 }
3824 
3825 static int
3826 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3827 {
3828 
3829 	/* XXX TODO */
3830 	IMPROVE();
3831 
3832 	/* Quick and dirty cheating hack. */
3833 	struct ieee80211_node *ni;
3834 
3835 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3836 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3837 }
3838 
3839 #ifdef LKPI_80211_HT
3840 static int
3841 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
3842     const uint8_t *frm, const uint8_t *efrm)
3843 {
3844 	struct ieee80211com *ic;
3845 	struct lkpi_hw *lhw;
3846 
3847 	ic = ni->ni_ic;
3848 	lhw = ic->ic_softc;
3849 
3850 	IMPROVE_HT();
3851 
3852 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
3853 }
3854 
3855 static int
3856 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
3857 {
3858 	struct ieee80211com *ic;
3859 	struct lkpi_hw *lhw;
3860 
3861 	ic = ni->ni_ic;
3862 	lhw = ic->ic_softc;
3863 
3864 	IMPROVE_HT();
3865 
3866 	return (lhw->ic_send_action(ni, category, action, sa));
3867 }
3868 
3869 
3870 static int
3871 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
3872 {
3873 	struct ieee80211com *ic;
3874 	struct lkpi_hw *lhw;
3875 
3876 	ic = ni->ni_ic;
3877 	lhw = ic->ic_softc;
3878 
3879 	IMPROVE_HT();
3880 
3881 	return (lhw->ic_ampdu_enable(ni, tap));
3882 }
3883 
3884 static int
3885 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
3886     int dialogtoken, int baparamset, int batimeout)
3887 {
3888 	struct ieee80211com *ic;
3889 	struct lkpi_hw *lhw;
3890 
3891 	ic = ni->ni_ic;
3892 	lhw = ic->ic_softc;
3893 
3894 	IMPROVE_HT();
3895 
3896 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
3897 }
3898 
3899 static int
3900 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
3901     int status, int baparamset, int batimeout)
3902 {
3903 	struct ieee80211com *ic;
3904 	struct lkpi_hw *lhw;
3905 
3906 	ic = ni->ni_ic;
3907 	lhw = ic->ic_softc;
3908 
3909 	IMPROVE_HT();
3910 
3911 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
3912 }
3913 
3914 static void
3915 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
3916 {
3917 	struct ieee80211com *ic;
3918 	struct lkpi_hw *lhw;
3919 
3920 	ic = ni->ni_ic;
3921 	lhw = ic->ic_softc;
3922 
3923 	IMPROVE_HT();
3924 
3925 	lhw->ic_addba_stop(ni, tap);
3926 }
3927 
3928 static void
3929 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
3930 {
3931 	struct ieee80211com *ic;
3932 	struct lkpi_hw *lhw;
3933 
3934 	ic = ni->ni_ic;
3935 	lhw = ic->ic_softc;
3936 
3937 	IMPROVE_HT();
3938 
3939 	lhw->ic_addba_response_timeout(ni, tap);
3940 }
3941 
3942 static void
3943 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
3944     int status)
3945 {
3946 	struct ieee80211com *ic;
3947 	struct lkpi_hw *lhw;
3948 
3949 	ic = ni->ni_ic;
3950 	lhw = ic->ic_softc;
3951 
3952 	IMPROVE_HT();
3953 
3954 	lhw->ic_bar_response(ni, tap, status);
3955 }
3956 
3957 static int
3958 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
3959     int baparamset, int batimeout, int baseqctl)
3960 {
3961 	struct ieee80211com *ic;
3962 	struct lkpi_hw *lhw;
3963 	struct ieee80211_hw *hw;
3964 	struct ieee80211vap *vap;
3965 	struct lkpi_vif *lvif;
3966 	struct ieee80211_vif *vif;
3967 	struct lkpi_sta *lsta;
3968         struct ieee80211_sta *sta;
3969 	struct ieee80211_ampdu_params params;
3970 	int error;
3971 
3972 	ic = ni->ni_ic;
3973 	lhw = ic->ic_softc;
3974 	hw = LHW_TO_HW(lhw);
3975 	vap = ni->ni_vap;
3976 	lvif = VAP_TO_LVIF(vap);
3977 	vif = LVIF_TO_VIF(lvif);
3978 	lsta = ni->ni_drv_data;
3979 	sta = LSTA_TO_STA(lsta);
3980 
3981 	params.sta = sta;
3982 	params.action = IEEE80211_AMPDU_RX_START;
3983 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
3984 	if (params.buf_size == 0)
3985 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
3986 	else
3987 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
3988 	if (params.buf_size > hw->max_rx_aggregation_subframes)
3989 		params.buf_size = hw->max_rx_aggregation_subframes;
3990 	params.timeout = le16toh(batimeout);
3991 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
3992 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
3993 	params.amsdu = false;
3994 
3995 	IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
3996 
3997 	/* This may call kalloc.  Make sure we can sleep. */
3998 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
3999 	if (error != 0) {
4000 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4001 		    __func__, error, ni, rap);
4002 		return (error);
4003 	}
4004 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
4005 
4006 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
4007 	return (error);
4008 }
4009 
4010 static void
4011 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
4012 {
4013 	struct ieee80211com *ic;
4014 	struct lkpi_hw *lhw;
4015 	struct ieee80211_hw *hw;
4016 	struct ieee80211vap *vap;
4017 	struct lkpi_vif *lvif;
4018 	struct ieee80211_vif *vif;
4019 	struct lkpi_sta *lsta;
4020         struct ieee80211_sta *sta;
4021 	struct ieee80211_ampdu_params params;
4022 	int error;
4023 	uint8_t tid;
4024 
4025 	ic = ni->ni_ic;
4026 	lhw = ic->ic_softc;
4027 
4028 	/*
4029 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
4030 	 * we did not START.  Some drivers pass it down to firmware which will
4031 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
4032 	 * ieee80211_ht_node_init() amongst others which will iterate over all
4033 	 * tid and call ic_ampdu_rx_stop() unconditionally.
4034 	 * XXX net80211 should probably be more "gentle" in these cases and
4035 	 * track some state itself.
4036 	 */
4037 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
4038 		goto net80211_only;
4039 
4040 	hw = LHW_TO_HW(lhw);
4041 	vap = ni->ni_vap;
4042 	lvif = VAP_TO_LVIF(vap);
4043 	vif = LVIF_TO_VIF(lvif);
4044 	lsta = ni->ni_drv_data;
4045 	sta = LSTA_TO_STA(lsta);
4046 
4047 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
4048 	for (tid = 0; tid < WME_NUM_TID; tid++) {
4049 		if (&ni->ni_rx_ampdu[tid] == rap)
4050 			break;
4051 	}
4052 
4053 	params.sta = sta;
4054 	params.action = IEEE80211_AMPDU_RX_STOP;
4055 	params.buf_size = 0;
4056 	params.timeout = 0;
4057 	params.ssn = 0;
4058 	params.tid = tid;
4059 	params.amsdu = false;
4060 
4061 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4062 	if (error != 0)
4063 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4064 		    __func__, error, ni, rap);
4065 
4066 net80211_only:
4067 	lhw->ic_ampdu_rx_stop(ni, rap);
4068 }
4069 #endif
4070 
4071 static void
4072 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
4073     uint8_t *bands, int *chan_flags, enum nl80211_band band)
4074 {
4075 #ifdef LKPI_80211_HT
4076 	struct ieee80211_sta_ht_cap *ht_cap;
4077 
4078 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
4079 	if (!ht_cap->ht_supported)
4080 		return;
4081 
4082 	switch (band) {
4083 	case NL80211_BAND_2GHZ:
4084 		setbit(bands, IEEE80211_MODE_11NG);
4085 		break;
4086 	case NL80211_BAND_5GHZ:
4087 		setbit(bands, IEEE80211_MODE_11NA);
4088 		break;
4089 	default:
4090 		IMPROVE("Unsupported band %d", band);
4091 		return;
4092 	}
4093 
4094 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
4095 
4096 	/*
4097 	 * Rather than manually checking each flag and
4098 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
4099 	 * simply copy the 16bits.
4100 	 */
4101 	ic->ic_htcaps |= ht_cap->cap;
4102 
4103 	/* Then deal with the other flags. */
4104 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
4105 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
4106 #ifdef __notyet__
4107 	if (ieee80211_hw_check(hw, TX_AMSDU))
4108 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
4109 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
4110 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
4111 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
4112 #endif
4113 
4114 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
4115 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
4116 
4117 	/* Only add HT40 channels if supported. */
4118 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
4119 	    chan_flags != NULL)
4120 		*chan_flags |= NET80211_CBW_FLAG_HT40;
4121 #endif
4122 }
4123 
4124 static void
4125 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
4126     int *n, struct ieee80211_channel *c)
4127 {
4128 	struct lkpi_hw *lhw;
4129 	struct ieee80211_hw *hw;
4130 	struct linuxkpi_ieee80211_channel *channels;
4131 	uint8_t bands[IEEE80211_MODE_BYTES];
4132 	int chan_flags, error, i, nchans;
4133 
4134 	/* Channels */
4135 	lhw = ic->ic_softc;
4136 	hw = LHW_TO_HW(lhw);
4137 
4138 	/* NL80211_BAND_2GHZ */
4139 	nchans = 0;
4140 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
4141 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
4142 	if (nchans > 0) {
4143 		memset(bands, 0, sizeof(bands));
4144 		chan_flags = 0;
4145 		setbit(bands, IEEE80211_MODE_11B);
4146 		/* XXX-BZ unclear how to check for 11g. */
4147 
4148 		IMPROVE("the bitrates may have flags?");
4149 		setbit(bands, IEEE80211_MODE_11G);
4150 
4151 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4152 		    NL80211_BAND_2GHZ);
4153 
4154 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4155 		for (i = 0; i < nchans && *n < maxchan; i++) {
4156 			uint32_t nflags = 0;
4157 			int cflags = chan_flags;
4158 
4159 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4160 				ic_printf(ic, "%s: Skipping disabled chan "
4161 				    "[%u/%u/%#x]\n", __func__,
4162 				    channels[i].hw_value,
4163 				    channels[i].center_freq, channels[i].flags);
4164 				continue;
4165 			}
4166 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4167 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4168 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
4169 				nflags |= IEEE80211_CHAN_DFS;
4170 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4171 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4172 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4173 				cflags &= ~NET80211_CBW_FLAG_VHT80;
4174 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
4175 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4176 				cflags &= ~NET80211_CBW_FLAG_HT40;
4177 
4178 			error = ieee80211_add_channel_cbw(c, maxchan, n,
4179 			    channels[i].hw_value, channels[i].center_freq,
4180 			    channels[i].max_power,
4181 			    nflags, bands, cflags);
4182 			/* net80211::ENOBUFS: *n >= maxchans */
4183 			if (error != 0 && error != ENOBUFS)
4184 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4185 				    "returned error %d\n",
4186 				    __func__, channels[i].hw_value,
4187 				    channels[i].center_freq, channels[i].flags,
4188 				    nflags, chan_flags, cflags, error);
4189 			if (error != 0)
4190 				break;
4191 		}
4192 	}
4193 
4194 	/* NL80211_BAND_5GHZ */
4195 	nchans = 0;
4196 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
4197 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
4198 	if (nchans > 0) {
4199 		memset(bands, 0, sizeof(bands));
4200 		chan_flags = 0;
4201 		setbit(bands, IEEE80211_MODE_11A);
4202 
4203 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4204 		    NL80211_BAND_5GHZ);
4205 
4206 #ifdef LKPI_80211_VHT
4207 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
4208 
4209 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4210 			ic->ic_vht_cap.vht_cap_info =
4211 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
4212 
4213 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
4214 			chan_flags |= NET80211_CBW_FLAG_VHT80;
4215 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4216 			    ic->ic_vht_cap.vht_cap_info))
4217 				chan_flags |= NET80211_CBW_FLAG_VHT160;
4218 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4219 			    ic->ic_vht_cap.vht_cap_info))
4220 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
4221 		}
4222 #endif
4223 
4224 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4225 		for (i = 0; i < nchans && *n < maxchan; i++) {
4226 			uint32_t nflags = 0;
4227 			int cflags = chan_flags;
4228 
4229 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4230 				ic_printf(ic, "%s: Skipping disabled chan "
4231 				    "[%u/%u/%#x]\n", __func__,
4232 				    channels[i].hw_value,
4233 				    channels[i].center_freq, channels[i].flags);
4234 				continue;
4235 			}
4236 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4237 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4238 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
4239 				nflags |= IEEE80211_CHAN_DFS;
4240 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4241 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4242 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4243 				cflags &= ~NET80211_CBW_FLAG_VHT80;
4244 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
4245 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4246 				cflags &= ~NET80211_CBW_FLAG_HT40;
4247 
4248 			error = ieee80211_add_channel_cbw(c, maxchan, n,
4249 			    channels[i].hw_value, channels[i].center_freq,
4250 			    channels[i].max_power,
4251 			    nflags, bands, cflags);
4252 			/* net80211::ENOBUFS: *n >= maxchans */
4253 			if (error != 0 && error != ENOBUFS)
4254 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4255 				    "returned error %d\n",
4256 				    __func__, channels[i].hw_value,
4257 				    channels[i].center_freq, channels[i].flags,
4258 				    nflags, chan_flags, cflags, error);
4259 			if (error != 0)
4260 				break;
4261 		}
4262 	}
4263 }
4264 
4265 static void *
4266 lkpi_ieee80211_ifalloc(void)
4267 {
4268 	struct ieee80211com *ic;
4269 
4270 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
4271 	if (ic == NULL)
4272 		return (NULL);
4273 
4274 	/* Setting these happens later when we have device information. */
4275 	ic->ic_softc = NULL;
4276 	ic->ic_name = "linuxkpi";
4277 
4278 	return (ic);
4279 }
4280 
4281 struct ieee80211_hw *
4282 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
4283 {
4284 	struct ieee80211_hw *hw;
4285 	struct lkpi_hw *lhw;
4286 	struct wiphy *wiphy;
4287 	int ac;
4288 
4289 	/* Get us and the driver data also allocated. */
4290 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
4291 	if (wiphy == NULL)
4292 		return (NULL);
4293 
4294 	lhw = wiphy_priv(wiphy);
4295 	lhw->ops = ops;
4296 
4297 	LKPI_80211_LHW_LOCK_INIT(lhw);
4298 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4299 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
4300 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
4301 	TAILQ_INIT(&lhw->lvif_head);
4302 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4303 		lhw->txq_generation[ac] = 1;
4304 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4305 	}
4306 
4307 	/* Deferred RX path. */
4308 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4309 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4310 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4311 	lhw->rxq_stopped = false;
4312 
4313 	/*
4314 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
4315 	 * not initialized.
4316 	 */
4317 	hw = LHW_TO_HW(lhw);
4318 	hw->wiphy = wiphy;
4319 	hw->conf.flags |= IEEE80211_CONF_IDLE;
4320 	hw->priv = (void *)(lhw + 1);
4321 
4322 	/* BSD Specific. */
4323 	lhw->ic = lkpi_ieee80211_ifalloc();
4324 	if (lhw->ic == NULL) {
4325 		ieee80211_free_hw(hw);
4326 		return (NULL);
4327 	}
4328 
4329 	IMPROVE();
4330 
4331 	return (hw);
4332 }
4333 
4334 void
4335 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
4336 {
4337 	struct lkpi_hw *lhw;
4338 	struct mbuf *m;
4339 
4340 	lhw = HW_TO_LHW(hw);
4341 	free(lhw->ic, M_LKPI80211);
4342 	lhw->ic = NULL;
4343 
4344 	/*
4345 	 * Drain the deferred RX path.
4346 	 */
4347 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4348 	lhw->rxq_stopped = true;
4349 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4350 
4351 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4352 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4353 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4354 
4355 	/* Flush mbufq (make sure to release ni refs!). */
4356 	m = mbufq_dequeue(&lhw->rxq);
4357 	while (m != NULL) {
4358 		struct m_tag *mtag;
4359 
4360 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4361 		if (mtag != NULL) {
4362 			struct lkpi_80211_tag_rxni *rxni;
4363 
4364 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4365 			ieee80211_free_node(rxni->ni);
4366 		}
4367 		m_freem(m);
4368 		m = mbufq_dequeue(&lhw->rxq);
4369 	}
4370 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4371 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4372 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4373 
4374 	/* Cleanup more of lhw here or in wiphy_free()? */
4375 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
4376 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4377 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4378 	sx_destroy(&lhw->lvif_sx);
4379 	IMPROVE();
4380 }
4381 
4382 void
4383 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
4384 {
4385 	struct lkpi_hw *lhw;
4386 	struct ieee80211com *ic;
4387 
4388 	lhw = HW_TO_LHW(hw);
4389 	ic = lhw->ic;
4390 
4391 	/* Now set a proper name before ieee80211_ifattach(). */
4392 	ic->ic_softc = lhw;
4393 	ic->ic_name = name;
4394 
4395 	/* XXX-BZ do we also need to set wiphy name? */
4396 }
4397 
4398 struct ieee80211_hw *
4399 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
4400 {
4401 	struct lkpi_hw *lhw;
4402 
4403 	lhw = wiphy_priv(wiphy);
4404 	return (LHW_TO_HW(lhw));
4405 }
4406 
4407 static void
4408 lkpi_radiotap_attach(struct lkpi_hw *lhw)
4409 {
4410 	struct ieee80211com *ic;
4411 
4412 	ic = lhw->ic;
4413 	ieee80211_radiotap_attach(ic,
4414 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
4415 	    LKPI_RTAP_TX_FLAGS_PRESENT,
4416 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
4417 	    LKPI_RTAP_RX_FLAGS_PRESENT);
4418 }
4419 
4420 int
4421 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
4422 {
4423 	struct ieee80211com *ic;
4424 	struct lkpi_hw *lhw;
4425 	int band, i;
4426 
4427 	lhw = HW_TO_LHW(hw);
4428 	ic = lhw->ic;
4429 
4430 	/* We do it this late as wiphy->dev should be set for the name. */
4431 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4432 	if (lhw->workq == NULL)
4433 		return (-EAGAIN);
4434 
4435 	/* XXX-BZ figure this out how they count his... */
4436 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
4437 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4438 		    hw->wiphy->perm_addr);
4439 	} else if (hw->wiphy->n_addresses > 0) {
4440 		/* We take the first one. */
4441 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4442 		    hw->wiphy->addresses[0].addr);
4443 	} else {
4444 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
4445 	}
4446 
4447 #ifdef __not_yet__
4448 	/* See comment in lkpi_80211_txq_tx_one(). */
4449 	ic->ic_headroom = hw->extra_tx_headroom;
4450 #endif
4451 
4452 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
4453 	ic->ic_opmode = IEEE80211_M_STA;
4454 
4455 	/* Set device capabilities. */
4456 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
4457 	ic->ic_caps =
4458 	    IEEE80211_C_STA |
4459 	    IEEE80211_C_MONITOR |
4460 	    IEEE80211_C_WPA |		/* WPA/RSN */
4461 #ifdef LKPI_80211_WME
4462 	    IEEE80211_C_WME |
4463 #endif
4464 #if 0
4465 	    IEEE80211_C_PMGT |
4466 #endif
4467 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
4468 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
4469 	    ;
4470 #if 0
4471 	/* Scanning is a different kind of beast to re-work. */
4472 	ic->ic_caps |= IEEE80211_C_BGSCAN;
4473 #endif
4474 	if (lhw->ops->hw_scan) {
4475 		/*
4476 		 * Advertise full-offload scanning.
4477 		 *
4478 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4479 		 * we essentially disable hw_scan for all drivers not setting
4480 		 * the flag.
4481 		 */
4482 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4483 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4484 	}
4485 
4486 	/*
4487 	 * The wiphy variables report bitmasks of avail antennas.
4488 	 * (*get_antenna) get the current bitmask sets which can be
4489 	 * altered by (*set_antenna) for some drivers.
4490 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4491 	 */
4492 	if (hw->wiphy->available_antennas_rx ||
4493 	    hw->wiphy->available_antennas_tx) {
4494 		uint32_t rxs, txs;
4495 
4496 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4497 			ic->ic_rxstream = bitcount32(rxs);
4498 			ic->ic_txstream = bitcount32(txs);
4499 		}
4500 	}
4501 
4502 	ic->ic_cryptocaps = 0;
4503 #ifdef LKPI_80211_HW_CRYPTO
4504 	if (hw->wiphy->n_cipher_suites > 0) {
4505 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
4506 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
4507 			    hw->wiphy->cipher_suites[i]);
4508 	}
4509 #endif
4510 
4511 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
4512 	    ic->ic_channels);
4513 
4514 	ieee80211_ifattach(ic);
4515 
4516 	ic->ic_update_mcast = lkpi_ic_update_mcast;
4517 	ic->ic_update_promisc = lkpi_ic_update_promisc;
4518 	ic->ic_update_chw = lkpi_ic_update_chw;
4519 	ic->ic_parent = lkpi_ic_parent;
4520 	ic->ic_scan_start = lkpi_ic_scan_start;
4521 	ic->ic_scan_end = lkpi_ic_scan_end;
4522 	ic->ic_set_channel = lkpi_ic_set_channel;
4523 	ic->ic_transmit = lkpi_ic_transmit;
4524 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
4525 	ic->ic_vap_create = lkpi_ic_vap_create;
4526 	ic->ic_vap_delete = lkpi_ic_vap_delete;
4527 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
4528 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
4529 
4530 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4531 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4532 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4533 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4534 
4535 	lhw->ic_node_alloc = ic->ic_node_alloc;
4536 	ic->ic_node_alloc = lkpi_ic_node_alloc;
4537 	lhw->ic_node_init = ic->ic_node_init;
4538 	ic->ic_node_init = lkpi_ic_node_init;
4539 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
4540 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
4541 	lhw->ic_node_free = ic->ic_node_free;
4542 	ic->ic_node_free = lkpi_ic_node_free;
4543 
4544 #ifdef LKPI_80211_HT
4545 	lhw->ic_recv_action = ic->ic_recv_action;
4546 	ic->ic_recv_action = lkpi_ic_recv_action;
4547 	lhw->ic_send_action = ic->ic_send_action;
4548 	ic->ic_send_action = lkpi_ic_send_action;
4549 
4550 	lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
4551 	ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
4552 
4553 	lhw->ic_addba_request = ic->ic_addba_request;
4554 	ic->ic_addba_request = lkpi_ic_addba_request;
4555 	lhw->ic_addba_response = ic->ic_addba_response;
4556 	ic->ic_addba_response = lkpi_ic_addba_response;
4557 	lhw->ic_addba_stop = ic->ic_addba_stop;
4558 	ic->ic_addba_stop = lkpi_ic_addba_stop;
4559 	lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
4560 	ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
4561 
4562 	lhw->ic_bar_response = ic->ic_bar_response;
4563 	ic->ic_bar_response = lkpi_ic_bar_response;
4564 
4565 	lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
4566 	ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
4567 	lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
4568 	ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
4569 #endif
4570 
4571 	lkpi_radiotap_attach(lhw);
4572 
4573 	/*
4574 	 * Assign the first possible channel for now;  seems Realtek drivers
4575 	 * expect one.
4576 	 * Also remember the amount of bands we support and the most rates
4577 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4578 	 */
4579 	lhw->supbands = lhw->max_rates = 0;
4580 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4581 		struct ieee80211_supported_band *supband;
4582 		struct linuxkpi_ieee80211_channel *channels;
4583 
4584 		supband = hw->wiphy->bands[band];
4585 		if (supband == NULL || supband->n_channels == 0)
4586 			continue;
4587 
4588 		lhw->supbands++;
4589 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4590 
4591 		/* If we have a channel, we need to keep counting supbands. */
4592 		if (hw->conf.chandef.chan != NULL)
4593 			continue;
4594 
4595 		channels = supband->channels;
4596 		for (i = 0; i < supband->n_channels; i++) {
4597 
4598 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4599 				continue;
4600 
4601 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
4602 #ifdef LKPI_80211_HT
4603 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
4604 #endif
4605 			    NL80211_CHAN_NO_HT);
4606 			break;
4607 		}
4608 	}
4609 
4610 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4611 
4612 	/* Make sure we do not support more than net80211 is willing to take. */
4613 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4614 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4615 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4616 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4617 	}
4618 
4619 	/*
4620 	 * The maximum supported bitrates on any band + size for
4621 	 * DSSS Parameter Set give our per-band IE size.
4622 	 * SSID is the responsibility of the driver and goes on the side.
4623 	 * The user specified bits coming from the vap go into the
4624 	 * "common ies" fields.
4625 	 */
4626 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4627 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4628 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
4629 
4630 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4631 		/*
4632 		 * net80211 does not seem to support the DSSS Parameter Set but
4633 		 * some of the drivers insert it so calculate the extra fixed
4634 		 * space in.
4635 		 */
4636 		lhw->scan_ie_len += 2 + 1;
4637 	}
4638 
4639 #if defined(LKPI_80211_HT)
4640 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
4641 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
4642 #endif
4643 #if defined(LKPI_80211_VHT)
4644 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
4645 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
4646 #endif
4647 
4648 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
4649 	if (hw->wiphy->max_scan_ie_len > 0) {
4650 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
4651 			goto err;
4652 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
4653 	}
4654 
4655 	if (bootverbose)
4656 		ieee80211_announce(ic);
4657 
4658 	return (0);
4659 err:
4660 	IMPROVE("TODO FIXME CLEANUP");
4661 	return (-EAGAIN);
4662 }
4663 
4664 void
4665 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
4666 {
4667 	struct lkpi_hw *lhw;
4668 	struct ieee80211com *ic;
4669 
4670 	lhw = HW_TO_LHW(hw);
4671 	ic = lhw->ic;
4672 	ieee80211_ifdetach(ic);
4673 }
4674 
4675 void
4676 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
4677     enum ieee80211_iface_iter flags,
4678     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
4679     void *arg)
4680 {
4681 	struct lkpi_hw *lhw;
4682 	struct lkpi_vif *lvif;
4683 	struct ieee80211_vif *vif;
4684 	bool active, atomic, nin_drv;
4685 
4686 	lhw = HW_TO_LHW(hw);
4687 
4688 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
4689 	    IEEE80211_IFACE_ITER_RESUME_ALL|
4690 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4691 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
4692 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
4693 		    __func__, flags);
4694 	}
4695 
4696 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
4697 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
4698 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
4699 
4700 	if (atomic)
4701 		LKPI_80211_LHW_LVIF_LOCK(lhw);
4702 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4703 		struct ieee80211vap *vap;
4704 
4705 		vif = LVIF_TO_VIF(lvif);
4706 
4707 		/*
4708 		 * If we want "active" interfaces, we need to distinguish on
4709 		 * whether the driver knows about them or not to be able to
4710 		 * handle the "resume" case correctly.  Skip the ones the
4711 		 * driver does not know about.
4712 		 */
4713 		if (active && !lvif->added_to_drv &&
4714 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
4715 			continue;
4716 
4717 		/*
4718 		 * If we shall skip interfaces not added to the driver do so
4719 		 * if we haven't yet.
4720 		 */
4721 		if (nin_drv && !lvif->added_to_drv)
4722 			continue;
4723 
4724 		/*
4725 		 * Run the iterator function if we are either not asking
4726 		 * asking for active only or if the VAP is "running".
4727 		 */
4728 		/* XXX-BZ probably should have state in the lvif as well. */
4729 		vap = LVIF_TO_VAP(lvif);
4730 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
4731 			iterfunc(arg, vif->addr, vif);
4732 	}
4733 	if (atomic)
4734 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4735 }
4736 
4737 void
4738 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
4739     struct ieee80211_vif *vif,
4740     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
4741         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
4742     void *arg)
4743 {
4744 
4745 	UNIMPLEMENTED;
4746 }
4747 
4748 void
4749 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
4750     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
4751 	void *),
4752     void *arg)
4753 {
4754 	struct lkpi_hw *lhw;
4755 	struct lkpi_vif *lvif;
4756 	struct ieee80211_vif *vif;
4757 	struct lkpi_chanctx *lchanctx;
4758 
4759 	KASSERT(hw != NULL && iterfunc != NULL,
4760 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4761 
4762 	lhw = HW_TO_LHW(hw);
4763 
4764 	IMPROVE("lchanctx should be its own list somewhere");
4765 
4766 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4767 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4768 
4769 		vif = LVIF_TO_VIF(lvif);
4770 		if (vif->chanctx_conf == NULL)
4771 			continue;
4772 
4773 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4774 		if (!lchanctx->added_to_drv)
4775 			continue;
4776 
4777 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
4778 	}
4779 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4780 }
4781 
4782 void
4783 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
4784    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
4785 {
4786 	struct lkpi_hw *lhw;
4787 	struct lkpi_vif *lvif;
4788 	struct lkpi_sta *lsta;
4789 	struct ieee80211_sta *sta;
4790 
4791 	KASSERT(hw != NULL && iterfunc != NULL,
4792 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4793 
4794 	lhw = HW_TO_LHW(hw);
4795 
4796 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4797 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4798 
4799 		LKPI_80211_LVIF_LOCK(lvif);
4800 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4801 			if (!lsta->added_to_drv)
4802 				continue;
4803 			sta = LSTA_TO_STA(lsta);
4804 			iterfunc(arg, sta);
4805 		}
4806 		LKPI_80211_LVIF_UNLOCK(lvif);
4807 	}
4808 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4809 }
4810 
4811 struct linuxkpi_ieee80211_regdomain *
4812 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4813 {
4814 	struct linuxkpi_ieee80211_regdomain *regd;
4815 
4816 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4817 	    GFP_KERNEL);
4818 	return (regd);
4819 }
4820 
4821 int
4822 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
4823     struct linuxkpi_ieee80211_regdomain *regd)
4824 {
4825 	struct lkpi_hw *lhw;
4826 	struct ieee80211com *ic;
4827 	struct ieee80211_regdomain *rd;
4828 
4829 	lhw = wiphy_priv(wiphy);
4830 	ic = lhw->ic;
4831 
4832 	rd = &ic->ic_regdomain;
4833 	if (rd->isocc[0] == '\0') {
4834 		rd->isocc[0] = regd->alpha2[0];
4835 		rd->isocc[1] = regd->alpha2[1];
4836 	}
4837 
4838 	TODO();
4839 	/* XXX-BZ finish the rest. */
4840 
4841 	return (0);
4842 }
4843 
4844 void
4845 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
4846     struct cfg80211_scan_info *info)
4847 {
4848 	struct lkpi_hw *lhw;
4849 	struct ieee80211com *ic;
4850 	struct ieee80211_scan_state *ss;
4851 
4852 	lhw = wiphy_priv(hw->wiphy);
4853 	ic = lhw->ic;
4854 	ss = ic->ic_scan;
4855 
4856 	ieee80211_scan_done(ss->ss_vap);
4857 
4858 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4859 	free(lhw->hw_req, M_LKPI80211);
4860 	lhw->hw_req = NULL;
4861 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4862 	wakeup(lhw);
4863 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4864 
4865 	return;
4866 }
4867 
4868 static void
4869 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
4870 {
4871 	struct ieee80211_node *ni;
4872 	struct m_tag *mtag;
4873 	int ok;
4874 
4875 	ni = NULL;
4876         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4877 	if (mtag != NULL) {
4878 		struct lkpi_80211_tag_rxni *rxni;
4879 
4880 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4881 		ni = rxni->ni;
4882 	}
4883 
4884 	if (ni != NULL) {
4885 		ok = ieee80211_input_mimo(ni, m);
4886 		ieee80211_free_node(ni);		/* Release the reference. */
4887 		if (ok < 0)
4888 			m_freem(m);
4889 	} else {
4890 		ok = ieee80211_input_mimo_all(lhw->ic, m);
4891 		/* mbuf got consumed. */
4892 	}
4893 
4894 #ifdef LINUXKPI_DEBUG_80211
4895 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4896 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4897 #endif
4898 }
4899 
4900 static void
4901 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
4902 {
4903 	struct lkpi_hw *lhw;
4904 	struct mbufq mq;
4905 	struct mbuf *m;
4906 
4907 	lhw = ctx;
4908 
4909 #ifdef LINUXKPI_DEBUG_80211
4910 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4911 		printf("%s:%d lhw %p pending %d mbuf_qlen %d\n",
4912 		    __func__, __LINE__, lhw, pending, mbufq_len(&lhw->rxq));
4913 #endif
4914 
4915 	mbufq_init(&mq, IFQ_MAXLEN);
4916 
4917 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4918 	mbufq_concat(&mq, &lhw->rxq);
4919 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4920 
4921 	m = mbufq_dequeue(&mq);
4922 	while (m != NULL) {
4923 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
4924 		m = mbufq_dequeue(&mq);
4925 	}
4926 }
4927 
4928 /* For %list see comment towards the end of the function. */
4929 void
4930 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4931     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4932     struct list_head *list __unused)
4933 {
4934 	struct lkpi_hw *lhw;
4935 	struct ieee80211com *ic;
4936 	struct mbuf *m;
4937 	struct skb_shared_info *shinfo;
4938 	struct ieee80211_rx_status *rx_status;
4939 	struct ieee80211_rx_stats rx_stats;
4940 	struct ieee80211_node *ni;
4941 	struct ieee80211vap *vap;
4942 	struct ieee80211_hdr *hdr;
4943 	struct lkpi_sta *lsta;
4944 	int i, offset, ok;
4945 	int8_t rssi;
4946 	bool is_beacon;
4947 
4948 	if (skb->len < 2) {
4949 		/* Need 80211 stats here. */
4950 		IMPROVE();
4951 		goto err;
4952 	}
4953 
4954 	/*
4955 	 * For now do the data copy; we can later improve things. Might even
4956 	 * have an mbuf backing the skb data then?
4957 	 */
4958 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
4959 	if (m == NULL)
4960 		goto err;
4961 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
4962 
4963 	shinfo = skb_shinfo(skb);
4964 	offset = m->m_len;
4965 	for (i = 0; i < shinfo->nr_frags; i++) {
4966 		m_copyback(m, offset, shinfo->frags[i].size,
4967 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
4968 		    shinfo->frags[i].offset);
4969 		offset += shinfo->frags[i].size;
4970 	}
4971 
4972 	rx_status = IEEE80211_SKB_RXCB(skb);
4973 
4974 	hdr = (void *)skb->data;
4975 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4976 
4977 #ifdef LINUXKPI_DEBUG_80211
4978 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
4979 		goto no_trace_beacons;
4980 
4981 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4982 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4983 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
4984 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
4985 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
4986 		    shinfo, shinfo->nr_frags,
4987 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
4988 
4989 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
4990 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
4991 
4992 	/* Implement a dump_rxcb() !!! */
4993 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4994 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
4995 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
4996 			__func__,
4997 			(uintmax_t)rx_status->boottime_ns,
4998 			(uintmax_t)rx_status->mactime,
4999 			rx_status->device_timestamp,
5000 			rx_status->flag,
5001 			rx_status->freq,
5002 			rx_status->bw,
5003 			rx_status->encoding,
5004 			rx_status->ampdu_reference,
5005 			rx_status->band,
5006 			rx_status->chains,
5007 			rx_status->chain_signal[0],
5008 			rx_status->chain_signal[1],
5009 			rx_status->chain_signal[2],
5010 			rx_status->chain_signal[3],
5011 			rx_status->signal,
5012 			rx_status->enc_flags,
5013 			rx_status->he_dcm,
5014 			rx_status->he_gi,
5015 			rx_status->he_ru,
5016 			rx_status->zero_length_psdu_type,
5017 			rx_status->nss,
5018 			rx_status->rate_idx);
5019 no_trace_beacons:
5020 #endif
5021 
5022 	memset(&rx_stats, 0, sizeof(rx_stats));
5023 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
5024 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
5025 	rx_stats.c_nf = -96;
5026 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
5027 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5028 		rssi = rx_status->signal;
5029 	else
5030 		rssi = rx_stats.c_nf;
5031 	/*
5032 	 * net80211 signal strength data are in .5 dBm units relative to
5033 	 * the current noise floor (see comment in ieee80211_node.h).
5034 	 */
5035 	rssi -= rx_stats.c_nf;
5036 	rx_stats.c_rssi = rssi * 2;
5037 	rx_stats.r_flags |= IEEE80211_R_BAND;
5038 	rx_stats.c_band =
5039 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
5040 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
5041 	rx_stats.c_freq = rx_status->freq;
5042 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
5043 
5044 	/* XXX (*sta_statistics)() to get to some of that? */
5045 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
5046 
5047 	lhw = HW_TO_LHW(hw);
5048 	ic = lhw->ic;
5049 
5050 	ok = ieee80211_add_rx_params(m, &rx_stats);
5051 	if (ok == 0) {
5052 		m_freem(m);
5053 		counter_u64_add(ic->ic_ierrors, 1);
5054 		goto err;
5055 	}
5056 
5057 	if (sta != NULL) {
5058 		lsta = STA_TO_LSTA(sta);
5059 		ni = ieee80211_ref_node(lsta->ni);
5060 	} else {
5061 		struct ieee80211_frame_min *wh;
5062 
5063 		wh = mtod(m, struct ieee80211_frame_min *);
5064 		ni = ieee80211_find_rxnode(ic, wh);
5065 		if (ni != NULL)
5066 			lsta = ni->ni_drv_data;
5067 	}
5068 
5069 	if (ni != NULL)
5070 		vap = ni->ni_vap;
5071 	else
5072 		/*
5073 		 * XXX-BZ can we improve this by looking at the frame hdr
5074 		 * or other meta-data passed up?
5075 		 */
5076 		vap = TAILQ_FIRST(&ic->ic_vaps);
5077 
5078 #ifdef LINUXKPI_DEBUG_80211
5079 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5080 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5081 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5082 		    ni, vap, is_beacon ? " beacon" : "");
5083 #endif
5084 
5085 	if (ni != NULL && vap != NULL && is_beacon &&
5086 	    rx_status->device_timestamp > 0 &&
5087 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5088 		struct lkpi_vif *lvif;
5089 		struct ieee80211_vif *vif;
5090 		struct ieee80211_frame *wh;
5091 
5092 		wh = mtod(m, struct ieee80211_frame *);
5093 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5094 			goto skip_device_ts;
5095 
5096 		lvif = VAP_TO_LVIF(vap);
5097 		vif = LVIF_TO_VIF(lvif);
5098 
5099 		IMPROVE("TIMING_BEACON_ONLY?");
5100 		/* mac80211 specific (not net80211) so keep it here. */
5101 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5102 		/*
5103 		 * net80211 should take care of the other information (sync_tsf,
5104 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5105 		 */
5106 skip_device_ts:
5107 		;
5108 	}
5109 
5110 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
5111 	    ieee80211_radiotap_active_vap(vap)) {
5112 		struct lkpi_radiotap_rx_hdr *rtap;
5113 
5114 		rtap = &lhw->rtap_rx;
5115 		rtap->wr_tsft = rx_status->device_timestamp;
5116 		rtap->wr_flags = 0;
5117 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
5118 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5119 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
5120 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5121 #if 0	/* .. or it does not given we strip it below. */
5122 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5123 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
5124 #endif
5125 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
5126 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
5127 		rtap->wr_rate = 0;
5128 		IMPROVE();
5129 		/* XXX TODO status->encoding / rate_index / bw */
5130 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
5131 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
5132 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5133 		rtap->wr_dbm_antsignal = rssi;
5134 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
5135 	}
5136 
5137 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5138 		m_adj(m, -IEEE80211_CRC_LEN);
5139 
5140 #if 0
5141 	if (list != NULL) {
5142 		/*
5143 		* Normally this would be queued up and delivered by
5144 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5145 		* See mt76::mac80211.c as only current possible consumer.
5146 		*/
5147 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5148 	}
5149 #endif
5150 
5151 	/*
5152 	 * Attach meta-information to the mbuf for the deferred RX path.
5153 	 * Currently this is best-effort.  Should we need to be hard,
5154 	 * drop the frame and goto err;
5155 	 */
5156 	if (ni != NULL) {
5157 		struct m_tag *mtag;
5158 		struct lkpi_80211_tag_rxni *rxni;
5159 
5160 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5161 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5162 		if (mtag != NULL) {
5163 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5164 			rxni->ni = ni;		/* We hold a reference. */
5165 			m_tag_prepend(m, mtag);
5166 		}
5167 	}
5168 
5169 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5170 	if (lhw->rxq_stopped) {
5171 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5172 		m_freem(m);
5173 		goto err;
5174 	}
5175 
5176 	mbufq_enqueue(&lhw->rxq, m);
5177 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5178 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5179 
5180 	IMPROVE();
5181 
5182 err:
5183 	/* The skb is ours so we can free it :-) */
5184 	kfree_skb(skb);
5185 }
5186 
5187 uint8_t
5188 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
5189 {
5190 	const struct ieee80211_frame *wh;
5191 	uint8_t tid;
5192 
5193 	/* Linux seems to assume this is a QOS-Data-Frame */
5194 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5195 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5196 	   hdr->frame_control));
5197 
5198 	wh = (const struct ieee80211_frame *)hdr;
5199 	tid = ieee80211_gettid(wh);
5200 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5201 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5202 
5203 	return (tid);
5204 }
5205 
5206 struct wiphy *
5207 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
5208 {
5209 	struct lkpi_wiphy *lwiphy;
5210 
5211 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
5212 	if (lwiphy == NULL)
5213 		return (NULL);
5214 	lwiphy->ops = ops;
5215 
5216 	/* XXX TODO */
5217 	return (LWIPHY_TO_WIPHY(lwiphy));
5218 }
5219 
5220 void
5221 linuxkpi_wiphy_free(struct wiphy *wiphy)
5222 {
5223 	struct lkpi_wiphy *lwiphy;
5224 
5225 	if (wiphy == NULL)
5226 		return;
5227 
5228 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5229 	kfree(lwiphy);
5230 }
5231 
5232 uint32_t
5233 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
5234     enum nl80211_band band)
5235 {
5236 
5237 	switch (band) {
5238 	case NL80211_BAND_2GHZ:
5239 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
5240 		break;
5241 	case NL80211_BAND_5GHZ:
5242 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
5243 		break;
5244 	default:
5245 		/* XXX abort, retry, error, panic? */
5246 		break;
5247 	}
5248 
5249 	return (0);
5250 }
5251 
5252 uint32_t
5253 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
5254 {
5255 
5256 	return (ieee80211_mhz2ieee(freq, 0));
5257 }
5258 
5259 #if 0
5260 static struct lkpi_sta *
5261 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
5262 {
5263 	struct lkpi_sta *lsta, *temp;
5264 
5265 	LKPI_80211_LVIF_LOCK(lvif);
5266 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5267 		if (lsta->ni == ni) {
5268 			LKPI_80211_LVIF_UNLOCK(lvif);
5269 			return (lsta);
5270 		}
5271 	}
5272 	LKPI_80211_LVIF_UNLOCK(lvif);
5273 
5274 	return (NULL);
5275 }
5276 #endif
5277 
5278 struct ieee80211_sta *
5279 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
5280 {
5281 	struct lkpi_vif *lvif;
5282 	struct lkpi_sta *lsta, *temp;
5283 	struct ieee80211_sta *sta;
5284 
5285 	lvif = VIF_TO_LVIF(vif);
5286 
5287 	LKPI_80211_LVIF_LOCK(lvif);
5288 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5289 		sta = LSTA_TO_STA(lsta);
5290 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
5291 			LKPI_80211_LVIF_UNLOCK(lvif);
5292 			return (sta);
5293 		}
5294 	}
5295 	LKPI_80211_LVIF_UNLOCK(lvif);
5296 	return (NULL);
5297 }
5298 
5299 struct ieee80211_sta *
5300 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
5301     const uint8_t *addr, const uint8_t *ourvifaddr)
5302 {
5303 	struct lkpi_hw *lhw;
5304 	struct lkpi_vif *lvif;
5305 	struct lkpi_sta *lsta;
5306 	struct ieee80211_vif *vif;
5307 	struct ieee80211_sta *sta;
5308 
5309 	lhw = wiphy_priv(hw->wiphy);
5310 	sta = NULL;
5311 
5312 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5313 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5314 
5315 		/* XXX-BZ check our address from the vif. */
5316 
5317 		vif = LVIF_TO_VIF(lvif);
5318 		if (ourvifaddr != NULL &&
5319 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
5320 			continue;
5321 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
5322 		if (sta != NULL)
5323 			break;
5324 	}
5325 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5326 
5327 	if (sta != NULL) {
5328 		lsta = STA_TO_LSTA(sta);
5329 		if (!lsta->added_to_drv)
5330 			return (NULL);
5331 	}
5332 
5333 	return (sta);
5334 }
5335 
5336 struct sk_buff *
5337 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
5338     struct ieee80211_txq *txq)
5339 {
5340 	struct lkpi_txq *ltxq;
5341 	struct lkpi_vif *lvif;
5342 	struct sk_buff *skb;
5343 
5344 	skb = NULL;
5345 	ltxq = TXQ_TO_LTXQ(txq);
5346 	ltxq->seen_dequeue = true;
5347 
5348 	if (ltxq->stopped)
5349 		goto stopped;
5350 
5351 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
5352 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
5353 		ltxq->stopped = true;
5354 		goto stopped;
5355 	}
5356 
5357 	IMPROVE("hw(TX_FRAG_LIST)");
5358 
5359 	LKPI_80211_LTXQ_LOCK(ltxq);
5360 	skb = skb_dequeue(&ltxq->skbq);
5361 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5362 
5363 stopped:
5364 	return (skb);
5365 }
5366 
5367 void
5368 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
5369     unsigned long *frame_cnt, unsigned long *byte_cnt)
5370 {
5371 	struct lkpi_txq *ltxq;
5372 	struct sk_buff *skb;
5373 	unsigned long fc, bc;
5374 
5375 	ltxq = TXQ_TO_LTXQ(txq);
5376 
5377 	fc = bc = 0;
5378 	LKPI_80211_LTXQ_LOCK(ltxq);
5379 	skb_queue_walk(&ltxq->skbq, skb) {
5380 		fc++;
5381 		bc += skb->len;
5382 	}
5383 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5384 	if (frame_cnt)
5385 		*frame_cnt = fc;
5386 	if (byte_cnt)
5387 		*byte_cnt = bc;
5388 
5389 	/* Validate that this is doing the correct thing. */
5390 	/* Should we keep track on en/dequeue? */
5391 	IMPROVE();
5392 }
5393 
5394 /*
5395  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
5396  * The latter tries to derive the success status from the info flags
5397  * passed back from the driver.  rawx_mit() saves the ni on the m and the
5398  * m on the skb for us to be able to give feedback to net80211.
5399  */
5400 static void
5401 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5402     int status)
5403 {
5404 	struct ieee80211_node *ni;
5405 	struct mbuf *m;
5406 
5407 	m = skb->m;
5408 	skb->m = NULL;
5409 
5410 	if (m != NULL) {
5411 		ni = m->m_pkthdr.PH_loc.ptr;
5412 		/* Status: 0 is ok, != 0 is error. */
5413 		ieee80211_tx_complete(ni, m, status);
5414 		/* ni & mbuf were consumed. */
5415 	}
5416 }
5417 
5418 void
5419 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5420     int status)
5421 {
5422 
5423 	_lkpi_ieee80211_free_txskb(hw, skb, status);
5424 	kfree_skb(skb);
5425 }
5426 
5427 void
5428 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5429     struct ieee80211_tx_status *txstat)
5430 {
5431 	struct sk_buff *skb;
5432 	struct ieee80211_tx_info *info;
5433 	struct ieee80211_ratectl_tx_status txs;
5434 	struct ieee80211_node *ni;
5435 	int status;
5436 
5437 	skb = txstat->skb;
5438 	if (skb->m != NULL) {
5439 		struct mbuf *m;
5440 
5441 		m = skb->m;
5442 		ni = m->m_pkthdr.PH_loc.ptr;
5443 		memset(&txs, 0, sizeof(txs));
5444 	} else {
5445 		ni = NULL;
5446 	}
5447 
5448 	info = txstat->info;
5449 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5450 		status = 0;	/* No error. */
5451 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5452 	} else {
5453 		status = 1;
5454 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5455 	}
5456 
5457 	if (ni != NULL) {
5458 		int ridx __unused;
5459 #ifdef LINUXKPI_DEBUG_80211
5460 		int old_rate;
5461 
5462 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5463 #endif
5464 		txs.pktlen = skb->len;
5465 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5466 		if (info->status.rates[0].count > 1) {
5467 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5468 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5469 		}
5470 #if 0		/* Unused in net80211 currently. */
5471 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5472 		txs.final_rate = info->status.rates[0].idx;
5473 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5474 #endif
5475 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5476 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5477 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5478 		}
5479 
5480 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5481 		ieee80211_ratectl_tx_complete(ni, &txs);
5482 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5483 
5484 #ifdef LINUXKPI_DEBUG_80211
5485 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5486 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5487 			    "long_retries %d\n", __func__,
5488 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5489 			    ridx, txs.long_retries);
5490 		}
5491 #endif
5492 	}
5493 
5494 #ifdef LINUXKPI_DEBUG_80211
5495 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5496 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5497 		    "band %u hw_queue %u tx_time_est %d : "
5498 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5499 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5500 		    "tx_time %u flags %#x "
5501 		    "status_driver_data [ %p %p ]\n",
5502 		    __func__, hw, skb, status, info->flags,
5503 		    info->band, info->hw_queue, info->tx_time_est,
5504 		    info->status.rates[0].idx, info->status.rates[0].count,
5505 		    info->status.rates[0].flags,
5506 		    info->status.rates[1].idx, info->status.rates[1].count,
5507 		    info->status.rates[1].flags,
5508 		    info->status.rates[2].idx, info->status.rates[2].count,
5509 		    info->status.rates[2].flags,
5510 		    info->status.rates[3].idx, info->status.rates[3].count,
5511 		    info->status.rates[3].flags,
5512 		    info->status.ack_signal, info->status.ampdu_ack_len,
5513 		    info->status.ampdu_len, info->status.antenna,
5514 		    info->status.tx_time, info->status.flags,
5515 		    info->status.status_driver_data[0],
5516 		    info->status.status_driver_data[1]);
5517 #endif
5518 
5519 	if (txstat->free_list) {
5520 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5521 		list_add_tail(&skb->list, txstat->free_list);
5522 	} else {
5523 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5524 	}
5525 }
5526 
5527 void
5528 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5529 {
5530 	struct ieee80211_tx_status status;
5531 
5532 	memset(&status, 0, sizeof(status));
5533 	status.info = IEEE80211_SKB_CB(skb);
5534 	status.skb = skb;
5535 	/* sta, n_rates, rates, free_list? */
5536 
5537 	ieee80211_tx_status_ext(hw, &status);
5538 }
5539 
5540 /*
5541  * This is an internal bandaid for the moment for the way we glue
5542  * skbs and mbufs together for TX.  Once we have skbs backed by
5543  * mbufs this should go away.
5544  * This is a public function but kept on the private KPI (lkpi_)
5545  * and is not exposed by a header file.
5546  */
5547 static void
5548 lkpi_ieee80211_free_skb_mbuf(void *p)
5549 {
5550 	struct ieee80211_node *ni;
5551 	struct mbuf *m;
5552 
5553 	if (p == NULL)
5554 		return;
5555 
5556 	m = (struct mbuf *)p;
5557 	M_ASSERTPKTHDR(m);
5558 
5559 	ni = m->m_pkthdr.PH_loc.ptr;
5560 	m->m_pkthdr.PH_loc.ptr = NULL;
5561 	if (ni != NULL)
5562 		ieee80211_free_node(ni);
5563 	m_freem(m);
5564 }
5565 
5566 void
5567 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
5568     struct delayed_work *w, int delay)
5569 {
5570 	struct lkpi_hw *lhw;
5571 
5572 	/* Need to make sure hw is in a stable (non-suspended) state. */
5573 	IMPROVE();
5574 
5575 	lhw = HW_TO_LHW(hw);
5576 	queue_delayed_work(lhw->workq, w, delay);
5577 }
5578 
5579 void
5580 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
5581     struct work_struct *w)
5582 {
5583 	struct lkpi_hw *lhw;
5584 
5585 	/* Need to make sure hw is in a stable (non-suspended) state. */
5586 	IMPROVE();
5587 
5588 	lhw = HW_TO_LHW(hw);
5589 	queue_work(lhw->workq, w);
5590 }
5591 
5592 struct sk_buff *
5593 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5594     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5595 {
5596 	struct sk_buff *skb;
5597 	struct ieee80211_frame *wh;
5598 	uint8_t *p;
5599 	size_t len;
5600 
5601 	len = sizeof(*wh);
5602 	len += 2 + ssid_len;
5603 
5604 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5605 	if (skb == NULL)
5606 		return (NULL);
5607 
5608 	skb_reserve(skb, hw->extra_tx_headroom);
5609 
5610 	wh = skb_put_zero(skb, sizeof(*wh));
5611 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5612 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5613 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5614 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5615 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5616 
5617 	p = skb_put(skb, 2 + ssid_len);
5618 	*p++ = IEEE80211_ELEMID_SSID;
5619 	*p++ = ssid_len;
5620 	if (ssid_len > 0)
5621 		memcpy(p, ssid, ssid_len);
5622 
5623 	return (skb);
5624 }
5625 
5626 struct sk_buff *
5627 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
5628     struct ieee80211_vif *vif)
5629 {
5630 	struct lkpi_vif *lvif;
5631 	struct ieee80211vap *vap;
5632 	struct sk_buff *skb;
5633 	struct ieee80211_frame_pspoll *psp;
5634 	uint16_t v;
5635 
5636 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
5637 	if (skb == NULL)
5638 		return (NULL);
5639 
5640 	skb_reserve(skb, hw->extra_tx_headroom);
5641 
5642 	lvif = VIF_TO_LVIF(vif);
5643 	vap = LVIF_TO_VAP(lvif);
5644 
5645 	psp = skb_put_zero(skb, sizeof(*psp));
5646 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
5647 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5648 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
5649 	memcpy(&psp->i_aid, &v, sizeof(v));
5650 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
5651 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
5652 
5653 	return (skb);
5654 }
5655 
5656 struct sk_buff *
5657 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5658     struct ieee80211_vif *vif, int linkid, bool qos)
5659 {
5660 	struct lkpi_vif *lvif;
5661 	struct ieee80211vap *vap;
5662 	struct sk_buff *skb;
5663 	struct ieee80211_frame *nullf;
5664 
5665 	IMPROVE("linkid");
5666 
5667 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
5668 	if (skb == NULL)
5669 		return (NULL);
5670 
5671 	skb_reserve(skb, hw->extra_tx_headroom);
5672 
5673 	lvif = VIF_TO_LVIF(vif);
5674 	vap = LVIF_TO_VAP(lvif);
5675 
5676 	nullf = skb_put_zero(skb, sizeof(*nullf));
5677 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
5678 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
5679 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5680 
5681 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
5682 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
5683 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
5684 
5685 	return (skb);
5686 }
5687 
5688 struct wireless_dev *
5689 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
5690 {
5691 	struct lkpi_vif *lvif;
5692 
5693 	lvif = VIF_TO_LVIF(vif);
5694 	return (&lvif->wdev);
5695 }
5696 
5697 void
5698 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
5699 {
5700 	struct lkpi_vif *lvif;
5701 	struct ieee80211vap *vap;
5702 	enum ieee80211_state nstate;
5703 	int arg;
5704 
5705 	lvif = VIF_TO_LVIF(vif);
5706 	vap = LVIF_TO_VAP(lvif);
5707 
5708 	/*
5709 	 * Go to init; otherwise we need to elaborately check state and
5710 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
5711 	 * Let the statemachine handle all neccessary changes.
5712 	 */
5713 	nstate = IEEE80211_S_INIT;
5714 	arg = 0;	/* Not a valid reason. */
5715 
5716 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5717 	    vif, vap, ieee80211_state_name[vap->iv_state]);
5718 	ieee80211_new_state(vap, nstate, arg);
5719 }
5720 
5721 void
5722 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
5723 {
5724 	struct lkpi_vif *lvif;
5725 	struct ieee80211vap *vap;
5726 
5727 	lvif = VIF_TO_LVIF(vif);
5728 	vap = LVIF_TO_VAP(lvif);
5729 
5730 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5731 	    vif, vap, ieee80211_state_name[vap->iv_state]);
5732 	ieee80211_beacon_miss(vap->iv_ic);
5733 }
5734 
5735 /* -------------------------------------------------------------------------- */
5736 
5737 void
5738 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
5739 {
5740 	struct lkpi_hw *lhw;
5741 	struct lkpi_vif *lvif;
5742 	struct ieee80211_vif *vif;
5743 	int ac_count, ac;
5744 
5745 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5746 	    __func__, qnum, hw->queues, hw));
5747 
5748 	lhw = wiphy_priv(hw->wiphy);
5749 
5750 	/* See lkpi_ic_vap_create(). */
5751 	if (hw->queues >= IEEE80211_NUM_ACS)
5752 		ac_count = IEEE80211_NUM_ACS;
5753 	else
5754 		ac_count = 1;
5755 
5756 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5757 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5758 
5759 		vif = LVIF_TO_VIF(lvif);
5760 		for (ac = 0; ac < ac_count; ac++) {
5761 			IMPROVE_TXQ("LOCKING");
5762 			if (qnum == vif->hw_queue[ac]) {
5763 #ifdef LINUXKPI_DEBUG_80211
5764 				/*
5765 				 * For now log this to better understand
5766 				 * how this is supposed to work.
5767 				 */
5768 				if (lvif->hw_queue_stopped[ac] &&
5769 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
5770 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
5771 					    "lvif %p vif %p ac %d qnum %d already "
5772 					    "stopped\n", __func__, __LINE__,
5773 					    lhw, hw, lvif, vif, ac, qnum);
5774 #endif
5775 				lvif->hw_queue_stopped[ac] = true;
5776 			}
5777 		}
5778 	}
5779 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5780 }
5781 
5782 void
5783 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
5784 {
5785 	int i;
5786 
5787 	IMPROVE_TXQ("Locking; do we need further info?");
5788 	for (i = 0; i < hw->queues; i++)
5789 		linuxkpi_ieee80211_stop_queue(hw, i);
5790 }
5791 
5792 
5793 static void
5794 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
5795 {
5796 	struct lkpi_hw *lhw;
5797 	struct lkpi_vif *lvif;
5798 	struct lkpi_sta *lsta;
5799 	int ac_count, ac, tid;
5800 
5801 	/* See lkpi_ic_vap_create(). */
5802 	if (hw->queues >= IEEE80211_NUM_ACS)
5803 		ac_count = IEEE80211_NUM_ACS;
5804 	else
5805 		ac_count = 1;
5806 
5807 	lhw = wiphy_priv(hw->wiphy);
5808 
5809 	IMPROVE_TXQ("Locking");
5810 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5811 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5812 		struct ieee80211_vif *vif;
5813 
5814 		vif = LVIF_TO_VIF(lvif);
5815 		for (ac = 0; ac < ac_count; ac++) {
5816 
5817 			if (hwq == vif->hw_queue[ac]) {
5818 
5819 				/* XXX-BZ what about software scan? */
5820 
5821 #ifdef LINUXKPI_DEBUG_80211
5822 				/*
5823 				 * For now log this to better understand
5824 				 * how this is supposed to work.
5825 				 */
5826 				if (!lvif->hw_queue_stopped[ac] &&
5827 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
5828 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
5829 					    "lvif %p vif %p ac %d hw_q not stopped\n",
5830 					    __func__, __LINE__,
5831 					    lhw, hw, lvif, vif, ac);
5832 #endif
5833 				lvif->hw_queue_stopped[ac] = false;
5834 
5835 				LKPI_80211_LVIF_LOCK(lvif);
5836 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
5837 					struct ieee80211_sta *sta;
5838 
5839 					sta = LSTA_TO_STA(lsta);
5840 					for (tid = 0; tid < nitems(sta->txq); tid++) {
5841 						struct lkpi_txq *ltxq;
5842 
5843 						if (sta->txq[tid] == NULL)
5844 							continue;
5845 
5846 						if (sta->txq[tid]->ac != ac)
5847 							continue;
5848 
5849 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5850 						if (!ltxq->stopped)
5851 							continue;
5852 
5853 						ltxq->stopped = false;
5854 
5855 						/* XXX-BZ see when this explodes with all the locking. taskq? */
5856 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
5857 					}
5858 				}
5859 				LKPI_80211_LVIF_UNLOCK(lvif);
5860 			}
5861 		}
5862 	}
5863 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5864 }
5865 
5866 void
5867 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
5868 {
5869 	int i;
5870 
5871 	IMPROVE_TXQ("Is this all/enough here?");
5872 	for (i = 0; i < hw->queues; i++)
5873 		lkpi_ieee80211_wake_queues(hw, i);
5874 }
5875 
5876 void
5877 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
5878 {
5879 
5880 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
5881 	    __func__, qnum, hw->queues, hw));
5882 
5883 	lkpi_ieee80211_wake_queues(hw, qnum);
5884 }
5885 
5886 /* This is just hardware queues. */
5887 void
5888 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
5889 {
5890 	struct lkpi_hw *lhw;
5891 
5892 	lhw = HW_TO_LHW(hw);
5893 
5894 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
5895 	IMPROVE_TXQ("LOCKING");
5896 	if (++lhw->txq_generation[ac] == 0)
5897 		lhw->txq_generation[ac]++;
5898 }
5899 
5900 struct ieee80211_txq *
5901 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
5902 {
5903 	struct lkpi_hw *lhw;
5904 	struct ieee80211_txq *txq;
5905 	struct lkpi_txq *ltxq;
5906 
5907 	lhw = HW_TO_LHW(hw);
5908 	txq = NULL;
5909 
5910 	IMPROVE_TXQ("LOCKING");
5911 
5912 	/* Check that we are scheduled. */
5913 	if (lhw->txq_generation[ac] == 0)
5914 		goto out;
5915 
5916 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
5917 	if (ltxq == NULL)
5918 		goto out;
5919 	if (ltxq->txq_generation == lhw->txq_generation[ac])
5920 		goto out;
5921 
5922 	ltxq->txq_generation = lhw->txq_generation[ac];
5923 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
5924 	txq = &ltxq->txq;
5925 	TAILQ_ELEM_INIT(ltxq, txq_entry);
5926 
5927 out:
5928 	return (txq);
5929 }
5930 
5931 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
5932     struct ieee80211_txq *txq, bool withoutpkts)
5933 {
5934 	struct lkpi_hw *lhw;
5935 	struct lkpi_txq *ltxq;
5936 	bool ltxq_empty;
5937 
5938 	ltxq = TXQ_TO_LTXQ(txq);
5939 
5940 	IMPROVE_TXQ("LOCKING");
5941 
5942 	/* Only schedule if work to do or asked to anyway. */
5943 	LKPI_80211_LTXQ_LOCK(ltxq);
5944 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
5945 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5946 	if (!withoutpkts && ltxq_empty)
5947 		goto out;
5948 
5949 	/* Make sure we do not double-schedule. */
5950 	if (ltxq->txq_entry.tqe_next != NULL)
5951 		goto out;
5952 
5953 	lhw = HW_TO_LHW(hw);
5954 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
5955 out:
5956 	return;
5957 }
5958 
5959 void
5960 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
5961     struct ieee80211_txq *txq)
5962 {
5963 	struct lkpi_hw *lhw;
5964 	struct ieee80211_txq *ntxq;
5965 	struct ieee80211_tx_control control;
5966         struct sk_buff *skb;
5967 
5968 	lhw = HW_TO_LHW(hw);
5969 
5970 	LKPI_80211_LHW_TXQ_LOCK(lhw);
5971 	ieee80211_txq_schedule_start(hw, txq->ac);
5972 	do {
5973 		ntxq = ieee80211_next_txq(hw, txq->ac);
5974 		if (ntxq == NULL)
5975 			break;
5976 
5977 		memset(&control, 0, sizeof(control));
5978 		control.sta = ntxq->sta;
5979 		do {
5980 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
5981 			if (skb == NULL)
5982 				break;
5983 			lkpi_80211_mo_tx(hw, &control, skb);
5984 		} while(1);
5985 
5986 		ieee80211_return_txq(hw, ntxq, false);
5987 	} while (1);
5988 	ieee80211_txq_schedule_end(hw, txq->ac);
5989 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
5990 }
5991 
5992 /* -------------------------------------------------------------------------- */
5993 
5994 struct lkpi_cfg80211_bss {
5995 	u_int refcnt;
5996 	struct cfg80211_bss bss;
5997 };
5998 
5999 struct lkpi_cfg80211_get_bss_iter_lookup {
6000 	struct wiphy *wiphy;
6001 	struct linuxkpi_ieee80211_channel *chan;
6002 	const uint8_t *bssid;
6003 	const uint8_t *ssid;
6004 	size_t ssid_len;
6005 	enum ieee80211_bss_type bss_type;
6006 	enum ieee80211_privacy privacy;
6007 
6008 	/*
6009 	 * Something to store a copy of the result as the net80211 scan cache
6010 	 * is not refoucnted so a scan entry might go away any time.
6011 	 */
6012 	bool match;
6013 	struct cfg80211_bss *bss;
6014 };
6015 
6016 static void
6017 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
6018 {
6019 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
6020 	size_t ielen;
6021 
6022 	lookup = arg;
6023 
6024 	/* Do not try to find another match. */
6025 	if (lookup->match)
6026 		return;
6027 
6028 	/* Nothing to store result. */
6029 	if (lookup->bss == NULL)
6030 		return;
6031 
6032 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
6033 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
6034 		/* We have no idea what to compare to as the drivers only request ANY */
6035 		return;
6036 	}
6037 
6038 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
6039 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
6040 		/* We have no idea what to compare to as the drivers only request ANY */
6041 		return;
6042 	}
6043 
6044 	if (lookup->chan != NULL) {
6045 		struct linuxkpi_ieee80211_channel *chan;
6046 
6047 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
6048 		    se->se_chan->ic_freq);
6049 		if (chan == NULL || chan != lookup->chan)
6050 			return;
6051 	}
6052 
6053 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
6054 		return;
6055 
6056 	if (lookup->ssid) {
6057 		if (lookup->ssid_len != se->se_ssid[1] ||
6058 		    se->se_ssid[1] == 0)
6059 			return;
6060 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
6061 			return;
6062 	}
6063 
6064 	ielen = se->se_ies.len;
6065 
6066 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
6067 	    M_LKPI80211, M_NOWAIT | M_ZERO);
6068 	if (lookup->bss->ies == NULL)
6069 		return;
6070 
6071 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
6072 	lookup->bss->ies->len = ielen;
6073 	if (ielen)
6074 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
6075 
6076 	lookup->match = true;
6077 }
6078 
6079 struct cfg80211_bss *
6080 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
6081     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
6082     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
6083 {
6084 	struct lkpi_cfg80211_bss *lbss;
6085 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
6086 	struct lkpi_hw *lhw;
6087 	struct ieee80211vap *vap;
6088 
6089 	lhw = wiphy_priv(wiphy);
6090 
6091 	/* Let's hope we can alloc. */
6092 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
6093 	if (lbss == NULL) {
6094 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
6095 		return (NULL);
6096 	}
6097 
6098 	lookup.wiphy = wiphy;
6099 	lookup.chan = chan;
6100 	lookup.bssid = bssid;
6101 	lookup.ssid = ssid;
6102 	lookup.ssid_len = ssid_len;
6103 	lookup.bss_type = bss_type;
6104 	lookup.privacy = privacy;
6105 	lookup.match = false;
6106 	lookup.bss = &lbss->bss;
6107 
6108 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
6109 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
6110 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
6111 	if (!lookup.match) {
6112 		free(lbss, M_LKPI80211);
6113 		return (NULL);
6114 	}
6115 
6116 	refcount_init(&lbss->refcnt, 1);
6117 	return (&lbss->bss);
6118 }
6119 
6120 void
6121 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
6122 {
6123 	struct lkpi_cfg80211_bss *lbss;
6124 
6125 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
6126 
6127 	/* Free everything again on refcount ... */
6128 	if (refcount_release(&lbss->refcnt)) {
6129 		free(lbss->bss.ies, M_LKPI80211);
6130 		free(lbss, M_LKPI80211);
6131 	}
6132 }
6133 
6134 void
6135 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
6136 {
6137 	struct lkpi_hw *lhw;
6138 	struct ieee80211com *ic;
6139 	struct ieee80211vap *vap;
6140 
6141 	lhw = wiphy_priv(wiphy);
6142 	ic = lhw->ic;
6143 
6144 	/*
6145 	 * If we haven't called ieee80211_ifattach() yet
6146 	 * or there is no VAP, there are no scans to flush.
6147 	 */
6148 	if (ic == NULL ||
6149 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
6150 		return;
6151 
6152 	/* Should only happen on the current one? Not seen it late enough. */
6153 	IEEE80211_LOCK(ic);
6154 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6155 		ieee80211_scan_flush(vap);
6156 	IEEE80211_UNLOCK(ic);
6157 }
6158 
6159 /* -------------------------------------------------------------------------- */
6160 
6161 MODULE_VERSION(linuxkpi_wlan, 1);
6162 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
6163 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6164