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