xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 7fdf0e883567d96d27d929c49a74d5fdb84d550b)
1 /*-
2  * Copyright (c) 2020-2021 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 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_media.h>
60 #include <net/ethernet.h>
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_proto.h>
64 #include <net80211/ieee80211_ratectl.h>
65 #include <net80211/ieee80211_radiotap.h>
66 
67 #define	LINUXKPI_NET80211
68 #include <net/mac80211.h>
69 
70 #include <linux/workqueue.h>
71 #include "linux_80211.h"
72 
73 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "Linux KPI 80211 compat");
74 
75 /* -------------------------------------------------------------------------- */
76 /* These are unrelated to 802.11 sysctl bug debugging during 802.11 work so   *
77  * keep them here rather than in a more general file.                         */
78 
79 int debug_skb;
80 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_skb, CTLFLAG_RWTUN,
81     &debug_skb, 0, "SKB debug level");
82 
83 /* -------------------------------------------------------------------------- */
84 
85 int debug_80211;
86 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_80211, CTLFLAG_RWTUN,
87     &debug_80211, 0, "80211 debug Level");
88 
89 #define LINUXKPI_DEBUG_80211
90 #ifdef LINUXKPI_DEBUG_80211
91 #ifndef	D80211_TODO
92 #define	D80211_TODO		0x1
93 #endif
94 #ifndef D80211_IMPROVE
95 #define	D80211_IMPROVE		0x2
96 #endif
97 #define	D80211_TRACE		0x10
98 #define	D80211_TRACEOK		0x20
99 #define	D80211_TRACE_TX		0x100
100 #define	D80211_TRACE_TX_DUMP	0x200
101 #define	D80211_TRACE_RX		0x1000
102 #define	D80211_TRACE_RX_DUMP	0x2000
103 #define	D80211_TRACE_RX_BEACONS	0x4000
104 #define	D80211_TRACEX		(D80211_TRACE_TX|D80211_TRACE_RX)
105 #define	D80211_TRACEX_DUMP	(D80211_TRACE_TX_DUMP|D80211_TRACE_RX_DUMP)
106 #define	UNIMPLEMENTED		if (debug_80211 & D80211_TODO)		\
107     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
108 #define	TRACEOK()		if (debug_80211 & D80211_TRACEOK)	\
109     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
110 #else
111 #define	UNIMPLEMENTED		do { } while (0)
112 #define	TRACEOK()		do { } while (0)
113 #endif
114 
115 /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
116 #ifndef PREP_TX_INFO_DURATION
117 #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
118 #endif
119 
120 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
121 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
122 
123 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
124 	/*
125 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
126 	 * mac80211 and to the driver or net80211.
127 	 * Can we pass some on 1:1? Need to compare the (*f)().
128 	 */
129 };
130 
131 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
132     struct ieee80211_node *);
133 static void lkpi_80211_txq_task(void *, int);
134 static void lkpi_ieee80211_free_skb_mbuf(void *);
135 
136 static enum nl80211_band
137 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
138 {
139 
140 	if (IEEE80211_IS_CHAN_2GHZ(c))
141 		return (NL80211_BAND_2GHZ);
142 	else if (IEEE80211_IS_CHAN_5GHZ(c))
143 		return (NL80211_BAND_5GHZ);
144 #ifdef __notyet__
145 	else if ()
146 		return (NL80211_BAND_6GHZ);
147 	else if ()
148 		return (NL80211_BAND_60GHZ);
149 	else if (IEEE80211_IS_CHAN_GSM(c))
150 		return (NL80211_BAND_XXX);
151 #endif
152 	else
153 		panic("%s: unsupported band. c %p flags %#x\n",
154 		    __func__, c, c->ic_flags);
155 }
156 
157 static uint32_t
158 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
159 {
160 
161 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
162 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
163 	switch (band) {
164 	case NL80211_BAND_2GHZ:
165 		return (IEEE80211_CHAN_2GHZ);
166 		break;
167 	case NL80211_BAND_5GHZ:
168 		return (IEEE80211_CHAN_5GHZ);
169 		break;
170 	case NL80211_BAND_60GHZ:
171 		break;
172 	case NL80211_BAND_6GHZ:
173 		break;
174 	default:
175 		panic("%s: unsupported band %u\n", __func__, band);
176 		break;
177 	}
178 
179 	IMPROVE();
180 	return (0x00);
181 }
182 
183 static enum ieee80211_ac_numbers
184 lkpi_ac_net_to_l80211(int ac)
185 {
186 
187 	switch (ac) {
188 	case WME_AC_VO:
189 		return (IEEE80211_AC_VO);
190 	case WME_AC_VI:
191 		return (IEEE80211_AC_VI);
192 	case WME_AC_BE:
193 		return (IEEE80211_AC_BE);
194 	case WME_AC_BK:
195 		return (IEEE80211_AC_BK);
196 	default:
197 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
198 		return (IEEE80211_AC_BE);
199 	}
200 }
201 
202 static enum nl80211_iftype
203 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
204 {
205 
206 	switch (opmode) {
207 	case IEEE80211_M_IBSS:
208 		return (NL80211_IFTYPE_ADHOC);
209 		break;
210 	case IEEE80211_M_STA:
211 		return (NL80211_IFTYPE_STATION);
212 		break;
213 	case IEEE80211_M_WDS:
214 		return (NL80211_IFTYPE_WDS);
215 		break;
216 	case IEEE80211_M_HOSTAP:
217 		return (NL80211_IFTYPE_AP);
218 		break;
219 	case IEEE80211_M_MONITOR:
220 		return (NL80211_IFTYPE_MONITOR);
221 		break;
222 	case IEEE80211_M_MBSS:
223 		return (NL80211_IFTYPE_MESH_POINT);
224 		break;
225 	case IEEE80211_M_AHDEMO:
226 		/* FALLTHROUGH */
227 	default:
228 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
229 		/* FALLTHROUGH */
230 	}
231 	return (NL80211_IFTYPE_UNSPECIFIED);
232 }
233 
234 #ifdef __notyet__
235 static uint32_t
236 lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
237 {
238 
239 	switch (wlan_cipher_suite) {
240 	case WLAN_CIPHER_SUITE_WEP40:
241 		return (IEEE80211_CRYPTO_WEP);
242 	case WLAN_CIPHER_SUITE_TKIP:
243 		return (IEEE80211_CRYPTO_TKIP);
244 	case WLAN_CIPHER_SUITE_CCMP:
245 		return (IEEE80211_CIPHER_AES_CCM);
246 	case WLAN_CIPHER_SUITE_WEP104:
247 		return (IEEE80211_CRYPTO_WEP);
248 	case WLAN_CIPHER_SUITE_AES_CMAC:
249 	case WLAN_CIPHER_SUITE_GCMP:
250 	case WLAN_CIPHER_SUITE_GCMP_256:
251 	case WLAN_CIPHER_SUITE_CCMP_256:
252 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
253 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
254 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
255 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
256 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
257 		break;
258 	default:
259 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
260 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
261 	}
262 
263 	return (0);
264 }
265 #endif
266 
267 #ifdef TRY_HW_CRYPTO
268 static uint32_t
269 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
270 {
271 
272 	switch (cipher) {
273 	case IEEE80211_CIPHER_TKIP:
274 		return (WLAN_CIPHER_SUITE_TKIP);
275 	case IEEE80211_CIPHER_AES_CCM:
276 		return (WLAN_CIPHER_SUITE_CCMP);
277 	case IEEE80211_CIPHER_WEP:
278 		if (keylen < 8)
279 			return (WLAN_CIPHER_SUITE_WEP40);
280 		else
281 			return (WLAN_CIPHER_SUITE_WEP104);
282 		break;
283 	case IEEE80211_CIPHER_AES_OCB:
284 	case IEEE80211_CIPHER_TKIPMIC:
285 	case IEEE80211_CIPHER_CKIP:
286 	case IEEE80211_CIPHER_NONE:
287 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
288 		break;
289 	default:
290 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
291 	};
292 	return (0);
293 }
294 #endif
295 
296 #ifdef __notyet__
297 static enum ieee80211_sta_state
298 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
299 {
300 
301 	/*
302 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
303 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
304 	 */
305 	switch (state) {
306 	case IEEE80211_S_INIT:
307 		return (IEEE80211_STA_NOTEXIST);
308 	case IEEE80211_S_SCAN:
309 		return (IEEE80211_STA_NONE);
310 	case IEEE80211_S_AUTH:
311 		return (IEEE80211_STA_AUTH);
312 	case IEEE80211_S_ASSOC:
313 		return (IEEE80211_STA_ASSOC);
314 	case IEEE80211_S_RUN:
315 		return (IEEE80211_STA_AUTHORIZED);
316 	case IEEE80211_S_CAC:
317 	case IEEE80211_S_CSA:
318 	case IEEE80211_S_SLEEP:
319 	default:
320 		UNIMPLEMENTED;
321 	};
322 
323 	return (IEEE80211_STA_NOTEXIST);
324 }
325 #endif
326 
327 static struct linuxkpi_ieee80211_channel *
328 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
329     struct ieee80211_channel *c)
330 {
331 	struct ieee80211_hw *hw;
332 	struct linuxkpi_ieee80211_channel *channels;
333 	enum nl80211_band band;
334 	int i, nchans;
335 
336 	hw = LHW_TO_HW(lhw);
337 	band = lkpi_net80211_chan_to_nl80211_band(c);
338 	if (hw->wiphy->bands[band] == NULL)
339 		return (NULL);
340 
341 	nchans = hw->wiphy->bands[band]->n_channels;
342 	if (nchans <= 0)
343 		return (NULL);
344 
345 	channels = hw->wiphy->bands[band]->channels;
346 	for (i = 0; i < nchans; i++) {
347 		if (channels[i].hw_value == c->ic_ieee)
348 			return (&channels[i]);
349 	}
350 
351 	return (NULL);
352 }
353 
354 static struct linuxkpi_ieee80211_channel *
355 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
356 {
357 	struct linuxkpi_ieee80211_channel *chan;
358 	struct ieee80211_channel *c;
359 	struct lkpi_hw *lhw;
360 
361 	chan = NULL;
362 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
363 		c = ni->ni_chan;
364 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
365 		c = ic->ic_bsschan;
366 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
367 		c = ic->ic_curchan;
368 	else
369 		c = NULL;
370 
371 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
372 		lhw = ic->ic_softc;
373 		chan = lkpi_find_lkpi80211_chan(lhw, c);
374 	}
375 
376 	return (chan);
377 }
378 
379 #ifdef TRY_HW_CRYPTO
380 static int
381 _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
382     enum set_key_cmd cmd)
383 {
384 	struct ieee80211com *ic;
385 	struct lkpi_hw *lhw;
386 	struct ieee80211_hw *hw;
387 	struct lkpi_vif *lvif;
388 	struct ieee80211_vif *vif;
389 	struct ieee80211_sta *sta;
390 	struct ieee80211_node *ni;
391 	struct ieee80211_key_conf *kc;
392 	int error;
393 
394 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
395 
396 	ic = vap->iv_ic;
397 	lhw = ic->ic_softc;
398 	hw = LHW_TO_HW(lhw);
399 	lvif = VAP_TO_LVIF(vap);
400 	vif = LVIF_TO_VIF(lvif);
401 
402 	memset(&kc, 0, sizeof(kc));
403 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
404 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
405 	    k->wk_cipher->ic_cipher, k->wk_keylen);
406 	kc->keyidx = k->wk_keyix;
407 #if 0
408 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
409 #endif
410 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
411 	kc->keylen = k->wk_keylen;
412 	memcpy(kc->key, k->wk_key, k->wk_keylen);
413 
414 	switch (kc->cipher) {
415 	case WLAN_CIPHER_SUITE_CCMP:
416 		kc->iv_len = k->wk_cipher->ic_header;
417 		kc->icv_len = k->wk_cipher->ic_trailer;
418 		break;
419 	case WLAN_CIPHER_SUITE_TKIP:
420 	default:
421 		IMPROVE();
422 		return (0);
423 	};
424 
425 	ni = vap->iv_bss;
426 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
427 	if (sta != NULL) {
428 		struct lkpi_sta *lsta;
429 
430 		lsta = STA_TO_LSTA(sta);
431 		lsta->kc = kc;
432 	}
433 
434 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
435 	if (error != 0) {
436 		/* XXX-BZ leaking kc currently */
437 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
438 		return (0);
439 	} else {
440 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
441 		    "flags %#10x\n", __func__,
442 		    kc->keyidx, kc->hw_key_idx, kc->flags);
443 		return (1);
444 	}
445 }
446 
447 static int
448 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
449 {
450 
451 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
452 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
453 }
454 static  int
455 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
456 {
457 
458 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
459 }
460 #endif
461 
462 static u_int
463 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
464 {
465 	struct netdev_hw_addr_list *mc_list;
466 	struct netdev_hw_addr *addr;
467 
468 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
469 	    __func__, arg, sdl, cnt));
470 
471 	mc_list = arg;
472 	/* If it is on the list already skip it. */
473 	netdev_hw_addr_list_for_each(addr, mc_list) {
474 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
475 			return (0);
476 	}
477 
478 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
479 	if (addr == NULL)
480 		return (0);
481 
482 	INIT_LIST_HEAD(&addr->addr_list);
483 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
484 	/* XXX this should be a netdev function? */
485 	list_add(&addr->addr_list, &mc_list->addr_list);
486 	mc_list->count++;
487 
488 	if (debug_80211 & D80211_TRACE)
489 		printf("%s:%d: mc_list count %d: added %6D\n",
490 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
491 
492 	return (1);
493 }
494 
495 static void
496 lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
497 {
498 	struct lkpi_hw *lhw;
499 	struct ieee80211_hw *hw;
500 	struct netdev_hw_addr_list mc_list;
501 	struct list_head *le, *next;
502 	struct netdev_hw_addr *addr;
503 	struct ieee80211vap *vap;
504 	u64 mc;
505 	unsigned int changed_flags, total_flags;
506 
507 	lhw = ic->ic_softc;
508 
509 	if (lhw->ops->prepare_multicast == NULL ||
510 	    lhw->ops->configure_filter == NULL)
511 		return;
512 
513 	if (!lhw->update_mc && !force)
514 		return;
515 
516 	changed_flags = total_flags = 0;
517 	mc_list.count = 0;
518 	INIT_LIST_HEAD(&mc_list.addr_list);
519 	if (ic->ic_allmulti == 0) {
520 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
521 			if_foreach_llmaddr(vap->iv_ifp,
522 			    lkpi_ic_update_mcast_copy, &mc_list);
523 	} else {
524 		changed_flags |= FIF_ALLMULTI;
525 	}
526 
527 	hw = LHW_TO_HW(lhw);
528 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
529 	/*
530 	 * XXX-BZ make sure to get this sorted what is a change,
531 	 * what gets all set; what was already set?
532 	 */
533 	total_flags = changed_flags;
534 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
535 
536 	if (debug_80211 & D80211_TRACE)
537 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
538 		    __func__, changed_flags, mc_list.count, total_flags);
539 
540 	if (mc_list.count != 0) {
541 		list_for_each_safe(le, next, &mc_list.addr_list) {
542 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
543 			free(addr, M_LKPI80211);
544 			mc_list.count--;
545 		}
546 	}
547 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
548 	    __func__, &mc_list, mc_list.count));
549 }
550 
551 const uint8_t tid_to_mac80211_ac[] = {
552 	IEEE80211_AC_BE,
553 	IEEE80211_AC_BK,
554 	IEEE80211_AC_BK,
555 	IEEE80211_AC_BE,
556 	IEEE80211_AC_VI,
557 	IEEE80211_AC_VI,
558 	IEEE80211_AC_VO,
559 	IEEE80211_AC_VO,
560 #if 0
561 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
562 #endif
563 };
564 
565 static void
566 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
567 {
568 	struct ieee80211_hw *hw;
569 	int error;
570 
571 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0)
572 		return;
573 
574 	hw = LHW_TO_HW(lhw);
575 
576 	IEEE80211_UNLOCK(lhw->ic);
577 	LKPI_80211_LHW_LOCK(lhw);
578 	/* Need to cancel the scan. */
579 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
580 
581 	/* Need to make sure we see ieee80211_scan_completed. */
582 	error = msleep(lhw, &lhw->mtx, 0, "lhwscanstop", hz/2);
583 	LKPI_80211_LHW_UNLOCK(lhw);
584 	IEEE80211_LOCK(lhw->ic);
585 
586 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0)
587 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
588 		    __func__, error, lhw, vif);
589 }
590 
591 static void
592 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
593 {
594 	struct lkpi_hw *lhw;
595 	int error;
596 	bool old;
597 
598 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
599 	if (old == new)
600 		return;
601 
602 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
603 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
604 	if (error != 0 && error != EOPNOTSUPP) {
605 		lhw = HW_TO_LHW(hw);
606 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
607 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
608 	}
609 }
610 
611 static void
612 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
613     struct lkpi_hw *lhw)
614 {
615 	sta->aid = 0;
616 	if (vif->bss_conf.assoc) {
617 		struct ieee80211_hw *hw;
618 		enum ieee80211_bss_changed changed;
619 
620 		lhw->update_mc = true;
621 		lkpi_update_mcast_filter(lhw->ic, true);
622 
623 		changed = 0;
624 		vif->bss_conf.assoc = false;
625 		vif->bss_conf.aid = 0;
626 		changed |= BSS_CHANGED_ASSOC;
627 		IMPROVE();
628 		hw = LHW_TO_HW(lhw);
629 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
630 		    changed);
631 
632 		lkpi_hw_conf_idle(hw, true);
633 	}
634 }
635 
636 static void
637 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
638     bool dequeue_seen, bool no_emptyq)
639 {
640 	struct lkpi_txq *ltxq;
641 	int tid;
642 
643 	/* Wake up all queues to know they are allocated in the driver. */
644 	for (tid = 0; tid < nitems(sta->txq); tid++) {
645 
646 			if (tid == IEEE80211_NUM_TIDS) {
647 				IMPROVE("station specific?");
648 				if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
649 					continue;
650 			} else if (tid >= hw->queues)
651 				continue;
652 
653 			if (sta->txq[tid] == NULL)
654 				continue;
655 
656 			ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
657 			if (dequeue_seen && !ltxq->seen_dequeue)
658 				continue;
659 
660 			if (no_emptyq && skb_queue_empty(&ltxq->skbq))
661 				continue;
662 
663 			lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
664 	}
665 }
666 
667 /* -------------------------------------------------------------------------- */
668 
669 static int
670 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
671 {
672 
673 	return (0);
674 }
675 
676 /* lkpi_iv_newstate() handles the stop scan case generally. */
677 #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
678 
679 static int
680 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
681 {
682 	struct linuxkpi_ieee80211_channel *chan;
683 	struct ieee80211_chanctx_conf *conf;
684 	struct lkpi_hw *lhw;
685 	struct ieee80211_hw *hw;
686 	struct lkpi_vif *lvif;
687 	struct ieee80211_vif *vif;
688 	struct ieee80211_node *ni;
689 	struct lkpi_sta *lsta;
690 	struct ieee80211_sta *sta;
691 	enum ieee80211_bss_changed bss_changed;
692 	struct ieee80211_prep_tx_info prep_tx_info;
693 	uint32_t changed;
694 	int error;
695 
696 	chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
697 	if (chan == NULL) {
698 		ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
699 		return (ESRCH);
700 	}
701 
702 	lhw = vap->iv_ic->ic_softc;
703 	hw = LHW_TO_HW(lhw);
704 	lvif = VAP_TO_LVIF(vap);
705 	vif = LVIF_TO_VIF(lvif);
706 
707 	IEEE80211_UNLOCK(vap->iv_ic);
708 
709 	/* Add chanctx (or if exists, change it). */
710 	if (vif->chanctx_conf != NULL) {
711 		conf = vif->chanctx_conf;
712 		IMPROVE("diff changes for changed, working on live copy, rcu");
713 	} else {
714 		/* Keep separate alloc as in Linux this is rcu managed? */
715 		conf = malloc(sizeof(*conf) + hw->chanctx_data_size,
716 		    M_LKPI80211, M_WAITOK | M_ZERO);
717 	}
718 
719 	conf->rx_chains_dynamic = 1;
720 	conf->rx_chains_static = 1;
721 	conf->radar_enabled =
722 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
723 	conf->def.chan = chan;
724 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
725 	conf->def.center_freq1 = chan->center_freq;
726 	conf->def.center_freq2 = 0;
727 	/* Responder ... */
728 	conf->min_def.chan = chan;
729 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
730 	conf->min_def.center_freq1 = chan->center_freq;
731 	conf->min_def.center_freq2 = 0;
732 	IMPROVE("currently 20_NOHT only");
733 
734 	ni = NULL;
735 	error = 0;
736 	if (vif->chanctx_conf != NULL) {
737 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
738 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
739 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
740 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
741 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
742 	} else {
743 		error = lkpi_80211_mo_add_chanctx(hw, conf);
744 		if (error == 0 || error == EOPNOTSUPP) {
745 			vif->bss_conf.chandef.chan = conf->def.chan;
746 			vif->bss_conf.chandef.width = conf->def.width;
747 			vif->bss_conf.chandef.center_freq1 =
748 			    conf->def.center_freq1;
749 			vif->bss_conf.chandef.center_freq2 =
750 			    conf->def.center_freq2;
751 		} else {
752 			goto out;
753 		}
754 		/* Assign vif chanctx. */
755 		if (error == 0)
756 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, conf);
757 		if (error == EOPNOTSUPP)
758 			error = 0;
759 		if (error != 0) {
760 			lkpi_80211_mo_remove_chanctx(hw, conf);
761 			free(conf, M_LKPI80211);
762 			goto out;
763 		}
764 	}
765 	IMPROVE("update radiotap chan fields too");
766 
767 	ni = ieee80211_ref_node(vap->iv_bss);
768 
769 	/* Set bss info (bss_info_changed). */
770 	bss_changed = 0;
771 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, ni->ni_bssid);
772 	bss_changed |= BSS_CHANGED_BSSID;
773 	vif->bss_conf.txpower = ni->ni_txpower;
774 	bss_changed |= BSS_CHANGED_TXPOWER;
775 	vif->bss_conf.idle = false;
776 	bss_changed |= BSS_CHANGED_IDLE;
777 	vif->bss_conf.beacon_int = ni->ni_intval;
778 	/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
779 	if (vif->bss_conf.beacon_int < 16)
780 		vif->bss_conf.beacon_int = 16;
781 	bss_changed |= BSS_CHANGED_BEACON_INT;
782 	/* Should almost assert it is this. */
783 	vif->bss_conf.assoc = false;
784 	vif->bss_conf.aid = 0;
785 	/* RATES */
786 	IMPROVE("bss info: not all needs to come now and rates are missing");
787 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
788 
789 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
790 	lsta = ni->ni_drv_data;
791 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
792 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
793 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
794 	sta = LSTA_TO_STA(lsta);
795 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
796 	if (error != 0) {
797 		IMPROVE("do we need to undo the chan ctx?");
798 		goto out;
799 	}
800 #if 0
801 	lsta->added_to_drv = true;	/* mo manages. */
802 #endif
803 
804 	/*
805 	 * Wakeup all queues now that sta is there so we have as much time to
806 	 * possibly prepare the queue in the driver to be ready for the 1st
807 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
808 	 * is no guarantee or way to check.
809 	 */
810 	lkpi_wake_tx_queues(hw, sta, false, false);
811 
812 	{
813 		int i, count;
814 
815 		for (i = 3 * (hw->queues + 1); i > 0; i--) {
816 			struct lkpi_txq *ltxq;
817 			int tid;
818 
819 			count = 0;
820 			/* Wake up all queues to know they are allocated in the driver. */
821 			for (tid = 0; tid < nitems(sta->txq); tid++) {
822 
823 				if (tid == IEEE80211_NUM_TIDS) {
824 					IMPROVE("station specific?");
825 					if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
826 						continue;
827 				} else if (tid >= hw->queues)
828 					continue;
829 
830 				if (sta->txq[tid] == NULL)
831 					continue;
832 
833 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
834 				if (!ltxq->seen_dequeue)
835 					count++;
836 			}
837 			if (count == 0)
838 				break;
839 #ifdef LINUXKPI_DEBUG_80211
840 			if (count > 0)
841 				ic_printf(vap->iv_ic, "%s: waiting for %d queues "
842 				    "to be allocated by driver\n", __func__, count);
843 #endif
844 			pause("lkpi80211txq", hz/10);
845 		}
846 #ifdef LINUXKPI_DEBUG_80211
847 		if (count > 0)
848 			ic_printf(vap->iv_ic, "%s: %d queues still not "
849 			    "allocated by driver\n", __func__, count);
850 #endif
851 	}
852 
853 	/* Start mgd_prepare_tx. */
854 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
855 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
856 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
857 	lsta->in_mgd = true;
858 
859 	/*
860 	 * What is going to happen next:
861 	 * - <twiddle> .. we should end up in "auth_to_assoc"
862 	 * - event_callback
863 	 * - update sta_state (NONE to AUTH)
864 	 * - mgd_complete_tx
865 	 * (ideally we'd do that on a callback for something else ...)
866 	 */
867 
868 out:
869 	IEEE80211_LOCK(vap->iv_ic);
870 	if (ni != NULL)
871 		ieee80211_free_node(ni);
872 	return (error);
873 }
874 
875 static int
876 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
877 {
878 	struct lkpi_hw *lhw;
879 	struct ieee80211_hw *hw;
880 	struct lkpi_vif *lvif;
881 	struct ieee80211_vif *vif;
882 	struct ieee80211_node *ni;
883 	struct lkpi_sta *lsta;
884 	struct ieee80211_sta *sta;
885 	struct ieee80211_prep_tx_info prep_tx_info;
886 	int error;
887 
888 	lhw = vap->iv_ic->ic_softc;
889 	hw = LHW_TO_HW(lhw);
890 	lvif = VAP_TO_LVIF(vap);
891 	vif = LVIF_TO_VIF(lvif);
892 
893 	/* Keep ni around. */
894 	ni = ieee80211_ref_node(vap->iv_bss);
895 
896 	IEEE80211_UNLOCK(vap->iv_ic);
897 	lsta = ni->ni_drv_data;
898 	sta = LSTA_TO_STA(lsta);
899 
900 	/* flush, drop. */
901 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
902 
903 	IEEE80211_LOCK(vap->iv_ic);
904 
905 	/* Call iv_newstate first so we get potential deauth packet out. */
906 	error = lvif->iv_newstate(vap, nstate, arg);
907 	if (error != 0)
908 		goto outni;
909 
910 	IEEE80211_UNLOCK(vap->iv_ic);
911 
912 	/* Wake tx queues to get packet(s) out. */
913 	lkpi_wake_tx_queues(hw, sta, true, true);
914 
915 	/* flush, no drop */
916 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
917 
918 	/* Take the station and chan ctx down again. */
919 
920 	IMPROVE("event callback with failure?");
921 
922 	/* End mgd_complete_tx. */
923 	if (lsta->in_mgd) {
924 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
925 		prep_tx_info.success = false;
926 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
927 		lsta->in_mgd = false;
928 	}
929 
930 #ifdef __not_yet__
931 	/* sync_rx_queues */
932 	lkpi_80211_mo_sync_rx_queues(hw);
933 
934 	/* sta_pre_rcu_remove */
935         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
936 #endif
937 
938 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
939 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
940 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
941 	    "NONE: %#x\n", __func__, lsta, lsta->state));
942 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
943 	if (error != 0) {
944 		IMPROVE("do we need to undo the chan ctx?");
945 		goto out;
946 	}
947 #if 0
948 	lsta->added_to_drv = false;	/* mo manages. */
949 #endif
950 
951 	IMPROVE("Any bss_info changes to announce?");
952 
953 	if (vif->chanctx_conf != NULL) {
954 		struct ieee80211_chanctx_conf *conf;
955 
956 		conf = vif->chanctx_conf;
957 		/* Remove vif context. */
958 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
959 		/* NB: vif->chanctx_conf is NULL now. */
960 
961 		/* Remove chan ctx. */
962 		lkpi_80211_mo_remove_chanctx(hw, conf);
963 		free(conf, M_LKPI80211);
964 	}
965 
966 	/* No need to start a scan; ic_scan_start should do. */
967 
968 	error = EALREADY;
969 out:
970 	IEEE80211_LOCK(vap->iv_ic);
971 outni:
972 	if (ni != NULL)
973 		ieee80211_free_node(ni);
974 	return (error);
975 }
976 
977 static int
978 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
979 {
980 	int error;
981 
982 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
983 	if (error == 0)
984 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
985 	return (error);
986 }
987 
988 static int
989 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
990 {
991 	struct lkpi_hw *lhw;
992 	struct ieee80211_hw *hw;
993 	struct lkpi_vif *lvif;
994 	struct ieee80211_vif *vif;
995 	struct ieee80211_node *ni;
996 	struct lkpi_sta *lsta;
997 	struct ieee80211_sta *sta;
998 	struct ieee80211_prep_tx_info prep_tx_info;
999 	int error;
1000 
1001 	lhw = vap->iv_ic->ic_softc;
1002 	hw = LHW_TO_HW(lhw);
1003 	lvif = VAP_TO_LVIF(vap);
1004 	vif = LVIF_TO_VIF(lvif);
1005 
1006 	IEEE80211_UNLOCK(vap->iv_ic);
1007 	ni = NULL;
1008 
1009 	/* Finish auth. */
1010 	IMPROVE("event callback");
1011 
1012 	/* Update sta_state (NONE to AUTH). */
1013 	ni = ieee80211_ref_node(vap->iv_bss);
1014 	lsta = ni->ni_drv_data;
1015 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1016 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1017 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1018 	sta = LSTA_TO_STA(lsta);
1019 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1020 	if (error != 0)
1021 		goto out;
1022 
1023 	/* End mgd_complete_tx. */
1024 	if (lsta->in_mgd) {
1025 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1026 		prep_tx_info.success = true;
1027 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1028 		lsta->in_mgd = false;
1029 	}
1030 
1031 	/* Now start assoc. */
1032 
1033 	/* Start mgd_prepare_tx. */
1034 	if (!lsta->in_mgd) {
1035 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1036 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1037 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1038 		lsta->in_mgd = true;
1039 	}
1040 
1041 	/* Wake tx queue to get packet out. */
1042 	lkpi_wake_tx_queues(hw, sta, true, true);
1043 
1044 	/*
1045 	 * <twiddle> .. we end up in "assoc_to_run"
1046 	 * - update sta_state (AUTH to ASSOC)
1047 	 * - conf_tx [all]
1048 	 * - bss_info_changed (assoc, aid, ssid, ..)
1049 	 * - change_chanctx (if needed)
1050 	 * - event_callback
1051 	 * - mgd_complete_tx
1052 	 */
1053 
1054 out:
1055 	IEEE80211_LOCK(vap->iv_ic);
1056 	if (ni != NULL)
1057 		ieee80211_free_node(ni);
1058 	return (error);
1059 }
1060 
1061 /* auth_to_auth, assoc_to_assoc. */
1062 static int
1063 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1064 {
1065 	struct lkpi_hw *lhw;
1066 	struct ieee80211_hw *hw;
1067 	struct lkpi_vif *lvif;
1068 	struct ieee80211_vif *vif;
1069 	struct ieee80211_node *ni;
1070 	struct lkpi_sta *lsta;
1071 	struct ieee80211_prep_tx_info prep_tx_info;
1072 
1073 	lhw = vap->iv_ic->ic_softc;
1074 	hw = LHW_TO_HW(lhw);
1075 	lvif = VAP_TO_LVIF(vap);
1076 	vif = LVIF_TO_VIF(lvif);
1077 
1078 	ni = ieee80211_ref_node(vap->iv_bss);
1079 
1080 	IEEE80211_UNLOCK(vap->iv_ic);
1081 	lsta = ni->ni_drv_data;
1082 
1083 	IMPROVE("event callback?");
1084 
1085 	/* End mgd_complete_tx. */
1086 	if (lsta->in_mgd) {
1087 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1088 		prep_tx_info.success = false;
1089 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1090 		lsta->in_mgd = false;
1091 	}
1092 
1093 	/* Now start assoc. */
1094 
1095 	/* Start mgd_prepare_tx. */
1096 	if (!lsta->in_mgd) {
1097 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1098 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1099 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1100 		lsta->in_mgd = true;
1101 	}
1102 
1103 	IEEE80211_LOCK(vap->iv_ic);
1104 	if (ni != NULL)
1105 		ieee80211_free_node(ni);
1106 
1107 	return (0);
1108 }
1109 
1110 static int
1111 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1112 {
1113 	struct lkpi_hw *lhw;
1114 	struct ieee80211_hw *hw;
1115 	struct lkpi_vif *lvif;
1116 	struct ieee80211_vif *vif;
1117 	struct ieee80211_node *ni;
1118 	struct lkpi_sta *lsta;
1119 	struct ieee80211_sta *sta;
1120 	struct ieee80211_prep_tx_info prep_tx_info;
1121 	int error;
1122 
1123 	lhw = vap->iv_ic->ic_softc;
1124 	hw = LHW_TO_HW(lhw);
1125 	lvif = VAP_TO_LVIF(vap);
1126 	vif = LVIF_TO_VIF(lvif);
1127 
1128 	/* Keep ni around. */
1129 	ni = ieee80211_ref_node(vap->iv_bss);
1130 
1131 	IEEE80211_UNLOCK(vap->iv_ic);
1132 	lsta = ni->ni_drv_data;
1133 	sta = LSTA_TO_STA(lsta);
1134 
1135 	/* End mgd_complete_tx. */
1136 	if (lsta->in_mgd) {
1137 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1138 		prep_tx_info.success = false;
1139 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1140 		lsta->in_mgd = false;
1141 	}
1142 
1143 	/* Update sta and change state (from AUTH) to NONE. */
1144 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1145 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1146 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1147 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1148 	if (error != 0)
1149 		goto out;
1150 
1151 	IMPROVE("anything else?");
1152 
1153 out:
1154 	IEEE80211_LOCK(vap->iv_ic);
1155 	if (ni != NULL)
1156 		ieee80211_free_node(ni);
1157 	return (error);
1158 }
1159 
1160 static int
1161 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1162 {
1163 	int error;
1164 
1165 	error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
1166 	if (error == 0)
1167 		error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1168 	return (error);
1169 }
1170 
1171 static int
1172 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1173 {
1174 	int error;
1175 
1176 	error = lkpi_sta_assoc_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_assoc_to_run(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 	enum ieee80211_bss_changed bss_changed;
1194 	int error;
1195 
1196 	lhw = vap->iv_ic->ic_softc;
1197 	hw = LHW_TO_HW(lhw);
1198 	lvif = VAP_TO_LVIF(vap);
1199 	vif = LVIF_TO_VIF(lvif);
1200 
1201 	IEEE80211_UNLOCK(vap->iv_ic);
1202 	ni = NULL;
1203 
1204 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1205 	    "and to lesser extend ieee80211_notify_node_join");
1206 
1207 	/* Finish assoc. */
1208 	/* Update sta_state (AUTH to ASSOC) and set aid. */
1209 	ni = ieee80211_ref_node(vap->iv_bss);
1210 	lsta = ni->ni_drv_data;
1211 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1212 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1213 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1214 	sta = LSTA_TO_STA(lsta);
1215 	sta->aid = IEEE80211_NODE_AID(ni);
1216 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1217 	if (error != 0)
1218 		goto out;
1219 
1220 	IMPROVE("wme / conf_tx [all]");
1221 
1222 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1223 	bss_changed = 0;
1224 	if (!vif->bss_conf.assoc || vif->bss_conf.aid != IEEE80211_NODE_AID(ni)) {
1225 		vif->bss_conf.assoc = true;
1226 		vif->bss_conf.aid = IEEE80211_NODE_AID(ni);
1227 		bss_changed |= BSS_CHANGED_ASSOC;
1228 	}
1229 	/* We set SSID but this is not BSSID! */
1230 	vif->bss_conf.ssid_len = ni->ni_esslen;
1231 	memcpy(vif->bss_conf.ssid, ni->ni_essid, ni->ni_esslen);
1232 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
1233 	    vif->bss_conf.use_short_preamble) {
1234 		vif->bss_conf.use_short_preamble ^= 1;
1235 		/* bss_changed |= BSS_CHANGED_??? */
1236 	}
1237 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
1238 	    vif->bss_conf.use_short_slot) {
1239 		vif->bss_conf.use_short_slot ^= 1;
1240 		/* bss_changed |= BSS_CHANGED_??? */
1241 	}
1242 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
1243 	    vif->bss_conf.qos) {
1244 		vif->bss_conf.qos ^= 1;
1245 		bss_changed |= BSS_CHANGED_QOS;
1246 	}
1247 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1248 		vif->bss_conf.beacon_int = ni->ni_intval;
1249 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1250 		if (vif->bss_conf.beacon_int < 16)
1251 			vif->bss_conf.beacon_int = 16;
1252 		bss_changed |= BSS_CHANGED_BEACON_INT;
1253 	}
1254 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1255 	    vap->iv_dtim_period > 0) {
1256 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1257 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1258 	}
1259 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1260 
1261 	/* - change_chanctx (if needed)
1262 	 * - event_callback
1263 	 */
1264 
1265 	/* End mgd_complete_tx. */
1266 	if (lsta->in_mgd) {
1267 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1268 		prep_tx_info.success = true;
1269 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1270 		lsta->in_mgd = false;
1271 	}
1272 
1273 	lkpi_hw_conf_idle(hw, false);
1274 
1275 	/*
1276 	 * And then:
1277 	 * - (more packets)?
1278 	 * - set_key
1279 	 * - set_default_unicast_key
1280 	 * - set_key (?)
1281 	 * - ipv6_addr_change (?)
1282 	 */
1283 	/* Prepare_multicast && configure_filter. */
1284 	lhw->update_mc = true;
1285 	lkpi_update_mcast_filter(vap->iv_ic, true);
1286 
1287 	if (!ieee80211_node_is_authorized(ni)) {
1288 		IMPROVE("net80211 does not consider node authorized");
1289 	}
1290 
1291 	/* Update sta_state (ASSOC to AUTHORIZED). */
1292 	ni = ieee80211_ref_node(vap->iv_bss);
1293 	lsta = ni->ni_drv_data;
1294 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1295 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1296 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1297 	sta = LSTA_TO_STA(lsta);
1298 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTHORIZED);
1299 	if (error != 0) {
1300 		IMPROVE("undo some changes?");
1301 		goto out;
1302 	}
1303 
1304 	/* - drv_config (?)
1305 	 * - bss_info_changed
1306 	 * - set_rekey_data (?)
1307 	 *
1308 	 * And now we should be passing packets.
1309 	 */
1310 	IMPROVE("Need that bssid setting, and the keys");
1311 
1312 out:
1313 	IEEE80211_LOCK(vap->iv_ic);
1314 	if (ni != NULL)
1315 		ieee80211_free_node(ni);
1316 	return (error);
1317 }
1318 
1319 static int
1320 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1321 {
1322 	int error;
1323 
1324 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
1325 	if (error == 0)
1326 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
1327 	return (error);
1328 }
1329 
1330 static int
1331 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1332 {
1333 	struct lkpi_hw *lhw;
1334 	struct ieee80211_hw *hw;
1335 	struct lkpi_vif *lvif;
1336 	struct ieee80211_vif *vif;
1337 	struct ieee80211_node *ni;
1338 	struct lkpi_sta *lsta;
1339 	struct ieee80211_sta *sta;
1340 	int error;
1341 
1342 	lhw = vap->iv_ic->ic_softc;
1343 	hw = LHW_TO_HW(lhw);
1344 	lvif = VAP_TO_LVIF(vap);
1345 	vif = LVIF_TO_VIF(lvif);
1346 
1347 	/* Keep ni around. */
1348 	ni = ieee80211_ref_node(vap->iv_bss);
1349 
1350 	IEEE80211_UNLOCK(vap->iv_ic);
1351 	lsta = ni->ni_drv_data;
1352 	sta = LSTA_TO_STA(lsta);
1353 
1354 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1355 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1356 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1357 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1358 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1359 	if (error != 0)
1360 		goto out;
1361 
1362 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1363 	lkpi_disassoc(sta, vif, lhw);
1364 
1365 	/* Update sta_state (ASSOC to AUTH). */
1366 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1367 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1368 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1369 	sta = LSTA_TO_STA(lsta);
1370 	sta->aid = 0;
1371 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1372 	if (error != 0)
1373 		goto out;
1374 
1375 	IMPROVE("if ASSOC is final state, prep_tx_info?");
1376 
1377 out:
1378 	IEEE80211_LOCK(vap->iv_ic);
1379 	if (ni != NULL)
1380 		ieee80211_free_node(ni);
1381 	return (error);
1382 }
1383 
1384 static int
1385 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1386 {
1387 	int error;
1388 
1389 	error = lkpi_sta_run_to_assoc(vap, nstate, arg);
1390 	if (error == 0)
1391 		error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
1392 	return (error);
1393 }
1394 
1395 static int
1396 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1397 {
1398 	int error;
1399 
1400 	error = lkpi_sta_run_to_auth(vap, nstate, arg);
1401 	if (error == 0)
1402 		error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1403 	return (error);
1404 }
1405 
1406 static int
1407 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1408 {
1409 	int error;
1410 
1411 	error = lkpi_sta_run_to_scan(vap, nstate, arg);
1412 	if (error == 0)
1413 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
1414 	return (error);
1415 }
1416 
1417 /*
1418  * The matches the documented state changes in net80211::sta_newstate().
1419  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
1420  * there are "invalid" (so there is a room for failure here).
1421  */
1422 struct fsm_state {
1423 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
1424 	enum ieee80211_state ostate;
1425 	enum ieee80211_state nstate;
1426 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
1427 } sta_state_fsm[] = {
1428 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
1429 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
1430 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
1431 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },
1432 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },
1433 
1434 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1435 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1436 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
1437 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
1438 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },
1439 
1440 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },
1441 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },
1442 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },
1443 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },
1444 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },
1445 
1446 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },
1447 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },
1448 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },
1449 
1450 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
1451 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
1452 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
1453 
1454 	/* Dummy at the end without handler. */
1455 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
1456 };
1457 
1458 static int
1459 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1460 {
1461 	struct ieee80211com *ic;
1462 	struct lkpi_hw *lhw;
1463 	struct lkpi_vif *lvif;
1464 	struct ieee80211_vif *vif;
1465 	struct fsm_state *s;
1466 	enum ieee80211_state ostate;
1467 	int error;
1468 
1469 	ic = vap->iv_ic;
1470 	IEEE80211_LOCK_ASSERT(ic);
1471 	ostate = vap->iv_state;
1472 
1473 	if (debug_80211 & D80211_TRACE)
1474 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
1475 		    __func__, __LINE__, vap, nstate, arg);
1476 
1477 	if (vap->iv_opmode == IEEE80211_M_STA) {
1478 
1479 		lhw = ic->ic_softc;
1480 		lvif = VAP_TO_LVIF(vap);
1481 		vif = LVIF_TO_VIF(lvif);
1482 
1483 		/* No need to replicate this in most state handlers. */
1484 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
1485 			lkpi_stop_hw_scan(lhw, vif);
1486 
1487 		s = sta_state_fsm;
1488 
1489 	} else {
1490 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
1491 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
1492 		return (ENOSYS);
1493 	}
1494 
1495 	error = 0;
1496 	for (; s->handler != NULL; s++) {
1497 		if (ostate == s->ostate && nstate == s->nstate) {
1498 			error = s->handler(vap, nstate, arg);
1499 			break;
1500 		}
1501 	}
1502 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
1503 
1504 	if (s->handler == NULL) {
1505 		IMPROVE("thurn this into a KASSERT\n");
1506 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
1507 		    "%d (%s) -> %d (%s)\n", __func__,
1508 		    ostate, ieee80211_state_name[ostate],
1509 		    nstate, ieee80211_state_name[nstate]);
1510 		return (ENOSYS);
1511 	}
1512 
1513 	if (error == EALREADY) {
1514 		IMPROVE("make this a debug log later");
1515 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
1516 		    "%d (%s) -> %d (%s): iv_newstate already handled.\n",
1517 		    __func__, error,
1518 		    ostate, ieee80211_state_name[ostate],
1519 		    nstate, ieee80211_state_name[nstate]);
1520 		return (0);
1521 	}
1522 
1523 	if (error != 0) {
1524 		/* XXX-BZ currently expected so ignore. */
1525 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
1526 		    "%d (%s) -> %d (%s)\n", __func__, error,
1527 		    ostate, ieee80211_state_name[ostate],
1528 		    nstate, ieee80211_state_name[nstate]);
1529 		/* return (error); */
1530 	}
1531 
1532 	if (debug_80211 & D80211_TRACE)
1533 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x calling net80211 parent\n",
1534 		    __func__, __LINE__, vap, nstate, arg);
1535 
1536 	return (lvif->iv_newstate(vap, nstate, arg));
1537 }
1538 
1539 /* -------------------------------------------------------------------------- */
1540 
1541 static int
1542 lkpi_ic_wme_update(struct ieee80211com *ic)
1543 {
1544 	/* This needs queuing and go at the right moment. */
1545 #ifdef WITH_WME_UPDATE
1546 	struct ieee80211vap *vap;
1547 	struct lkpi_hw *lhw;
1548 	struct ieee80211_hw *hw;
1549 	struct lkpi_vif *lvif;
1550 	struct ieee80211_vif *vif;
1551 	struct chanAccParams chp;
1552 	struct wmeParams wmeparr[WME_NUM_AC];
1553 	struct ieee80211_tx_queue_params txqp;
1554 	enum ieee80211_bss_changed changed;
1555 	int error;
1556 	uint16_t ac;
1557 #endif
1558 
1559 	IMPROVE();
1560 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
1561 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
1562 
1563 #ifdef WITH_WME_UPDATE
1564 	vap = TAILQ_FIRST(&ic->ic_vaps);
1565 	if (vap == NULL)
1566 		return (0);
1567 
1568 	/* We should factor this out into per-vap (*wme_update). */
1569 	lhw = ic->ic_softc;
1570 	if (lhw->ops->conf_tx == NULL)
1571 		return (0);
1572 
1573 	/* XXX-BZ check amount of hw queues */
1574 	hw = LHW_TO_HW(lhw);
1575 	lvif = VAP_TO_LVIF(vap);
1576 	vif = LVIF_TO_VIF(lvif);
1577 
1578 	ieee80211_wme_ic_getparams(ic, &chp);
1579 	IEEE80211_LOCK(ic);
1580 	for (ac = 0; ac < WME_NUM_AC; ac++)
1581 		wmeparr[ac] = chp.cap_wmeParams[ac];
1582 	IEEE80211_UNLOCK(ic);
1583 
1584 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
1585 	LKPI_80211_LHW_LOCK(lhw);
1586 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1587 		struct wmeParams *wmep;
1588 
1589 		/* XXX-BZ should keep this in lvif? */
1590 		wmep = &wmeparr[ac];
1591 		bzero(&txqp, sizeof(txqp));
1592 		txqp.cw_min = wmep->wmep_logcwmin;
1593 		txqp.cw_max = wmep->wmep_logcwmax;
1594 		txqp.txop = wmep->wmep_txopLimit;
1595 		txqp.aifs = wmep->wmep_aifsn;
1596 		error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
1597 		if (error != 0)
1598 			printf("%s: conf_tx ac %u failed %d\n",
1599 			    __func__, ac, error);
1600 	}
1601 	LKPI_80211_LHW_UNLOCK(lhw);
1602 	changed = BSS_CHANGED_QOS;
1603 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
1604 #endif
1605 
1606 	return (0);
1607 }
1608 
1609 static struct ieee80211vap *
1610 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
1611     int unit, enum ieee80211_opmode opmode, int flags,
1612     const uint8_t bssid[IEEE80211_ADDR_LEN],
1613     const uint8_t mac[IEEE80211_ADDR_LEN])
1614 {
1615 	struct lkpi_hw *lhw;
1616 	struct ieee80211_hw *hw;
1617 	struct lkpi_vif *lvif;
1618 	struct ieee80211vap *vap;
1619 	struct ieee80211_vif *vif;
1620 	enum ieee80211_bss_changed changed;
1621 	size_t len;
1622 	int error;
1623 
1624 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
1625 		return (NULL);
1626 
1627 	lhw = ic->ic_softc;
1628 	hw = LHW_TO_HW(lhw);
1629 
1630 	len = sizeof(*lvif);
1631 	len += hw->vif_data_size;	/* vif->drv_priv */
1632 
1633 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
1634 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
1635 	TAILQ_INIT(&lvif->lsta_head);
1636 	vap = LVIF_TO_VAP(lvif);
1637 
1638 	vif = LVIF_TO_VIF(lvif);
1639 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
1640 	vif->p2p = false;
1641 	vif->probe_req_reg = false;
1642 	vif->type = lkpi_opmode_to_vif_type(opmode);
1643 	lvif->wdev.iftype = vif->type;
1644 	/* Need to fill in other fields as well. */
1645 	IMPROVE();
1646 
1647 	/* XXX-BZ hardcoded for now! */
1648 #if 1
1649 	vif->chanctx_conf = NULL;
1650 	vif->bss_conf.idle = true;
1651 	vif->bss_conf.ps = false;
1652 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
1653 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
1654 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
1655 	vif->bss_conf.qos = false;
1656 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
1657 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1658 	vif->bss_conf.assoc = false;
1659 	vif->bss_conf.aid = 0;
1660 #endif
1661 #if 0
1662 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
1663 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
1664 	vif->bss_conf.beacon_int = ic->ic_bintval;
1665 	/* iwlwifi bug. */
1666 	if (vif->bss_conf.beacon_int < 16)
1667 		vif->bss_conf.beacon_int = 16;
1668 #endif
1669 	IMPROVE();
1670 
1671 	error = lkpi_80211_mo_start(hw);
1672 	if (error != 0) {
1673 		printf("%s: failed to start hw: %d\n", __func__, error);
1674 		mtx_destroy(&lvif->mtx);
1675 		free(lvif, M_80211_VAP);
1676 		return (NULL);
1677 	}
1678 
1679 	error = lkpi_80211_mo_add_interface(hw, vif);
1680 	if (error != 0) {
1681 		IMPROVE();	/* XXX-BZ mo_stop()? */
1682 		printf("%s: failed to add interface: %d\n", __func__, error);
1683 		mtx_destroy(&lvif->mtx);
1684 		free(lvif, M_80211_VAP);
1685 		return (NULL);
1686 	}
1687 
1688 	LKPI_80211_LHW_LOCK(lhw);
1689 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
1690 	LKPI_80211_LHW_UNLOCK(lhw);
1691 
1692 	/* Set bss_info. */
1693 	changed = 0;
1694 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
1695 
1696 	/* conf_tx setup; default WME? */
1697 
1698 	/* Force MC init. */
1699 	lkpi_update_mcast_filter(ic, true);
1700 
1701 	IMPROVE();
1702 
1703 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
1704 
1705 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
1706 	lvif->iv_newstate = vap->iv_newstate;
1707 	vap->iv_newstate = lkpi_iv_newstate;
1708 
1709 	/* Key management. */
1710 	if (lhw->ops->set_key != NULL) {
1711 #ifdef TRY_HW_CRYPTO
1712 		vap->iv_key_set = lkpi_iv_key_set;
1713 		vap->iv_key_delete = lkpi_iv_key_delete;
1714 #endif
1715 	}
1716 
1717 	ieee80211_ratectl_init(vap);
1718 
1719 	/* Complete setup. */
1720 	ieee80211_vap_attach(vap, ieee80211_media_change,
1721 	    ieee80211_media_status, mac);
1722 
1723 	if (hw->max_listen_interval == 0)
1724 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
1725 	hw->conf.listen_interval = hw->max_listen_interval;
1726 	ic->ic_set_channel(ic);
1727 
1728 	/* XXX-BZ do we need to be able to update these? */
1729 	hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
1730 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
1731 	hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
1732 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
1733 	/* any others? */
1734 	IMPROVE();
1735 
1736 	return (vap);
1737 }
1738 
1739 static void
1740 lkpi_ic_vap_delete(struct ieee80211vap *vap)
1741 {
1742 	struct ieee80211com *ic;
1743 	struct lkpi_hw *lhw;
1744 	struct ieee80211_hw *hw;
1745 	struct lkpi_vif *lvif;
1746 	struct ieee80211_vif *vif;
1747 
1748 	lvif = VAP_TO_LVIF(vap);
1749 	vif = LVIF_TO_VIF(lvif);
1750 	ic = vap->iv_ic;
1751 	lhw = ic->ic_softc;
1752 	hw = LHW_TO_HW(lhw);
1753 
1754 	LKPI_80211_LHW_LOCK(lhw);
1755 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
1756 	LKPI_80211_LHW_UNLOCK(lhw);
1757 	lkpi_80211_mo_remove_interface(hw, vif);
1758 
1759 	ieee80211_ratectl_deinit(vap);
1760 	ieee80211_vap_detach(vap);
1761 	mtx_destroy(&lvif->mtx);
1762 	free(lvif, M_80211_VAP);
1763 }
1764 
1765 static void
1766 lkpi_ic_update_mcast(struct ieee80211com *ic)
1767 {
1768 
1769 	lkpi_update_mcast_filter(ic, false);
1770 	TRACEOK();
1771 }
1772 
1773 static void
1774 lkpi_ic_update_promisc(struct ieee80211com *ic)
1775 {
1776 
1777 	UNIMPLEMENTED;
1778 }
1779 
1780 static void
1781 lkpi_ic_update_chw(struct ieee80211com *ic)
1782 {
1783 
1784 	UNIMPLEMENTED;
1785 }
1786 
1787 /* Start / stop device. */
1788 static void
1789 lkpi_ic_parent(struct ieee80211com *ic)
1790 {
1791 	struct lkpi_hw *lhw;
1792 	struct ieee80211_hw *hw;
1793 	int error;
1794 	bool start_all;
1795 
1796 	IMPROVE();
1797 
1798 	lhw = ic->ic_softc;
1799 	hw = LHW_TO_HW(lhw);
1800 	start_all = false;
1801 
1802 	if (ic->ic_nrunning > 0) {
1803 		error = lkpi_80211_mo_start(hw);
1804 		if (error == 0)
1805 			start_all = true;
1806 	} else {
1807 		lkpi_80211_mo_stop(hw);
1808 	}
1809 
1810 	if (start_all)
1811 		ieee80211_start_all(ic);
1812 }
1813 
1814 bool
1815 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
1816     size_t ie_ids_len)
1817 {
1818 	int i;
1819 
1820 	for (i = 0; i < ie_ids_len; i++) {
1821 		if (ie == *ie_ids)
1822 			return (true);
1823 	}
1824 
1825 	return (false);
1826 }
1827 
1828 /* Return true if skipped; false if error. */
1829 bool
1830 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
1831 {
1832 	size_t x;
1833 	uint8_t l;
1834 
1835 	x = *xp;
1836 
1837 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
1838 	    __func__, x, ies_len, ies));
1839 	l = ies[x + 1];
1840 	x += 2 + l;
1841 
1842 	if (x > ies_len)
1843 		return (false);
1844 
1845 	*xp = x;
1846 	return (true);
1847 }
1848 
1849 static int
1850 lkpi_ieee80211_probereq_ie_alloc(struct ieee80211vap *vap,
1851     struct ieee80211com *ic, struct ieee80211_scan_ies *scan_ies,
1852     const uint8_t *ssid, size_t ssidlen)
1853 {
1854 
1855 	return (ieee80211_probereq_ie(vap, ic,
1856 	    &scan_ies->common_ies, &scan_ies->common_ie_len,
1857 	    ssid, ssidlen, true));
1858 }
1859 
1860 static void
1861 lkpi_ic_scan_start(struct ieee80211com *ic)
1862 {
1863 	struct lkpi_hw *lhw;
1864 	struct ieee80211_hw *hw;
1865 	struct lkpi_vif *lvif;
1866 	struct ieee80211_vif *vif;
1867 	struct ieee80211_scan_state *ss;
1868 	struct ieee80211vap *vap;
1869 	int error;
1870 
1871 	lhw = ic->ic_softc;
1872 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0) {
1873 		/* A scan is still running. */
1874 		return;
1875 	}
1876 
1877 	ss = ic->ic_scan;
1878 	vap = ss->ss_vap;
1879 	if (vap->iv_state != IEEE80211_S_SCAN) {
1880 		/* Do not start a scan for now. */
1881 		return;
1882 	}
1883 
1884 	hw = LHW_TO_HW(lhw);
1885 	if ((ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) {
1886 
1887 		lvif = VAP_TO_LVIF(vap);
1888 		vif = LVIF_TO_VIF(lvif);
1889 
1890 		if (vap->iv_state == IEEE80211_S_SCAN)
1891 			lkpi_hw_conf_idle(hw, false);
1892 
1893 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
1894 		/* net80211::scan_start() handled PS for us. */
1895 		IMPROVE();
1896 		/* XXX Also means it is too late to flush queues?
1897 		 * need to check iv_sta_ps or overload? */
1898 		/* XXX want to adjust ss end time/ maxdwell? */
1899 
1900 	} else {
1901 		struct ieee80211_channel *c;
1902 		struct ieee80211_scan_request *hw_req;
1903 		struct linuxkpi_ieee80211_channel *lc, **cpp;
1904 		struct cfg80211_ssid *ssids;
1905 		struct cfg80211_scan_6ghz_params *s6gp;
1906 		size_t chan_len, nchan, ssids_len, s6ghzlen;
1907 		int i;
1908 
1909 		ssids_len = ss->ss_nssid * sizeof(*ssids);;
1910 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
1911 
1912 		nchan = 0;
1913 		for (i = ss->ss_next; i < ss->ss_last; i++)
1914 			nchan++;
1915 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
1916 
1917 		KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
1918 		    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
1919 		lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
1920 		    s6ghzlen + chan_len, M_LKPI80211, M_WAITOK | M_ZERO);
1921 
1922 		error = lkpi_ieee80211_probereq_ie_alloc(vap, ic,
1923 		    &hw_req->ies, NULL, -1);
1924 		if (error != 0)
1925 			ic_printf(ic, "ERROR: %s: probereq_ie returned %d\n",
1926 			    __func__, error);
1927 
1928 		hw_req->req.flags = 0;			/* XXX ??? */
1929 		/* hw_req->req.wdev */
1930 		hw_req->req.wiphy = hw->wiphy;
1931 		hw_req->req.no_cck = false;		/* XXX */
1932 #if 0
1933 		/* This seems to pessimise default scanning behaviour. */
1934 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
1935 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
1936 #endif
1937 #ifdef __notyet__
1938 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
1939 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
1940 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
1941 #endif
1942 #if 0
1943 		hw_req->req.ie_len = ;
1944 		hw_req->req.ie = ;
1945 #endif
1946 
1947 		hw_req->req.n_channels = nchan;
1948 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
1949 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
1950 		for (i = 0; i < nchan; i++) {
1951 			*(cpp + i) =
1952 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
1953 		}
1954 		for (i = 0; i < nchan; i++) {
1955 			c = ss->ss_chans[ss->ss_next + i];
1956 
1957 			lc->hw_value = c->ic_ieee;
1958 			lc->center_freq = c->ic_freq;
1959 			/* lc->flags */
1960 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
1961 			lc->max_power = c->ic_maxpower;
1962 			/* lc-> ... */
1963 			lc++;
1964 		}
1965 
1966 		hw_req->req.n_ssids = ss->ss_nssid;
1967 		if (hw_req->req.n_ssids > 0) {
1968 			ssids = (struct cfg80211_ssid *)lc;
1969 			hw_req->req.ssids = ssids;
1970 			for (i = 0; i < ss->ss_nssid; i++) {
1971 				ssids->ssid_len = ss->ss_ssid[i].len;
1972 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
1973 				    ss->ss_ssid[i].len);
1974 				ssids++;
1975 			}
1976 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
1977 		} else {
1978 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
1979 		}
1980 
1981 		/* 6GHz one day. */
1982 		hw_req->req.n_6ghz_params = 0;
1983 		hw_req->req.scan_6ghz_params = NULL;
1984 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
1985 		/* s6gp->... */
1986 
1987 		lvif = VAP_TO_LVIF(vap);
1988 		vif = LVIF_TO_VIF(lvif);
1989 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
1990 		if (error != 0) {
1991 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
1992 			    __func__, error);
1993 			ieee80211_cancel_scan(vap);
1994 			free(hw_req->ies.common_ies, M_80211_VAP);
1995 			free(hw_req, M_LKPI80211);
1996 			lhw->hw_req = NULL;
1997 		}
1998 	}
1999 }
2000 
2001 static void
2002 lkpi_ic_scan_end(struct ieee80211com *ic)
2003 {
2004 	struct lkpi_hw *lhw;
2005 
2006 	lhw = ic->ic_softc;
2007 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
2008 		return;
2009 	}
2010 
2011 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
2012 		/* Nothing to do. */
2013 	} else {
2014 		struct ieee80211_hw *hw;
2015 		struct lkpi_vif *lvif;
2016 		struct ieee80211_vif *vif;
2017 		struct ieee80211_scan_state *ss;
2018 		struct ieee80211vap *vap;
2019 
2020 		hw = LHW_TO_HW(lhw);
2021 		ss = ic->ic_scan;
2022 		vap = ss->ss_vap;
2023 		lvif = VAP_TO_LVIF(vap);
2024 		vif = LVIF_TO_VIF(lvif);
2025 		lkpi_80211_mo_sw_scan_complete(hw, vif);
2026 
2027 		/* Send PS to stop buffering if n80211 does not for us? */
2028 
2029 		if (vap->iv_state == IEEE80211_S_SCAN)
2030 			lkpi_hw_conf_idle(hw, true);
2031 	}
2032 }
2033 
2034 static void
2035 lkpi_ic_scan_curchan_nada(struct ieee80211_scan_state *ss __unused,
2036     unsigned long maxdwell __unused)
2037 {
2038 }
2039 
2040 static void
2041 lkpi_ic_set_channel(struct ieee80211com *ic)
2042 {
2043 	struct lkpi_hw *lhw;
2044 	struct ieee80211_hw *hw;
2045 	int error;
2046 
2047 	lhw = ic->ic_softc;
2048 #ifdef __no_longer__
2049 	/* For now only be concerned if scanning. */
2050 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
2051 		IMPROVE();
2052 		return;
2053 	}
2054 #endif
2055 
2056 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
2057 		/*
2058 		 * AP scanning is taken care of by firmware, so only switch
2059 		 * channels in monitor mode (maybe, maybe not; to be
2060 		 * investigated at the right time).
2061 		 */
2062 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2063 			UNIMPLEMENTED;
2064 		}
2065 	} else {
2066 		struct ieee80211_channel *c = ic->ic_curchan;
2067 		struct linuxkpi_ieee80211_channel *chan;
2068 		struct cfg80211_chan_def chandef;
2069 
2070 		if (c == NULL || c == IEEE80211_CHAN_ANYC ||
2071 		    lhw->ops->config == NULL) {
2072 			ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
2073 			    c, lhw->ops->config);
2074 			return;
2075 		}
2076 
2077 		chan = lkpi_find_lkpi80211_chan(lhw, c);
2078 		if (chan == NULL) {
2079 			ic_printf(ic, "%s: c %p chan %p\n", __func__,
2080 			    c, chan);
2081 			return;
2082 		}
2083 
2084 		memset(&chandef, 0, sizeof(chandef));
2085 		chandef.chan = chan;
2086 		chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2087 		chandef.center_freq1 = chandef.chan->center_freq;
2088 
2089 		/* XXX max power for scanning? */
2090 		IMPROVE();
2091 
2092 		hw = LHW_TO_HW(lhw);
2093 		hw->conf.chandef = chandef;
2094 
2095 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
2096 		if (error != 0 && error != EOPNOTSUPP) {
2097 			ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
2098 			    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
2099 			/* XXX should we unroll to the previous chandef? */
2100 			IMPROVE();
2101 		} else {
2102 			/* Update radiotap channels as well. */
2103 			lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
2104 			lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
2105 			lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
2106 			lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
2107 		}
2108 
2109 		/* Currently PS is hard coded off! Not sure it belongs here. */
2110 		IMPROVE();
2111 		if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
2112 		    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
2113 			hw->conf.flags &= ~IEEE80211_CONF_PS;
2114 			error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
2115 			if (error != 0 && error != EOPNOTSUPP)
2116 				ic_printf(ic, "ERROR: %s: config %#0x returned "
2117 				    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
2118 				    error);
2119 		}
2120 	}
2121 }
2122 
2123 static struct ieee80211_node *
2124 lkpi_ic_node_alloc(struct ieee80211vap *vap,
2125     const uint8_t mac[IEEE80211_ADDR_LEN])
2126 {
2127 	struct ieee80211com *ic;
2128 	struct lkpi_hw *lhw;
2129 	struct ieee80211_hw *hw;
2130 	struct lkpi_sta *lsta;
2131 	struct ieee80211_sta *sta;
2132 	struct lkpi_vif *lvif;
2133 	struct ieee80211_vif *vif;
2134 	struct ieee80211_node *ni;
2135 	int tid;
2136 
2137 	ic = vap->iv_ic;
2138 	lhw = ic->ic_softc;
2139 
2140 	/* We keep allocations de-coupled so we can deal with the two worlds. */
2141 	if (lhw->ic_node_alloc != NULL) {
2142 		ni = lhw->ic_node_alloc(vap, mac);
2143 		if (ni == NULL)
2144 			return (NULL);
2145 	}
2146 
2147 	hw = LHW_TO_HW(lhw);
2148 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2149 	    M_NOWAIT | M_ZERO);
2150 	if (lsta == NULL) {
2151 		if (lhw->ic_node_free != NULL)
2152 			lhw->ic_node_free(ni);
2153 		return (NULL);
2154 	}
2155 
2156 	lsta->added_to_drv = false;
2157 	lsta->state = IEEE80211_STA_NOTEXIST;
2158 #if 0
2159 	/*
2160 	 * This needs to be done in node_init() as ieee80211_alloc_node()
2161 	 * will initialise the refcount after us.
2162 	 */
2163 	lsta->ni = ieee80211_ref_node(ni);
2164 #endif
2165 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2166 	ni->ni_drv_data = lsta;
2167 
2168 	lvif = VAP_TO_LVIF(vap);
2169 	vif = LVIF_TO_VIF(lvif);
2170 	sta = LSTA_TO_STA(lsta);
2171 
2172 	IEEE80211_ADDR_COPY(sta->addr, mac);
2173 	for (tid = 0; tid < nitems(sta->txq); tid++) {
2174 		struct lkpi_txq *ltxq;
2175 
2176 		/*
2177 		 * We are neither limiting ourselves to hw.queues here,
2178 		 * nor do we check if driver wants IEEE80211_NUM_TIDS queue.
2179 		 */
2180 
2181 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
2182 		    M_LKPI80211, M_NOWAIT | M_ZERO);
2183 		if (ltxq == NULL)
2184 			goto cleanup;
2185 		ltxq->seen_dequeue = false;
2186 		skb_queue_head_init(&ltxq->skbq);
2187 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
2188 		if (tid == IEEE80211_NUM_TIDS) {
2189 			IMPROVE();
2190 			ltxq->txq.ac = IEEE80211_AC_VO;
2191 		} else {
2192 			ltxq->txq.ac = tid_to_mac80211_ac[tid & 7];
2193 		}
2194 		ltxq->txq.tid = tid;
2195 		ltxq->txq.sta = sta;
2196 		ltxq->txq.vif = vif;
2197 		sta->txq[tid] = &ltxq->txq;
2198 	}
2199 
2200 	/* Deferred TX path. */
2201 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
2202 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
2203 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
2204 
2205 	return (ni);
2206 
2207 cleanup:
2208 	for (; tid >= 0; tid--)
2209 		free(sta->txq[tid], M_LKPI80211);
2210 	free(lsta, M_LKPI80211);
2211 	if (lhw->ic_node_free != NULL)
2212 		lhw->ic_node_free(ni);
2213 	return (NULL);
2214 }
2215 
2216 static int
2217 lkpi_ic_node_init(struct ieee80211_node *ni)
2218 {
2219 	struct ieee80211com *ic;
2220 	struct lkpi_hw *lhw;
2221 	struct lkpi_sta *lsta;
2222 	struct lkpi_vif *lvif;
2223 	int error;
2224 
2225 	ic = ni->ni_ic;
2226 	lhw = ic->ic_softc;
2227 
2228 	if (lhw->ic_node_init != NULL) {
2229 		error = lhw->ic_node_init(ni);
2230 		if (error != 0)
2231 			return (error);
2232 	}
2233 
2234 	lvif = VAP_TO_LVIF(ni->ni_vap);
2235 
2236 	lsta = ni->ni_drv_data;
2237 
2238 	/* Now take the reference before linking it to the table. */
2239 	lsta->ni = ieee80211_ref_node(ni);
2240 
2241 	LKPI_80211_LVIF_LOCK(lvif);
2242 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
2243 	LKPI_80211_LVIF_UNLOCK(lvif);
2244 
2245 	/* XXX-BZ Sync other state over. */
2246 	IMPROVE();
2247 
2248 	return (0);
2249 }
2250 
2251 static void
2252 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
2253 {
2254 	struct ieee80211com *ic;
2255 	struct lkpi_hw *lhw;
2256 
2257 	ic = ni->ni_ic;
2258 	lhw = ic->ic_softc;
2259 
2260 	/* XXX-BZ remove from driver, ... */
2261 	IMPROVE();
2262 
2263 	if (lhw->ic_node_cleanup != NULL)
2264 		lhw->ic_node_cleanup(ni);
2265 }
2266 
2267 static void
2268 lkpi_ic_node_free(struct ieee80211_node *ni)
2269 {
2270 	struct ieee80211com *ic;
2271 	struct lkpi_hw *lhw;
2272 	struct lkpi_sta *lsta;
2273 
2274 	ic = ni->ni_ic;
2275 	lhw = ic->ic_softc;
2276 	lsta = ni->ni_drv_data;
2277 
2278 	/* XXX-BZ free resources, ... */
2279 	IMPROVE();
2280 
2281 	/* Flush mbufq (make sure to release ni refs!). */
2282 #ifdef __notyet__
2283 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
2284 	    __func__, lsta, mbufq_len(&lsta->txq)));
2285 #endif
2286 	/* Drain taskq. */
2287 
2288 	/* Drain sta->txq[] */
2289 	mtx_destroy(&lsta->txq_mtx);
2290 
2291 	/* Remove lsta if added_to_drv. */
2292 	/* Remove lsta from vif */
2293 
2294 	/* remove ref from lsta node... */
2295 
2296 	if (lhw->ic_node_free != NULL)
2297 		lhw->ic_node_free(ni);
2298 
2299 	/* Free lsta. */
2300 }
2301 
2302 static int
2303 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2304         const struct ieee80211_bpf_params *params __unused)
2305 {
2306 	struct lkpi_sta *lsta;
2307 
2308 	lsta = ni->ni_drv_data;
2309 
2310 	/* Queue the packet and enqueue the task to handle it. */
2311 	LKPI_80211_LSTA_LOCK(lsta);
2312 	mbufq_enqueue(&lsta->txq, m);
2313 	LKPI_80211_LSTA_UNLOCK(lsta);
2314 
2315 	if (debug_80211 & D80211_TRACE_TX)
2316 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
2317 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
2318 		    mbufq_len(&lsta->txq));
2319 
2320 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
2321 	return (0);
2322 }
2323 
2324 static void
2325 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
2326 {
2327 	struct ieee80211_node *ni;
2328 	struct ieee80211_frame *wh;
2329 	struct ieee80211_key *k;
2330 	struct sk_buff *skb;
2331 	struct ieee80211com *ic;
2332 	struct lkpi_hw *lhw;
2333 	struct ieee80211_hw *hw;
2334 	struct lkpi_vif *lvif;
2335 	struct ieee80211_vif *vif;
2336 	struct ieee80211_channel *c;
2337 	struct ieee80211_tx_control control;
2338 	struct ieee80211_tx_info *info;
2339 	struct ieee80211_sta *sta;
2340 	void *buf;
2341 	int ac;
2342 
2343 	M_ASSERTPKTHDR(m);
2344 #ifdef LINUXKPI_DEBUG_80211
2345 	if (debug_80211 & D80211_TRACE_TX_DUMP)
2346 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
2347 #endif
2348 
2349 	ni = lsta->ni;
2350 #ifndef TRY_HW_CRYPTO
2351 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
2352 	wh = mtod(m, struct ieee80211_frame *);
2353 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2354 		/* Retrieve key for TX && do software encryption. */
2355 		k = ieee80211_crypto_encap(ni, m);
2356 		if (k == NULL) {
2357 			ieee80211_free_node(ni);
2358 			m_freem(m);
2359 			return;
2360 		}
2361 	}
2362 #endif
2363 
2364 	ic = ni->ni_ic;
2365 	lhw = ic->ic_softc;
2366 	hw = LHW_TO_HW(lhw);
2367 	c = ni->ni_chan;
2368 
2369 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
2370 		struct lkpi_radiotap_tx_hdr *rtap;
2371 
2372 		rtap = &lhw->rtap_tx;
2373 		rtap->wt_flags = 0;
2374 		if (k != NULL)
2375 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2376 		if (m->m_flags & M_FRAG)
2377 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2378 		IMPROVE();
2379 		rtap->wt_rate = 0;
2380 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
2381 			rtap->wt_chan_freq = htole16(c->ic_freq);
2382 			rtap->wt_chan_flags = htole16(c->ic_flags);
2383 		}
2384 
2385 		ieee80211_radiotap_tx(ni->ni_vap, m);
2386 	}
2387 
2388 	/*
2389 	 * net80211 should handle hw->extra_tx_headroom.
2390 	 * Though for as long as we are copying we don't mind.
2391 	 */
2392 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
2393 	if (skb == NULL) {
2394 		printf("XXX ERROR %s: skb alloc failed\n", __func__);
2395 		ieee80211_free_node(ni);
2396 		m_freem(m);
2397 		return;
2398 	}
2399 	skb_reserve(skb, hw->extra_tx_headroom);
2400 
2401 	/* XXX-BZ we need a SKB version understanding mbuf. */
2402 	/* Save the mbuf for ieee80211_tx_complete(). */
2403 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
2404 	skb->m = m;
2405 #if 0
2406 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
2407 #else
2408 	buf = skb_put(skb, m->m_pkthdr.len);
2409 	m_copydata(m, 0, m->m_pkthdr.len, buf);
2410 #endif
2411 	/* Save the ni. */
2412 	m->m_pkthdr.PH_loc.ptr = ni;
2413 
2414 	lvif = VAP_TO_LVIF(ni->ni_vap);
2415 	vif = LVIF_TO_VIF(lvif);
2416 
2417 	/* XXX-BZ review this at some point [4] vs. [8] vs. [16](TID). */
2418 	ac = M_WME_GETAC(m);
2419 	skb->priority = WME_AC_TO_TID(ac);
2420 	ac = lkpi_ac_net_to_l80211(ac);
2421 	skb_set_queue_mapping(skb, ac);
2422 
2423 	info = IEEE80211_SKB_CB(skb);
2424 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2425 	/* Slight delay; probably only happens on scanning so fine? */
2426 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
2427 		c = ic->ic_curchan;
2428 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
2429 	info->hw_queue = ac;		/* XXX-BZ is this correct? */
2430 	if (m->m_flags & M_EAPOL)
2431 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
2432 	info->control.vif = vif;
2433 	/* XXX-BZ info->control.rates */
2434 
2435 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
2436 	if (lsta != NULL) {
2437 		sta = LSTA_TO_STA(lsta);
2438 #ifdef TRY_HW_CRYPTO
2439 		info->control.hw_key = lsta->kc;
2440 #endif
2441 	} else {
2442 		sta = NULL;
2443 	}
2444 
2445 	IMPROVE();
2446 
2447 	if (sta != NULL) {
2448 		struct lkpi_txq *ltxq;
2449 
2450 		ltxq = TXQ_TO_LTXQ(sta->txq[ac]);	/* XXX-BZ re-check */
2451 		/*
2452 		 * We currently do not use queues but do direct TX.
2453 		 * The exception to the rule is initial packets, as we cannot
2454 		 * TX until queues are allocated (at least for iwlwifi).
2455 		 * So we wake_tx_queue in newstate and register any dequeue
2456 		 * calls.  In the time until then we queue packets and
2457 		 * let the driver deal with them.
2458 		 */
2459 		if (!ltxq->seen_dequeue) {
2460 
2461 			/* Prevent an ordering problem, likely other issues. */
2462 			while (!skb_queue_empty(&ltxq->skbq)) {
2463 				struct sk_buff *skb2;
2464 
2465 				skb2 = skb_dequeue(&ltxq->skbq);
2466 				if (skb2 != NULL) {
2467 					memset(&control, 0, sizeof(control));
2468 					control.sta = sta;
2469 					lkpi_80211_mo_tx(hw, &control, skb2);
2470 				}
2471 			}
2472 			goto ops_tx;
2473 		}
2474 		if (0 && ltxq->seen_dequeue && skb_queue_empty(&ltxq->skbq))
2475 			goto ops_tx;
2476 
2477 		skb_queue_tail(&ltxq->skbq, skb);
2478 		if (debug_80211 & D80211_TRACE_TX)
2479 			printf("%s:%d lsta %p sta %p ni %p %6D skb %p lxtq %p "
2480 			    "qlen %u WAKE_TX_Q ac %d prio %u qmap %u\n",
2481 			    __func__, __LINE__, lsta, sta, ni,
2482 			    ni->ni_macaddr, ":", skb, ltxq,
2483 			    skb_queue_len(&ltxq->skbq), ac,
2484 			    skb->priority, skb->qmap);
2485 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[ac]);	/* XXX-BZ */
2486 		return;
2487 	}
2488 
2489 ops_tx:
2490 	if (debug_80211 & D80211_TRACE_TX)
2491 		printf("%s:%d lsta %p sta %p ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
2492 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
2493 		    skb, ac, skb->priority, skb->qmap);
2494 	memset(&control, 0, sizeof(control));
2495 	control.sta = sta;
2496 
2497 	lkpi_80211_mo_tx(hw, &control, skb);
2498 	return;
2499 }
2500 
2501 static void
2502 lkpi_80211_txq_task(void *ctx, int pending)
2503 {
2504 	struct lkpi_sta *lsta;
2505 	struct ieee80211_node *ni;
2506 	struct mbufq mq;
2507 	struct mbuf *m;
2508 
2509 	lsta = ctx;
2510 	ni = lsta->ni;
2511 
2512 	if (debug_80211 & D80211_TRACE_TX)
2513 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
2514 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
2515 		    pending, mbufq_len(&lsta->txq));
2516 
2517 	mbufq_init(&mq, IFQ_MAXLEN);
2518 
2519 	LKPI_80211_LSTA_LOCK(lsta);
2520 	mbufq_concat(&mq, &lsta->txq);
2521 	LKPI_80211_LSTA_UNLOCK(lsta);
2522 
2523 	m = mbufq_dequeue(&mq);
2524 	while (m != NULL) {
2525 		lkpi_80211_txq_tx_one(lsta, m);
2526 		m = mbufq_dequeue(&mq);
2527 	}
2528 }
2529 
2530 static int
2531 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
2532 {
2533 
2534 	/* XXX TODO */
2535 	IMPROVE();
2536 
2537 	/* Quick and dirty cheating hack. */
2538 	struct ieee80211_node *ni;
2539 
2540 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2541 	return (lkpi_ic_raw_xmit(ni, m, NULL));
2542 }
2543 
2544 static void
2545 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
2546     int *n, struct ieee80211_channel *c)
2547 {
2548 	struct lkpi_hw *lhw;
2549 	struct ieee80211_hw *hw;
2550 	struct linuxkpi_ieee80211_channel *channels;
2551 	uint8_t bands[IEEE80211_MODE_BYTES];
2552 	int chan_flags, error, i, nchans;
2553 
2554 	/* Channels */
2555 	lhw = ic->ic_softc;
2556 	hw = LHW_TO_HW(lhw);
2557 
2558 	/* NL80211_BAND_2GHZ */
2559 	nchans = 0;
2560 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
2561 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
2562 	if (nchans > 0) {
2563 		memset(bands, 0, sizeof(bands));
2564 		chan_flags = 0;
2565 		setbit(bands, IEEE80211_MODE_11B);
2566 		/* XXX-BZ unclear how to check for 11g. */
2567 		setbit(bands, IEEE80211_MODE_11G);
2568 #ifdef __notyet__
2569 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
2570 			setbit(bands, IEEE80211_MODE_11NG);
2571 			chan_flags |= NET80211_CBW_FLAG_HT40;
2572 		}
2573 #endif
2574 
2575 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
2576 		for (i = 0; i < nchans; i++) {
2577 			uint32_t nflags = 0;
2578 			int cflags = chan_flags;
2579 
2580 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
2581 				printf("%s: %s: Skipping disabled chan "
2582 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
2583 				    channels[i].hw_value,
2584 				    channels[i].center_freq, channels[i].flags);
2585 				continue;
2586 			}
2587 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
2588 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
2589 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
2590 				nflags |= IEEE80211_CHAN_DFS;
2591 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
2592 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
2593 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
2594 				cflags &= ~NET80211_CBW_FLAG_VHT80;
2595 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
2596 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
2597 				cflags &= ~NET80211_CBW_FLAG_HT40;
2598 
2599 			error = ieee80211_add_channel_cbw(c, maxchan, n,
2600 			    channels[i].hw_value, channels[i].center_freq,
2601 			    channels[i].max_power,
2602 			    nflags, bands, chan_flags);
2603 			if (error != 0) {
2604 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
2605 				    "returned error %d\n", ic->ic_name,
2606 				    __func__, channels[i].hw_value,
2607 				    channels[i].center_freq, channels[i].flags,
2608 				    nflags, chan_flags, cflags, error);
2609 				break;
2610 			}
2611 		}
2612 	}
2613 
2614 	/* NL80211_BAND_5GHZ */
2615 	nchans = 0;
2616 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
2617 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
2618 	if (nchans > 0) {
2619 		memset(bands, 0, sizeof(bands));
2620 		chan_flags = 0;
2621 		setbit(bands, IEEE80211_MODE_11A);
2622 #ifdef __not_yet__
2623 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
2624 			setbit(bands, IEEE80211_MODE_11NA);
2625 			chan_flags |= NET80211_CBW_FLAG_HT40;
2626 		}
2627 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
2628 
2629 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
2630 			ic->ic_vhtcaps =
2631 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
2632 
2633 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
2634 			chan_flags |= NET80211_CBW_FLAG_VHT80;
2635 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
2636 			    ic->ic_vhtcaps))
2637 				chan_flags |= NET80211_CBW_FLAG_VHT160;
2638 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
2639 			    ic->ic_vhtcaps))
2640 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
2641 		}
2642 #endif
2643 
2644 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
2645 		for (i = 0; i < nchans; i++) {
2646 			uint32_t nflags = 0;
2647 			int cflags = chan_flags;
2648 
2649 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
2650 				printf("%s: %s: Skipping disabled chan "
2651 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
2652 				    channels[i].hw_value,
2653 				    channels[i].center_freq, channels[i].flags);
2654 				continue;
2655 			}
2656 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
2657 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
2658 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
2659 				nflags |= IEEE80211_CHAN_DFS;
2660 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
2661 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
2662 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
2663 				cflags &= ~NET80211_CBW_FLAG_VHT80;
2664 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
2665 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
2666 				cflags &= ~NET80211_CBW_FLAG_HT40;
2667 
2668 			error = ieee80211_add_channel_cbw(c, maxchan, n,
2669 			    channels[i].hw_value, channels[i].center_freq,
2670 			    channels[i].max_power,
2671 			    nflags, bands, chan_flags);
2672 			if (error != 0) {
2673 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
2674 				    "returned error %d\n", ic->ic_name,
2675 				    __func__, channels[i].hw_value,
2676 				    channels[i].center_freq, channels[i].flags,
2677 				    nflags, chan_flags, cflags, error);
2678 				break;
2679 			}
2680 		}
2681 	}
2682 }
2683 
2684 static void *
2685 lkpi_ieee80211_ifalloc(void)
2686 {
2687 	struct ieee80211com *ic;
2688 
2689 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
2690 	if (ic == NULL)
2691 		return (NULL);
2692 
2693 	/* Setting these happens later when we have device information. */
2694 	ic->ic_softc = NULL;
2695 	ic->ic_name = "linuxkpi";
2696 
2697 	return (ic);
2698 }
2699 
2700 struct ieee80211_hw *
2701 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
2702 {
2703 	struct ieee80211_hw *hw;
2704 	struct lkpi_hw *lhw;
2705 	struct wiphy *wiphy;
2706 
2707 	/* Get us and the driver data also allocated. */
2708 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
2709 	if (wiphy == NULL)
2710 		return (NULL);
2711 
2712 	lhw = wiphy_priv(wiphy);
2713 	lhw->ops = ops;
2714 	lhw->workq = alloc_ordered_workqueue(wiphy_name(wiphy), 0);
2715 	if (lhw->workq == NULL) {
2716 		wiphy_free(wiphy);
2717 		return (NULL);
2718 	}
2719 	mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
2720 	TAILQ_INIT(&lhw->lvif_head);
2721 
2722 	/*
2723 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
2724 	 * not initialized.
2725 	 */
2726 	hw = LHW_TO_HW(lhw);
2727 	hw->wiphy = wiphy;
2728 	hw->conf.flags |= IEEE80211_CONF_IDLE;
2729 	hw->priv = (void *)(lhw + 1);
2730 
2731 	/* BSD Specific. */
2732 	lhw->ic = lkpi_ieee80211_ifalloc();
2733 	if (lhw->ic == NULL) {
2734 		ieee80211_free_hw(hw);
2735 		return (NULL);
2736 	}
2737 
2738 	IMPROVE();
2739 
2740 	return (hw);
2741 }
2742 
2743 void
2744 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
2745 {
2746 	struct lkpi_hw *lhw;
2747 
2748 	lhw = HW_TO_LHW(hw);
2749 	free(lhw->ic, M_LKPI80211);
2750 	lhw->ic = NULL;
2751 
2752 	/* Cleanup more of lhw here or in wiphy_free()? */
2753 	mtx_destroy(&lhw->mtx);
2754 	IMPROVE();
2755 }
2756 
2757 void
2758 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
2759 {
2760 	struct lkpi_hw *lhw;
2761 	struct ieee80211com *ic;
2762 
2763 	lhw = HW_TO_LHW(hw);
2764 	ic = lhw->ic;
2765 
2766 	/* Now set a proper name before ieee80211_ifattach(). */
2767 	ic->ic_softc = lhw;
2768 	ic->ic_name = name;
2769 
2770 	/* XXX-BZ do we also need to set wiphy name? */
2771 }
2772 
2773 struct ieee80211_hw *
2774 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
2775 {
2776 	struct lkpi_hw *lhw;
2777 
2778 	lhw = wiphy_priv(wiphy);
2779 	return (LHW_TO_HW(lhw));
2780 }
2781 
2782 static void
2783 lkpi_radiotap_attach(struct lkpi_hw *lhw)
2784 {
2785 	struct ieee80211com *ic;
2786 
2787 	ic = lhw->ic;
2788 	ieee80211_radiotap_attach(ic,
2789 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
2790 	    LKPI_RTAP_TX_FLAGS_PRESENT,
2791 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
2792 	    LKPI_RTAP_RX_FLAGS_PRESENT);
2793 }
2794 
2795 void
2796 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
2797 {
2798 	struct ieee80211com *ic;
2799 	struct lkpi_hw *lhw;
2800 #ifdef TRY_HW_CRYPTO
2801 	int i;
2802 #endif
2803 
2804 	lhw = HW_TO_LHW(hw);
2805 	ic = lhw->ic;
2806 
2807 	/* XXX-BZ figure this out how they count his... */
2808 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
2809 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
2810 		    hw->wiphy->perm_addr);
2811 	} else if (hw->wiphy->n_addresses > 0) {
2812 		/* We take the first one. */
2813 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
2814 		    hw->wiphy->addresses[0].addr);
2815 	} else {
2816 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
2817 	}
2818 
2819 	ic->ic_headroom = hw->extra_tx_headroom;
2820 
2821 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
2822 	ic->ic_opmode = IEEE80211_M_STA;
2823 
2824 	/* Set device capabilities. */
2825 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
2826 	ic->ic_caps =
2827 	    IEEE80211_C_STA |
2828 	    IEEE80211_C_MONITOR |
2829 	    IEEE80211_C_WPA |		/* WPA/RSN */
2830 	    IEEE80211_C_WME |
2831 #if 0
2832 	    IEEE80211_C_PMGT |
2833 #endif
2834 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
2835 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
2836 	    ;
2837 #if 0
2838 	/* Scanning is a different kind of beast to re-work. */
2839 	ic->ic_caps |= IEEE80211_C_BGSCAN;
2840 #endif
2841 	if (lhw->ops->hw_scan &&
2842 	    ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2843 		/* Advertise full-offload scanning */
2844 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
2845 	}
2846 
2847 #ifdef __notyet__
2848 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
2849 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
2850 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
2851 		    | IEEE80211_HTCAP_MAXAMSDU_3839
2852 						/* max A-MSDU length */
2853 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
2854 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
2855 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
2856 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
2857 #endif
2858 
2859 	ic->ic_cryptocaps = 0;
2860 #ifdef TRY_HW_CRYPTO
2861 	if (hw->wiphy->n_cipher_suites > 0) {
2862 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
2863 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
2864 			    hw->wiphy->cipher_suites[i]);
2865 	}
2866 #endif
2867 
2868 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
2869 	    ic->ic_channels);
2870 
2871 	ieee80211_ifattach(ic);
2872 
2873 	ic->ic_update_mcast = lkpi_ic_update_mcast;
2874 	ic->ic_update_promisc = lkpi_ic_update_promisc;
2875 	ic->ic_update_chw = lkpi_ic_update_chw;
2876 	ic->ic_parent = lkpi_ic_parent;
2877 	ic->ic_scan_start = lkpi_ic_scan_start;
2878 	ic->ic_scan_end = lkpi_ic_scan_end;
2879 	if (lhw->ops->hw_scan &&
2880 	    ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS))
2881 		ic->ic_scan_curchan = lkpi_ic_scan_curchan_nada;
2882 	ic->ic_set_channel = lkpi_ic_set_channel;
2883 	ic->ic_transmit = lkpi_ic_transmit;
2884 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
2885 	ic->ic_vap_create = lkpi_ic_vap_create;
2886 	ic->ic_vap_delete = lkpi_ic_vap_delete;
2887 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
2888 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
2889 
2890 	lhw->ic_node_alloc = ic->ic_node_alloc;
2891 	ic->ic_node_alloc = lkpi_ic_node_alloc;
2892 	lhw->ic_node_init = ic->ic_node_init;
2893 	ic->ic_node_init = lkpi_ic_node_init;
2894 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
2895 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
2896 	lhw->ic_node_free = ic->ic_node_free;
2897 	ic->ic_node_free = lkpi_ic_node_free;
2898 
2899 	lkpi_radiotap_attach(lhw);
2900 
2901 	if (bootverbose)
2902 		ieee80211_announce(ic);
2903 }
2904 
2905 void
2906 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
2907 {
2908 	struct lkpi_hw *lhw;
2909 	struct ieee80211com *ic;
2910 
2911 	lhw = HW_TO_LHW(hw);
2912 	ic = lhw->ic;
2913 	ieee80211_ifdetach(ic);
2914 }
2915 
2916 void
2917 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
2918     enum ieee80211_iface_iter flags,
2919     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
2920     void *arg)
2921 {
2922 	struct lkpi_hw *lhw;
2923 	struct lkpi_vif *lvif;
2924 	struct ieee80211_vif *vif;
2925 	bool active, atomic;
2926 
2927 	lhw = HW_TO_LHW(hw);
2928 
2929 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
2930 	    IEEE80211_IFACE_ITER_RESUME_ALL|
2931 	    IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
2932 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
2933 		    __func__, flags);
2934 	}
2935 
2936 	active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
2937 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
2938 
2939 	if (atomic)
2940 		LKPI_80211_LHW_LOCK(lhw);
2941 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
2942 		struct ieee80211vap *vap;
2943 
2944 		vif = LVIF_TO_VIF(lvif);
2945 
2946 		/*
2947 		 * If we want "active" interfaces, we need to distinguish on
2948 		 * whether the driver knows about them or not to be able to
2949 		 * handle the "resume" case correctly.  Skip the ones the
2950 		 * driver does not know about.
2951 		 */
2952 		if (active && !lvif->added_to_drv &&
2953 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
2954 			continue;
2955 
2956 		/*
2957 		 * Run the iterator function if we are either not asking
2958 		 * asking for active only or if the VAP is "running".
2959 		 */
2960 		/* XXX-BZ probably should have state in the lvif as well. */
2961 		vap = LVIF_TO_VAP(lvif);
2962 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
2963 			iterfunc(arg, vif->addr, vif);
2964 	}
2965 	if (atomic)
2966 		LKPI_80211_LHW_UNLOCK(lhw);
2967 }
2968 
2969 void
2970 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
2971     struct ieee80211_vif *vif,
2972     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
2973         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
2974     void *arg)
2975 {
2976 
2977 	UNIMPLEMENTED;
2978 }
2979 
2980 void
2981 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
2982     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
2983 	void *),
2984     void *arg)
2985 {
2986 
2987 	UNIMPLEMENTED;
2988 }
2989 
2990 void
2991 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
2992    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
2993 {
2994 	struct lkpi_hw *lhw;
2995 	struct lkpi_vif *lvif;
2996 	struct lkpi_sta *lsta;
2997 	struct ieee80211_sta *sta;
2998 
2999 	KASSERT(hw != NULL && iterfunc != NULL,
3000 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
3001 
3002 	lhw = HW_TO_LHW(hw);
3003 
3004 	LKPI_80211_LHW_LOCK(lhw);
3005 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3006 
3007 		LKPI_80211_LVIF_LOCK(lvif);
3008 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
3009 			if (!lsta->added_to_drv)
3010 				continue;
3011 			sta = LSTA_TO_STA(lsta);
3012 			iterfunc(arg, sta);
3013 		}
3014 		LKPI_80211_LVIF_UNLOCK(lvif);
3015 	}
3016 	LKPI_80211_LHW_UNLOCK(lhw);
3017 }
3018 
3019 int
3020 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
3021     struct linuxkpi_ieee80211_regdomain *regd)
3022 {
3023 	struct lkpi_hw *lhw;
3024 	struct ieee80211com *ic;
3025 	struct ieee80211_regdomain *rd;
3026 
3027 	lhw = wiphy_priv(wiphy);
3028 	ic = lhw->ic;
3029 
3030 	rd = &ic->ic_regdomain;
3031 	if (rd->isocc[0] == '\0') {
3032 		rd->isocc[0] = regd->alpha2[0];
3033 		rd->isocc[1] = regd->alpha2[1];
3034 	}
3035 
3036 	TODO();
3037 	/* XXX-BZ finish the rest. */
3038 
3039 	return (0);
3040 }
3041 
3042 void
3043 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
3044     struct cfg80211_scan_info *info)
3045 {
3046 	struct lkpi_hw *lhw;
3047 	struct ieee80211com *ic;
3048 	struct ieee80211_scan_state *ss;
3049 
3050 	lhw = wiphy_priv(hw->wiphy);
3051 	ic = lhw->ic;
3052 	ss = ic->ic_scan;
3053 
3054 	ieee80211_scan_done(ss->ss_vap);
3055 
3056 	LKPI_80211_LHW_LOCK(lhw);
3057 	free(lhw->hw_req->ies.common_ies, M_80211_VAP);
3058 	free(lhw->hw_req, M_LKPI80211);
3059 	lhw->hw_req = NULL;
3060 	lhw->scan_flags &= ~LKPI_SCAN_RUNNING;
3061 	wakeup(lhw);
3062 	LKPI_80211_LHW_UNLOCK(lhw);
3063 
3064 	return;
3065 }
3066 
3067 void
3068 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3069     struct ieee80211_sta *sta, struct napi_struct *napi __unused)
3070 {
3071 	struct epoch_tracker et;
3072 	struct lkpi_hw *lhw;
3073 	struct ieee80211com *ic;
3074 	struct mbuf *m;
3075 	struct skb_shared_info *shinfo;
3076 	struct ieee80211_rx_status *rx_status;
3077 	struct ieee80211_rx_stats rx_stats;
3078 	struct ieee80211_node *ni;
3079 	struct ieee80211vap *vap;
3080 	struct ieee80211_frame_min *wh;
3081 	struct ieee80211_hdr *hdr;
3082 	struct lkpi_sta *lsta;
3083 	int i, offset, ok, type;
3084 
3085 	if (skb->len < 2) {
3086 		/* Need 80211 stats here. */
3087 		IMPROVE();
3088 		goto err;
3089 	}
3090 
3091 	/*
3092 	 * For now do the data copy; we can later improve things. Might even
3093 	 * have an mbuf backing the skb data then?
3094 	 */
3095 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
3096 	if (m == NULL)
3097 		goto err;
3098 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
3099 
3100 	shinfo = skb_shinfo(skb);
3101 	offset = m->m_len;
3102 	for (i = 0; i < shinfo->nr_frags; i++) {
3103 		m_copyback(m, offset, shinfo->frags[i].size,
3104 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
3105 		    shinfo->frags[i].offset);
3106 		offset += shinfo->frags[i].size;
3107 	}
3108 
3109 	rx_status = IEEE80211_SKB_RXCB(skb);
3110 
3111 #ifdef LINUXKPI_DEBUG_80211
3112 	hdr = (void *)skb->data;
3113 	if ((debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
3114 	    ieee80211_is_beacon(hdr->frame_control))
3115 		goto no_trace_beacons;
3116 
3117 	if (debug_80211 & D80211_TRACE_RX)
3118 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
3119 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u\n",
3120 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
3121 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
3122 		    shinfo, shinfo->nr_frags,
3123 		    m, m->m_pkthdr.len, m->m_len);
3124 
3125 	if (debug_80211 & D80211_TRACE_RX_DUMP)
3126 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
3127 
3128 	/* Implement a dump_rxcb() !!! */
3129 	if (debug_80211 & D80211_TRACE_RX)
3130 		printf("TRACE %s: RXCB: %u %u %u, %#0x, %u, %#0x, %#0x, "
3131 		    "%u band %u, %u %u %u %u, %u, %#x %#x %#x %#x %u %u %u\n",
3132 			__func__,
3133 			rx_status->boottime_ns,
3134 			rx_status->mactime,
3135 			rx_status->device_timestamp,
3136 			rx_status->flag,
3137 			rx_status->freq,
3138 			rx_status->bw,
3139 			rx_status->encoding,
3140 			rx_status->ampdu_reference,
3141 			rx_status->band,
3142 			rx_status->chains,
3143 			rx_status->chain_signal[0],
3144 			rx_status->chain_signal[1],
3145 			rx_status->chain_signal[2],
3146 			rx_status->signal,
3147 			rx_status->enc_flags,
3148 			rx_status->he_dcm,
3149 			rx_status->he_gi,
3150 			rx_status->he_ru,
3151 			rx_status->zero_length_psdu_type,
3152 			rx_status->nss,
3153 			rx_status->rate_idx);
3154 no_trace_beacons:
3155 #endif
3156 
3157 	memset(&rx_stats, 0, sizeof(rx_stats));
3158 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
3159 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
3160 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3161 		rx_stats.c_rssi = rx_status->signal;
3162 	else
3163 		rx_stats.c_rssi = 0;			/* XXX */
3164 	rx_stats.c_nf = -96;				/* XXX */
3165 	rx_stats.r_flags |= IEEE80211_R_BAND;
3166 	rx_stats.c_band =
3167 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
3168 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
3169 	rx_stats.c_freq = rx_status->freq;
3170 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
3171 
3172 	/* XXX-BZ correct hardcoded rssi and noise floor. */
3173 	/* XXX (*sta_statistics)() to get to some of that? */
3174 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
3175 
3176 	lhw = HW_TO_LHW(hw);
3177 	ic = lhw->ic;
3178 
3179 	ok = ieee80211_add_rx_params(m, &rx_stats);
3180 	if (ok == 0) {
3181 		counter_u64_add(ic->ic_ierrors, 1);
3182 		goto err;
3183 	}
3184 
3185 	if (sta != NULL) {
3186 		lsta = STA_TO_LSTA(sta);
3187 		ni = ieee80211_ref_node(lsta->ni);
3188 	} else {
3189 		wh = mtod(m, struct ieee80211_frame_min *);
3190 		ni = ieee80211_find_rxnode(ic, wh);
3191 		if (ni != NULL)
3192 			lsta = ni->ni_drv_data;
3193 	}
3194 
3195 	if (ni != NULL)
3196 		vap = ni->ni_vap;
3197 	else
3198 		/*
3199 		 * XXX-BZ can we improve this by looking at the frame hdr
3200 		 * or other meta-data passed up?
3201 		 */
3202 		vap = TAILQ_FIRST(&ic->ic_vaps);
3203 
3204 	if (debug_80211 & D80211_TRACE_RX)
3205 		printf("TRACE %s: sta %p lsta %p ni %p vap %p\n", __func__, sta, lsta, ni, vap);
3206 
3207 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
3208 	    ieee80211_radiotap_active_vap(vap)) {
3209 		struct lkpi_radiotap_rx_hdr *rtap;
3210 
3211 		rtap = &lhw->rtap_rx;
3212 		rtap->wr_tsft = rx_status->device_timestamp;
3213 		rtap->wr_flags = 0;
3214 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
3215 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3216 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3217 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3218 #if 0	/* .. or it does not given we strip it below. */
3219 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3220 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
3221 #endif
3222 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
3223 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
3224 		rtap->wr_rate = 0;
3225 		IMPROVE();
3226 		/* XXX TODO status->encoding / rate_index / bw */
3227 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
3228 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
3229 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
3230 		rtap->wr_dbm_antsignal = rx_stats.c_rssi;
3231 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
3232 	}
3233 
3234 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3235 		m_adj(m, -IEEE80211_CRC_LEN);
3236 
3237 	NET_EPOCH_ENTER(et);
3238 	if (ni != NULL) {
3239 		type = ieee80211_input_mimo(ni, m);
3240 		ieee80211_free_node(ni);
3241 	} else {
3242 		type = ieee80211_input_mimo_all(ic, m);
3243 	}
3244 	NET_EPOCH_EXIT(et);
3245 
3246 	if (debug_80211 & D80211_TRACE_RX)
3247 		printf("TRACE %s: handled frame type %#0x\n", __func__, type);
3248 
3249 	IMPROVE();
3250 
3251 err:
3252 	/* The skb is ours so we can free it :-) */
3253 	kfree_skb(skb);
3254 }
3255 
3256 uint8_t
3257 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr)
3258 {
3259 	const struct ieee80211_frame *wh;
3260 
3261 	wh = (const struct ieee80211_frame *)hdr;
3262 	return (ieee80211_gettid(wh));
3263 }
3264 
3265 struct wiphy *
3266 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
3267 {
3268 	struct lkpi_wiphy *lwiphy;
3269 
3270 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
3271 	if (lwiphy == NULL)
3272 		return (NULL);
3273 	lwiphy->ops = ops;
3274 
3275 	/* XXX TODO */
3276 	return (LWIPHY_TO_WIPHY(lwiphy));
3277 }
3278 
3279 void
3280 linuxkpi_wiphy_free(struct wiphy *wiphy)
3281 {
3282 	struct lkpi_wiphy *lwiphy;
3283 
3284 	if (wiphy == NULL)
3285 		return;
3286 
3287 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
3288 	kfree(lwiphy);
3289 }
3290 
3291 uint32_t
3292 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
3293     enum nl80211_band band)
3294 {
3295 
3296 	switch (band) {
3297 	case NL80211_BAND_2GHZ:
3298 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
3299 		break;
3300 	case NL80211_BAND_5GHZ:
3301 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
3302 		break;
3303 	default:
3304 		/* XXX abort, retry, error, panic? */
3305 		break;
3306 	}
3307 
3308 	return (0);
3309 }
3310 
3311 uint32_t
3312 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
3313 {
3314 
3315 	return (ieee80211_mhz2ieee(freq, 0));
3316 }
3317 
3318 static struct lkpi_sta *
3319 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
3320 {
3321 	struct lkpi_sta *lsta, *temp;
3322 
3323 	LKPI_80211_LVIF_LOCK(lvif);
3324 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
3325 		if (lsta->ni == ni) {
3326 			LKPI_80211_LVIF_UNLOCK(lvif);
3327 			return (lsta);
3328 		}
3329 	}
3330 	LKPI_80211_LVIF_UNLOCK(lvif);
3331 
3332 	return (NULL);
3333 }
3334 
3335 struct ieee80211_sta *
3336 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
3337 {
3338 	struct lkpi_vif *lvif;
3339 	struct lkpi_sta *lsta, *temp;
3340 	struct ieee80211_sta *sta;
3341 
3342 	lvif = VIF_TO_LVIF(vif);
3343 
3344 	LKPI_80211_LVIF_LOCK(lvif);
3345 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
3346 		sta = LSTA_TO_STA(lsta);
3347 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
3348 			LKPI_80211_LVIF_UNLOCK(lvif);
3349 			return (sta);
3350 		}
3351 	}
3352 	LKPI_80211_LVIF_UNLOCK(lvif);
3353 	return (NULL);
3354 }
3355 
3356 struct ieee80211_sta *
3357 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, uint8_t *addr,
3358     uint8_t *ourvifaddr)
3359 {
3360 	struct lkpi_hw *lhw;
3361 	struct lkpi_vif *lvif;
3362 	struct lkpi_sta *lsta;
3363 	struct ieee80211_vif *vif;
3364 	struct ieee80211_sta *sta;
3365 
3366 	lhw = wiphy_priv(hw->wiphy);
3367 	sta = NULL;
3368 
3369 	LKPI_80211_LHW_LOCK(lhw);
3370 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3371 
3372 		/* XXX-BZ check our address from the vif. */
3373 
3374 		vif = LVIF_TO_VIF(lvif);
3375 		if (ourvifaddr != NULL &&
3376 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
3377 			continue;
3378 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
3379 		if (sta != NULL)
3380 			break;
3381 	}
3382 	LKPI_80211_LHW_UNLOCK(lhw);
3383 
3384 	if (sta != NULL) {
3385 		lsta = STA_TO_LSTA(sta);
3386 		if (!lsta->added_to_drv)
3387 			return (NULL);
3388 	}
3389 
3390 	return (sta);
3391 }
3392 
3393 struct sk_buff *
3394 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3395     struct ieee80211_txq *txq)
3396 {
3397 	struct lkpi_txq *ltxq;
3398 	struct sk_buff *skb;
3399 
3400 	ltxq = TXQ_TO_LTXQ(txq);
3401 	ltxq->seen_dequeue = true;
3402 
3403 	skb = skb_dequeue(&ltxq->skbq);
3404 
3405 	return (skb);
3406 }
3407 
3408 void
3409 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
3410     uint64_t *frame_cnt, uint64_t *byte_cnt)
3411 {
3412 	struct lkpi_txq *ltxq;
3413 	struct sk_buff *skb;
3414 	uint64_t fc, bc;
3415 
3416 	ltxq = TXQ_TO_LTXQ(txq);
3417 
3418 	fc = bc = 0;
3419 	skb_queue_walk(&ltxq->skbq, skb) {
3420 		fc++;
3421 		bc += skb->len;
3422 	}
3423 	if (frame_cnt)
3424 		*frame_cnt = fc;
3425 	if (byte_cnt)
3426 		*byte_cnt = bc;
3427 
3428 	/* Validate that this is doing the correct thing. */
3429 	/* Should we keep track on en/dequeue? */
3430 	IMPROVE();
3431 }
3432 
3433 /*
3434  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
3435  * The latter tries to derive the success status from the info flags
3436  * passed back from the driver.  rawx_mit() saves the ni on the m and the
3437  * m on the skb for us to be able to give feedback to net80211.
3438  */
3439 void
3440 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
3441     int status)
3442 {
3443 	struct ieee80211_node *ni;
3444 	struct mbuf *m;
3445 
3446 	m = skb->m;
3447 	skb->m = NULL;
3448 
3449 	if (m != NULL) {
3450 		ni = m->m_pkthdr.PH_loc.ptr;
3451 		/* Status: 0 is ok, != 0 is error. */
3452 		ieee80211_tx_complete(ni, m, status);
3453 		/* ni & mbuf were consumed. */
3454 	}
3455 
3456 	kfree_skb(skb);
3457 }
3458 
3459 /*
3460  * This is an internal bandaid for the moment for the way we glue
3461  * skbs and mbufs together for TX.  Once we have skbs backed by
3462  * mbufs this should go away.
3463  * This is a public function but kept on the private KPI (lkpi_)
3464  * and is not exposed by a header file.
3465  */
3466 static void
3467 lkpi_ieee80211_free_skb_mbuf(void *p)
3468 {
3469 	struct ieee80211_node *ni;
3470 	struct mbuf *m;
3471 
3472 	if (p == NULL)
3473 		return;
3474 
3475 	m = (struct mbuf *)p;
3476 	M_ASSERTPKTHDR(m);
3477 
3478 	ni = m->m_pkthdr.PH_loc.ptr;
3479 	m->m_pkthdr.PH_loc.ptr = NULL;
3480 	if (ni != NULL)
3481 		ieee80211_free_node(ni);
3482 	m_freem(m);
3483 }
3484 
3485 void
3486 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
3487     struct delayed_work *w, int delay)
3488 {
3489 	struct lkpi_hw *lhw;
3490 
3491 	/* Need to make sure hw is in a stable (non-suspended) state. */
3492 	IMPROVE();
3493 
3494 	lhw = HW_TO_LHW(hw);
3495 	queue_delayed_work(lhw->workq, w, delay);
3496 }
3497 
3498 void
3499 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
3500     struct work_struct *w)
3501 {
3502 	struct lkpi_hw *lhw;
3503 
3504 	/* Need to make sure hw is in a stable (non-suspended) state. */
3505 	IMPROVE();
3506 
3507 	lhw = HW_TO_LHW(hw);
3508 	queue_work(lhw->workq, w);
3509 }
3510 
3511 struct sk_buff *
3512 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
3513     struct ieee80211_vif *vif)
3514 {
3515 	struct lkpi_vif *lvif;
3516 	struct ieee80211vap *vap;
3517 	struct sk_buff *skb;
3518 	struct ieee80211_frame_pspoll *psp;
3519 	uint16_t v;
3520 
3521 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
3522 	if (skb == NULL)
3523 		return (NULL);
3524 
3525 	skb_reserve(skb, hw->extra_tx_headroom);
3526 
3527 	lvif = VIF_TO_LVIF(vif);
3528 	vap = LVIF_TO_VAP(lvif);
3529 
3530 	psp = skb_put_zero(skb, sizeof(*psp));
3531 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
3532 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
3533 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
3534 	memcpy(&psp->i_aid, &v, sizeof(v));
3535 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
3536 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
3537 
3538 	return (skb);
3539 }
3540 
3541 struct sk_buff *
3542 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
3543     struct ieee80211_vif *vif, bool qos)
3544 {
3545 	struct lkpi_vif *lvif;
3546 	struct ieee80211vap *vap;
3547 	struct sk_buff *skb;
3548 	struct ieee80211_frame *nullf;
3549 
3550 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
3551 	if (skb == NULL)
3552 		return (NULL);
3553 
3554 	skb_reserve(skb, hw->extra_tx_headroom);
3555 
3556 	lvif = VIF_TO_LVIF(vif);
3557 	vap = LVIF_TO_VAP(lvif);
3558 
3559 	nullf = skb_put_zero(skb, sizeof(*nullf));
3560 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
3561 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
3562 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
3563 
3564 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
3565 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
3566 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
3567 
3568 	return (skb);
3569 }
3570 
3571 struct wireless_dev *
3572 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
3573 {
3574 	struct lkpi_vif *lvif;
3575 
3576 	lvif = VIF_TO_LVIF(vif);
3577 	return (&lvif->wdev);
3578 }
3579 
3580 void
3581 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
3582 {
3583 	struct lkpi_vif *lvif;
3584 	struct ieee80211vap *vap;
3585 	enum ieee80211_state nstate;
3586 	int arg;
3587 
3588 	lvif = VIF_TO_LVIF(vif);
3589 	vap = LVIF_TO_VAP(lvif);
3590 
3591 	/*
3592 	 * Go to scan; otherwise we need to elaborately check state and
3593 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
3594 	 * Let the statemachine handle all neccessary changes.
3595 	 */
3596 	nstate = IEEE80211_S_SCAN;
3597 	arg = 0;
3598 
3599 	if (debug_80211 & D80211_TRACE)
3600 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
3601 	ieee80211_new_state(vap, nstate, arg);
3602 }
3603 
3604 MODULE_VERSION(linuxkpi_wlan, 1);
3605 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
3606 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
3607