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