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