xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision a839757109891573123d8777b539fafa2d2198bb)
16b4cac81SBjoern A. Zeeb /*-
22e183d99SBjoern A. Zeeb  * Copyright (c) 2020-2022 The FreeBSD Foundation
3086be6a8SBjoern A. Zeeb  * Copyright (c) 2020-2022 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
426b4cac81SBjoern A. Zeeb #include <sys/cdefs.h>
436b4cac81SBjoern A. Zeeb __FBSDID("$FreeBSD$");
446b4cac81SBjoern A. Zeeb 
456b4cac81SBjoern A. Zeeb #include <sys/param.h>
466b4cac81SBjoern A. Zeeb #include <sys/types.h>
476b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
486b4cac81SBjoern A. Zeeb #include <sys/errno.h>
496b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
506b4cac81SBjoern A. Zeeb #include <sys/module.h>
516b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
526b4cac81SBjoern A. Zeeb #include <sys/socket.h>
536b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
546b4cac81SBjoern A. Zeeb #include <sys/queue.h>
556b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
56527687a9SBjoern A. Zeeb #include <sys/libkern.h>
576b4cac81SBjoern A. Zeeb 
586b4cac81SBjoern A. Zeeb #include <net/if.h>
596b4cac81SBjoern A. Zeeb #include <net/if_var.h>
606b4cac81SBjoern A. Zeeb #include <net/if_media.h>
616b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
626b4cac81SBjoern A. Zeeb 
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
646b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
656b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
666b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
676b4cac81SBjoern A. Zeeb 
686b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
696b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
706b4cac81SBjoern A. Zeeb 
716b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
726b4cac81SBjoern A. Zeeb #include "linux_80211.h"
736b4cac81SBjoern A. Zeeb 
744a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
75b35f6cd0SBjoern A. Zeeb /* #define	LKPI_80211_HW_CRYPTO */
76b35f6cd0SBjoern A. Zeeb 
776b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
786b4cac81SBjoern A. Zeeb 
796b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
806b4cac81SBjoern A. Zeeb 
819d9ba2b7SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
829d9ba2b7SBjoern A. Zeeb int linuxkpi_debug_80211;
836b4cac81SBjoern A. Zeeb 
846b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
859d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
869d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
879d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
889d9ba2b7SBjoern A. Zeeb 
899d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
909d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
919d9ba2b7SBjoern A. Zeeb 
929d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
936b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
949d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
956b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
966b4cac81SBjoern A. Zeeb #else
976b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
986b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
996b4cac81SBjoern A. Zeeb #endif
1006b4cac81SBjoern A. Zeeb 
1016b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1026b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1036b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1046b4cac81SBjoern A. Zeeb #endif
1056b4cac81SBjoern A. Zeeb 
106caaa79c3SBjoern A. Zeeb /* c.f. ieee80211_ioctl.c */
107caaa79c3SBjoern A. Zeeb static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
108caaa79c3SBjoern A. Zeeb 
1096b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1106b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1116b4cac81SBjoern A. Zeeb 
112b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
113b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
114b0f73768SBjoern A. Zeeb 
1154f61ef8bSBjoern A. Zeeb const uint8_t tid_to_mac80211_ac[] = {
1164f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1174f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1184f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1194f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1204f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1214f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1224f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1234f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1244f61ef8bSBjoern A. Zeeb #if 0
1254f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1264f61ef8bSBjoern A. Zeeb #endif
1274f61ef8bSBjoern A. Zeeb };
1284f61ef8bSBjoern A. Zeeb 
1296b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1306b4cac81SBjoern A. Zeeb 	/*
1316b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1326b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1336b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1346b4cac81SBjoern A. Zeeb 	 */
1356b4cac81SBjoern A. Zeeb };
1366b4cac81SBjoern A. Zeeb 
1376b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1386b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
1396b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
1406b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1414a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1424a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1434a67f1dfSBjoern A. Zeeb #endif
1446b4cac81SBjoern A. Zeeb 
145d9f59799SBjoern A. Zeeb static void
14667674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
14767674c1cSBjoern A. Zeeb     const char *_f, int _l)
148d9f59799SBjoern A. Zeeb {
149d9f59799SBjoern A. Zeeb 
1509d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1519d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
152d9f59799SBjoern A. Zeeb 		return;
153d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
154d9f59799SBjoern A. Zeeb 		return;
155d9f59799SBjoern A. Zeeb 
156d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
15767674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
15867674c1cSBjoern A. Zeeb 	if (ni != NULL)
15967674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
160d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
161d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
162d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
1639d9ba2b7SBjoern A. Zeeb #endif
164d9f59799SBjoern A. Zeeb }
165d9f59799SBjoern A. Zeeb 
166d9f59799SBjoern A. Zeeb static void
167d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
168d9f59799SBjoern A. Zeeb {
169d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
170d9f59799SBjoern A. Zeeb 
171d9f59799SBjoern A. Zeeb 	ni = lsta->ni;
172d9f59799SBjoern A. Zeeb 
173d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
174d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
175d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
176d9f59799SBjoern A. Zeeb 
177d9f59799SBjoern A. Zeeb 	lsta->ni = NULL;
178d9f59799SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
179e24e8103SBjoern A. Zeeb 	if (ni != NULL)
180d9f59799SBjoern A. Zeeb 		ieee80211_free_node(ni);
181d9f59799SBjoern A. Zeeb 
182e24e8103SBjoern A. Zeeb 	IMPROVE("more from lkpi_ic_node_free() should happen here.");
183e24e8103SBjoern A. Zeeb 
184e24e8103SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
185d9f59799SBjoern A. Zeeb }
186d9f59799SBjoern A. Zeeb 
1874f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
1884f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1894f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
1904f61ef8bSBjoern A. Zeeb {
1914f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
1924f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1934f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
1944f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
195b6b352e4SBjoern A. Zeeb 	int band, i, tid;
1964f61ef8bSBjoern A. Zeeb 
1974f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
1984f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
1994f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2004f61ef8bSBjoern A. Zeeb 		return (NULL);
2014f61ef8bSBjoern A. Zeeb 
2024f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2034f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2044f61ef8bSBjoern A. Zeeb #if 0
2054f61ef8bSBjoern A. Zeeb 	/*
2064f61ef8bSBjoern A. Zeeb 	 * This needs to be done in node_init() as ieee80211_alloc_node()
2074f61ef8bSBjoern A. Zeeb 	 * will initialise the refcount after us.
2084f61ef8bSBjoern A. Zeeb 	 */
2094f61ef8bSBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
2104f61ef8bSBjoern A. Zeeb #endif
2114f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2124f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2134f61ef8bSBjoern A. Zeeb 
2144f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2154f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2164f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2174f61ef8bSBjoern A. Zeeb 
2184f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
219b6b352e4SBjoern A. Zeeb 
220b6b352e4SBjoern A. Zeeb 	/* TXQ */
2214f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
2224f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
2234f61ef8bSBjoern A. Zeeb 
224d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
2254f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
2264f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
2274f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
2284f61ef8bSBjoern A. Zeeb 			goto cleanup;
2294f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
2304f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
231d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
232d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
233d0d29110SBjoern A. Zeeb 				continue;
234d0d29110SBjoern A. Zeeb 			}
235d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
2364f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
2374f61ef8bSBjoern A. Zeeb 		} else {
2384f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = tid_to_mac80211_ac[tid & 7];
2394f61ef8bSBjoern A. Zeeb 		}
240d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
241d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
2424f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
2434f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
244d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
2454f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
2464f61ef8bSBjoern A. Zeeb 	}
2474f61ef8bSBjoern A. Zeeb 
248b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
249b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
250b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
251b6b352e4SBjoern A. Zeeb 
252b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
253b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
254b6b352e4SBjoern A. Zeeb 			continue;
255b6b352e4SBjoern A. Zeeb 
256b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
257b6b352e4SBjoern A. Zeeb 
258b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
259b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
260b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
261b6b352e4SBjoern A. Zeeb 		}
262b6b352e4SBjoern A. Zeeb 	}
263b6b352e4SBjoern A. Zeeb 	IMPROVE("ht, vht, he, ... bandwidth, smps_mode, ..");
264b6b352e4SBjoern A. Zeeb 	/* bandwidth = IEEE80211_STA_RX_... */
265b6b352e4SBjoern A. Zeeb 
2664f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
2674f61ef8bSBjoern A. Zeeb 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
2684f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
2694f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
2704f61ef8bSBjoern A. Zeeb 
2714f61ef8bSBjoern A. Zeeb 	return (lsta);
2724f61ef8bSBjoern A. Zeeb 
2734f61ef8bSBjoern A. Zeeb cleanup:
2744f61ef8bSBjoern A. Zeeb 	for (; tid >= 0; tid--)
2754f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
2764f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
2774f61ef8bSBjoern A. Zeeb 	return (NULL);
2784f61ef8bSBjoern A. Zeeb }
2794f61ef8bSBjoern A. Zeeb 
2806b4cac81SBjoern A. Zeeb static enum nl80211_band
2816b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
2826b4cac81SBjoern A. Zeeb {
2836b4cac81SBjoern A. Zeeb 
2846b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
2856b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
2866b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
2876b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
2886b4cac81SBjoern A. Zeeb #ifdef __notyet__
2896b4cac81SBjoern A. Zeeb 	else if ()
2906b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
2916b4cac81SBjoern A. Zeeb 	else if ()
2926b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
2936b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
2946b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
2956b4cac81SBjoern A. Zeeb #endif
2966b4cac81SBjoern A. Zeeb 	else
2976b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
2986b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
2996b4cac81SBjoern A. Zeeb }
3006b4cac81SBjoern A. Zeeb 
3016b4cac81SBjoern A. Zeeb static uint32_t
3026b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
3036b4cac81SBjoern A. Zeeb {
3046b4cac81SBjoern A. Zeeb 
3056b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
3066b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
3076b4cac81SBjoern A. Zeeb 	switch (band) {
3086b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
3096b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
3106b4cac81SBjoern A. Zeeb 		break;
3116b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
3126b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
3136b4cac81SBjoern A. Zeeb 		break;
3146b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
3156b4cac81SBjoern A. Zeeb 		break;
3166b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
3176b4cac81SBjoern A. Zeeb 		break;
3186b4cac81SBjoern A. Zeeb 	default:
3196b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
3206b4cac81SBjoern A. Zeeb 		break;
3216b4cac81SBjoern A. Zeeb 	}
3226b4cac81SBjoern A. Zeeb 
3236b4cac81SBjoern A. Zeeb 	IMPROVE();
3246b4cac81SBjoern A. Zeeb 	return (0x00);
3256b4cac81SBjoern A. Zeeb }
3266b4cac81SBjoern A. Zeeb 
327e3a0b120SBjoern A. Zeeb #if 0
3286b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
3296b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
3306b4cac81SBjoern A. Zeeb {
3316b4cac81SBjoern A. Zeeb 
3326b4cac81SBjoern A. Zeeb 	switch (ac) {
3336b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
3346b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
3356b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
3366b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
3376b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
3386b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
3396b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
3406b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
3416b4cac81SBjoern A. Zeeb 	default:
3426b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
3436b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
3446b4cac81SBjoern A. Zeeb 	}
3456b4cac81SBjoern A. Zeeb }
346e3a0b120SBjoern A. Zeeb #endif
3476b4cac81SBjoern A. Zeeb 
3486b4cac81SBjoern A. Zeeb static enum nl80211_iftype
3496b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
3506b4cac81SBjoern A. Zeeb {
3516b4cac81SBjoern A. Zeeb 
3526b4cac81SBjoern A. Zeeb 	switch (opmode) {
3536b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
3546b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
3556b4cac81SBjoern A. Zeeb 		break;
3566b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
3576b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
3586b4cac81SBjoern A. Zeeb 		break;
3596b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
3606b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
3616b4cac81SBjoern A. Zeeb 		break;
3626b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
3636b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
3646b4cac81SBjoern A. Zeeb 		break;
3656b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
3666b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
3676b4cac81SBjoern A. Zeeb 		break;
3686b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
3696b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
3706b4cac81SBjoern A. Zeeb 		break;
3716b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
3726b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
3736b4cac81SBjoern A. Zeeb 	default:
3746b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
3756b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
3766b4cac81SBjoern A. Zeeb 	}
3776b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
3786b4cac81SBjoern A. Zeeb }
3796b4cac81SBjoern A. Zeeb 
380b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
3816b4cac81SBjoern A. Zeeb static uint32_t
3826b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
3836b4cac81SBjoern A. Zeeb {
3846b4cac81SBjoern A. Zeeb 
3856b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
3866b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
3876b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
3886b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
3896b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
3906b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
3916b4cac81SBjoern A. Zeeb 		return (IEEE80211_CIPHER_AES_CCM);
3926b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
3936b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
3946b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
3956b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
3966b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
3976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
3986b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3996b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4006b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
4016b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
4026b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
4036b4cac81SBjoern A. Zeeb 		break;
4046b4cac81SBjoern A. Zeeb 	default:
4056b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
4066b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
4076b4cac81SBjoern A. Zeeb 	}
4086b4cac81SBjoern A. Zeeb 
4096b4cac81SBjoern A. Zeeb 	return (0);
4106b4cac81SBjoern A. Zeeb }
4116b4cac81SBjoern A. Zeeb 
4126b4cac81SBjoern A. Zeeb static uint32_t
4136b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
4146b4cac81SBjoern A. Zeeb {
4156b4cac81SBjoern A. Zeeb 
4166b4cac81SBjoern A. Zeeb 	switch (cipher) {
4176b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
4186b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
4196b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
4206b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
4216b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
4226b4cac81SBjoern A. Zeeb 		if (keylen < 8)
4236b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
4246b4cac81SBjoern A. Zeeb 		else
4256b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
4266b4cac81SBjoern A. Zeeb 		break;
4276b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
4286b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
4296b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
4306b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
4316b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
4326b4cac81SBjoern A. Zeeb 		break;
4336b4cac81SBjoern A. Zeeb 	default:
4346b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
4356b4cac81SBjoern A. Zeeb 	};
4366b4cac81SBjoern A. Zeeb 	return (0);
4376b4cac81SBjoern A. Zeeb }
4386b4cac81SBjoern A. Zeeb #endif
4396b4cac81SBjoern A. Zeeb 
4406b4cac81SBjoern A. Zeeb #ifdef __notyet__
4416b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
4426b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
4436b4cac81SBjoern A. Zeeb {
4446b4cac81SBjoern A. Zeeb 
4456b4cac81SBjoern A. Zeeb 	/*
4466b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
4476b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
4486b4cac81SBjoern A. Zeeb 	 */
4496b4cac81SBjoern A. Zeeb 	switch (state) {
4506b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
4516b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
4526b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
4536b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
4546b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
4556b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
4566b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
4576b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
4586b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
4596b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
4606b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
4616b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
4626b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
4636b4cac81SBjoern A. Zeeb 	default:
4646b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
4656b4cac81SBjoern A. Zeeb 	};
4666b4cac81SBjoern A. Zeeb 
4676b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
4686b4cac81SBjoern A. Zeeb }
4696b4cac81SBjoern A. Zeeb #endif
4706b4cac81SBjoern A. Zeeb 
4716b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
4726b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
4736b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
4746b4cac81SBjoern A. Zeeb {
4756b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4766b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
4776b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
4786b4cac81SBjoern A. Zeeb 	int i, nchans;
4796b4cac81SBjoern A. Zeeb 
4806b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4816b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
4826b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
4836b4cac81SBjoern A. Zeeb 		return (NULL);
4846b4cac81SBjoern A. Zeeb 
4856b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
4866b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
4876b4cac81SBjoern A. Zeeb 		return (NULL);
4886b4cac81SBjoern A. Zeeb 
4896b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
4906b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
4916b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
4926b4cac81SBjoern A. Zeeb 			return (&channels[i]);
4936b4cac81SBjoern A. Zeeb 	}
4946b4cac81SBjoern A. Zeeb 
4956b4cac81SBjoern A. Zeeb 	return (NULL);
4966b4cac81SBjoern A. Zeeb }
4976b4cac81SBjoern A. Zeeb 
4986b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
4996b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
5006b4cac81SBjoern A. Zeeb {
5016b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
5026b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
5036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5046b4cac81SBjoern A. Zeeb 
5056b4cac81SBjoern A. Zeeb 	chan = NULL;
5066b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
5076b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
5086b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
5096b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
5106b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
5116b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
5126b4cac81SBjoern A. Zeeb 	else
5136b4cac81SBjoern A. Zeeb 		c = NULL;
5146b4cac81SBjoern A. Zeeb 
5156b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
5166b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
5176b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
5186b4cac81SBjoern A. Zeeb 	}
5196b4cac81SBjoern A. Zeeb 
5206b4cac81SBjoern A. Zeeb 	return (chan);
5216b4cac81SBjoern A. Zeeb }
5226b4cac81SBjoern A. Zeeb 
5232e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
5242e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
5252e183d99SBjoern A. Zeeb {
5262e183d99SBjoern A. Zeeb 	enum nl80211_band band;
5272e183d99SBjoern A. Zeeb 
5282e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5292e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5302e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5312e183d99SBjoern A. Zeeb 		int i;
5322e183d99SBjoern A. Zeeb 
5332e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
5342e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5352e183d99SBjoern A. Zeeb 			continue;
5362e183d99SBjoern A. Zeeb 
5372e183d99SBjoern A. Zeeb 		channels = supband->channels;
5382e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5392e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
5402e183d99SBjoern A. Zeeb 				return (&channels[i]);
5412e183d99SBjoern A. Zeeb 		}
5422e183d99SBjoern A. Zeeb 	}
5432e183d99SBjoern A. Zeeb 
5442e183d99SBjoern A. Zeeb 	return (NULL);
5452e183d99SBjoern A. Zeeb }
5462e183d99SBjoern A. Zeeb 
547b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
5486b4cac81SBjoern A. Zeeb static int
5496b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
5506b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
5516b4cac81SBjoern A. Zeeb {
5526b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
5536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5546b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5556b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5566b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5576b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5586b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
5596b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
5606b4cac81SBjoern A. Zeeb 	int error;
5616b4cac81SBjoern A. Zeeb 
5626b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
5636b4cac81SBjoern A. Zeeb 
5646b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
5656b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
5666b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5676b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5686b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5696b4cac81SBjoern A. Zeeb 
5706b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
5716b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
5726b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
5736b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
5746b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
5756b4cac81SBjoern A. Zeeb #if 0
5766b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
5776b4cac81SBjoern A. Zeeb #endif
5786b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
5796b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
5806b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
5816b4cac81SBjoern A. Zeeb 
5826b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
5836b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
5846b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
5856b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
5866b4cac81SBjoern A. Zeeb 		break;
5876b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5886b4cac81SBjoern A. Zeeb 	default:
5896b4cac81SBjoern A. Zeeb 		IMPROVE();
5906b4cac81SBjoern A. Zeeb 		return (0);
5916b4cac81SBjoern A. Zeeb 	};
5926b4cac81SBjoern A. Zeeb 
5936b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
5946b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
5956b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
5966b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
5976b4cac81SBjoern A. Zeeb 
5986b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
5996b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
6006b4cac81SBjoern A. Zeeb 	}
6016b4cac81SBjoern A. Zeeb 
6026b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
6036b4cac81SBjoern A. Zeeb 	if (error != 0) {
6046b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
6056b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
6066b4cac81SBjoern A. Zeeb 		return (0);
6076b4cac81SBjoern A. Zeeb 	} else {
6086b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
6096b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
6106b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
6116b4cac81SBjoern A. Zeeb 		return (1);
6126b4cac81SBjoern A. Zeeb 	}
6136b4cac81SBjoern A. Zeeb }
6146b4cac81SBjoern A. Zeeb 
6156b4cac81SBjoern A. Zeeb static int
6166b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
6176b4cac81SBjoern A. Zeeb {
6186b4cac81SBjoern A. Zeeb 
6196b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
6206b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
6216b4cac81SBjoern A. Zeeb }
6226b4cac81SBjoern A. Zeeb static  int
6236b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
6246b4cac81SBjoern A. Zeeb {
6256b4cac81SBjoern A. Zeeb 
6266b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
6276b4cac81SBjoern A. Zeeb }
6286b4cac81SBjoern A. Zeeb #endif
6296b4cac81SBjoern A. Zeeb 
6306b4cac81SBjoern A. Zeeb static u_int
6316b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6326b4cac81SBjoern A. Zeeb {
6336b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
6346b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
6356b4cac81SBjoern A. Zeeb 
6366b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
6376b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
6386b4cac81SBjoern A. Zeeb 
6396b4cac81SBjoern A. Zeeb 	mc_list = arg;
6406b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
6416b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
6426b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
6436b4cac81SBjoern A. Zeeb 			return (0);
6446b4cac81SBjoern A. Zeeb 	}
6456b4cac81SBjoern A. Zeeb 
6466b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
6476b4cac81SBjoern A. Zeeb 	if (addr == NULL)
6486b4cac81SBjoern A. Zeeb 		return (0);
6496b4cac81SBjoern A. Zeeb 
6506b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
6516b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
6526b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
6536b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
6546b4cac81SBjoern A. Zeeb 	mc_list->count++;
6556b4cac81SBjoern A. Zeeb 
6569d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6579d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
6586b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
6596b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
6609d9ba2b7SBjoern A. Zeeb #endif
6616b4cac81SBjoern A. Zeeb 
6626b4cac81SBjoern A. Zeeb 	return (1);
6636b4cac81SBjoern A. Zeeb }
6646b4cac81SBjoern A. Zeeb 
6656b4cac81SBjoern A. Zeeb static void
6666b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
6676b4cac81SBjoern A. Zeeb {
6686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6706b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
6716b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
6726b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
6736b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
6746b4cac81SBjoern A. Zeeb 	u64 mc;
6756b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
6766b4cac81SBjoern A. Zeeb 
6776b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
6786b4cac81SBjoern A. Zeeb 
6796b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
6806b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
6816b4cac81SBjoern A. Zeeb 		return;
6826b4cac81SBjoern A. Zeeb 
6836b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
6846b4cac81SBjoern A. Zeeb 		return;
6856b4cac81SBjoern A. Zeeb 
6866b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
6876b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
6886b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
6896b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
6906b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6916b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
6926b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
6936b4cac81SBjoern A. Zeeb 	} else {
6946b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
6956b4cac81SBjoern A. Zeeb 	}
6966b4cac81SBjoern A. Zeeb 
6976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6986b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
6996b4cac81SBjoern A. Zeeb 	/*
7006b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
7016b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
7026b4cac81SBjoern A. Zeeb 	 */
7036b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
7046b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
7056b4cac81SBjoern A. Zeeb 
7069d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7079d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
7086b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
7096b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
7109d9ba2b7SBjoern A. Zeeb #endif
7116b4cac81SBjoern A. Zeeb 
7126b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
7136b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
7146b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
7156b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
7166b4cac81SBjoern A. Zeeb 			mc_list.count--;
7176b4cac81SBjoern A. Zeeb 		}
7186b4cac81SBjoern A. Zeeb 	}
7196b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
7206b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
7216b4cac81SBjoern A. Zeeb }
7226b4cac81SBjoern A. Zeeb 
723fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
724fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
725fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
726fa8f007dSBjoern A. Zeeb {
727fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
728fa8f007dSBjoern A. Zeeb 
729fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
730fa8f007dSBjoern A. Zeeb 
7319d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7329d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
733fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
734fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
735fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
736fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
737fa8f007dSBjoern A. Zeeb 			vif->bss_conf.assoc, vif->bss_conf.aid,
738fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
739fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
740fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
741fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
742fa8f007dSBjoern A. Zeeb 			bss_changed);
7439d9ba2b7SBjoern A. Zeeb #endif
744fa8f007dSBjoern A. Zeeb 
745fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
746fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
747fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
748fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
749fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
750fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
751fa8f007dSBjoern A. Zeeb 	}
752fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
753fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
754fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
755fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
756fa8f007dSBjoern A. Zeeb 	}
757fa8f007dSBjoern A. Zeeb 
758fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
759fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
760fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
761fa8f007dSBjoern A. Zeeb 
7629d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7639d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
764fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
765fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
766fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
767fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
768fa8f007dSBjoern A. Zeeb 			vif->bss_conf.assoc, vif->bss_conf.aid,
769fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
770fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
771fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
772fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
773fa8f007dSBjoern A. Zeeb 			bss_changed);
7749d9ba2b7SBjoern A. Zeeb #endif
775fa8f007dSBjoern A. Zeeb 
776fa8f007dSBjoern A. Zeeb 	return (bss_changed);
777fa8f007dSBjoern A. Zeeb }
778fa8f007dSBjoern A. Zeeb 
7796b4cac81SBjoern A. Zeeb static void
7806b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
7816b4cac81SBjoern A. Zeeb {
7826b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7836b4cac81SBjoern A. Zeeb 	int error;
7846b4cac81SBjoern A. Zeeb 
785a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0)
7866b4cac81SBjoern A. Zeeb 		return;
7876b4cac81SBjoern A. Zeeb 
7886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7896b4cac81SBjoern A. Zeeb 
790bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
791bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
7926b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
7936b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
7946b4cac81SBjoern A. Zeeb 
7956b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
7966b4cac81SBjoern A. Zeeb 	error = msleep(lhw, &lhw->mtx, 0, "lhwscanstop", hz/2);
797bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
798bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
7996b4cac81SBjoern A. Zeeb 
800a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
8016b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
8026b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
8036b4cac81SBjoern A. Zeeb }
8046b4cac81SBjoern A. Zeeb 
8056b4cac81SBjoern A. Zeeb static void
806086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
807086be6a8SBjoern A. Zeeb {
808086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
809086be6a8SBjoern A. Zeeb 	int error;
810086be6a8SBjoern A. Zeeb 	bool old;
811086be6a8SBjoern A. Zeeb 
812086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
813086be6a8SBjoern A. Zeeb 	if (old == new)
814086be6a8SBjoern A. Zeeb 		return;
815086be6a8SBjoern A. Zeeb 
816086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
817086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
818086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
819086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
820086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
821086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
822086be6a8SBjoern A. Zeeb 	}
823086be6a8SBjoern A. Zeeb }
824086be6a8SBjoern A. Zeeb 
825086be6a8SBjoern A. Zeeb static void
8266b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
8276b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
8286b4cac81SBjoern A. Zeeb {
8296b4cac81SBjoern A. Zeeb 	sta->aid = 0;
8306b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.assoc) {
8316b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
8326b4cac81SBjoern A. Zeeb 		enum ieee80211_bss_changed changed;
8336b4cac81SBjoern A. Zeeb 
8346b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
8356b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
8366b4cac81SBjoern A. Zeeb 
8376b4cac81SBjoern A. Zeeb 		changed = 0;
8386b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = false;
8396b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = 0;
8406b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
841d9f59799SBjoern A. Zeeb 		/*
842d9f59799SBjoern A. Zeeb 		 * This will remove the sta from firmware for iwlwifi.
843d9f59799SBjoern A. Zeeb 		 * So confusing that they use state and flags and ... ^%$%#%$^.
844d9f59799SBjoern A. Zeeb 		 */
8456b4cac81SBjoern A. Zeeb 		IMPROVE();
8466b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
8476b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
8486b4cac81SBjoern A. Zeeb 		    changed);
849086be6a8SBjoern A. Zeeb 
850086be6a8SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
8516b4cac81SBjoern A. Zeeb 	}
8526b4cac81SBjoern A. Zeeb }
8536b4cac81SBjoern A. Zeeb 
8546b4cac81SBjoern A. Zeeb static void
8556b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
8566b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
8576b4cac81SBjoern A. Zeeb {
8586b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
8596b4cac81SBjoern A. Zeeb 	int tid;
8606b4cac81SBjoern A. Zeeb 
8616b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
8626b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
8636b4cac81SBjoern A. Zeeb 
8646b4cac81SBjoern A. Zeeb 			if (tid == IEEE80211_NUM_TIDS) {
8656b4cac81SBjoern A. Zeeb 				IMPROVE("station specific?");
8666b4cac81SBjoern A. Zeeb 				if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
8676b4cac81SBjoern A. Zeeb 					continue;
8686b4cac81SBjoern A. Zeeb 			} else if (tid >= hw->queues)
8696b4cac81SBjoern A. Zeeb 				continue;
8706b4cac81SBjoern A. Zeeb 
8716b4cac81SBjoern A. Zeeb 			if (sta->txq[tid] == NULL)
8726b4cac81SBjoern A. Zeeb 				continue;
8736b4cac81SBjoern A. Zeeb 
8746b4cac81SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8756b4cac81SBjoern A. Zeeb 			if (dequeue_seen && !ltxq->seen_dequeue)
8766b4cac81SBjoern A. Zeeb 				continue;
8776b4cac81SBjoern A. Zeeb 
8786b4cac81SBjoern A. Zeeb 			if (no_emptyq && skb_queue_empty(&ltxq->skbq))
8796b4cac81SBjoern A. Zeeb 				continue;
8806b4cac81SBjoern A. Zeeb 
8816b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
8826b4cac81SBjoern A. Zeeb 	}
8836b4cac81SBjoern A. Zeeb }
8846b4cac81SBjoern A. Zeeb 
8856b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8866b4cac81SBjoern A. Zeeb 
8876b4cac81SBjoern A. Zeeb static int
8886b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
8896b4cac81SBjoern A. Zeeb {
8906b4cac81SBjoern A. Zeeb 
8916b4cac81SBjoern A. Zeeb 	return (0);
8926b4cac81SBjoern A. Zeeb }
8936b4cac81SBjoern A. Zeeb 
8946b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
8956b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
8966b4cac81SBjoern A. Zeeb 
8976b4cac81SBjoern A. Zeeb static int
8986b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
8996b4cac81SBjoern A. Zeeb {
9006b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
9016b4cac81SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *conf;
9026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
9056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
9066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
9076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
9086b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
9096b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
9106b4cac81SBjoern A. Zeeb 	uint32_t changed;
9116b4cac81SBjoern A. Zeeb 	int error;
9126b4cac81SBjoern A. Zeeb 
9136b4cac81SBjoern A. Zeeb 	chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
9146b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
9156b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
9166b4cac81SBjoern A. Zeeb 		return (ESRCH);
9176b4cac81SBjoern A. Zeeb 	}
9186b4cac81SBjoern A. Zeeb 
9196b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
9206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9216b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
9226b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
9236b4cac81SBjoern A. Zeeb 
924d9f59799SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
925d9f59799SBjoern A. Zeeb 
9266b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
9276b4cac81SBjoern A. Zeeb 
9286b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
9296b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
9306b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
9316b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
9326b4cac81SBjoern A. Zeeb 	} else {
9336b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
9346b4cac81SBjoern A. Zeeb 		conf = malloc(sizeof(*conf) + hw->chanctx_data_size,
9356b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
9366b4cac81SBjoern A. Zeeb 	}
9376b4cac81SBjoern A. Zeeb 
9386b4cac81SBjoern A. Zeeb 	conf->rx_chains_dynamic = 1;
9396b4cac81SBjoern A. Zeeb 	conf->rx_chains_static = 1;
9406b4cac81SBjoern A. Zeeb 	conf->radar_enabled =
9416b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
9426b4cac81SBjoern A. Zeeb 	conf->def.chan = chan;
9436b4cac81SBjoern A. Zeeb 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
9446b4cac81SBjoern A. Zeeb 	conf->def.center_freq1 = chan->center_freq;
9456b4cac81SBjoern A. Zeeb 	conf->def.center_freq2 = 0;
9466b4cac81SBjoern A. Zeeb 	/* Responder ... */
9476b4cac81SBjoern A. Zeeb 	conf->min_def.chan = chan;
9486b4cac81SBjoern A. Zeeb 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
9496b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq1 = chan->center_freq;
9506b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq2 = 0;
9516b4cac81SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT only");
9526b4cac81SBjoern A. Zeeb 
9536b4cac81SBjoern A. Zeeb 	error = 0;
9546b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
9556b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
9566b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
9576b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
9586b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
9596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
9606b4cac81SBjoern A. Zeeb 	} else {
9616b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, conf);
9626b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
9636b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.chan = conf->def.chan;
9646b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.width = conf->def.width;
9656b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
9666b4cac81SBjoern A. Zeeb 			    conf->def.center_freq1;
9676b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
9686b4cac81SBjoern A. Zeeb 			    conf->def.center_freq2;
9696b4cac81SBjoern A. Zeeb 		} else {
9706b4cac81SBjoern A. Zeeb 			goto out;
9716b4cac81SBjoern A. Zeeb 		}
9726b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
9736b4cac81SBjoern A. Zeeb 		if (error == 0)
9746b4cac81SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, conf);
9756b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
9766b4cac81SBjoern A. Zeeb 			error = 0;
9776b4cac81SBjoern A. Zeeb 		if (error != 0) {
9786b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, conf);
9796b4cac81SBjoern A. Zeeb 			free(conf, M_LKPI80211);
9806b4cac81SBjoern A. Zeeb 			goto out;
9816b4cac81SBjoern A. Zeeb 		}
9826b4cac81SBjoern A. Zeeb 	}
9836b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
9846b4cac81SBjoern A. Zeeb 
9856b4cac81SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
9866b4cac81SBjoern A. Zeeb 	bss_changed = 0;
987caaa79c3SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
9886b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
9896b4cac81SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
9906b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
9916b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = false;
9926b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
993c8dafefaSBjoern A. Zeeb 
9946b4cac81SBjoern A. Zeeb 	/* Should almost assert it is this. */
9956b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
9966b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
997fa8f007dSBjoern A. Zeeb 
998fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
999fa8f007dSBjoern A. Zeeb 
10006b4cac81SBjoern A. Zeeb 	/* RATES */
10016b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
10026b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
10036b4cac81SBjoern A. Zeeb 
1004d9f59799SBjoern A. Zeeb 	/*
1005d9f59799SBjoern A. Zeeb 	 * This is a bandaid for now.  If we went through (*iv_update_bss)()
1006d9f59799SBjoern A. Zeeb 	 * and then removed the lsta we end up here without a lsta and have
1007d9f59799SBjoern A. Zeeb 	 * to manually allocate and link it in as lkpi_ic_node_alloc()/init()
1008d9f59799SBjoern A. Zeeb 	 * would normally do.
1009d9f59799SBjoern A. Zeeb 	 * XXX-BZ I do not like this but currently we have no good way of
1010d9f59799SBjoern A. Zeeb 	 * intercepting the bss swap and state changes and packets going out
1011d9f59799SBjoern A. Zeeb 	 * workflow so live with this.  It is a compat layer after all.
1012d9f59799SBjoern A. Zeeb 	 */
1013d9f59799SBjoern A. Zeeb 	if (ni->ni_drv_data == NULL) {
1014d9f59799SBjoern A. Zeeb 		lsta = lkpi_lsta_alloc(vap, ni->ni_macaddr, hw, ni);
1015d9f59799SBjoern A. Zeeb 		if (lsta == NULL) {
1016d9f59799SBjoern A. Zeeb 			error = ENOMEM;
1017d9f59799SBjoern A. Zeeb 			goto out;
1018d9f59799SBjoern A. Zeeb 		}
1019d9f59799SBjoern A. Zeeb 		lsta->ni = ieee80211_ref_node(ni);
1020d9f59799SBjoern A. Zeeb 	} else {
10216b4cac81SBjoern A. Zeeb 		lsta = ni->ni_drv_data;
1022d9f59799SBjoern A. Zeeb 	}
1023e24e8103SBjoern A. Zeeb 
1024e24e8103SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1025e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1026e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1027e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1028e24e8103SBjoern A. Zeeb 
1029d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
10306b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
10316b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
10326b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1033e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
10346b4cac81SBjoern A. Zeeb 	if (error != 0) {
10356b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
10366b4cac81SBjoern A. Zeeb 		goto out;
10376b4cac81SBjoern A. Zeeb 	}
10386b4cac81SBjoern A. Zeeb #if 0
10396b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
10406b4cac81SBjoern A. Zeeb #endif
10416b4cac81SBjoern A. Zeeb 
10429d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
10439d9ba2b7SBjoern A. Zeeb 
10446b4cac81SBjoern A. Zeeb 	/*
10456b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
10466b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
10476b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
10486b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1049d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1050d9f59799SBjoern A. Zeeb 	 * for all queues.
10516b4cac81SBjoern A. Zeeb 	 */
1052e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
10536b4cac81SBjoern A. Zeeb 
10546b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
10556b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
10566b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
10576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
10586b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
10596b4cac81SBjoern A. Zeeb 
10606b4cac81SBjoern A. Zeeb 	/*
10616b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
10626b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
10636b4cac81SBjoern A. Zeeb 	 * - event_callback
10646b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
10656b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
10666b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
10676b4cac81SBjoern A. Zeeb 	 */
10686b4cac81SBjoern A. Zeeb 
10696b4cac81SBjoern A. Zeeb out:
10706b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
10716b4cac81SBjoern A. Zeeb 	if (ni != NULL)
10726b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
10736b4cac81SBjoern A. Zeeb 	return (error);
10746b4cac81SBjoern A. Zeeb }
10756b4cac81SBjoern A. Zeeb 
10766b4cac81SBjoern A. Zeeb static int
10776b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10786b4cac81SBjoern A. Zeeb {
10796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10836b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10846b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10856b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
10866b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10876b4cac81SBjoern A. Zeeb 	int error;
10886b4cac81SBjoern A. Zeeb 
10896b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
10906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10916b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10926b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10936b4cac81SBjoern A. Zeeb 
10946b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
10956b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
10966b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
10976b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
10986b4cac81SBjoern A. Zeeb 
109967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
11006b4cac81SBjoern A. Zeeb 
11016b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11026b4cac81SBjoern A. Zeeb 
1103d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1104d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1105d9f59799SBjoern A. Zeeb 
11066b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
11076b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
11086b4cac81SBjoern A. Zeeb 
11096b4cac81SBjoern A. Zeeb 	/* flush, no drop */
11106b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
11116b4cac81SBjoern A. Zeeb 
11126b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
11136b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
11146b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
11156b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
11166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
11176b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
11186b4cac81SBjoern A. Zeeb 	}
11196b4cac81SBjoern A. Zeeb 
11206b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
11216b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
11226b4cac81SBjoern A. Zeeb 
11236b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
11246b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1125d9f59799SBjoern A. Zeeb 
1126d9f59799SBjoern A. Zeeb 	/* Take the station down. */
11276b4cac81SBjoern A. Zeeb 
11286b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
11296b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
11306b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1131d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1132e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
11336b4cac81SBjoern A. Zeeb 	if (error != 0) {
11346b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
11356b4cac81SBjoern A. Zeeb 		goto out;
11366b4cac81SBjoern A. Zeeb 	}
11376b4cac81SBjoern A. Zeeb #if 0
11386b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
11396b4cac81SBjoern A. Zeeb #endif
11406b4cac81SBjoern A. Zeeb 
114167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
11426b4cac81SBjoern A. Zeeb 
1143d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1144d9f59799SBjoern A. Zeeb 
1145d9f59799SBjoern A. Zeeb 	/* conf_tx */
1146d9f59799SBjoern A. Zeeb 
1147d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
11486b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
11496b4cac81SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
11506b4cac81SBjoern A. Zeeb 
11516b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
11526b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
11536b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
11546b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
11556b4cac81SBjoern A. Zeeb 
11566b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
11576b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
11586b4cac81SBjoern A. Zeeb 		free(conf, M_LKPI80211);
11596b4cac81SBjoern A. Zeeb 	}
11606b4cac81SBjoern A. Zeeb 
11616b4cac81SBjoern A. Zeeb out:
11626b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
11636b4cac81SBjoern A. Zeeb 	if (ni != NULL)
11646b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
11656b4cac81SBjoern A. Zeeb 	return (error);
11666b4cac81SBjoern A. Zeeb }
11676b4cac81SBjoern A. Zeeb 
11686b4cac81SBjoern A. Zeeb static int
11696b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11706b4cac81SBjoern A. Zeeb {
11716b4cac81SBjoern A. Zeeb 	int error;
11726b4cac81SBjoern A. Zeeb 
11736b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
11746b4cac81SBjoern A. Zeeb 	if (error == 0)
11756b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
11766b4cac81SBjoern A. Zeeb 	return (error);
11776b4cac81SBjoern A. Zeeb }
11786b4cac81SBjoern A. Zeeb 
11796b4cac81SBjoern A. Zeeb static int
11806b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11816b4cac81SBjoern A. Zeeb {
11826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11846b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
11856b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11866b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11876b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11886b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
11896b4cac81SBjoern A. Zeeb 	int error;
11906b4cac81SBjoern A. Zeeb 
11916b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11926b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11936b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11946b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11956b4cac81SBjoern A. Zeeb 
11966b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11976b4cac81SBjoern A. Zeeb 	ni = NULL;
11986b4cac81SBjoern A. Zeeb 
11996b4cac81SBjoern A. Zeeb 	/* Finish auth. */
12006b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
12016b4cac81SBjoern A. Zeeb 
12026b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
12036b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
12046b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
12056b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12066b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
12076b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1208e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
12096b4cac81SBjoern A. Zeeb 	if (error != 0)
12106b4cac81SBjoern A. Zeeb 		goto out;
12116b4cac81SBjoern A. Zeeb 
12126b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
12136b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
12146b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12156b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
12166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
12176b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
12186b4cac81SBjoern A. Zeeb 	}
12196b4cac81SBjoern A. Zeeb 
12206b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
12216b4cac81SBjoern A. Zeeb 
12226b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
12236b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
12246b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12256b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
12266b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
12276b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
12286b4cac81SBjoern A. Zeeb 	}
12296b4cac81SBjoern A. Zeeb 
12306b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
1231e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), true, true);
12326b4cac81SBjoern A. Zeeb 
12336b4cac81SBjoern A. Zeeb 	/*
12346b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
12356b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
12366b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
12376b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
12386b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
12396b4cac81SBjoern A. Zeeb 	 * - event_callback
12406b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
12416b4cac81SBjoern A. Zeeb 	 */
12426b4cac81SBjoern A. Zeeb 
12436b4cac81SBjoern A. Zeeb out:
12446b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
12456b4cac81SBjoern A. Zeeb 	if (ni != NULL)
12466b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
12476b4cac81SBjoern A. Zeeb 	return (error);
12486b4cac81SBjoern A. Zeeb }
12496b4cac81SBjoern A. Zeeb 
12506b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
12516b4cac81SBjoern A. Zeeb static int
12526b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
12536b4cac81SBjoern A. Zeeb {
12546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12556b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12566b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
12576b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12586b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12596b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12606b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
12616b4cac81SBjoern A. Zeeb 
12626b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
12636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
12646b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
12656b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
12666b4cac81SBjoern A. Zeeb 
12676b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
12686b4cac81SBjoern A. Zeeb 
12696b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
12706b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
12716b4cac81SBjoern A. Zeeb 
12726b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
12736b4cac81SBjoern A. Zeeb 
12746b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
12756b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
12766b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12776b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
12786b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
12796b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
12806b4cac81SBjoern A. Zeeb 	}
12816b4cac81SBjoern A. Zeeb 
12826b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
12836b4cac81SBjoern A. Zeeb 
12846b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
12856b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
12866b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12876b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
12886b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
12896b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
12906b4cac81SBjoern A. Zeeb 	}
12916b4cac81SBjoern A. Zeeb 
12926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
12936b4cac81SBjoern A. Zeeb 	if (ni != NULL)
12946b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
12956b4cac81SBjoern A. Zeeb 
12966b4cac81SBjoern A. Zeeb 	return (0);
12976b4cac81SBjoern A. Zeeb }
12986b4cac81SBjoern A. Zeeb 
12996b4cac81SBjoern A. Zeeb static int
1300d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13016b4cac81SBjoern A. Zeeb {
13026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13086b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13096b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1310d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
13116b4cac81SBjoern A. Zeeb 	int error;
13126b4cac81SBjoern A. Zeeb 
13136b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13156b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13166b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13176b4cac81SBjoern A. Zeeb 
13186b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
13196b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
13206b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
13216b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13226b4cac81SBjoern A. Zeeb 
132367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1324d9f59799SBjoern A. Zeeb 
1325d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1326d9f59799SBjoern A. Zeeb 
1327d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1328d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1329d9f59799SBjoern A. Zeeb 
1330d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1331d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1332d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1333d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1334d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1335d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1336d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1337d9f59799SBjoern A. Zeeb 	}
1338d9f59799SBjoern A. Zeeb 
1339d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1340d9f59799SBjoern A. Zeeb 
1341d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1342d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1343d9f59799SBjoern A. Zeeb 	if (error != 0)
1344d9f59799SBjoern A. Zeeb 		goto outni;
1345d9f59799SBjoern A. Zeeb 
1346d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1347d9f59799SBjoern A. Zeeb 
134867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1349d9f59799SBjoern A. Zeeb 
1350d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1351d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1352d9f59799SBjoern A. Zeeb 
1353d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1354d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1355d9f59799SBjoern A. Zeeb 
13566b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
13576b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
13586b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13596b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
13606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
13616b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
13626b4cac81SBjoern A. Zeeb 	}
13636b4cac81SBjoern A. Zeeb 
1364d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1365d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1366d9f59799SBjoern A. Zeeb 
1367d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1368d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1369d9f59799SBjoern A. Zeeb 
1370d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1371d9f59799SBjoern A. Zeeb 
13726b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
13736b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13746b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
13756b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1376e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
13776b4cac81SBjoern A. Zeeb 	if (error != 0)
13786b4cac81SBjoern A. Zeeb 		goto out;
13796b4cac81SBjoern A. Zeeb 
138067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13816b4cac81SBjoern A. Zeeb 
1382d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1383d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1384d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1385d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1386e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1387d9f59799SBjoern A. Zeeb 	if (error != 0) {
1388d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1389d9f59799SBjoern A. Zeeb 		goto out;
1390d9f59799SBjoern A. Zeeb 	}
1391d9f59799SBjoern A. Zeeb #if 0
1392d9f59799SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
1393d9f59799SBjoern A. Zeeb #endif
1394d9f59799SBjoern A. Zeeb 
139567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1396d9f59799SBjoern A. Zeeb 
1397d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1398d9f59799SBjoern A. Zeeb 	/* We need to do this now, can only do after sta is IEEE80211_STA_NOTEXIST. */
1399d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1400d9f59799SBjoern A. Zeeb 
1401d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1402d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1403d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1404d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1405d9f59799SBjoern A. Zeeb 	vif->bss_conf.ssid_len = 0;
1406d9f59799SBjoern A. Zeeb 	memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1407d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1408d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1409d9f59799SBjoern A. Zeeb 
1410d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1411d9f59799SBjoern A. Zeeb 
1412d9f59799SBjoern A. Zeeb 	/* conf_tx */
1413d9f59799SBjoern A. Zeeb 
1414d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1415d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1416d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1417d9f59799SBjoern A. Zeeb 
1418d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1419d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
1420d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1421d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1422d9f59799SBjoern A. Zeeb 
1423d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1424d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1425d9f59799SBjoern A. Zeeb 		free(conf, M_LKPI80211);
1426d9f59799SBjoern A. Zeeb 	}
1427d9f59799SBjoern A. Zeeb 
1428d9f59799SBjoern A. Zeeb 	error = EALREADY;
14296b4cac81SBjoern A. Zeeb out:
14306b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1431d9f59799SBjoern A. Zeeb outni:
14326b4cac81SBjoern A. Zeeb 	if (ni != NULL)
14336b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
14346b4cac81SBjoern A. Zeeb 	return (error);
14356b4cac81SBjoern A. Zeeb }
14366b4cac81SBjoern A. Zeeb 
14376b4cac81SBjoern A. Zeeb static int
1438d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1439d9f59799SBjoern A. Zeeb {
1440d9f59799SBjoern A. Zeeb 	int error;
1441d9f59799SBjoern A. Zeeb 
1442d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1443d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1444d9f59799SBjoern A. Zeeb 		return (error);
1445d9f59799SBjoern A. Zeeb 
1446d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1447d9f59799SBjoern A. Zeeb 
1448d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1449d9f59799SBjoern A. Zeeb 	return (error);
1450d9f59799SBjoern A. Zeeb }
1451d9f59799SBjoern A. Zeeb 
1452d9f59799SBjoern A. Zeeb static int
14536b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14546b4cac81SBjoern A. Zeeb {
14556b4cac81SBjoern A. Zeeb 	int error;
14566b4cac81SBjoern A. Zeeb 
1457d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
14586b4cac81SBjoern A. Zeeb 	return (error);
14596b4cac81SBjoern A. Zeeb }
14606b4cac81SBjoern A. Zeeb 
14616b4cac81SBjoern A. Zeeb static int
14626b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14636b4cac81SBjoern A. Zeeb {
14646b4cac81SBjoern A. Zeeb 	int error;
14656b4cac81SBjoern A. Zeeb 
1466d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
14676b4cac81SBjoern A. Zeeb 	return (error);
14686b4cac81SBjoern A. Zeeb }
14696b4cac81SBjoern A. Zeeb 
14706b4cac81SBjoern A. Zeeb static int
14716b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14726b4cac81SBjoern A. Zeeb {
14736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14746b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14756b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14766b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14776b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
14786b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14796b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
14806b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14816b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
14826b4cac81SBjoern A. Zeeb 	int error;
14836b4cac81SBjoern A. Zeeb 
14846b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14866b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14876b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14886b4cac81SBjoern A. Zeeb 
14896b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14909597f7cbSBjoern A. Zeeb 	ni = NULL;
14916b4cac81SBjoern A. Zeeb 
14926b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
14936b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
14946b4cac81SBjoern A. Zeeb 
14959597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
14969597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
14976b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
14986b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
14996b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15009597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
15019597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
15026b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
15039597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
15044a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
15054a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
15064a67f1dfSBjoern A. Zeeb 		sta->wme = true;
15074a67f1dfSBjoern A. Zeeb #endif
1508e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
15099597f7cbSBjoern A. Zeeb 	if (error != 0)
15109597f7cbSBjoern A. Zeeb 		goto out;
15116b4cac81SBjoern A. Zeeb 
15126b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
15136b4cac81SBjoern A. Zeeb 
15146b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
15156b4cac81SBjoern A. Zeeb 	bss_changed = 0;
15164a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
15174a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
15184a67f1dfSBjoern A. Zeeb #endif
15196b4cac81SBjoern A. Zeeb 	if (!vif->bss_conf.assoc || vif->bss_conf.aid != IEEE80211_NODE_AID(ni)) {
15206b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = true;
15216b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = IEEE80211_NODE_AID(ni);
15226b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
15236b4cac81SBjoern A. Zeeb 	}
15246b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
15256b4cac81SBjoern A. Zeeb 	vif->bss_conf.ssid_len = ni->ni_esslen;
15266b4cac81SBjoern A. Zeeb 	memcpy(vif->bss_conf.ssid, ni->ni_essid, ni->ni_esslen);
15276b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
15286b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
15296b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
15306b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
15316b4cac81SBjoern A. Zeeb 	}
15326b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
15336b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
15346b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
15356b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
15366b4cac81SBjoern A. Zeeb 	}
15376b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
15386b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
15396b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
15406b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
15416b4cac81SBjoern A. Zeeb 	}
1542fa8f007dSBjoern A. Zeeb 
1543fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1544fa8f007dSBjoern A. Zeeb 
15456b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
15466b4cac81SBjoern A. Zeeb 
15476b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
15486b4cac81SBjoern A. Zeeb 	 * - event_callback
15496b4cac81SBjoern A. Zeeb 	 */
15506b4cac81SBjoern A. Zeeb 
15516b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
15526b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
15536b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15546b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
15556b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
15566b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
15576b4cac81SBjoern A. Zeeb 	}
15586b4cac81SBjoern A. Zeeb 
1559086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1560086be6a8SBjoern A. Zeeb 
15616b4cac81SBjoern A. Zeeb 	/*
15626b4cac81SBjoern A. Zeeb 	 * And then:
15636b4cac81SBjoern A. Zeeb 	 * - (more packets)?
15646b4cac81SBjoern A. Zeeb 	 * - set_key
15656b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
15666b4cac81SBjoern A. Zeeb 	 * - set_key (?)
15676b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
15686b4cac81SBjoern A. Zeeb 	 */
15696b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
15706b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
15716b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
15726b4cac81SBjoern A. Zeeb 
15736b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
15746b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
15756b4cac81SBjoern A. Zeeb 	}
15766b4cac81SBjoern A. Zeeb 
15776b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
15786b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15796b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
15806b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1581e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
15826b4cac81SBjoern A. Zeeb 	if (error != 0) {
15836b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
15846b4cac81SBjoern A. Zeeb 		goto out;
15856b4cac81SBjoern A. Zeeb 	}
15866b4cac81SBjoern A. Zeeb 
15876b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
15886b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
15896b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
15906b4cac81SBjoern A. Zeeb 	 *
15916b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
15926b4cac81SBjoern A. Zeeb 	 */
15936b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
15946b4cac81SBjoern A. Zeeb 
1595fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1596fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1597fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1598fa8f007dSBjoern A. Zeeb 
15996b4cac81SBjoern A. Zeeb out:
16006b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16016b4cac81SBjoern A. Zeeb 	if (ni != NULL)
16026b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
16036b4cac81SBjoern A. Zeeb 	return (error);
16046b4cac81SBjoern A. Zeeb }
16056b4cac81SBjoern A. Zeeb 
16066b4cac81SBjoern A. Zeeb static int
16076b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16086b4cac81SBjoern A. Zeeb {
16096b4cac81SBjoern A. Zeeb 	int error;
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
16126b4cac81SBjoern A. Zeeb 	if (error == 0)
16136b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
16146b4cac81SBjoern A. Zeeb 	return (error);
16156b4cac81SBjoern A. Zeeb }
16166b4cac81SBjoern A. Zeeb 
16176b4cac81SBjoern A. Zeeb static int
16186b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16196b4cac81SBjoern A. Zeeb {
16206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16246b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16256b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
1627d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1628d9f59799SBjoern A. Zeeb #if 0
1629d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1630d9f59799SBjoern A. Zeeb #endif
16316b4cac81SBjoern A. Zeeb 	int error;
16326b4cac81SBjoern A. Zeeb 
16336b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16356b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16366b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16376b4cac81SBjoern A. Zeeb 
16386b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
16396b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
16406b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
16416b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
16426b4cac81SBjoern A. Zeeb 
164367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1644d9f59799SBjoern A. Zeeb 
1645d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1646d9f59799SBjoern A. Zeeb 
1647d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1648d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1649d9f59799SBjoern A. Zeeb 
1650d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1651d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1652d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1653d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1654d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1655d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1656d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1657d9f59799SBjoern A. Zeeb 	}
1658d9f59799SBjoern A. Zeeb 
1659d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1660d9f59799SBjoern A. Zeeb 
1661d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1662d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1663d9f59799SBjoern A. Zeeb 	if (error != 0)
1664d9f59799SBjoern A. Zeeb 		goto outni;
1665d9f59799SBjoern A. Zeeb 
1666d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1667d9f59799SBjoern A. Zeeb 
166867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1669d9f59799SBjoern A. Zeeb 
1670d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1671d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1672d9f59799SBjoern A. Zeeb 
1673d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1674d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1675d9f59799SBjoern A. Zeeb 
1676d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
1677d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
1678d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1679d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
1680d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1681d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
1682d9f59799SBjoern A. Zeeb 	}
1683d9f59799SBjoern A. Zeeb 
1684d9f59799SBjoern A. Zeeb #if 0
1685d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1686d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1687d9f59799SBjoern A. Zeeb 
1688d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1689d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1690d9f59799SBjoern A. Zeeb #endif
1691d9f59799SBjoern A. Zeeb 
1692d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1693d9f59799SBjoern A. Zeeb 
16946b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
16956b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
16966b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
16976b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1698e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
16996b4cac81SBjoern A. Zeeb 	if (error != 0)
17006b4cac81SBjoern A. Zeeb 		goto out;
17016b4cac81SBjoern A. Zeeb 
170267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
17036b4cac81SBjoern A. Zeeb 
17046b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
17056b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
17066b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
17076b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1708e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
17096b4cac81SBjoern A. Zeeb 	if (error != 0)
17106b4cac81SBjoern A. Zeeb 		goto out;
17116b4cac81SBjoern A. Zeeb 
171267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
17136b4cac81SBjoern A. Zeeb 
1714d9f59799SBjoern A. Zeeb #if 0
1715d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1716d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1717d9f59799SBjoern A. Zeeb #endif
1718d9f59799SBjoern A. Zeeb 
1719d9f59799SBjoern A. Zeeb 	error = EALREADY;
17206b4cac81SBjoern A. Zeeb out:
17216b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1722d9f59799SBjoern A. Zeeb outni:
17236b4cac81SBjoern A. Zeeb 	if (ni != NULL)
17246b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
17256b4cac81SBjoern A. Zeeb 	return (error);
17266b4cac81SBjoern A. Zeeb }
17276b4cac81SBjoern A. Zeeb 
17286b4cac81SBjoern A. Zeeb static int
1729d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1730d9f59799SBjoern A. Zeeb {
1731d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1732d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
1733d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
1734d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
1735d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
1736d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
1737d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
1738d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1739d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1740d9f59799SBjoern A. Zeeb 	int error;
1741d9f59799SBjoern A. Zeeb 
1742d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
1743d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1744d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1745d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
1746d9f59799SBjoern A. Zeeb 
1747d9f59799SBjoern A. Zeeb 	/* Keep ni around. */
1748d9f59799SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
1749d9f59799SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1750d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
1751d9f59799SBjoern A. Zeeb 
175267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1753d9f59799SBjoern A. Zeeb 
1754d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1755d9f59799SBjoern A. Zeeb 
1756d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1757d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1758d9f59799SBjoern A. Zeeb 
1759d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1760d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1761d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1762d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1763d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1764d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1765d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1766d9f59799SBjoern A. Zeeb 	}
1767d9f59799SBjoern A. Zeeb 
1768d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1769d9f59799SBjoern A. Zeeb 
1770d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1771d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1772d9f59799SBjoern A. Zeeb 	if (error != 0)
1773d9f59799SBjoern A. Zeeb 		goto outni;
1774d9f59799SBjoern A. Zeeb 
1775d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1776d9f59799SBjoern A. Zeeb 
177767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1778d9f59799SBjoern A. Zeeb 
1779d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1780d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1781d9f59799SBjoern A. Zeeb 
1782d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1783d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1784d9f59799SBjoern A. Zeeb 
1785d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
1786d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
1787d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1788d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
1789d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1790d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
1791d9f59799SBjoern A. Zeeb 	}
1792d9f59799SBjoern A. Zeeb 
1793d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1794d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1795d9f59799SBjoern A. Zeeb 
1796d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1797d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1798d9f59799SBjoern A. Zeeb 
1799d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1800d9f59799SBjoern A. Zeeb 
1801d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1802d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1803d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1804d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1805e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1806d9f59799SBjoern A. Zeeb 	if (error != 0)
1807d9f59799SBjoern A. Zeeb 		goto out;
1808d9f59799SBjoern A. Zeeb 
180967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1810d9f59799SBjoern A. Zeeb 
1811d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
1812d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1813d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1814d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1815e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1816d9f59799SBjoern A. Zeeb 	if (error != 0)
1817d9f59799SBjoern A. Zeeb 		goto out;
1818d9f59799SBjoern A. Zeeb 
181967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1820d9f59799SBjoern A. Zeeb 
1821d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
1822d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1823d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1824d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1825e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1826d9f59799SBjoern A. Zeeb 	if (error != 0)
1827d9f59799SBjoern A. Zeeb 		goto out;
1828d9f59799SBjoern A. Zeeb 
182967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1830d9f59799SBjoern A. Zeeb 
1831d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1832d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1833d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1834d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1835e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1836d9f59799SBjoern A. Zeeb 	if (error != 0) {
1837d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1838d9f59799SBjoern A. Zeeb 		goto out;
1839d9f59799SBjoern A. Zeeb 	}
1840d9f59799SBjoern A. Zeeb #if 0
1841d9f59799SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
1842d9f59799SBjoern A. Zeeb #endif
1843d9f59799SBjoern A. Zeeb 
184467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1845d9f59799SBjoern A. Zeeb 
1846d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1847d9f59799SBjoern A. Zeeb 	/*
1848d9f59799SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
1849d9f59799SBjoern A. Zeeb 	 * See comment there; removes the sta from fw.
1850d9f59799SBjoern A. Zeeb 	 */
1851d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1852d9f59799SBjoern A. Zeeb 
1853d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1854d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1855d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1856d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1857d9f59799SBjoern A. Zeeb 	vif->bss_conf.ssid_len = 0;
1858d9f59799SBjoern A. Zeeb 	memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1859d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1860d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1861d9f59799SBjoern A. Zeeb 
1862d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1863d9f59799SBjoern A. Zeeb 
1864d9f59799SBjoern A. Zeeb 	/* conf_tx */
1865d9f59799SBjoern A. Zeeb 
1866d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1867d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1868d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1869d9f59799SBjoern A. Zeeb 
1870d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1871d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
1872d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1873d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1874d9f59799SBjoern A. Zeeb 
1875d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1876d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1877d9f59799SBjoern A. Zeeb 		free(conf, M_LKPI80211);
1878d9f59799SBjoern A. Zeeb 	}
1879d9f59799SBjoern A. Zeeb 
1880d9f59799SBjoern A. Zeeb 	error = EALREADY;
1881d9f59799SBjoern A. Zeeb out:
1882d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1883d9f59799SBjoern A. Zeeb outni:
1884d9f59799SBjoern A. Zeeb 	if (ni != NULL)
1885d9f59799SBjoern A. Zeeb 		ieee80211_free_node(ni);
1886d9f59799SBjoern A. Zeeb 	return (error);
1887d9f59799SBjoern A. Zeeb }
1888d9f59799SBjoern A. Zeeb 
1889d9f59799SBjoern A. Zeeb static int
1890d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1891d9f59799SBjoern A. Zeeb {
1892d9f59799SBjoern A. Zeeb 
1893d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
1894d9f59799SBjoern A. Zeeb }
1895d9f59799SBjoern A. Zeeb 
1896d9f59799SBjoern A. Zeeb static int
18976b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18986b4cac81SBjoern A. Zeeb {
18996b4cac81SBjoern A. Zeeb 	int error;
19006b4cac81SBjoern A. Zeeb 
1901d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
1902d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1903d9f59799SBjoern A. Zeeb 		return (error);
1904d9f59799SBjoern A. Zeeb 
1905d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1906d9f59799SBjoern A. Zeeb 
1907d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
19086b4cac81SBjoern A. Zeeb 	return (error);
19096b4cac81SBjoern A. Zeeb }
19106b4cac81SBjoern A. Zeeb 
1911d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
19126b4cac81SBjoern A. Zeeb 
19136b4cac81SBjoern A. Zeeb /*
19146b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
19156b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
19166b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
19176b4cac81SBjoern A. Zeeb  */
19186b4cac81SBjoern A. Zeeb struct fsm_state {
19196b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
19206b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
19216b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
19226b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
19236b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
19246b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
19256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
19266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
1927d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
1928d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
19296b4cac81SBjoern A. Zeeb 
19306b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
19316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
19326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
19336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
1934d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
19356b4cac81SBjoern A. Zeeb 
1936d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1937d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1938d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
1939d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
1940d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
19416b4cac81SBjoern A. Zeeb 
1942d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
1943d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
1944d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
19456b4cac81SBjoern A. Zeeb 
19466b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
19476b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
19486b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
19496b4cac81SBjoern A. Zeeb 
19506b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
19516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
19526b4cac81SBjoern A. Zeeb };
19536b4cac81SBjoern A. Zeeb 
19546b4cac81SBjoern A. Zeeb static int
19556b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19566b4cac81SBjoern A. Zeeb {
19576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
19586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19606b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19616b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
19626b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
19636b4cac81SBjoern A. Zeeb 	int error;
19646b4cac81SBjoern A. Zeeb 
19656b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
19666b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
19676b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
19686b4cac81SBjoern A. Zeeb 
19699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
19716b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
19726b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
19739d9ba2b7SBjoern A. Zeeb #endif
19746b4cac81SBjoern A. Zeeb 
19756b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
19766b4cac81SBjoern A. Zeeb 
19776b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
19786b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
19796b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
19806b4cac81SBjoern A. Zeeb 
19816b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
19826b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
19836b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
19846b4cac81SBjoern A. Zeeb 
19856b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
19866b4cac81SBjoern A. Zeeb 
19876b4cac81SBjoern A. Zeeb 	} else {
19886b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
19896b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
19906b4cac81SBjoern A. Zeeb 		return (ENOSYS);
19916b4cac81SBjoern A. Zeeb 	}
19926b4cac81SBjoern A. Zeeb 
19936b4cac81SBjoern A. Zeeb 	error = 0;
19946b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
19956b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
19969d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19979d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
1998d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
1999d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2000d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2001d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
20029d9ba2b7SBjoern A. Zeeb #endif
20036b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
20046b4cac81SBjoern A. Zeeb 			break;
20056b4cac81SBjoern A. Zeeb 		}
20066b4cac81SBjoern A. Zeeb 	}
20076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
20086b4cac81SBjoern A. Zeeb 
20096b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2010d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
20116b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
20126b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
20136b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
20146b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
20156b4cac81SBjoern A. Zeeb 		return (ENOSYS);
20166b4cac81SBjoern A. Zeeb 	}
20176b4cac81SBjoern A. Zeeb 
20186b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
20199d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20209d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2021d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2022d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2023d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2024d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
20259d9ba2b7SBjoern A. Zeeb #endif
20266b4cac81SBjoern A. Zeeb 		return (0);
20276b4cac81SBjoern A. Zeeb 	}
20286b4cac81SBjoern A. Zeeb 
20296b4cac81SBjoern A. Zeeb 	if (error != 0) {
20306b4cac81SBjoern A. Zeeb 		/* XXX-BZ currently expected so ignore. */
20316b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
20326b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
20336b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
20346b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
20356b4cac81SBjoern A. Zeeb 		/* return (error); */
20366b4cac81SBjoern A. Zeeb 	}
20376b4cac81SBjoern A. Zeeb 
20389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20399d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2040d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2041d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
20426b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
20439d9ba2b7SBjoern A. Zeeb #endif
20446b4cac81SBjoern A. Zeeb 
20456b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
20466b4cac81SBjoern A. Zeeb }
20476b4cac81SBjoern A. Zeeb 
20486b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
20496b4cac81SBjoern A. Zeeb 
2050d9f59799SBjoern A. Zeeb /*
2051d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2052d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2053d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2054d9f59799SBjoern A. Zeeb  */
2055d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2056d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2057d9f59799SBjoern A. Zeeb {
2058d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2059d9f59799SBjoern A. Zeeb 	struct ieee80211_node *obss;
2060d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2061ed3ef56bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
2062d9f59799SBjoern A. Zeeb 
2063d9f59799SBjoern A. Zeeb 	obss = vap->iv_bss;
2064d9f59799SBjoern A. Zeeb 
20659d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20669d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
20679d9ba2b7SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
20689d9ba2b7SBjoern A. Zeeb 		    "ni %p ni_drv_data %p\n", __func__,
20699d9ba2b7SBjoern A. Zeeb 		    obss, (obss != NULL) ? obss->ni_drv_data : NULL,
20709d9ba2b7SBjoern A. Zeeb 		    ni, (ni != NULL) ? ni->ni_drv_data : NULL);
20719d9ba2b7SBjoern A. Zeeb #endif
20729d9ba2b7SBjoern A. Zeeb 
2073d9f59799SBjoern A. Zeeb 	/* Nothing to copy from.  Just return. */
2074d9f59799SBjoern A. Zeeb 	if (obss == NULL || obss->ni_drv_data == NULL)
2075d9f59799SBjoern A. Zeeb 		goto out;
2076d9f59799SBjoern A. Zeeb 
2077d9f59799SBjoern A. Zeeb 	/* Nothing to copy to.  Just return. */
2078d9f59799SBjoern A. Zeeb 	IMPROVE("clearing the obss might still be needed?");
2079d9f59799SBjoern A. Zeeb 	if (ni == NULL)
2080d9f59799SBjoern A. Zeeb 		goto out;
2081d9f59799SBjoern A. Zeeb 
2082d9f59799SBjoern A. Zeeb 	/* Nothing changed? panic? */
2083d9f59799SBjoern A. Zeeb 	if (obss == ni)
2084d9f59799SBjoern A. Zeeb 		goto out;
2085d9f59799SBjoern A. Zeeb 
2086d9f59799SBjoern A. Zeeb 	lsta = obss->ni_drv_data;
2087d9f59799SBjoern A. Zeeb 	obss->ni_drv_data = ni->ni_drv_data;
2088d9f59799SBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2089ed3ef56bSBjoern A. Zeeb 	if (lsta != NULL) {
2090d9f59799SBjoern A. Zeeb 		lsta->ni = ni;
2091ed3ef56bSBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
2092ed3ef56bSBjoern A. Zeeb 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2093ed3ef56bSBjoern A. Zeeb 	}
2094d9f59799SBjoern A. Zeeb 	lsta = obss->ni_drv_data;
2095ed3ef56bSBjoern A. Zeeb 	if (lsta != NULL) {
2096d9f59799SBjoern A. Zeeb 		lsta->ni = obss;
2097ed3ef56bSBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
2098ed3ef56bSBjoern A. Zeeb 		IEEE80211_ADDR_COPY(sta->addr, lsta->ni->ni_macaddr);
2099ed3ef56bSBjoern A. Zeeb 	}
2100d9f59799SBjoern A. Zeeb 
2101d9f59799SBjoern A. Zeeb out:
2102ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2103d9f59799SBjoern A. Zeeb 	return (lvif->iv_update_bss(vap, ni));
2104d9f59799SBjoern A. Zeeb }
2105d9f59799SBjoern A. Zeeb 
21064a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
21076b4cac81SBjoern A. Zeeb static int
21084a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
21096b4cac81SBjoern A. Zeeb {
21104a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
21116b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21126b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21136b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21146b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
21156b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
21166b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
21176b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
21186b4cac81SBjoern A. Zeeb 	int error;
21196b4cac81SBjoern A. Zeeb 	uint16_t ac;
21206b4cac81SBjoern A. Zeeb 
21216b4cac81SBjoern A. Zeeb 	IMPROVE();
21226b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
21236b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
21246b4cac81SBjoern A. Zeeb 
21256b4cac81SBjoern A. Zeeb 	if (vap == NULL)
21266b4cac81SBjoern A. Zeeb 		return (0);
21276b4cac81SBjoern A. Zeeb 
21284a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
21294a67f1dfSBjoern A. Zeeb 		return (0);
21304a67f1dfSBjoern A. Zeeb 
21316b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
21326b4cac81SBjoern A. Zeeb 		return (0);
21336b4cac81SBjoern A. Zeeb 
21344a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
21354a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
21364a67f1dfSBjoern A. Zeeb 		return (0);
21374a67f1dfSBjoern A. Zeeb 	}
21384a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
21396b4cac81SBjoern A. Zeeb 
21404a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
21416b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
21426b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
21436b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
21446b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
21456b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
21466b4cac81SBjoern A. Zeeb 
21474a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21484a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21494a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21504a67f1dfSBjoern A. Zeeb 
21516b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
21526b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21536b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
21546b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
21556b4cac81SBjoern A. Zeeb 
21566b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
21576b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
21586b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
21596b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
21606b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
21616b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
21626b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
21636b4cac81SBjoern A. Zeeb 		if (error != 0)
21646b4cac81SBjoern A. Zeeb 			printf("%s: conf_tx ac %u failed %d\n",
21656b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
21666b4cac81SBjoern A. Zeeb 	}
21676b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21686b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
21694a67f1dfSBjoern A. Zeeb 	if (!planned)
21706b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
21714a67f1dfSBjoern A. Zeeb 
21724a67f1dfSBjoern A. Zeeb 	return (changed);
21734a67f1dfSBjoern A. Zeeb }
21746b4cac81SBjoern A. Zeeb #endif
21756b4cac81SBjoern A. Zeeb 
21764a67f1dfSBjoern A. Zeeb static int
21774a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
21784a67f1dfSBjoern A. Zeeb {
21794a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
21804a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
21814a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
21824a67f1dfSBjoern A. Zeeb 
21834a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
21844a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
21854a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
21866b4cac81SBjoern A. Zeeb 		return (0);
21874a67f1dfSBjoern A. Zeeb 
21884a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
21894a67f1dfSBjoern A. Zeeb 
21904a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
21914a67f1dfSBjoern A. Zeeb #endif
21924a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
21936b4cac81SBjoern A. Zeeb }
21946b4cac81SBjoern A. Zeeb 
21956b4cac81SBjoern A. Zeeb static struct ieee80211vap *
21966b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
21976b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
21986b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
21996b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
22006b4cac81SBjoern A. Zeeb {
22016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22026b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22046b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
22056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22066b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
22076b4cac81SBjoern A. Zeeb 	size_t len;
2208e3a0b120SBjoern A. Zeeb 	int error, i;
22096b4cac81SBjoern A. Zeeb 
22106b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
22116b4cac81SBjoern A. Zeeb 		return (NULL);
22126b4cac81SBjoern A. Zeeb 
22136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
22146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22156b4cac81SBjoern A. Zeeb 
22166b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
22176b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
22186b4cac81SBjoern A. Zeeb 
22196b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
22206b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
22216b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
22226b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
22236b4cac81SBjoern A. Zeeb 
22246b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22256b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
22266b4cac81SBjoern A. Zeeb 	vif->p2p = false;
22276b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
22286b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
22296b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
22306b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
22316b4cac81SBjoern A. Zeeb 	IMPROVE();
22326b4cac81SBjoern A. Zeeb 
22336b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
22346b4cac81SBjoern A. Zeeb #if 1
22356b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
22366b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = true;
22376b4cac81SBjoern A. Zeeb 	vif->bss_conf.ps = false;
22386b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
22396b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
22406b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
22416b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
22426b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
22436b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
22446b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
22456b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
2246caaa79c3SBjoern A. Zeeb 	/*
2247caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2248caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2249caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2250caaa79c3SBjoern A. Zeeb 	 * NB: the logic there with using an array as bssid_override and checking
2251caaa79c3SBjoern A. Zeeb 	 * for non-NULL later is flawed but in their workflow does not seem to
2252caaa79c3SBjoern A. Zeeb 	 * matter.
2253caaa79c3SBjoern A. Zeeb 	 */
2254caaa79c3SBjoern A. Zeeb 	vif->bss_conf.bssid = zerobssid;
22556b4cac81SBjoern A. Zeeb #endif
22566b4cac81SBjoern A. Zeeb #if 0
22576b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
22586b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
22596b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
22606b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
22616b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
22626b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
22636b4cac81SBjoern A. Zeeb #endif
2264e3a0b120SBjoern A. Zeeb 
2265e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2266e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2267e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2268e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2269e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2270e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2271e3a0b120SBjoern A. Zeeb 		else
2272e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
2273e3a0b120SBjoern A. Zeeb 	}
2274e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2275e3a0b120SBjoern A. Zeeb 
22766b4cac81SBjoern A. Zeeb 	IMPROVE();
22776b4cac81SBjoern A. Zeeb 
22786b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
22796b4cac81SBjoern A. Zeeb 	if (error != 0) {
22806b4cac81SBjoern A. Zeeb 		printf("%s: failed to start hw: %d\n", __func__, error);
22816b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
22826b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
22836b4cac81SBjoern A. Zeeb 		return (NULL);
22846b4cac81SBjoern A. Zeeb 	}
22856b4cac81SBjoern A. Zeeb 
22866b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
22876b4cac81SBjoern A. Zeeb 	if (error != 0) {
22886b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
22896b4cac81SBjoern A. Zeeb 		printf("%s: failed to add interface: %d\n", __func__, error);
22906b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
22916b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
22926b4cac81SBjoern A. Zeeb 		return (NULL);
22936b4cac81SBjoern A. Zeeb 	}
22946b4cac81SBjoern A. Zeeb 
22958891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
22966b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
22978891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
22986b4cac81SBjoern A. Zeeb 
22996b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
23006b4cac81SBjoern A. Zeeb 	changed = 0;
23016b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
23026b4cac81SBjoern A. Zeeb 
23036b4cac81SBjoern A. Zeeb 	/* conf_tx setup; default WME? */
23046b4cac81SBjoern A. Zeeb 
23056b4cac81SBjoern A. Zeeb 	/* Force MC init. */
23066b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
23076b4cac81SBjoern A. Zeeb 
23086b4cac81SBjoern A. Zeeb 	IMPROVE();
23096b4cac81SBjoern A. Zeeb 
23106b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
23116b4cac81SBjoern A. Zeeb 
23126b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
23136b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
23146b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2315d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2316d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
23176b4cac81SBjoern A. Zeeb 
23186b4cac81SBjoern A. Zeeb 	/* Key management. */
23196b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2320b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
23216b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
23226b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
23236b4cac81SBjoern A. Zeeb #endif
23246b4cac81SBjoern A. Zeeb 	}
23256b4cac81SBjoern A. Zeeb 
23266b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
23276b4cac81SBjoern A. Zeeb 
23286b4cac81SBjoern A. Zeeb 	/* Complete setup. */
23296b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
23306b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
23316b4cac81SBjoern A. Zeeb 
23326b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
23336b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
23346b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
23356b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
23366b4cac81SBjoern A. Zeeb 
23376b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
23386b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
23396b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
23406b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
23416b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
23426b4cac81SBjoern A. Zeeb 	/* any others? */
23436b4cac81SBjoern A. Zeeb 	IMPROVE();
23446b4cac81SBjoern A. Zeeb 
23456b4cac81SBjoern A. Zeeb 	return (vap);
23466b4cac81SBjoern A. Zeeb }
23476b4cac81SBjoern A. Zeeb 
23484b0af114SBjoern A. Zeeb void
23494b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
23504b0af114SBjoern A. Zeeb {
23514b0af114SBjoern A. Zeeb 
23524b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
23534b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
23544b0af114SBjoern A. Zeeb 
23554b0af114SBjoern A. Zeeb 	IMPROVE();
23564b0af114SBjoern A. Zeeb }
23574b0af114SBjoern A. Zeeb 
23584b0af114SBjoern A. Zeeb void
23594b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
23604b0af114SBjoern A. Zeeb {
23614b0af114SBjoern A. Zeeb 
23624b0af114SBjoern A. Zeeb 	TODO();
23634b0af114SBjoern A. Zeeb }
23644b0af114SBjoern A. Zeeb 
23656b4cac81SBjoern A. Zeeb static void
23666b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
23676b4cac81SBjoern A. Zeeb {
23686b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
23696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23736b4cac81SBjoern A. Zeeb 
23746b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23756b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23766b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
23776b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
23786b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23796b4cac81SBjoern A. Zeeb 
23808891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
23816b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
23828891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
23836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
23846b4cac81SBjoern A. Zeeb 
23856b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
23866b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
23876b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
23886b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
23896b4cac81SBjoern A. Zeeb }
23906b4cac81SBjoern A. Zeeb 
23916b4cac81SBjoern A. Zeeb static void
23926b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
23936b4cac81SBjoern A. Zeeb {
23946b4cac81SBjoern A. Zeeb 
23956b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
23966b4cac81SBjoern A. Zeeb 	TRACEOK();
23976b4cac81SBjoern A. Zeeb }
23986b4cac81SBjoern A. Zeeb 
23996b4cac81SBjoern A. Zeeb static void
24006b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
24016b4cac81SBjoern A. Zeeb {
24026b4cac81SBjoern A. Zeeb 
24036b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
24046b4cac81SBjoern A. Zeeb }
24056b4cac81SBjoern A. Zeeb 
24066b4cac81SBjoern A. Zeeb static void
24076b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
24086b4cac81SBjoern A. Zeeb {
24096b4cac81SBjoern A. Zeeb 
24106b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
24116b4cac81SBjoern A. Zeeb }
24126b4cac81SBjoern A. Zeeb 
24136b4cac81SBjoern A. Zeeb /* Start / stop device. */
24146b4cac81SBjoern A. Zeeb static void
24156b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
24166b4cac81SBjoern A. Zeeb {
24176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24186b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24196b4cac81SBjoern A. Zeeb 	int error;
24206b4cac81SBjoern A. Zeeb 	bool start_all;
24216b4cac81SBjoern A. Zeeb 
24226b4cac81SBjoern A. Zeeb 	IMPROVE();
24236b4cac81SBjoern A. Zeeb 
24246b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
24256b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24266b4cac81SBjoern A. Zeeb 	start_all = false;
24276b4cac81SBjoern A. Zeeb 
24286b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
24296b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
24306b4cac81SBjoern A. Zeeb 		if (error == 0)
24316b4cac81SBjoern A. Zeeb 			start_all = true;
24326b4cac81SBjoern A. Zeeb 	} else {
24336b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
24346b4cac81SBjoern A. Zeeb 	}
24356b4cac81SBjoern A. Zeeb 
24366b4cac81SBjoern A. Zeeb 	if (start_all)
24376b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
24386b4cac81SBjoern A. Zeeb }
24396b4cac81SBjoern A. Zeeb 
24406b4cac81SBjoern A. Zeeb bool
24416b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
24426b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
24436b4cac81SBjoern A. Zeeb {
24446b4cac81SBjoern A. Zeeb 	int i;
24456b4cac81SBjoern A. Zeeb 
24466b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
24476b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
24486b4cac81SBjoern A. Zeeb 			return (true);
24496b4cac81SBjoern A. Zeeb 	}
24506b4cac81SBjoern A. Zeeb 
24516b4cac81SBjoern A. Zeeb 	return (false);
24526b4cac81SBjoern A. Zeeb }
24536b4cac81SBjoern A. Zeeb 
24546b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
24556b4cac81SBjoern A. Zeeb bool
24566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
24576b4cac81SBjoern A. Zeeb {
24586b4cac81SBjoern A. Zeeb 	size_t x;
24596b4cac81SBjoern A. Zeeb 	uint8_t l;
24606b4cac81SBjoern A. Zeeb 
24616b4cac81SBjoern A. Zeeb 	x = *xp;
24626b4cac81SBjoern A. Zeeb 
24636b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
24646b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
24656b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
24666b4cac81SBjoern A. Zeeb 	x += 2 + l;
24676b4cac81SBjoern A. Zeeb 
24686b4cac81SBjoern A. Zeeb 	if (x > ies_len)
24696b4cac81SBjoern A. Zeeb 		return (false);
24706b4cac81SBjoern A. Zeeb 
24716b4cac81SBjoern A. Zeeb 	*xp = x;
24726b4cac81SBjoern A. Zeeb 	return (true);
24736b4cac81SBjoern A. Zeeb }
24746b4cac81SBjoern A. Zeeb 
2475d9945d78SBjoern A. Zeeb static uint8_t *
2476d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2477d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
24786b4cac81SBjoern A. Zeeb {
2479d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
2480d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
2481d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
2482d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
2483d9945d78SBjoern A. Zeeb 	uint8_t *pb;
2484d9945d78SBjoern A. Zeeb 	int band, i;
24856b4cac81SBjoern A. Zeeb 
2486d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2487d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
2488d9945d78SBjoern A. Zeeb 			continue;
2489d9945d78SBjoern A. Zeeb 
2490d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
2491d9945d78SBjoern A. Zeeb 		/*
2492d9945d78SBjoern A. Zeeb 		 * This should not happen;
2493d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
2494d9945d78SBjoern A. Zeeb 		 */
2495d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
2496d9945d78SBjoern A. Zeeb 			continue;
2497d9945d78SBjoern A. Zeeb 
2498d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
2499d9945d78SBjoern A. Zeeb 		channels = supband->channels;
2500d9945d78SBjoern A. Zeeb 		chan = NULL;
2501d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
2502d9945d78SBjoern A. Zeeb 
2503d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2504d9945d78SBjoern A. Zeeb 				continue;
2505d9945d78SBjoern A. Zeeb 
2506d9945d78SBjoern A. Zeeb 			chan = ieee80211_find_channel(vap->iv_ic,
2507d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
2508d9945d78SBjoern A. Zeeb 			if (chan != NULL)
2509d9945d78SBjoern A. Zeeb 				break;
2510d9945d78SBjoern A. Zeeb 		}
2511d9945d78SBjoern A. Zeeb 
2512d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
2513d9945d78SBjoern A. Zeeb 		if (chan == NULL)
2514d9945d78SBjoern A. Zeeb 			continue;
2515d9945d78SBjoern A. Zeeb 
2516d9945d78SBjoern A. Zeeb 		pb = p;
2517d9945d78SBjoern A. Zeeb 		rs = ieee80211_get_suprates(vap->iv_ic, chan);	/* calls chan2mode */
2518d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
2519d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
2520d9945d78SBjoern A. Zeeb 
2521d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
2522d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
2523d9945d78SBjoern A. Zeeb 	}
2524d9945d78SBjoern A. Zeeb 
2525d9945d78SBjoern A. Zeeb 	/* Add common_ies */
2526d9945d78SBjoern A. Zeeb 	pb = p;
2527d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2528d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
2529d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2530d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
2531d9945d78SBjoern A. Zeeb 	}
2532d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
2533d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
2534d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
2535d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
2536d9945d78SBjoern A. Zeeb 	}
2537d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
2538d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
2539d9945d78SBjoern A. Zeeb 
2540d9945d78SBjoern A. Zeeb 	return (p);
25416b4cac81SBjoern A. Zeeb }
25426b4cac81SBjoern A. Zeeb 
25436b4cac81SBjoern A. Zeeb static void
25446b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
25456b4cac81SBjoern A. Zeeb {
25466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25476b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25496b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25506b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
25516b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
25526b4cac81SBjoern A. Zeeb 	int error;
25536b4cac81SBjoern A. Zeeb 
25546b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
2555a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
25566b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
25576b4cac81SBjoern A. Zeeb 		return;
25586b4cac81SBjoern A. Zeeb 	}
25596b4cac81SBjoern A. Zeeb 
25606b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
25616b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
25626b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
2563d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
25646b4cac81SBjoern A. Zeeb 		return;
25656b4cac81SBjoern A. Zeeb 	}
25666b4cac81SBjoern A. Zeeb 
25676b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2568a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_HW) == 0) {
2569a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
2570a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2571d3ef7fb4SBjoern A. Zeeb sw_scan:
25726b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25736b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
2574086be6a8SBjoern A. Zeeb 
2575086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
2576086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
2577086be6a8SBjoern A. Zeeb 
25786b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
25796b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
25806b4cac81SBjoern A. Zeeb 		IMPROVE();
25816b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
25826b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
25836b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
25846b4cac81SBjoern A. Zeeb 
25856b4cac81SBjoern A. Zeeb 	} else {
25866b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
25876b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
25886b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
25896b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
25906b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
25916b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
2592d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
2593d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
2594d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
25956b4cac81SBjoern A. Zeeb 
2596d9945d78SBjoern A. Zeeb 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
2597d9945d78SBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported");
2598d9945d78SBjoern A. Zeeb 			/* In theory net80211 would have to drive this. */
2599d9945d78SBjoern A. Zeeb 			return;
2600d9945d78SBjoern A. Zeeb 		}
2601d9945d78SBjoern A. Zeeb 
2602d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
2603d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
26046b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
26056b4cac81SBjoern A. Zeeb 
2606d9945d78SBjoern A. Zeeb 		band_mask = 0;
26076b4cac81SBjoern A. Zeeb 		nchan = 0;
2608d9945d78SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++) {
26096b4cac81SBjoern A. Zeeb 			nchan++;
2610d9945d78SBjoern A. Zeeb 			band = lkpi_net80211_chan_to_nl80211_band(
2611d9945d78SBjoern A. Zeeb 			    ss->ss_chans[ss->ss_next + i]);
2612d9945d78SBjoern A. Zeeb 			band_mask |= (1 << band);
2613d9945d78SBjoern A. Zeeb 		}
26146b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
26156b4cac81SBjoern A. Zeeb 
2616d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
2617d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2618d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
2619d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
2620d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
2621d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
2622d9945d78SBjoern A. Zeeb 
2623d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
2624d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
2625d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
2626d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
2627d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
2628d9945d78SBjoern A. Zeeb 		}
2629d9945d78SBjoern A. Zeeb 
26306b4cac81SBjoern A. Zeeb 		KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
26316b4cac81SBjoern A. Zeeb 		    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
26326b4cac81SBjoern A. Zeeb 
2633d9945d78SBjoern A. Zeeb 		lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
2634d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
2635d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
26366b4cac81SBjoern A. Zeeb 
26376b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
26386b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
26396b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
26406b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
26416b4cac81SBjoern A. Zeeb #if 0
26426b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
26436b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
26446b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
26456b4cac81SBjoern A. Zeeb #endif
26466b4cac81SBjoern A. Zeeb #ifdef __notyet__
26476b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
26486b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
26496b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
26506b4cac81SBjoern A. Zeeb #endif
26516b4cac81SBjoern A. Zeeb 
26526b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
26536b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
26546b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
26556b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
26566b4cac81SBjoern A. Zeeb 			*(cpp + i) =
26576b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
26586b4cac81SBjoern A. Zeeb 		}
26596b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
26606b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
26616b4cac81SBjoern A. Zeeb 
26626b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
26636b4cac81SBjoern A. Zeeb 			lc->center_freq = c->ic_freq;
26646b4cac81SBjoern A. Zeeb 			/* lc->flags */
26656b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
26666b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
26676b4cac81SBjoern A. Zeeb 			/* lc-> ... */
26686b4cac81SBjoern A. Zeeb 			lc++;
26696b4cac81SBjoern A. Zeeb 		}
26706b4cac81SBjoern A. Zeeb 
2671d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
26726b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
26736b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
26746b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
2675d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
26766b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
26776b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
26786b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
26796b4cac81SBjoern A. Zeeb 				ssids++;
26806b4cac81SBjoern A. Zeeb 			}
26816b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
26826b4cac81SBjoern A. Zeeb 		} else {
26836b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
26846b4cac81SBjoern A. Zeeb 		}
26856b4cac81SBjoern A. Zeeb 
26866b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
26876b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
26886b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
26896b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
26906b4cac81SBjoern A. Zeeb 		/* s6gp->... */
26916b4cac81SBjoern A. Zeeb 
2692d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
2693d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
2694d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
2695d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
2696d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
2697d9945d78SBjoern A. Zeeb 
26986b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
26996b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
27006b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
27016b4cac81SBjoern A. Zeeb 		if (error != 0) {
2702d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
2703d9945d78SBjoern A. Zeeb 
27046b4cac81SBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
27056b4cac81SBjoern A. Zeeb 			lhw->hw_req = NULL;
2706d3ef7fb4SBjoern A. Zeeb 
2707d3ef7fb4SBjoern A. Zeeb 			/*
2708d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
2709d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
2710d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
2711d3ef7fb4SBjoern A. Zeeb 			 */
2712196cfd0bSBjoern A. Zeeb 			if (error == 1) {
2713a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
2714196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
2715196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
2716196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
2717196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
2718196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
2719196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2720196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2721196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
2722d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
2723196cfd0bSBjoern A. Zeeb 			}
2724d3ef7fb4SBjoern A. Zeeb 
2725d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2726d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
27276b4cac81SBjoern A. Zeeb 		}
27286b4cac81SBjoern A. Zeeb 	}
27296b4cac81SBjoern A. Zeeb }
27306b4cac81SBjoern A. Zeeb 
27316b4cac81SBjoern A. Zeeb static void
27326b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
27336b4cac81SBjoern A. Zeeb {
27346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27356b4cac81SBjoern A. Zeeb 
27366b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
2737a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
27386b4cac81SBjoern A. Zeeb 		return;
27396b4cac81SBjoern A. Zeeb 	}
27406b4cac81SBjoern A. Zeeb 
2741a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_HW) == 0) {
2742a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
2743a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
27446b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
27456b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
27466b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
27476b4cac81SBjoern A. Zeeb 
2748a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
2749a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
27506b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
27516b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
27526b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
2753a486fbbdSBjoern A. Zeeb 
27546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
27556b4cac81SBjoern A. Zeeb 
27566b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
2757086be6a8SBjoern A. Zeeb 
2758086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
2759086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
27606b4cac81SBjoern A. Zeeb 	}
27616b4cac81SBjoern A. Zeeb }
27626b4cac81SBjoern A. Zeeb 
27636b4cac81SBjoern A. Zeeb static void
2764a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
2765a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
2766a486fbbdSBjoern A. Zeeb {
2767a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
2768a486fbbdSBjoern A. Zeeb 
2769a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
2770a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_HW) == 0)
2771a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
2772a486fbbdSBjoern A. Zeeb }
2773a486fbbdSBjoern A. Zeeb 
2774a486fbbdSBjoern A. Zeeb static void
2775a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
2776a486fbbdSBjoern A. Zeeb {
2777a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
2778a486fbbdSBjoern A. Zeeb 
2779a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
2780a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_HW) == 0)
2781a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
2782a486fbbdSBjoern A. Zeeb }
2783a486fbbdSBjoern A. Zeeb 
2784a486fbbdSBjoern A. Zeeb static void
27856b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
27866b4cac81SBjoern A. Zeeb {
27876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2789b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
2790b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
27916b4cac81SBjoern A. Zeeb 	int error;
27926b4cac81SBjoern A. Zeeb 
27936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
2794b2cf3c21SBjoern A. Zeeb 
2795b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
2796b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
27976b4cac81SBjoern A. Zeeb 		return;
27986b4cac81SBjoern A. Zeeb 
2799a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
2800a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
2801a486fbbdSBjoern A. Zeeb 	    (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW))
2802a486fbbdSBjoern A. Zeeb 		return;
2803a486fbbdSBjoern A. Zeeb 
2804b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
2805b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
28066b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
28076b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
28086b4cac81SBjoern A. Zeeb 		return;
28096b4cac81SBjoern A. Zeeb 	}
28106b4cac81SBjoern A. Zeeb 
28116b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
28126b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
28136b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
28146b4cac81SBjoern A. Zeeb 		    c, chan);
28156b4cac81SBjoern A. Zeeb 		return;
28166b4cac81SBjoern A. Zeeb 	}
28176b4cac81SBjoern A. Zeeb 
28186b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
28196b4cac81SBjoern A. Zeeb 	IMPROVE();
28206b4cac81SBjoern A. Zeeb 
28216b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28224a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
28234a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
28246b4cac81SBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
28266b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
28276b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
28286b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
28296b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
28306b4cac81SBjoern A. Zeeb 		IMPROVE();
28316b4cac81SBjoern A. Zeeb 	} else {
28326b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
28336b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
28346b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
28356b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
28366b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
28376b4cac81SBjoern A. Zeeb 	}
28386b4cac81SBjoern A. Zeeb 
28396b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
28406b4cac81SBjoern A. Zeeb 	IMPROVE();
28416b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
28426b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
28436b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
28446b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
28456b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
28466b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
28476b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
28486b4cac81SBjoern A. Zeeb 			    error);
28496b4cac81SBjoern A. Zeeb 	}
28506b4cac81SBjoern A. Zeeb }
28516b4cac81SBjoern A. Zeeb 
28526b4cac81SBjoern A. Zeeb static struct ieee80211_node *
28536b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
28546b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
28556b4cac81SBjoern A. Zeeb {
28566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28584f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
28596b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28616b4cac81SBjoern A. Zeeb 
28626b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
28636b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28646b4cac81SBjoern A. Zeeb 
28656b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
28664f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
28674f61ef8bSBjoern A. Zeeb 		return (NULL);
28684f61ef8bSBjoern A. Zeeb 
28696b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
28706b4cac81SBjoern A. Zeeb 	if (ni == NULL)
28716b4cac81SBjoern A. Zeeb 		return (NULL);
28726b4cac81SBjoern A. Zeeb 
28736b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28744f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
28756b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
28766b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
28776b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
28786b4cac81SBjoern A. Zeeb 		return (NULL);
28796b4cac81SBjoern A. Zeeb 	}
28806b4cac81SBjoern A. Zeeb 
28816b4cac81SBjoern A. Zeeb 	return (ni);
28826b4cac81SBjoern A. Zeeb }
28836b4cac81SBjoern A. Zeeb 
28846b4cac81SBjoern A. Zeeb static int
28856b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
28866b4cac81SBjoern A. Zeeb {
28876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28896b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28906b4cac81SBjoern A. Zeeb 	int error;
28916b4cac81SBjoern A. Zeeb 
28926b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
28936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28946b4cac81SBjoern A. Zeeb 
28956b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
28966b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
28976b4cac81SBjoern A. Zeeb 		if (error != 0)
28986b4cac81SBjoern A. Zeeb 			return (error);
28996b4cac81SBjoern A. Zeeb 	}
29006b4cac81SBjoern A. Zeeb 
29016b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
29026b4cac81SBjoern A. Zeeb 
29036b4cac81SBjoern A. Zeeb 	/* Now take the reference before linking it to the table. */
29046b4cac81SBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
29056b4cac81SBjoern A. Zeeb 
29066b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
29076b4cac81SBjoern A. Zeeb 	IMPROVE();
29086b4cac81SBjoern A. Zeeb 
29096b4cac81SBjoern A. Zeeb 	return (0);
29106b4cac81SBjoern A. Zeeb }
29116b4cac81SBjoern A. Zeeb 
29126b4cac81SBjoern A. Zeeb static void
29136b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
29146b4cac81SBjoern A. Zeeb {
29156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
29166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29176b4cac81SBjoern A. Zeeb 
29186b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
29196b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
29206b4cac81SBjoern A. Zeeb 
29216b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
29226b4cac81SBjoern A. Zeeb 	IMPROVE();
29236b4cac81SBjoern A. Zeeb 
29246b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
29256b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
29266b4cac81SBjoern A. Zeeb }
29276b4cac81SBjoern A. Zeeb 
29286b4cac81SBjoern A. Zeeb static void
29296b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
29306b4cac81SBjoern A. Zeeb {
29316b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
29326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
29346b4cac81SBjoern A. Zeeb 
29356b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
29366b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
29376b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2938d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
2939d9f59799SBjoern A. Zeeb 		goto out;
29406b4cac81SBjoern A. Zeeb 
29416b4cac81SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
29426b4cac81SBjoern A. Zeeb 	IMPROVE();
29436b4cac81SBjoern A. Zeeb 
29446b4cac81SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
29456b4cac81SBjoern A. Zeeb #ifdef __notyet__
29466b4cac81SBjoern A. Zeeb 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
29476b4cac81SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
29486b4cac81SBjoern A. Zeeb #endif
29496b4cac81SBjoern A. Zeeb 	/* Drain taskq. */
29506b4cac81SBjoern A. Zeeb 
29516b4cac81SBjoern A. Zeeb 	/* Drain sta->txq[] */
29526b4cac81SBjoern A. Zeeb 	mtx_destroy(&lsta->txq_mtx);
29536b4cac81SBjoern A. Zeeb 
29546b4cac81SBjoern A. Zeeb 	/* Remove lsta if added_to_drv. */
2955e24e8103SBjoern A. Zeeb 
29566b4cac81SBjoern A. Zeeb 	/* Remove lsta from vif */
2957e24e8103SBjoern A. Zeeb 	/* Remove ref from lsta node... */
2958d9f59799SBjoern A. Zeeb 	/* Free lsta. */
2959e24e8103SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, VAP_TO_LVIF(ni->ni_vap));
2960d9f59799SBjoern A. Zeeb 
2961d9f59799SBjoern A. Zeeb out:
29626b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
29636b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
29646b4cac81SBjoern A. Zeeb }
29656b4cac81SBjoern A. Zeeb 
29666b4cac81SBjoern A. Zeeb static int
29676b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
29686b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
29696b4cac81SBjoern A. Zeeb {
29706b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
29716b4cac81SBjoern A. Zeeb 
29726b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
29736b4cac81SBjoern A. Zeeb 
29746b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
29756b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
29766b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
29776b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
29786b4cac81SBjoern A. Zeeb 
29799d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
29809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
29816b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
29826b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
29836b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
29849d9ba2b7SBjoern A. Zeeb #endif
29856b4cac81SBjoern A. Zeeb 
29866b4cac81SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
29876b4cac81SBjoern A. Zeeb 	return (0);
29886b4cac81SBjoern A. Zeeb }
29896b4cac81SBjoern A. Zeeb 
29906b4cac81SBjoern A. Zeeb static void
29916b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
29926b4cac81SBjoern A. Zeeb {
29936b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
2994b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
29956b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
2996b35f6cd0SBjoern A. Zeeb #endif
29976b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
29986b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
29996b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30026b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30036b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30046b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
30056b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
30066b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
30076b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3008e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
30096b4cac81SBjoern A. Zeeb 	void *buf;
3010e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
30116b4cac81SBjoern A. Zeeb 
30126b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
30136b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
30156b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
30166b4cac81SBjoern A. Zeeb #endif
30176b4cac81SBjoern A. Zeeb 
30186b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3019b35f6cd0SBjoern A. Zeeb 	k = NULL;
3020b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
30216b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
30226b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
30236b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
30246b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
30256b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
30266b4cac81SBjoern A. Zeeb 		if (k == NULL) {
30276b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
30286b4cac81SBjoern A. Zeeb 			m_freem(m);
30296b4cac81SBjoern A. Zeeb 			return;
30306b4cac81SBjoern A. Zeeb 		}
30316b4cac81SBjoern A. Zeeb 	}
30326b4cac81SBjoern A. Zeeb #endif
30336b4cac81SBjoern A. Zeeb 
30346b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
30356b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30366b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30376b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
30386b4cac81SBjoern A. Zeeb 
30396b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
30406b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
30416b4cac81SBjoern A. Zeeb 
30426b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
30436b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
30446b4cac81SBjoern A. Zeeb 		if (k != NULL)
30456b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
30466b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
30476b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
30486b4cac81SBjoern A. Zeeb 		IMPROVE();
30496b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
30506b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
30516b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
30526b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
30536b4cac81SBjoern A. Zeeb 		}
30546b4cac81SBjoern A. Zeeb 
30556b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
30566b4cac81SBjoern A. Zeeb 	}
30576b4cac81SBjoern A. Zeeb 
30586b4cac81SBjoern A. Zeeb 	/*
30596b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
30606b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
30613d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
30623d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
30636b4cac81SBjoern A. Zeeb 	 */
30646b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
30656b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
30666b4cac81SBjoern A. Zeeb 		printf("XXX ERROR %s: skb alloc failed\n", __func__);
30676b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
30686b4cac81SBjoern A. Zeeb 		m_freem(m);
30696b4cac81SBjoern A. Zeeb 		return;
30706b4cac81SBjoern A. Zeeb 	}
30716b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
30726b4cac81SBjoern A. Zeeb 
30736b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
30746b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
30756b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
30766b4cac81SBjoern A. Zeeb 	skb->m = m;
30776b4cac81SBjoern A. Zeeb #if 0
30786b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
30796b4cac81SBjoern A. Zeeb #else
30806b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
30816b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
30826b4cac81SBjoern A. Zeeb #endif
30836b4cac81SBjoern A. Zeeb 	/* Save the ni. */
30846b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
30856b4cac81SBjoern A. Zeeb 
30866b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
30876b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
30886b4cac81SBjoern A. Zeeb 
3089e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3090e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3091e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3092e3a0b120SBjoern A. Zeeb 		skb->priority = 0;
3093e3a0b120SBjoern A. Zeeb 		ac = IEEE80211_AC_BE;
3094e3a0b120SBjoern A. Zeeb 	} else {
3095e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3096e3a0b120SBjoern A. Zeeb 		ac = tid_to_mac80211_ac[tid & 7];
3097e3a0b120SBjoern A. Zeeb 	}
30986b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
30996b4cac81SBjoern A. Zeeb 
31006b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
31016b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
31026b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
31036b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
31046b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
31056b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3106e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
31076b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
31086b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
31096b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
31106b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
31116b4cac81SBjoern A. Zeeb 
31126b4cac81SBjoern A. Zeeb 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
31136b4cac81SBjoern A. Zeeb 	if (lsta != NULL) {
31146b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
3115b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
31166b4cac81SBjoern A. Zeeb 		info->control.hw_key = lsta->kc;
31176b4cac81SBjoern A. Zeeb #endif
31186b4cac81SBjoern A. Zeeb 	} else {
31196b4cac81SBjoern A. Zeeb 		sta = NULL;
31206b4cac81SBjoern A. Zeeb 	}
31216b4cac81SBjoern A. Zeeb 
31226b4cac81SBjoern A. Zeeb 	IMPROVE();
31236b4cac81SBjoern A. Zeeb 
31246b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
31256b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
31266b4cac81SBjoern A. Zeeb 
3127e3a0b120SBjoern A. Zeeb 		ltxq = NULL;
3128e3a0b120SBjoern A. Zeeb 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3129e3a0b120SBjoern A. Zeeb 			if (vif->type == NL80211_IFTYPE_STATION &&
3130e3a0b120SBjoern A. Zeeb 			    lsta->added_to_drv &&
3131e3a0b120SBjoern A. Zeeb 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3132d0d29110SBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3133e3a0b120SBjoern A. Zeeb 		} else if (lsta->added_to_drv &&
3134e3a0b120SBjoern A. Zeeb 		    sta->txq[skb->priority] != NULL) {
3135e3a0b120SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3136e3a0b120SBjoern A. Zeeb 		}
3137e3a0b120SBjoern A. Zeeb 		if (ltxq == NULL)
3138d0d29110SBjoern A. Zeeb 			goto ops_tx;
3139e3a0b120SBjoern A. Zeeb 
3140d0d29110SBjoern A. Zeeb 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3141d0d29110SBjoern A. Zeeb 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3142d0d29110SBjoern A. Zeeb 
31436b4cac81SBjoern A. Zeeb 		skb_queue_tail(&ltxq->skbq, skb);
31449d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31459d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3146d0d29110SBjoern A. Zeeb 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3147d0d29110SBjoern A. Zeeb 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3148d0d29110SBjoern A. Zeeb 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3149fb6eaf74SBjoern A. Zeeb 			    __func__, __LINE__,
3150d0d29110SBjoern A. Zeeb 			    curthread->td_tid, (unsigned int)ticks,
3151d0d29110SBjoern A. Zeeb 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3152d0d29110SBjoern A. Zeeb 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3153d0d29110SBjoern A. Zeeb 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
31549d9ba2b7SBjoern A. Zeeb #endif
3155d0d29110SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
31566b4cac81SBjoern A. Zeeb 		return;
31576b4cac81SBjoern A. Zeeb 	}
31586b4cac81SBjoern A. Zeeb 
31596b4cac81SBjoern A. Zeeb ops_tx:
31609d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31619d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3162d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3163d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
31646b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
31656b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
31669d9ba2b7SBjoern A. Zeeb #endif
31676b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
31686b4cac81SBjoern A. Zeeb 	control.sta = sta;
31696b4cac81SBjoern A. Zeeb 
31706b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
31716b4cac81SBjoern A. Zeeb 	return;
31726b4cac81SBjoern A. Zeeb }
31736b4cac81SBjoern A. Zeeb 
31746b4cac81SBjoern A. Zeeb static void
31756b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
31766b4cac81SBjoern A. Zeeb {
31776b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
31786b4cac81SBjoern A. Zeeb 	struct mbufq mq;
31796b4cac81SBjoern A. Zeeb 	struct mbuf *m;
31806b4cac81SBjoern A. Zeeb 
31816b4cac81SBjoern A. Zeeb 	lsta = ctx;
31826b4cac81SBjoern A. Zeeb 
31839d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31849d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
31856b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
31869d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
31876b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
31889d9ba2b7SBjoern A. Zeeb #endif
31896b4cac81SBjoern A. Zeeb 
31906b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
31916b4cac81SBjoern A. Zeeb 
31926b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
31936b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
31946b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
31956b4cac81SBjoern A. Zeeb 
31966b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
31976b4cac81SBjoern A. Zeeb 	while (m != NULL) {
31986b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
31996b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
32006b4cac81SBjoern A. Zeeb 	}
32016b4cac81SBjoern A. Zeeb }
32026b4cac81SBjoern A. Zeeb 
32036b4cac81SBjoern A. Zeeb static int
32046b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
32056b4cac81SBjoern A. Zeeb {
32066b4cac81SBjoern A. Zeeb 
32076b4cac81SBjoern A. Zeeb 	/* XXX TODO */
32086b4cac81SBjoern A. Zeeb 	IMPROVE();
32096b4cac81SBjoern A. Zeeb 
32106b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
32116b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
32126b4cac81SBjoern A. Zeeb 
32136b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
32146b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
32156b4cac81SBjoern A. Zeeb }
32166b4cac81SBjoern A. Zeeb 
32176b4cac81SBjoern A. Zeeb static void
32186b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
32196b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
32206b4cac81SBjoern A. Zeeb {
32216b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32226b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32236b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
32246b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
32256b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
32266b4cac81SBjoern A. Zeeb 
32276b4cac81SBjoern A. Zeeb 	/* Channels */
32286b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32306b4cac81SBjoern A. Zeeb 
32316b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
32326b4cac81SBjoern A. Zeeb 	nchans = 0;
32336b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
32346b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
32356b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
32366b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
32376b4cac81SBjoern A. Zeeb 		chan_flags = 0;
32386b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
32396b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
32406b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
32416b4cac81SBjoern A. Zeeb #ifdef __notyet__
32426b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
32436b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NG);
32446b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
32456b4cac81SBjoern A. Zeeb 		}
32466b4cac81SBjoern A. Zeeb #endif
32476b4cac81SBjoern A. Zeeb 
32486b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3249cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
32506b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
32516b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
32526b4cac81SBjoern A. Zeeb 
32536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
32546b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
32556b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
32566b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
32576b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
32586b4cac81SBjoern A. Zeeb 				continue;
32596b4cac81SBjoern A. Zeeb 			}
32606b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
32616b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
32626b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
32636b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
32646b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
32656b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
32666b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
32676b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
32686b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
32696b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
32706b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
32716b4cac81SBjoern A. Zeeb 
32726b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
32736b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
32746b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
32756b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
3276cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
3277cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
32786b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
32796b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
32806b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
32816b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
32826b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
3283cee56e77SBjoern A. Zeeb 			if (error != 0)
32846b4cac81SBjoern A. Zeeb 				break;
32856b4cac81SBjoern A. Zeeb 		}
32866b4cac81SBjoern A. Zeeb 	}
32876b4cac81SBjoern A. Zeeb 
32886b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
32896b4cac81SBjoern A. Zeeb 	nchans = 0;
32906b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
32916b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
32926b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
32936b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
32946b4cac81SBjoern A. Zeeb 		chan_flags = 0;
32956b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
32966b4cac81SBjoern A. Zeeb #ifdef __not_yet__
32976b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
32986b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NA);
32996b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
33006b4cac81SBjoern A. Zeeb 		}
33016b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
33026b4cac81SBjoern A. Zeeb 
33036b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
33046b4cac81SBjoern A. Zeeb 			ic->ic_vhtcaps =
33056b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
33066b4cac81SBjoern A. Zeeb 
33076b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
33086b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
33096b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
33106b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
33116b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
33126b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
33136b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
33146b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
33156b4cac81SBjoern A. Zeeb 		}
33166b4cac81SBjoern A. Zeeb #endif
33176b4cac81SBjoern A. Zeeb 
33186b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3319cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
33206b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
33216b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
33226b4cac81SBjoern A. Zeeb 
33236b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
33246b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
33256b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
33266b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
33276b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
33286b4cac81SBjoern A. Zeeb 				continue;
33296b4cac81SBjoern A. Zeeb 			}
33306b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
33316b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
33326b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
33336b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
33346b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
33356b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
33366b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
33376b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
33386b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
33396b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
33406b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
33416b4cac81SBjoern A. Zeeb 
33426b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
33436b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
33446b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
33456b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
3346cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
3347cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
33486b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
33496b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
33506b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
33516b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
33526b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
3353cee56e77SBjoern A. Zeeb 			if (error != 0)
33546b4cac81SBjoern A. Zeeb 				break;
33556b4cac81SBjoern A. Zeeb 		}
33566b4cac81SBjoern A. Zeeb 	}
33576b4cac81SBjoern A. Zeeb }
33586b4cac81SBjoern A. Zeeb 
33596b4cac81SBjoern A. Zeeb static void *
33606b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
33616b4cac81SBjoern A. Zeeb {
33626b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33636b4cac81SBjoern A. Zeeb 
33646b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
33656b4cac81SBjoern A. Zeeb 	if (ic == NULL)
33666b4cac81SBjoern A. Zeeb 		return (NULL);
33676b4cac81SBjoern A. Zeeb 
33686b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
33696b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
33706b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
33716b4cac81SBjoern A. Zeeb 
33726b4cac81SBjoern A. Zeeb 	return (ic);
33736b4cac81SBjoern A. Zeeb }
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb struct ieee80211_hw *
33766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
33776b4cac81SBjoern A. Zeeb {
33786b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33806b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
33816b4cac81SBjoern A. Zeeb 
33826b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
33836b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
33846b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
33856b4cac81SBjoern A. Zeeb 		return (NULL);
33866b4cac81SBjoern A. Zeeb 
33876b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
33886b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
3389652e22d3SBjoern A. Zeeb 
33906b4cac81SBjoern A. Zeeb 	mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
33918891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
33926b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
33936b4cac81SBjoern A. Zeeb 
33946b4cac81SBjoern A. Zeeb 	/*
33956b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
33966b4cac81SBjoern A. Zeeb 	 * not initialized.
33976b4cac81SBjoern A. Zeeb 	 */
33986b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33996b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
3400086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
34016b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
34026b4cac81SBjoern A. Zeeb 
34036b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
34046b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
34056b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
34066b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
34076b4cac81SBjoern A. Zeeb 		return (NULL);
34086b4cac81SBjoern A. Zeeb 	}
34096b4cac81SBjoern A. Zeeb 
34106b4cac81SBjoern A. Zeeb 	IMPROVE();
34116b4cac81SBjoern A. Zeeb 
34126b4cac81SBjoern A. Zeeb 	return (hw);
34136b4cac81SBjoern A. Zeeb }
34146b4cac81SBjoern A. Zeeb 
34156b4cac81SBjoern A. Zeeb void
34166b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
34176b4cac81SBjoern A. Zeeb {
34186b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34196b4cac81SBjoern A. Zeeb 
34206b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
34216b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
34226b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
34236b4cac81SBjoern A. Zeeb 
34246b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
34258891c455SBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
34266b4cac81SBjoern A. Zeeb 	mtx_destroy(&lhw->mtx);
34276b4cac81SBjoern A. Zeeb 	IMPROVE();
34286b4cac81SBjoern A. Zeeb }
34296b4cac81SBjoern A. Zeeb 
34306b4cac81SBjoern A. Zeeb void
34316b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
34326b4cac81SBjoern A. Zeeb {
34336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34346b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34356b4cac81SBjoern A. Zeeb 
34366b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
34376b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
34386b4cac81SBjoern A. Zeeb 
34396b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
34406b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
34416b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
34426b4cac81SBjoern A. Zeeb 
34436b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
34446b4cac81SBjoern A. Zeeb }
34456b4cac81SBjoern A. Zeeb 
34466b4cac81SBjoern A. Zeeb struct ieee80211_hw *
34476b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
34486b4cac81SBjoern A. Zeeb {
34496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34506b4cac81SBjoern A. Zeeb 
34516b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
34526b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
34536b4cac81SBjoern A. Zeeb }
34546b4cac81SBjoern A. Zeeb 
34556b4cac81SBjoern A. Zeeb static void
34566b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
34576b4cac81SBjoern A. Zeeb {
34586b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34596b4cac81SBjoern A. Zeeb 
34606b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
34616b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
34626b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
34636b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
34646b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
34656b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
34666b4cac81SBjoern A. Zeeb }
34676b4cac81SBjoern A. Zeeb 
34682e183d99SBjoern A. Zeeb int
34696b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
34706b4cac81SBjoern A. Zeeb {
34716b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3473c5b96b3eSBjoern A. Zeeb 	int band, i;
34746b4cac81SBjoern A. Zeeb 
34756b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
34766b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
34776b4cac81SBjoern A. Zeeb 
3478652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
3479652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3480652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
3481652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
3482652e22d3SBjoern A. Zeeb 
34836b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
34846b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
34856b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
34866b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
34876b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
34886b4cac81SBjoern A. Zeeb 		/* We take the first one. */
34896b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
34906b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
34916b4cac81SBjoern A. Zeeb 	} else {
34926b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
34936b4cac81SBjoern A. Zeeb 	}
34946b4cac81SBjoern A. Zeeb 
34953d09d310SBjoern A. Zeeb #ifdef __not_yet__
34963d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
34976b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
34983d09d310SBjoern A. Zeeb #endif
34996b4cac81SBjoern A. Zeeb 
35006b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
35016b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
35026b4cac81SBjoern A. Zeeb 
35036b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
35046b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
35056b4cac81SBjoern A. Zeeb 	ic->ic_caps =
35066b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
35076b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
35086b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
35094a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
35106b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
35114a67f1dfSBjoern A. Zeeb #endif
35126b4cac81SBjoern A. Zeeb #if 0
35136b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
35146b4cac81SBjoern A. Zeeb #endif
35156b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
35166b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
35176b4cac81SBjoern A. Zeeb 	    ;
35186b4cac81SBjoern A. Zeeb #if 0
35196b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
35206b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
35216b4cac81SBjoern A. Zeeb #endif
3522cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
3523cc4e78d5SBjoern A. Zeeb 		/*
3524cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
3525cc4e78d5SBjoern A. Zeeb 		 *
3526cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3527cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
3528cc4e78d5SBjoern A. Zeeb 		 * the flag.
3529cc4e78d5SBjoern A. Zeeb 		 */
35306b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3531a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
35326b4cac81SBjoern A. Zeeb 	}
35336b4cac81SBjoern A. Zeeb 
35346b4cac81SBjoern A. Zeeb #ifdef __notyet__
35356b4cac81SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
35366b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
35376b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
35386b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_MAXAMSDU_3839
35396b4cac81SBjoern A. Zeeb 						/* max A-MSDU length */
35406b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
35416b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
35426b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
35436b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
35446b4cac81SBjoern A. Zeeb #endif
35456b4cac81SBjoern A. Zeeb 
3546527687a9SBjoern A. Zeeb 	/*
3547527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
3548527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
3549527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
3550527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
3551527687a9SBjoern A. Zeeb 	 */
3552527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
3553527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
3554527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
3555527687a9SBjoern A. Zeeb 
3556527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
3557527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
3558527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
3559527687a9SBjoern A. Zeeb 		}
3560527687a9SBjoern A. Zeeb 	}
3561527687a9SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
3563b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
35646b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
35656b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
35666b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
35676b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
35686b4cac81SBjoern A. Zeeb 	}
35696b4cac81SBjoern A. Zeeb #endif
35706b4cac81SBjoern A. Zeeb 
35716b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
35726b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
35736b4cac81SBjoern A. Zeeb 
35746b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
35756b4cac81SBjoern A. Zeeb 
35766b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
35776b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
35786b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
35796b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
35806b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
35816b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
35826b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
35836b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
35846b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
35856b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
35866b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
35876b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
35886b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
35896b4cac81SBjoern A. Zeeb 
3590a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
3591a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
3592a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
3593a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
3594a486fbbdSBjoern A. Zeeb 
35956b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
35966b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
35976b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
35986b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
35996b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
36006b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
36016b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
36026b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
36036b4cac81SBjoern A. Zeeb 
36046b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
36056b4cac81SBjoern A. Zeeb 
3606c5b96b3eSBjoern A. Zeeb 	/*
3607c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
3608c5b96b3eSBjoern A. Zeeb 	 * expect one.
3609d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
3610d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
3611c5b96b3eSBjoern A. Zeeb 	 */
3612d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
3613c5b96b3eSBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS &&
3614c5b96b3eSBjoern A. Zeeb 	    hw->conf.chandef.chan == NULL; band++) {
3615c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
3616c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
3617c5b96b3eSBjoern A. Zeeb 
3618c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3619c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3620c5b96b3eSBjoern A. Zeeb 			continue;
3621c5b96b3eSBjoern A. Zeeb 
3622d9945d78SBjoern A. Zeeb 		lhw->supbands++;
3623d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
3624d9945d78SBjoern A. Zeeb 
3625c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
3626c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3627c5b96b3eSBjoern A. Zeeb 
3628c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3629c5b96b3eSBjoern A. Zeeb 				continue;
3630c5b96b3eSBjoern A. Zeeb 
36314a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3632c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
3633c5b96b3eSBjoern A. Zeeb 			break;
3634c5b96b3eSBjoern A. Zeeb 		}
3635c5b96b3eSBjoern A. Zeeb 	}
3636c5b96b3eSBjoern A. Zeeb 
3637d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
3638d9945d78SBjoern A. Zeeb 
3639d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
3640d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
3641d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
3642d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
3643d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
3644d9945d78SBjoern A. Zeeb 	}
3645d9945d78SBjoern A. Zeeb 
3646d9945d78SBjoern A. Zeeb 	/*
3647d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
3648d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
3649d9945d78SBjoern A. Zeeb 	 * XXX-BZ FIXME add HT VHT ... later
3650d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
3651d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
3652d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
3653d9945d78SBjoern A. Zeeb 	 */
3654d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
3655d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
3656d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
3657d9945d78SBjoern A. Zeeb 	/*
3658d9945d78SBjoern A. Zeeb 	 * net80211 does not seem to support the DSSS Parameter Set but some of
3659d9945d78SBjoern A. Zeeb 	 * the drivers insert it so calculate the extra fixed space in.
3660d9945d78SBjoern A. Zeeb 	 */
3661d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len += 2 + 1;
3662d9945d78SBjoern A. Zeeb 
3663d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
3664d9945d78SBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0)
3665d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
3666d9945d78SBjoern A. Zeeb 
36676b4cac81SBjoern A. Zeeb 	if (bootverbose)
36686b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
36692e183d99SBjoern A. Zeeb 
36702e183d99SBjoern A. Zeeb 	return (0);
36716b4cac81SBjoern A. Zeeb }
36726b4cac81SBjoern A. Zeeb 
36736b4cac81SBjoern A. Zeeb void
36746b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
36756b4cac81SBjoern A. Zeeb {
36766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36776b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36786b4cac81SBjoern A. Zeeb 
36796b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
36806b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
36816b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
36826b4cac81SBjoern A. Zeeb }
36836b4cac81SBjoern A. Zeeb 
36846b4cac81SBjoern A. Zeeb void
36856b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
36866b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
36876b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
36886b4cac81SBjoern A. Zeeb     void *arg)
36896b4cac81SBjoern A. Zeeb {
36906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36916b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36926b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
369361a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
36946b4cac81SBjoern A. Zeeb 
36956b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
36966b4cac81SBjoern A. Zeeb 
36976b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
36986b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
369961a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
37006b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
37016b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
37026b4cac81SBjoern A. Zeeb 		    __func__, flags);
37036b4cac81SBjoern A. Zeeb 	}
37046b4cac81SBjoern A. Zeeb 
37056b4cac81SBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
37066b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
370761a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
37086b4cac81SBjoern A. Zeeb 
37096b4cac81SBjoern A. Zeeb 	if (atomic)
37108891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
37116b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
37126b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
37136b4cac81SBjoern A. Zeeb 
37146b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
37156b4cac81SBjoern A. Zeeb 
37166b4cac81SBjoern A. Zeeb 		/*
37176b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
37186b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
37196b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
37206b4cac81SBjoern A. Zeeb 		 * driver does not know about.
37216b4cac81SBjoern A. Zeeb 		 */
37226b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
37236b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
37246b4cac81SBjoern A. Zeeb 			continue;
37256b4cac81SBjoern A. Zeeb 
37266b4cac81SBjoern A. Zeeb 		/*
372761a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
372861a68e50SBjoern A. Zeeb 		 * if we haven't yet.
372961a68e50SBjoern A. Zeeb 		 */
373061a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
373161a68e50SBjoern A. Zeeb 			continue;
373261a68e50SBjoern A. Zeeb 
373361a68e50SBjoern A. Zeeb 		/*
37346b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
37356b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
37366b4cac81SBjoern A. Zeeb 		 */
37376b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
37386b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
37396b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
37406b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
37416b4cac81SBjoern A. Zeeb 	}
37426b4cac81SBjoern A. Zeeb 	if (atomic)
37438891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
37446b4cac81SBjoern A. Zeeb }
37456b4cac81SBjoern A. Zeeb 
37466b4cac81SBjoern A. Zeeb void
37476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
37486b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
37496b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
37506b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
37516b4cac81SBjoern A. Zeeb     void *arg)
37526b4cac81SBjoern A. Zeeb {
37536b4cac81SBjoern A. Zeeb 
37546b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
37556b4cac81SBjoern A. Zeeb }
37566b4cac81SBjoern A. Zeeb 
37576b4cac81SBjoern A. Zeeb void
37586b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
37596b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
37606b4cac81SBjoern A. Zeeb 	void *),
37616b4cac81SBjoern A. Zeeb     void *arg)
37626b4cac81SBjoern A. Zeeb {
37636b4cac81SBjoern A. Zeeb 
37646b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
37656b4cac81SBjoern A. Zeeb }
37666b4cac81SBjoern A. Zeeb 
37676b4cac81SBjoern A. Zeeb void
37686b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
37696b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
37706b4cac81SBjoern A. Zeeb {
37716b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37726b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37736b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
37746b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
37756b4cac81SBjoern A. Zeeb 
37766b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
37776b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
37786b4cac81SBjoern A. Zeeb 
37796b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
37806b4cac81SBjoern A. Zeeb 
37818891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
37826b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
37836b4cac81SBjoern A. Zeeb 
37846b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
37856b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
37866b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
37876b4cac81SBjoern A. Zeeb 				continue;
37886b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
37896b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
37906b4cac81SBjoern A. Zeeb 		}
37916b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
37926b4cac81SBjoern A. Zeeb 	}
37938891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
37946b4cac81SBjoern A. Zeeb }
37956b4cac81SBjoern A. Zeeb 
37966b4cac81SBjoern A. Zeeb int
37976b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
37986b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
37996b4cac81SBjoern A. Zeeb {
38006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38016b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38026b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
38036b4cac81SBjoern A. Zeeb 
38046b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
38056b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
38066b4cac81SBjoern A. Zeeb 
38076b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
38086b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
38096b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
38106b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
38116b4cac81SBjoern A. Zeeb 	}
38126b4cac81SBjoern A. Zeeb 
38136b4cac81SBjoern A. Zeeb 	TODO();
38146b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
38156b4cac81SBjoern A. Zeeb 
38166b4cac81SBjoern A. Zeeb 	return (0);
38176b4cac81SBjoern A. Zeeb }
38186b4cac81SBjoern A. Zeeb 
38196b4cac81SBjoern A. Zeeb void
38206b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
38216b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
38226b4cac81SBjoern A. Zeeb {
38236b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38246b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38256b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
38266b4cac81SBjoern A. Zeeb 
38276b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
38286b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
38296b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
38306b4cac81SBjoern A. Zeeb 
38316b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
38326b4cac81SBjoern A. Zeeb 
38336b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
38346b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
38356b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
3836a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
38376b4cac81SBjoern A. Zeeb 	wakeup(lhw);
38386b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
38396b4cac81SBjoern A. Zeeb 
38406b4cac81SBjoern A. Zeeb 	return;
38416b4cac81SBjoern A. Zeeb }
38426b4cac81SBjoern A. Zeeb 
3843e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
38446b4cac81SBjoern A. Zeeb void
38456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3846e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
3847e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
38486b4cac81SBjoern A. Zeeb {
38496b4cac81SBjoern A. Zeeb 	struct epoch_tracker et;
38506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38516b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38526b4cac81SBjoern A. Zeeb 	struct mbuf *m;
38536b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
38546b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
38556b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
38566b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
38576b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
38586b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
38596b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
38609d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
3861170acccfSBjoern A. Zeeb 	int8_t rssi;
3862c0cadd99SBjoern A. Zeeb 	bool is_beacon;
38636b4cac81SBjoern A. Zeeb 
38646b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
38656b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
38666b4cac81SBjoern A. Zeeb 		IMPROVE();
38676b4cac81SBjoern A. Zeeb 		goto err;
38686b4cac81SBjoern A. Zeeb 	}
38696b4cac81SBjoern A. Zeeb 
38706b4cac81SBjoern A. Zeeb 	/*
38716b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
38726b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
38736b4cac81SBjoern A. Zeeb 	 */
38746b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
38756b4cac81SBjoern A. Zeeb 	if (m == NULL)
38766b4cac81SBjoern A. Zeeb 		goto err;
38776b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
38786b4cac81SBjoern A. Zeeb 
38796b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
38806b4cac81SBjoern A. Zeeb 	offset = m->m_len;
38816b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
38826b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
38836b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
38846b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
38856b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
38866b4cac81SBjoern A. Zeeb 	}
38876b4cac81SBjoern A. Zeeb 
38886b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
38896b4cac81SBjoern A. Zeeb 
38906b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
3891c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
3892c0cadd99SBjoern A. Zeeb 
3893c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
38949d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
38956b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
38966b4cac81SBjoern A. Zeeb 
38979d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
38986b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
3899c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
39006b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
39016b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
39026b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
3903c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
39046b4cac81SBjoern A. Zeeb 
39059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
39066b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
39076b4cac81SBjoern A. Zeeb 
39086b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
39099d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3910c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
391160970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
39126b4cac81SBjoern A. Zeeb 			__func__,
3913c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
3914c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
39156b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
39166b4cac81SBjoern A. Zeeb 			rx_status->flag,
39176b4cac81SBjoern A. Zeeb 			rx_status->freq,
39186b4cac81SBjoern A. Zeeb 			rx_status->bw,
39196b4cac81SBjoern A. Zeeb 			rx_status->encoding,
39206b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
39216b4cac81SBjoern A. Zeeb 			rx_status->band,
39226b4cac81SBjoern A. Zeeb 			rx_status->chains,
39236b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
39246b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
39256b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
392660970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
39276b4cac81SBjoern A. Zeeb 			rx_status->signal,
39286b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
39296b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
39306b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
39316b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
39326b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
39336b4cac81SBjoern A. Zeeb 			rx_status->nss,
39346b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
39356b4cac81SBjoern A. Zeeb no_trace_beacons:
39366b4cac81SBjoern A. Zeeb #endif
39376b4cac81SBjoern A. Zeeb 
39386b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
39396b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
394060970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
394160970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
39426b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
39436b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3944170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
39456b4cac81SBjoern A. Zeeb 	else
3946170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
3947170acccfSBjoern A. Zeeb 	/*
3948170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
3949170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
3950170acccfSBjoern A. Zeeb 	 */
3951170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
3952170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
39536b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
39546b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
39556b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
39566b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
39576b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
39586b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
39596b4cac81SBjoern A. Zeeb 
39606b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
39616b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
39626b4cac81SBjoern A. Zeeb 
39636b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
39646b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
39656b4cac81SBjoern A. Zeeb 
39666b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
39676b4cac81SBjoern A. Zeeb 	if (ok == 0) {
3968dbc06dd9SBjoern A. Zeeb 		m_freem(m);
39696b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
39706b4cac81SBjoern A. Zeeb 		goto err;
39716b4cac81SBjoern A. Zeeb 	}
39726b4cac81SBjoern A. Zeeb 
39736b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
39746b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
39756b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
39766b4cac81SBjoern A. Zeeb 	} else {
3977c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
3978c8dafefaSBjoern A. Zeeb 
39796b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
39806b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
39816b4cac81SBjoern A. Zeeb 		if (ni != NULL)
39826b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
39836b4cac81SBjoern A. Zeeb 	}
39846b4cac81SBjoern A. Zeeb 
39856b4cac81SBjoern A. Zeeb 	if (ni != NULL)
39866b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
39876b4cac81SBjoern A. Zeeb 	else
39886b4cac81SBjoern A. Zeeb 		/*
39896b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
39906b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
39916b4cac81SBjoern A. Zeeb 		 */
39926b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
39936b4cac81SBjoern A. Zeeb 
39949d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39959d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3996c0cadd99SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
3997c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
3998c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
39999d9ba2b7SBjoern A. Zeeb #endif
40006b4cac81SBjoern A. Zeeb 
4001c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
4002c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
4003c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4004c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
4005c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
4006c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
4007c8dafefaSBjoern A. Zeeb 
4008c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
4009c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4010c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
4011c8dafefaSBjoern A. Zeeb 
4012c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
4013c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4014c8dafefaSBjoern A. Zeeb 
4015c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
4016c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
4017c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4018c8dafefaSBjoern A. Zeeb 		/*
4019c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
4020c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4021c8dafefaSBjoern A. Zeeb 		 */
4022c8dafefaSBjoern A. Zeeb 	}
4023c8dafefaSBjoern A. Zeeb skip_device_ts:
4024c8dafefaSBjoern A. Zeeb 
40256b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
40266b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
40276b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
40286b4cac81SBjoern A. Zeeb 
40296b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
40306b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
40316b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
40326b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
40336b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
40346b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
40356b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
40366b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
40376b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
40386b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
40396b4cac81SBjoern A. Zeeb #endif
40406b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
40416b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
40426b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
40436b4cac81SBjoern A. Zeeb 		IMPROVE();
40446b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
40456b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
40466b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
40476b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4048170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
40496b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
40506b4cac81SBjoern A. Zeeb 	}
40516b4cac81SBjoern A. Zeeb 
40526b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
40536b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
40546b4cac81SBjoern A. Zeeb 
4055e30e05d3SBjoern A. Zeeb #if 0
4056e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
4057e30e05d3SBjoern A. Zeeb 		/*
4058e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
4059e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
4060e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
4061e30e05d3SBjoern A. Zeeb 		*/
4062e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
4063e30e05d3SBjoern A. Zeeb 	}
4064e30e05d3SBjoern A. Zeeb #endif
4065e30e05d3SBjoern A. Zeeb 
40666b4cac81SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
40676b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
40689d9ba2b7SBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
40696b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
4070dbc06dd9SBjoern A. Zeeb 		if (ok < 0)
4071dbc06dd9SBjoern A. Zeeb 			m_freem(m);
40726b4cac81SBjoern A. Zeeb 	} else {
40739d9ba2b7SBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(ic, m);
4074dbc06dd9SBjoern A. Zeeb 		/* mbuf got consumed. */
40756b4cac81SBjoern A. Zeeb 	}
40766b4cac81SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
40776b4cac81SBjoern A. Zeeb 
40789d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
40799d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
40809d9ba2b7SBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
40819d9ba2b7SBjoern A. Zeeb #endif
40826b4cac81SBjoern A. Zeeb 
40836b4cac81SBjoern A. Zeeb 	IMPROVE();
40846b4cac81SBjoern A. Zeeb 
40856b4cac81SBjoern A. Zeeb err:
40866b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
40876b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
40886b4cac81SBjoern A. Zeeb }
40896b4cac81SBjoern A. Zeeb 
40906b4cac81SBjoern A. Zeeb uint8_t
4091ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
40926b4cac81SBjoern A. Zeeb {
40936b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
4094ec190d91SBjoern A. Zeeb 	uint8_t tid;
4095ec190d91SBjoern A. Zeeb 
4096ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
4097ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
4098ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
4099ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
41006b4cac81SBjoern A. Zeeb 
41016b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
4102ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
4103ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
4104ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
4105ec190d91SBjoern A. Zeeb 
4106ec190d91SBjoern A. Zeeb 	return (tid);
41076b4cac81SBjoern A. Zeeb }
41086b4cac81SBjoern A. Zeeb 
41096b4cac81SBjoern A. Zeeb struct wiphy *
41106b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
41116b4cac81SBjoern A. Zeeb {
41126b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
41136b4cac81SBjoern A. Zeeb 
41146b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
41156b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
41166b4cac81SBjoern A. Zeeb 		return (NULL);
41176b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
41186b4cac81SBjoern A. Zeeb 
41196b4cac81SBjoern A. Zeeb 	/* XXX TODO */
41206b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
41216b4cac81SBjoern A. Zeeb }
41226b4cac81SBjoern A. Zeeb 
41236b4cac81SBjoern A. Zeeb void
41246b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
41256b4cac81SBjoern A. Zeeb {
41266b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
41276b4cac81SBjoern A. Zeeb 
41286b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
41296b4cac81SBjoern A. Zeeb 		return;
41306b4cac81SBjoern A. Zeeb 
41316b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
41326b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
41336b4cac81SBjoern A. Zeeb }
41346b4cac81SBjoern A. Zeeb 
41356b4cac81SBjoern A. Zeeb uint32_t
41366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
41376b4cac81SBjoern A. Zeeb     enum nl80211_band band)
41386b4cac81SBjoern A. Zeeb {
41396b4cac81SBjoern A. Zeeb 
41406b4cac81SBjoern A. Zeeb 	switch (band) {
41416b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
41426b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
41436b4cac81SBjoern A. Zeeb 		break;
41446b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
41456b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
41466b4cac81SBjoern A. Zeeb 		break;
41476b4cac81SBjoern A. Zeeb 	default:
41486b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
41496b4cac81SBjoern A. Zeeb 		break;
41506b4cac81SBjoern A. Zeeb 	}
41516b4cac81SBjoern A. Zeeb 
41526b4cac81SBjoern A. Zeeb 	return (0);
41536b4cac81SBjoern A. Zeeb }
41546b4cac81SBjoern A. Zeeb 
41556b4cac81SBjoern A. Zeeb uint32_t
41566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
41576b4cac81SBjoern A. Zeeb {
41586b4cac81SBjoern A. Zeeb 
41596b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
41606b4cac81SBjoern A. Zeeb }
41616b4cac81SBjoern A. Zeeb 
41626b4cac81SBjoern A. Zeeb static struct lkpi_sta *
41636b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
41646b4cac81SBjoern A. Zeeb {
41656b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
41666b4cac81SBjoern A. Zeeb 
41676b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
41686b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
41696b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
41706b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
41716b4cac81SBjoern A. Zeeb 			return (lsta);
41726b4cac81SBjoern A. Zeeb 		}
41736b4cac81SBjoern A. Zeeb 	}
41746b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
41756b4cac81SBjoern A. Zeeb 
41766b4cac81SBjoern A. Zeeb 	return (NULL);
41776b4cac81SBjoern A. Zeeb }
41786b4cac81SBjoern A. Zeeb 
41796b4cac81SBjoern A. Zeeb struct ieee80211_sta *
41806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
41816b4cac81SBjoern A. Zeeb {
41826b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41836b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
41846b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
41856b4cac81SBjoern A. Zeeb 
41866b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
41876b4cac81SBjoern A. Zeeb 
41886b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
41896b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
41906b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
41916b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
41926b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
41936b4cac81SBjoern A. Zeeb 			return (sta);
41946b4cac81SBjoern A. Zeeb 		}
41956b4cac81SBjoern A. Zeeb 	}
41966b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
41976b4cac81SBjoern A. Zeeb 	return (NULL);
41986b4cac81SBjoern A. Zeeb }
41996b4cac81SBjoern A. Zeeb 
42006b4cac81SBjoern A. Zeeb struct ieee80211_sta *
42012e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
42022e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
42036b4cac81SBjoern A. Zeeb {
42046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42056b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
42066b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42076b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
42086b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
42096b4cac81SBjoern A. Zeeb 
42106b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
42116b4cac81SBjoern A. Zeeb 	sta = NULL;
42126b4cac81SBjoern A. Zeeb 
42138891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
42146b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
42156b4cac81SBjoern A. Zeeb 
42166b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
42176b4cac81SBjoern A. Zeeb 
42186b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
42196b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
42206b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
42216b4cac81SBjoern A. Zeeb 			continue;
42226b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
42236b4cac81SBjoern A. Zeeb 		if (sta != NULL)
42246b4cac81SBjoern A. Zeeb 			break;
42256b4cac81SBjoern A. Zeeb 	}
42268891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
42276b4cac81SBjoern A. Zeeb 
42286b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
42296b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
42306b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
42316b4cac81SBjoern A. Zeeb 			return (NULL);
42326b4cac81SBjoern A. Zeeb 	}
42336b4cac81SBjoern A. Zeeb 
42346b4cac81SBjoern A. Zeeb 	return (sta);
42356b4cac81SBjoern A. Zeeb }
42366b4cac81SBjoern A. Zeeb 
42376b4cac81SBjoern A. Zeeb struct sk_buff *
42386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
42396b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
42406b4cac81SBjoern A. Zeeb {
42416b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
42426b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
42436b4cac81SBjoern A. Zeeb 
42446b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
42456b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
42466b4cac81SBjoern A. Zeeb 
42476b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
42486b4cac81SBjoern A. Zeeb 
42496b4cac81SBjoern A. Zeeb 	return (skb);
42506b4cac81SBjoern A. Zeeb }
42516b4cac81SBjoern A. Zeeb 
42526b4cac81SBjoern A. Zeeb void
42536b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
425486220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
42556b4cac81SBjoern A. Zeeb {
42566b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
42576b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
425886220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
42596b4cac81SBjoern A. Zeeb 
42606b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
42616b4cac81SBjoern A. Zeeb 
42626b4cac81SBjoern A. Zeeb 	fc = bc = 0;
42636b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
42646b4cac81SBjoern A. Zeeb 		fc++;
42656b4cac81SBjoern A. Zeeb 		bc += skb->len;
42666b4cac81SBjoern A. Zeeb 	}
42676b4cac81SBjoern A. Zeeb 	if (frame_cnt)
42686b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
42696b4cac81SBjoern A. Zeeb 	if (byte_cnt)
42706b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
42716b4cac81SBjoern A. Zeeb 
42726b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
42736b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
42746b4cac81SBjoern A. Zeeb 	IMPROVE();
42756b4cac81SBjoern A. Zeeb }
42766b4cac81SBjoern A. Zeeb 
42776b4cac81SBjoern A. Zeeb /*
42786b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
42796b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
42806b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
42816b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
42826b4cac81SBjoern A. Zeeb  */
4283*a8397571SBjoern A. Zeeb static void
4284*a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
42856b4cac81SBjoern A. Zeeb     int status)
42866b4cac81SBjoern A. Zeeb {
42876b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
42886b4cac81SBjoern A. Zeeb 	struct mbuf *m;
42896b4cac81SBjoern A. Zeeb 
42906b4cac81SBjoern A. Zeeb 	m = skb->m;
42916b4cac81SBjoern A. Zeeb 	skb->m = NULL;
42926b4cac81SBjoern A. Zeeb 
42936b4cac81SBjoern A. Zeeb 	if (m != NULL) {
42946b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
42956b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
42966b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
42976b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
42986b4cac81SBjoern A. Zeeb 	}
4299*a8397571SBjoern A. Zeeb }
43006b4cac81SBjoern A. Zeeb 
4301*a8397571SBjoern A. Zeeb void
4302*a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4303*a8397571SBjoern A. Zeeb     int status)
4304*a8397571SBjoern A. Zeeb {
4305*a8397571SBjoern A. Zeeb 
4306*a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
43076b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
43086b4cac81SBjoern A. Zeeb }
43096b4cac81SBjoern A. Zeeb 
4310383b3e8fSBjoern A. Zeeb void
4311*a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
4312*a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
4313383b3e8fSBjoern A. Zeeb {
4314*a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
4315383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
4316383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
4317383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
4318383b3e8fSBjoern A. Zeeb 	int status;
4319383b3e8fSBjoern A. Zeeb 
4320*a8397571SBjoern A. Zeeb 	skb = txstat->skb;
4321383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
4322383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
4323383b3e8fSBjoern A. Zeeb 
4324383b3e8fSBjoern A. Zeeb 		m = skb->m;
4325383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
4326383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
4327383b3e8fSBjoern A. Zeeb 	} else {
4328383b3e8fSBjoern A. Zeeb 		ni = NULL;
4329383b3e8fSBjoern A. Zeeb 	}
4330383b3e8fSBjoern A. Zeeb 
4331*a8397571SBjoern A. Zeeb 	info = txstat->info;
4332383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
4333383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
4334383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
4335383b3e8fSBjoern A. Zeeb 	} else {
4336383b3e8fSBjoern A. Zeeb 		status = 1;
4337383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
4338383b3e8fSBjoern A. Zeeb 	}
4339383b3e8fSBjoern A. Zeeb 
4340383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
4341e2c5ab09SJohn Baldwin 		int ridx __unused;
4342383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4343383b3e8fSBjoern A. Zeeb 		int old_rate;
4344383b3e8fSBjoern A. Zeeb 
4345383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
4346383b3e8fSBjoern A. Zeeb #endif
4347383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
4348383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
4349383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
4350383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
4351383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
4352383b3e8fSBjoern A. Zeeb 		}
4353383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
4354*a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
4355383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
4356383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
4357383b3e8fSBjoern A. Zeeb #endif
4358383b3e8fSBjoern A. Zeeb 		if (info->status.is_valid_ack_signal) {
4359383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
4360383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
4361383b3e8fSBjoern A. Zeeb 		}
4362383b3e8fSBjoern A. Zeeb 
4363383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
4364383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
4365383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
4366383b3e8fSBjoern A. Zeeb 
4367383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4368383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
4369383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
4370383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
4371383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
4372383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
4373383b3e8fSBjoern A. Zeeb 		}
4374383b3e8fSBjoern A. Zeeb #endif
4375383b3e8fSBjoern A. Zeeb 	}
4376383b3e8fSBjoern A. Zeeb 
4377383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4378383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4379383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
4380383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
4381383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
4382383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
4383383b3e8fSBjoern A. Zeeb 		    "tx_time %u is_valid_ack_signal %u "
4384383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
4385383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
4386383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
4387383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
4388383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
4389383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
4390383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
4391383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
4392383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
4393383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
4394383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
4395383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
4396383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
4397383b3e8fSBjoern A. Zeeb 		    info->status.tx_time, info->status.is_valid_ack_signal,
4398383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
4399383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
4400383b3e8fSBjoern A. Zeeb #endif
4401383b3e8fSBjoern A. Zeeb 
4402*a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
4403*a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
4404*a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
4405*a8397571SBjoern A. Zeeb 	} else {
4406383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
4407383b3e8fSBjoern A. Zeeb 	}
4408*a8397571SBjoern A. Zeeb }
4409*a8397571SBjoern A. Zeeb 
4410*a8397571SBjoern A. Zeeb void
4411*a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
4412*a8397571SBjoern A. Zeeb {
4413*a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
4414*a8397571SBjoern A. Zeeb 
4415*a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
4416*a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
4417*a8397571SBjoern A. Zeeb 	status.skb = skb;
4418*a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
4419*a8397571SBjoern A. Zeeb 
4420*a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
4421*a8397571SBjoern A. Zeeb }
4422383b3e8fSBjoern A. Zeeb 
44236b4cac81SBjoern A. Zeeb /*
44246b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
44256b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
44266b4cac81SBjoern A. Zeeb  * mbufs this should go away.
44276b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
44286b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
44296b4cac81SBjoern A. Zeeb  */
44306b4cac81SBjoern A. Zeeb static void
44316b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
44326b4cac81SBjoern A. Zeeb {
44336b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
44346b4cac81SBjoern A. Zeeb 	struct mbuf *m;
44356b4cac81SBjoern A. Zeeb 
44366b4cac81SBjoern A. Zeeb 	if (p == NULL)
44376b4cac81SBjoern A. Zeeb 		return;
44386b4cac81SBjoern A. Zeeb 
44396b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
44406b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
44416b4cac81SBjoern A. Zeeb 
44426b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
44436b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
44446b4cac81SBjoern A. Zeeb 	if (ni != NULL)
44456b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
44466b4cac81SBjoern A. Zeeb 	m_freem(m);
44476b4cac81SBjoern A. Zeeb }
44486b4cac81SBjoern A. Zeeb 
44496b4cac81SBjoern A. Zeeb void
44506b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
44516b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
44526b4cac81SBjoern A. Zeeb {
44536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44546b4cac81SBjoern A. Zeeb 
44556b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
44566b4cac81SBjoern A. Zeeb 	IMPROVE();
44576b4cac81SBjoern A. Zeeb 
44586b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
44596b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
44606b4cac81SBjoern A. Zeeb }
44616b4cac81SBjoern A. Zeeb 
44626b4cac81SBjoern A. Zeeb void
44636b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
44646b4cac81SBjoern A. Zeeb     struct work_struct *w)
44656b4cac81SBjoern A. Zeeb {
44666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44676b4cac81SBjoern A. Zeeb 
44686b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
44696b4cac81SBjoern A. Zeeb 	IMPROVE();
44706b4cac81SBjoern A. Zeeb 
44716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
44726b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
44736b4cac81SBjoern A. Zeeb }
44746b4cac81SBjoern A. Zeeb 
44756b4cac81SBjoern A. Zeeb struct sk_buff *
4476ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
4477ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
4478ade774b1SBjoern A. Zeeb {
4479ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
4480ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
4481ade774b1SBjoern A. Zeeb 	uint8_t *p;
4482ade774b1SBjoern A. Zeeb 	size_t len;
4483ade774b1SBjoern A. Zeeb 
4484ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
4485ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
4486ade774b1SBjoern A. Zeeb 
4487ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
4488ade774b1SBjoern A. Zeeb 	if (skb == NULL)
4489ade774b1SBjoern A. Zeeb 		return (NULL);
4490ade774b1SBjoern A. Zeeb 
4491ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
4492ade774b1SBjoern A. Zeeb 
4493ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
4494ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
4495ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
4496ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4497ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
4498ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4499ade774b1SBjoern A. Zeeb 
4500ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
4501ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
4502ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
4503ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
4504ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
4505ade774b1SBjoern A. Zeeb 
4506ade774b1SBjoern A. Zeeb 	return (skb);
4507ade774b1SBjoern A. Zeeb }
4508ade774b1SBjoern A. Zeeb 
4509ade774b1SBjoern A. Zeeb struct sk_buff *
45106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
45116b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
45126b4cac81SBjoern A. Zeeb {
45136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45146b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
45156b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
45166b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
45176b4cac81SBjoern A. Zeeb 	uint16_t v;
45186b4cac81SBjoern A. Zeeb 
45196b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
45206b4cac81SBjoern A. Zeeb 	if (skb == NULL)
45216b4cac81SBjoern A. Zeeb 		return (NULL);
45226b4cac81SBjoern A. Zeeb 
45236b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
45246b4cac81SBjoern A. Zeeb 
45256b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
45266b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
45276b4cac81SBjoern A. Zeeb 
45286b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
45296b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
45306b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
45316b4cac81SBjoern A. Zeeb 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
45326b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
45336b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
45346b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
45356b4cac81SBjoern A. Zeeb 
45366b4cac81SBjoern A. Zeeb 	return (skb);
45376b4cac81SBjoern A. Zeeb }
45386b4cac81SBjoern A. Zeeb 
45396b4cac81SBjoern A. Zeeb struct sk_buff *
45406b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
45416b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif, bool qos)
45426b4cac81SBjoern A. Zeeb {
45436b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45446b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
45456b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
45466b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
45476b4cac81SBjoern A. Zeeb 
45486b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
45496b4cac81SBjoern A. Zeeb 	if (skb == NULL)
45506b4cac81SBjoern A. Zeeb 		return (NULL);
45516b4cac81SBjoern A. Zeeb 
45526b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
45536b4cac81SBjoern A. Zeeb 
45546b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
45556b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
45566b4cac81SBjoern A. Zeeb 
45576b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
45586b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
45596b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
45606b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
45616b4cac81SBjoern A. Zeeb 
45626b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
45636b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
45646b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
45656b4cac81SBjoern A. Zeeb 
45666b4cac81SBjoern A. Zeeb 	return (skb);
45676b4cac81SBjoern A. Zeeb }
45686b4cac81SBjoern A. Zeeb 
45696b4cac81SBjoern A. Zeeb struct wireless_dev *
45706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
45716b4cac81SBjoern A. Zeeb {
45726b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45736b4cac81SBjoern A. Zeeb 
45746b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
45756b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
45766b4cac81SBjoern A. Zeeb }
45776b4cac81SBjoern A. Zeeb 
45786b4cac81SBjoern A. Zeeb void
45796b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
45806b4cac81SBjoern A. Zeeb {
45816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45826b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
45836b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
45846b4cac81SBjoern A. Zeeb 	int arg;
45856b4cac81SBjoern A. Zeeb 
45866b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
45876b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
45886b4cac81SBjoern A. Zeeb 
45896b4cac81SBjoern A. Zeeb 	/*
4590f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
45916b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
45926b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
45936b4cac81SBjoern A. Zeeb 	 */
4594f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
4595bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
45966b4cac81SBjoern A. Zeeb 
45979d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45989d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
45996b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
46009d9ba2b7SBjoern A. Zeeb #endif
46016b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
46026b4cac81SBjoern A. Zeeb }
46036b4cac81SBjoern A. Zeeb 
4604bb81db90SBjoern A. Zeeb void
4605bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4606bb81db90SBjoern A. Zeeb {
4607bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4608bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
4609bb81db90SBjoern A. Zeeb 
4610bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
4611bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
4612bb81db90SBjoern A. Zeeb 
46139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4615bb81db90SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4616bb81db90SBjoern A. Zeeb 		    vif, vap, ieee80211_state_name[vap->iv_state]);
46179d9ba2b7SBjoern A. Zeeb #endif
46183540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
4619bb81db90SBjoern A. Zeeb }
4620bb81db90SBjoern A. Zeeb 
46215edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
46225edde07cSBjoern A. Zeeb 
46235edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
46245edde07cSBjoern A. Zeeb 	u_int refcnt;
46255edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
46265edde07cSBjoern A. Zeeb };
46275edde07cSBjoern A. Zeeb 
46285edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
46295edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
46305edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
46315edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
46325edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
46335edde07cSBjoern A. Zeeb 	size_t ssid_len;
46345edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
46355edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
46365edde07cSBjoern A. Zeeb 
46375edde07cSBjoern A. Zeeb 	/*
46385edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
46395edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
46405edde07cSBjoern A. Zeeb 	 */
46415edde07cSBjoern A. Zeeb 	bool match;
46425edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
46435edde07cSBjoern A. Zeeb };
46445edde07cSBjoern A. Zeeb 
46455edde07cSBjoern A. Zeeb static void
46465edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
46475edde07cSBjoern A. Zeeb {
46485edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
46495edde07cSBjoern A. Zeeb 	size_t ielen;
46505edde07cSBjoern A. Zeeb 
46515edde07cSBjoern A. Zeeb 	lookup = arg;
46525edde07cSBjoern A. Zeeb 
46535edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
46545edde07cSBjoern A. Zeeb 	if (lookup->match)
46555edde07cSBjoern A. Zeeb 		return;
46565edde07cSBjoern A. Zeeb 
46575edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
46585edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
46595edde07cSBjoern A. Zeeb 		return;
46605edde07cSBjoern A. Zeeb 
46615edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
46625edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
46635edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
46645edde07cSBjoern A. Zeeb 		return;
46655edde07cSBjoern A. Zeeb 	}
46665edde07cSBjoern A. Zeeb 
46675edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
46685edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
46695edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
46705edde07cSBjoern A. Zeeb 		return;
46715edde07cSBjoern A. Zeeb 	}
46725edde07cSBjoern A. Zeeb 
46735edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
46745edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
46755edde07cSBjoern A. Zeeb 
46765edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
46775edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
46785edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
46795edde07cSBjoern A. Zeeb 			return;
46805edde07cSBjoern A. Zeeb 	}
46815edde07cSBjoern A. Zeeb 
46825edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
46835edde07cSBjoern A. Zeeb 		return;
46845edde07cSBjoern A. Zeeb 
46855edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
46865edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
46875edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
46885edde07cSBjoern A. Zeeb 			return;
46895edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
46905edde07cSBjoern A. Zeeb 			return;
46915edde07cSBjoern A. Zeeb 	}
46925edde07cSBjoern A. Zeeb 
46935edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
46945edde07cSBjoern A. Zeeb 
46955edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
46965edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
46975edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
46985edde07cSBjoern A. Zeeb 		return;
46995edde07cSBjoern A. Zeeb 
47005edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
47015edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
47025edde07cSBjoern A. Zeeb 	if (ielen)
47035edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
47045edde07cSBjoern A. Zeeb 
47055edde07cSBjoern A. Zeeb 	lookup->match = true;
47065edde07cSBjoern A. Zeeb }
47075edde07cSBjoern A. Zeeb 
47085edde07cSBjoern A. Zeeb struct cfg80211_bss *
47095edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
47105edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
47115edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
47125edde07cSBjoern A. Zeeb {
47135edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
47145edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
47155edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
47165edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
47175edde07cSBjoern A. Zeeb 
47185edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
47195edde07cSBjoern A. Zeeb 
47205edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
47215edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
47225edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
47235edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
47245edde07cSBjoern A. Zeeb 		return (NULL);
47255edde07cSBjoern A. Zeeb 	}
47265edde07cSBjoern A. Zeeb 
47275edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
47285edde07cSBjoern A. Zeeb 	lookup.chan = chan;
47295edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
47305edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
47315edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
47325edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
47335edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
47345edde07cSBjoern A. Zeeb 	lookup.match = false;
47355edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
47365edde07cSBjoern A. Zeeb 
47375edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
47385edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
47395edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
47405edde07cSBjoern A. Zeeb 	if (!lookup.match) {
47415edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
47425edde07cSBjoern A. Zeeb 		return (NULL);
47435edde07cSBjoern A. Zeeb 	}
47445edde07cSBjoern A. Zeeb 
47455edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
47465edde07cSBjoern A. Zeeb 	return (&lbss->bss);
47475edde07cSBjoern A. Zeeb }
47485edde07cSBjoern A. Zeeb 
47495edde07cSBjoern A. Zeeb void
47505edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
47515edde07cSBjoern A. Zeeb {
47525edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
47535edde07cSBjoern A. Zeeb 
47545edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
47555edde07cSBjoern A. Zeeb 
47565edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
47575edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
47585edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
47595edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
47605edde07cSBjoern A. Zeeb 	}
47615edde07cSBjoern A. Zeeb }
47625edde07cSBjoern A. Zeeb 
47635edde07cSBjoern A. Zeeb void
47645edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
47655edde07cSBjoern A. Zeeb {
47665edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
47675edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
47685edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
47695edde07cSBjoern A. Zeeb 
47705edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
47715edde07cSBjoern A. Zeeb 	ic = lhw->ic;
47725edde07cSBjoern A. Zeeb 
47735edde07cSBjoern A. Zeeb 	/*
47745edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
47755edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
47765edde07cSBjoern A. Zeeb 	 */
47775edde07cSBjoern A. Zeeb 	if (ic == NULL ||
47785edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
47795edde07cSBjoern A. Zeeb 		return;
47805edde07cSBjoern A. Zeeb 
47815edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
47825edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
47835edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
47845edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
47855edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
47865edde07cSBjoern A. Zeeb }
47875edde07cSBjoern A. Zeeb 
47885edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
47895edde07cSBjoern A. Zeeb 
47906b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
47916b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
47926b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
4793