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