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