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