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