xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision d9f59799fc3e7940c47aa674c25994e640eae45e)
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>
566b4cac81SBjoern A. Zeeb 
576b4cac81SBjoern A. Zeeb #include <net/if.h>
586b4cac81SBjoern A. Zeeb #include <net/if_var.h>
596b4cac81SBjoern A. Zeeb #include <net/if_media.h>
606b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
646b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
656b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
686b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
696b4cac81SBjoern A. Zeeb 
706b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
716b4cac81SBjoern A. Zeeb #include "linux_80211.h"
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "Linux KPI 80211 compat");
746b4cac81SBjoern A. Zeeb 
756b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
766b4cac81SBjoern A. Zeeb 
776b4cac81SBjoern A. Zeeb int debug_80211;
786b4cac81SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_80211, CTLFLAG_RWTUN,
796b4cac81SBjoern A. Zeeb     &debug_80211, 0, "80211 debug Level");
806b4cac81SBjoern A. Zeeb 
816b4cac81SBjoern A. Zeeb #define LINUXKPI_DEBUG_80211
826b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
836b4cac81SBjoern A. Zeeb #ifndef	D80211_TODO
846b4cac81SBjoern A. Zeeb #define	D80211_TODO		0x1
856b4cac81SBjoern A. Zeeb #endif
866b4cac81SBjoern A. Zeeb #ifndef D80211_IMPROVE
876b4cac81SBjoern A. Zeeb #define	D80211_IMPROVE		0x2
886b4cac81SBjoern A. Zeeb #endif
896b4cac81SBjoern A. Zeeb #define	D80211_TRACE		0x10
906b4cac81SBjoern A. Zeeb #define	D80211_TRACEOK		0x20
916b4cac81SBjoern A. Zeeb #define	D80211_TRACE_TX		0x100
926b4cac81SBjoern A. Zeeb #define	D80211_TRACE_TX_DUMP	0x200
936b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX		0x1000
946b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX_DUMP	0x2000
956b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX_BEACONS	0x4000
966b4cac81SBjoern A. Zeeb #define	D80211_TRACEX		(D80211_TRACE_TX|D80211_TRACE_RX)
976b4cac81SBjoern A. Zeeb #define	D80211_TRACEX_DUMP	(D80211_TRACE_TX_DUMP|D80211_TRACE_RX_DUMP)
98*d9f59799SBjoern A. Zeeb #define	D80211_TRACE_STA	0x10000
996b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		if (debug_80211 & D80211_TODO)		\
1006b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1016b4cac81SBjoern A. Zeeb #define	TRACEOK()		if (debug_80211 & D80211_TRACEOK)	\
1026b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1036b4cac81SBjoern A. Zeeb #else
1046b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1056b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1066b4cac81SBjoern A. Zeeb #endif
1076b4cac81SBjoern A. Zeeb 
1086b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1096b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1106b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1116b4cac81SBjoern A. Zeeb #endif
1126b4cac81SBjoern A. Zeeb 
1136b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1146b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1156b4cac81SBjoern A. Zeeb 
1164f61ef8bSBjoern A. Zeeb const uint8_t tid_to_mac80211_ac[] = {
1174f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1184f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1194f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1204f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1214f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1224f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1234f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1244f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1254f61ef8bSBjoern A. Zeeb #if 0
1264f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1274f61ef8bSBjoern A. Zeeb #endif
1284f61ef8bSBjoern A. Zeeb };
1294f61ef8bSBjoern A. Zeeb 
1306b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1316b4cac81SBjoern A. Zeeb 	/*
1326b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1336b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1346b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1356b4cac81SBjoern A. Zeeb 	 */
1366b4cac81SBjoern A. Zeeb };
1376b4cac81SBjoern A. Zeeb 
1386b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1396b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
1406b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
1416b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1426b4cac81SBjoern A. Zeeb 
143*d9f59799SBjoern A. Zeeb static void
144*d9f59799SBjoern A. Zeeb lkpi_dump_lsta(struct lkpi_sta *lsta, const char *_f, int _l)
145*d9f59799SBjoern A. Zeeb {
146*d9f59799SBjoern A. Zeeb 
147*d9f59799SBjoern A. Zeeb 	if ((debug_80211 & D80211_TRACE_STA) == 0)
148*d9f59799SBjoern A. Zeeb 		return;
149*d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
150*d9f59799SBjoern A. Zeeb 		return;
151*d9f59799SBjoern A. Zeeb 
152*d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
153*d9f59799SBjoern A. Zeeb 	    _f, _l, lsta, lsta->ni, &lsta->sta);
154*d9f59799SBjoern A. Zeeb 	if (lsta->ni != NULL)
155*d9f59799SBjoern A. Zeeb 		ieee80211_dump_node(NULL, lsta->ni);
156*d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
157*d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
158*d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
159*d9f59799SBjoern A. Zeeb }
160*d9f59799SBjoern A. Zeeb 
161*d9f59799SBjoern A. Zeeb static void
162*d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
163*d9f59799SBjoern A. Zeeb {
164*d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
165*d9f59799SBjoern A. Zeeb 
166*d9f59799SBjoern A. Zeeb 	ni = lsta->ni;
167*d9f59799SBjoern A. Zeeb 
168*d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
169*d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
170*d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
171*d9f59799SBjoern A. Zeeb 
172*d9f59799SBjoern A. Zeeb 	lsta->ni = NULL;
173*d9f59799SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
174*d9f59799SBjoern A. Zeeb 	ieee80211_free_node(ni);
175*d9f59799SBjoern A. Zeeb 
176*d9f59799SBjoern A. Zeeb 	IMPROVE("free lsta here?  We won't have a pointer to it from the node anymore.");
177*d9f59799SBjoern A. Zeeb }
178*d9f59799SBjoern A. Zeeb 
1794f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
1804f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1814f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
1824f61ef8bSBjoern A. Zeeb {
1834f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
1844f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1854f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
1864f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
1874f61ef8bSBjoern A. Zeeb 	int tid;
1884f61ef8bSBjoern A. Zeeb 
1894f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
1904f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
1914f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
1924f61ef8bSBjoern A. Zeeb 		return (NULL);
1934f61ef8bSBjoern A. Zeeb 
1944f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
1954f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
1964f61ef8bSBjoern A. Zeeb #if 0
1974f61ef8bSBjoern A. Zeeb 	/*
1984f61ef8bSBjoern A. Zeeb 	 * This needs to be done in node_init() as ieee80211_alloc_node()
1994f61ef8bSBjoern A. Zeeb 	 * will initialise the refcount after us.
2004f61ef8bSBjoern A. Zeeb 	 */
2014f61ef8bSBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
2024f61ef8bSBjoern A. Zeeb #endif
2034f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2044f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2054f61ef8bSBjoern A. Zeeb 
2064f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2074f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2084f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2094f61ef8bSBjoern A. Zeeb 
2104f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
2114f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
2124f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
2134f61ef8bSBjoern A. Zeeb 
2144f61ef8bSBjoern A. Zeeb 		/*
2154f61ef8bSBjoern A. Zeeb 		 * We are neither limiting ourselves to hw.queues here,
2164f61ef8bSBjoern A. Zeeb 		 * nor do we check if driver wants IEEE80211_NUM_TIDS queue.
2174f61ef8bSBjoern A. Zeeb 		 */
2184f61ef8bSBjoern A. Zeeb 
2194f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
2204f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
2214f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
2224f61ef8bSBjoern A. Zeeb 			goto cleanup;
2234f61ef8bSBjoern A. Zeeb 		ltxq->seen_dequeue = false;
2244f61ef8bSBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
2254f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
2264f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
2274f61ef8bSBjoern A. Zeeb 			IMPROVE();
2284f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
2294f61ef8bSBjoern A. Zeeb 		} else {
2304f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = tid_to_mac80211_ac[tid & 7];
2314f61ef8bSBjoern A. Zeeb 		}
2324f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
2334f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
2344f61ef8bSBjoern A. Zeeb 		ltxq->txq.vif = vif;
2354f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
2364f61ef8bSBjoern A. Zeeb 	}
2374f61ef8bSBjoern A. Zeeb 
2384f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
2394f61ef8bSBjoern A. Zeeb 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
2404f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
2414f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
2424f61ef8bSBjoern A. Zeeb 
2434f61ef8bSBjoern A. Zeeb 	return (lsta);
2444f61ef8bSBjoern A. Zeeb 
2454f61ef8bSBjoern A. Zeeb cleanup:
2464f61ef8bSBjoern A. Zeeb 	for (; tid >= 0; tid--)
2474f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
2484f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
2494f61ef8bSBjoern A. Zeeb 	return (NULL);
2504f61ef8bSBjoern A. Zeeb }
2514f61ef8bSBjoern A. Zeeb 
2526b4cac81SBjoern A. Zeeb static enum nl80211_band
2536b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
2546b4cac81SBjoern A. Zeeb {
2556b4cac81SBjoern A. Zeeb 
2566b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
2576b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
2586b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
2596b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
2606b4cac81SBjoern A. Zeeb #ifdef __notyet__
2616b4cac81SBjoern A. Zeeb 	else if ()
2626b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
2636b4cac81SBjoern A. Zeeb 	else if ()
2646b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
2656b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
2666b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
2676b4cac81SBjoern A. Zeeb #endif
2686b4cac81SBjoern A. Zeeb 	else
2696b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
2706b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
2716b4cac81SBjoern A. Zeeb }
2726b4cac81SBjoern A. Zeeb 
2736b4cac81SBjoern A. Zeeb static uint32_t
2746b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
2756b4cac81SBjoern A. Zeeb {
2766b4cac81SBjoern A. Zeeb 
2776b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
2786b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
2796b4cac81SBjoern A. Zeeb 	switch (band) {
2806b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
2816b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
2826b4cac81SBjoern A. Zeeb 		break;
2836b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
2846b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
2856b4cac81SBjoern A. Zeeb 		break;
2866b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
2876b4cac81SBjoern A. Zeeb 		break;
2886b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
2896b4cac81SBjoern A. Zeeb 		break;
2906b4cac81SBjoern A. Zeeb 	default:
2916b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
2926b4cac81SBjoern A. Zeeb 		break;
2936b4cac81SBjoern A. Zeeb 	}
2946b4cac81SBjoern A. Zeeb 
2956b4cac81SBjoern A. Zeeb 	IMPROVE();
2966b4cac81SBjoern A. Zeeb 	return (0x00);
2976b4cac81SBjoern A. Zeeb }
2986b4cac81SBjoern A. Zeeb 
2996b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
3006b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
3016b4cac81SBjoern A. Zeeb {
3026b4cac81SBjoern A. Zeeb 
3036b4cac81SBjoern A. Zeeb 	switch (ac) {
3046b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
3056b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
3066b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
3076b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
3086b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
3096b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
3106b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
3116b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
3126b4cac81SBjoern A. Zeeb 	default:
3136b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
3146b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
3156b4cac81SBjoern A. Zeeb 	}
3166b4cac81SBjoern A. Zeeb }
3176b4cac81SBjoern A. Zeeb 
3186b4cac81SBjoern A. Zeeb static enum nl80211_iftype
3196b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
3206b4cac81SBjoern A. Zeeb {
3216b4cac81SBjoern A. Zeeb 
3226b4cac81SBjoern A. Zeeb 	switch (opmode) {
3236b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
3246b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
3256b4cac81SBjoern A. Zeeb 		break;
3266b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
3276b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
3286b4cac81SBjoern A. Zeeb 		break;
3296b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
3306b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
3316b4cac81SBjoern A. Zeeb 		break;
3326b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
3336b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
3346b4cac81SBjoern A. Zeeb 		break;
3356b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
3366b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
3376b4cac81SBjoern A. Zeeb 		break;
3386b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
3396b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
3406b4cac81SBjoern A. Zeeb 		break;
3416b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
3426b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
3436b4cac81SBjoern A. Zeeb 	default:
3446b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
3456b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
3466b4cac81SBjoern A. Zeeb 	}
3476b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
3486b4cac81SBjoern A. Zeeb }
3496b4cac81SBjoern A. Zeeb 
3506b4cac81SBjoern A. Zeeb #ifdef __notyet__
3516b4cac81SBjoern A. Zeeb static uint32_t
3526b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
3536b4cac81SBjoern A. Zeeb {
3546b4cac81SBjoern A. Zeeb 
3556b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
3566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
3576b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
3586b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
3596b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
3606b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
3616b4cac81SBjoern A. Zeeb 		return (IEEE80211_CIPHER_AES_CCM);
3626b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
3636b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
3646b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
3656b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
3666b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
3676b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
3686b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3696b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3706b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3716b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
3726b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
3736b4cac81SBjoern A. Zeeb 		break;
3746b4cac81SBjoern A. Zeeb 	default:
3756b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
3766b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
3776b4cac81SBjoern A. Zeeb 	}
3786b4cac81SBjoern A. Zeeb 
3796b4cac81SBjoern A. Zeeb 	return (0);
3806b4cac81SBjoern A. Zeeb }
3816b4cac81SBjoern A. Zeeb #endif
3826b4cac81SBjoern A. Zeeb 
3836b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
3846b4cac81SBjoern A. Zeeb static uint32_t
3856b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
3866b4cac81SBjoern A. Zeeb {
3876b4cac81SBjoern A. Zeeb 
3886b4cac81SBjoern A. Zeeb 	switch (cipher) {
3896b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
3906b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
3916b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
3926b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
3936b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
3946b4cac81SBjoern A. Zeeb 		if (keylen < 8)
3956b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
3966b4cac81SBjoern A. Zeeb 		else
3976b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
3986b4cac81SBjoern A. Zeeb 		break;
3996b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
4006b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
4016b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
4026b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
4036b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
4046b4cac81SBjoern A. Zeeb 		break;
4056b4cac81SBjoern A. Zeeb 	default:
4066b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
4076b4cac81SBjoern A. Zeeb 	};
4086b4cac81SBjoern A. Zeeb 	return (0);
4096b4cac81SBjoern A. Zeeb }
4106b4cac81SBjoern A. Zeeb #endif
4116b4cac81SBjoern A. Zeeb 
4126b4cac81SBjoern A. Zeeb #ifdef __notyet__
4136b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
4146b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
4156b4cac81SBjoern A. Zeeb {
4166b4cac81SBjoern A. Zeeb 
4176b4cac81SBjoern A. Zeeb 	/*
4186b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
4196b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
4206b4cac81SBjoern A. Zeeb 	 */
4216b4cac81SBjoern A. Zeeb 	switch (state) {
4226b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
4236b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
4246b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
4256b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
4266b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
4276b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
4286b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
4296b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
4306b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
4316b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
4326b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
4336b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
4346b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
4356b4cac81SBjoern A. Zeeb 	default:
4366b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
4376b4cac81SBjoern A. Zeeb 	};
4386b4cac81SBjoern A. Zeeb 
4396b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
4406b4cac81SBjoern A. Zeeb }
4416b4cac81SBjoern A. Zeeb #endif
4426b4cac81SBjoern A. Zeeb 
4436b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
4446b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
4456b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
4466b4cac81SBjoern A. Zeeb {
4476b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4486b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
4496b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
4506b4cac81SBjoern A. Zeeb 	int i, nchans;
4516b4cac81SBjoern A. Zeeb 
4526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4536b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
4546b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
4556b4cac81SBjoern A. Zeeb 		return (NULL);
4566b4cac81SBjoern A. Zeeb 
4576b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
4586b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
4596b4cac81SBjoern A. Zeeb 		return (NULL);
4606b4cac81SBjoern A. Zeeb 
4616b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
4626b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
4636b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
4646b4cac81SBjoern A. Zeeb 			return (&channels[i]);
4656b4cac81SBjoern A. Zeeb 	}
4666b4cac81SBjoern A. Zeeb 
4676b4cac81SBjoern A. Zeeb 	return (NULL);
4686b4cac81SBjoern A. Zeeb }
4696b4cac81SBjoern A. Zeeb 
4706b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
4716b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
4726b4cac81SBjoern A. Zeeb {
4736b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
4746b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
4756b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4766b4cac81SBjoern A. Zeeb 
4776b4cac81SBjoern A. Zeeb 	chan = NULL;
4786b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
4796b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
4806b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
4816b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
4826b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
4836b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
4846b4cac81SBjoern A. Zeeb 	else
4856b4cac81SBjoern A. Zeeb 		c = NULL;
4866b4cac81SBjoern A. Zeeb 
4876b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
4886b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
4896b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
4906b4cac81SBjoern A. Zeeb 	}
4916b4cac81SBjoern A. Zeeb 
4926b4cac81SBjoern A. Zeeb 	return (chan);
4936b4cac81SBjoern A. Zeeb }
4946b4cac81SBjoern A. Zeeb 
4952e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
4962e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
4972e183d99SBjoern A. Zeeb {
4982e183d99SBjoern A. Zeeb 	enum nl80211_band band;
4992e183d99SBjoern A. Zeeb 
5002e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5012e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5022e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5032e183d99SBjoern A. Zeeb 		int i;
5042e183d99SBjoern A. Zeeb 
5052e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
5062e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5072e183d99SBjoern A. Zeeb 			continue;
5082e183d99SBjoern A. Zeeb 
5092e183d99SBjoern A. Zeeb 		channels = supband->channels;
5102e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5112e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
5122e183d99SBjoern A. Zeeb 				return (&channels[i]);
5132e183d99SBjoern A. Zeeb 		}
5142e183d99SBjoern A. Zeeb 	}
5152e183d99SBjoern A. Zeeb 
5162e183d99SBjoern A. Zeeb 	return (NULL);
5172e183d99SBjoern A. Zeeb }
5182e183d99SBjoern A. Zeeb 
5196b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
5206b4cac81SBjoern A. Zeeb static int
5216b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
5226b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
5236b4cac81SBjoern A. Zeeb {
5246b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
5256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5296b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5306b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
5316b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
5326b4cac81SBjoern A. Zeeb 	int error;
5336b4cac81SBjoern A. Zeeb 
5346b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
5356b4cac81SBjoern A. Zeeb 
5366b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
5376b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
5386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5396b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5406b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5416b4cac81SBjoern A. Zeeb 
5426b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
5436b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
5446b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
5456b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
5466b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
5476b4cac81SBjoern A. Zeeb #if 0
5486b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
5496b4cac81SBjoern A. Zeeb #endif
5506b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
5516b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
5526b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
5536b4cac81SBjoern A. Zeeb 
5546b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
5556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
5566b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
5576b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
5586b4cac81SBjoern A. Zeeb 		break;
5596b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5606b4cac81SBjoern A. Zeeb 	default:
5616b4cac81SBjoern A. Zeeb 		IMPROVE();
5626b4cac81SBjoern A. Zeeb 		return (0);
5636b4cac81SBjoern A. Zeeb 	};
5646b4cac81SBjoern A. Zeeb 
5656b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
5666b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
5676b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
5686b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
5696b4cac81SBjoern A. Zeeb 
5706b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
5716b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
5726b4cac81SBjoern A. Zeeb 	}
5736b4cac81SBjoern A. Zeeb 
5746b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
5756b4cac81SBjoern A. Zeeb 	if (error != 0) {
5766b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
5776b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
5786b4cac81SBjoern A. Zeeb 		return (0);
5796b4cac81SBjoern A. Zeeb 	} else {
5806b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
5816b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
5826b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
5836b4cac81SBjoern A. Zeeb 		return (1);
5846b4cac81SBjoern A. Zeeb 	}
5856b4cac81SBjoern A. Zeeb }
5866b4cac81SBjoern A. Zeeb 
5876b4cac81SBjoern A. Zeeb static int
5886b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
5896b4cac81SBjoern A. Zeeb {
5906b4cac81SBjoern A. Zeeb 
5916b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
5926b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
5936b4cac81SBjoern A. Zeeb }
5946b4cac81SBjoern A. Zeeb static  int
5956b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
5966b4cac81SBjoern A. Zeeb {
5976b4cac81SBjoern A. Zeeb 
5986b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
5996b4cac81SBjoern A. Zeeb }
6006b4cac81SBjoern A. Zeeb #endif
6016b4cac81SBjoern A. Zeeb 
6026b4cac81SBjoern A. Zeeb static u_int
6036b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6046b4cac81SBjoern A. Zeeb {
6056b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
6066b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
6076b4cac81SBjoern A. Zeeb 
6086b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
6096b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
6106b4cac81SBjoern A. Zeeb 
6116b4cac81SBjoern A. Zeeb 	mc_list = arg;
6126b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
6136b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
6146b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
6156b4cac81SBjoern A. Zeeb 			return (0);
6166b4cac81SBjoern A. Zeeb 	}
6176b4cac81SBjoern A. Zeeb 
6186b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
6196b4cac81SBjoern A. Zeeb 	if (addr == NULL)
6206b4cac81SBjoern A. Zeeb 		return (0);
6216b4cac81SBjoern A. Zeeb 
6226b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
6236b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
6246b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
6256b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
6266b4cac81SBjoern A. Zeeb 	mc_list->count++;
6276b4cac81SBjoern A. Zeeb 
6286b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
6296b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
6306b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
6316b4cac81SBjoern A. Zeeb 
6326b4cac81SBjoern A. Zeeb 	return (1);
6336b4cac81SBjoern A. Zeeb }
6346b4cac81SBjoern A. Zeeb 
6356b4cac81SBjoern A. Zeeb static void
6366b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
6376b4cac81SBjoern A. Zeeb {
6386b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6396b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6406b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
6416b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
6426b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
6436b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
6446b4cac81SBjoern A. Zeeb 	u64 mc;
6456b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
6466b4cac81SBjoern A. Zeeb 
6476b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
6486b4cac81SBjoern A. Zeeb 
6496b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
6506b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
6516b4cac81SBjoern A. Zeeb 		return;
6526b4cac81SBjoern A. Zeeb 
6536b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
6546b4cac81SBjoern A. Zeeb 		return;
6556b4cac81SBjoern A. Zeeb 
6566b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
6576b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
6586b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
6596b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
6606b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6616b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
6626b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
6636b4cac81SBjoern A. Zeeb 	} else {
6646b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
6656b4cac81SBjoern A. Zeeb 	}
6666b4cac81SBjoern A. Zeeb 
6676b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6686b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
6696b4cac81SBjoern A. Zeeb 	/*
6706b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
6716b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
6726b4cac81SBjoern A. Zeeb 	 */
6736b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
6746b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
6756b4cac81SBjoern A. Zeeb 
6766b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
6776b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
6786b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
6796b4cac81SBjoern A. Zeeb 
6806b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
6816b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
6826b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
6836b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
6846b4cac81SBjoern A. Zeeb 			mc_list.count--;
6856b4cac81SBjoern A. Zeeb 		}
6866b4cac81SBjoern A. Zeeb 	}
6876b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
6886b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
6896b4cac81SBjoern A. Zeeb }
6906b4cac81SBjoern A. Zeeb 
691fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
692fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
693fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
694fa8f007dSBjoern A. Zeeb {
695fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
696fa8f007dSBjoern A. Zeeb 
697fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
698fa8f007dSBjoern A. Zeeb 
699fa8f007dSBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
700fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
701fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
702fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
703fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
704fa8f007dSBjoern A. Zeeb 			vif->bss_conf.assoc, vif->bss_conf.aid,
705fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
706fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
707fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
708fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
709fa8f007dSBjoern A. Zeeb 			bss_changed);
710fa8f007dSBjoern A. Zeeb 
711fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
712fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
713fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
714fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
715fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
716fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
717fa8f007dSBjoern A. Zeeb 	}
718fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
719fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
720fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
721fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
722fa8f007dSBjoern A. Zeeb 	}
723fa8f007dSBjoern A. Zeeb 
724fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
725fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
726fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
727fa8f007dSBjoern A. Zeeb 
728fa8f007dSBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
729fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
730fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
731fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
732fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
733fa8f007dSBjoern A. Zeeb 			vif->bss_conf.assoc, vif->bss_conf.aid,
734fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
735fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
736fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
737fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
738fa8f007dSBjoern A. Zeeb 			bss_changed);
739fa8f007dSBjoern A. Zeeb 
740fa8f007dSBjoern A. Zeeb 	return (bss_changed);
741fa8f007dSBjoern A. Zeeb }
742fa8f007dSBjoern A. Zeeb 
7436b4cac81SBjoern A. Zeeb static void
7446b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
7456b4cac81SBjoern A. Zeeb {
7466b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7476b4cac81SBjoern A. Zeeb 	int error;
7486b4cac81SBjoern A. Zeeb 
7496b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0)
7506b4cac81SBjoern A. Zeeb 		return;
7516b4cac81SBjoern A. Zeeb 
7526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7536b4cac81SBjoern A. Zeeb 
754bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
755bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
7566b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
7576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
7586b4cac81SBjoern A. Zeeb 
7596b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
7606b4cac81SBjoern A. Zeeb 	error = msleep(lhw, &lhw->mtx, 0, "lhwscanstop", hz/2);
761bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
762bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
7636b4cac81SBjoern A. Zeeb 
7646b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0)
7656b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
7666b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
7676b4cac81SBjoern A. Zeeb }
7686b4cac81SBjoern A. Zeeb 
7696b4cac81SBjoern A. Zeeb static void
770086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
771086be6a8SBjoern A. Zeeb {
772086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
773086be6a8SBjoern A. Zeeb 	int error;
774086be6a8SBjoern A. Zeeb 	bool old;
775086be6a8SBjoern A. Zeeb 
776086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
777086be6a8SBjoern A. Zeeb 	if (old == new)
778086be6a8SBjoern A. Zeeb 		return;
779086be6a8SBjoern A. Zeeb 
780086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
781086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
782086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
783086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
784086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
785086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
786086be6a8SBjoern A. Zeeb 	}
787086be6a8SBjoern A. Zeeb }
788086be6a8SBjoern A. Zeeb 
789086be6a8SBjoern A. Zeeb static void
7906b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
7916b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
7926b4cac81SBjoern A. Zeeb {
7936b4cac81SBjoern A. Zeeb 	sta->aid = 0;
7946b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.assoc) {
7956b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
7966b4cac81SBjoern A. Zeeb 		enum ieee80211_bss_changed changed;
7976b4cac81SBjoern A. Zeeb 
7986b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
7996b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
8006b4cac81SBjoern A. Zeeb 
8016b4cac81SBjoern A. Zeeb 		changed = 0;
8026b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = false;
8036b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = 0;
8046b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
805*d9f59799SBjoern A. Zeeb 		/*
806*d9f59799SBjoern A. Zeeb 		 * This will remove the sta from firmware for iwlwifi.
807*d9f59799SBjoern A. Zeeb 		 * So confusing that they use state and flags and ... ^%$%#%$^.
808*d9f59799SBjoern A. Zeeb 		 */
8096b4cac81SBjoern A. Zeeb 		IMPROVE();
8106b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
8116b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
8126b4cac81SBjoern A. Zeeb 		    changed);
813086be6a8SBjoern A. Zeeb 
814086be6a8SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
8156b4cac81SBjoern A. Zeeb 	}
8166b4cac81SBjoern A. Zeeb }
8176b4cac81SBjoern A. Zeeb 
8186b4cac81SBjoern A. Zeeb static void
8196b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
8206b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
8216b4cac81SBjoern A. Zeeb {
8226b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
8236b4cac81SBjoern A. Zeeb 	int tid;
8246b4cac81SBjoern A. Zeeb 
8256b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
8266b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
8276b4cac81SBjoern A. Zeeb 
8286b4cac81SBjoern A. Zeeb 			if (tid == IEEE80211_NUM_TIDS) {
8296b4cac81SBjoern A. Zeeb 				IMPROVE("station specific?");
8306b4cac81SBjoern A. Zeeb 				if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
8316b4cac81SBjoern A. Zeeb 					continue;
8326b4cac81SBjoern A. Zeeb 			} else if (tid >= hw->queues)
8336b4cac81SBjoern A. Zeeb 				continue;
8346b4cac81SBjoern A. Zeeb 
8356b4cac81SBjoern A. Zeeb 			if (sta->txq[tid] == NULL)
8366b4cac81SBjoern A. Zeeb 				continue;
8376b4cac81SBjoern A. Zeeb 
8386b4cac81SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8396b4cac81SBjoern A. Zeeb 			if (dequeue_seen && !ltxq->seen_dequeue)
8406b4cac81SBjoern A. Zeeb 				continue;
8416b4cac81SBjoern A. Zeeb 
8426b4cac81SBjoern A. Zeeb 			if (no_emptyq && skb_queue_empty(&ltxq->skbq))
8436b4cac81SBjoern A. Zeeb 				continue;
8446b4cac81SBjoern A. Zeeb 
8456b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
8466b4cac81SBjoern A. Zeeb 	}
8476b4cac81SBjoern A. Zeeb }
8486b4cac81SBjoern A. Zeeb 
8496b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8506b4cac81SBjoern A. Zeeb 
8516b4cac81SBjoern A. Zeeb static int
8526b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
8536b4cac81SBjoern A. Zeeb {
8546b4cac81SBjoern A. Zeeb 
8556b4cac81SBjoern A. Zeeb 	return (0);
8566b4cac81SBjoern A. Zeeb }
8576b4cac81SBjoern A. Zeeb 
8586b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
8596b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
8606b4cac81SBjoern A. Zeeb 
8616b4cac81SBjoern A. Zeeb static int
8626b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
8636b4cac81SBjoern A. Zeeb {
8646b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
8656b4cac81SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *conf;
8666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8676b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8686b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
8696b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
8706b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
8716b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
8726b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
8736b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
8746b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
8756b4cac81SBjoern A. Zeeb 	uint32_t changed;
8766b4cac81SBjoern A. Zeeb 	int error;
8776b4cac81SBjoern A. Zeeb 
8786b4cac81SBjoern A. Zeeb 	chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
8796b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
8806b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
8816b4cac81SBjoern A. Zeeb 		return (ESRCH);
8826b4cac81SBjoern A. Zeeb 	}
8836b4cac81SBjoern A. Zeeb 
8846b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
8856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8866b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
8876b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
8886b4cac81SBjoern A. Zeeb 
889*d9f59799SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
890*d9f59799SBjoern A. Zeeb 
8916b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
8926b4cac81SBjoern A. Zeeb 
8936b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
8946b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
8956b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
8966b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
8976b4cac81SBjoern A. Zeeb 	} else {
8986b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
8996b4cac81SBjoern A. Zeeb 		conf = malloc(sizeof(*conf) + hw->chanctx_data_size,
9006b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
9016b4cac81SBjoern A. Zeeb 	}
9026b4cac81SBjoern A. Zeeb 
9036b4cac81SBjoern A. Zeeb 	conf->rx_chains_dynamic = 1;
9046b4cac81SBjoern A. Zeeb 	conf->rx_chains_static = 1;
9056b4cac81SBjoern A. Zeeb 	conf->radar_enabled =
9066b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
9076b4cac81SBjoern A. Zeeb 	conf->def.chan = chan;
9086b4cac81SBjoern A. Zeeb 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
9096b4cac81SBjoern A. Zeeb 	conf->def.center_freq1 = chan->center_freq;
9106b4cac81SBjoern A. Zeeb 	conf->def.center_freq2 = 0;
9116b4cac81SBjoern A. Zeeb 	/* Responder ... */
9126b4cac81SBjoern A. Zeeb 	conf->min_def.chan = chan;
9136b4cac81SBjoern A. Zeeb 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
9146b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq1 = chan->center_freq;
9156b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq2 = 0;
9166b4cac81SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT only");
9176b4cac81SBjoern A. Zeeb 
9186b4cac81SBjoern A. Zeeb 	error = 0;
9196b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
9206b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
9216b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
9226b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
9236b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
9246b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
9256b4cac81SBjoern A. Zeeb 	} else {
9266b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, conf);
9276b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
9286b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.chan = conf->def.chan;
9296b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.width = conf->def.width;
9306b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
9316b4cac81SBjoern A. Zeeb 			    conf->def.center_freq1;
9326b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
9336b4cac81SBjoern A. Zeeb 			    conf->def.center_freq2;
9346b4cac81SBjoern A. Zeeb 		} else {
9356b4cac81SBjoern A. Zeeb 			goto out;
9366b4cac81SBjoern A. Zeeb 		}
9376b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
9386b4cac81SBjoern A. Zeeb 		if (error == 0)
9396b4cac81SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, conf);
9406b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
9416b4cac81SBjoern A. Zeeb 			error = 0;
9426b4cac81SBjoern A. Zeeb 		if (error != 0) {
9436b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, conf);
9446b4cac81SBjoern A. Zeeb 			free(conf, M_LKPI80211);
9456b4cac81SBjoern A. Zeeb 			goto out;
9466b4cac81SBjoern A. Zeeb 		}
9476b4cac81SBjoern A. Zeeb 	}
9486b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
9496b4cac81SBjoern A. Zeeb 
9506b4cac81SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
9516b4cac81SBjoern A. Zeeb 	bss_changed = 0;
9526b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, ni->ni_bssid);
9536b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
9546b4cac81SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
9556b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
9566b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = false;
9576b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
958c8dafefaSBjoern A. Zeeb 
9596b4cac81SBjoern A. Zeeb 	/* Should almost assert it is this. */
9606b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
9616b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
962fa8f007dSBjoern A. Zeeb 
963fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
964fa8f007dSBjoern A. Zeeb 
9656b4cac81SBjoern A. Zeeb 	/* RATES */
9666b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
9676b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
9686b4cac81SBjoern A. Zeeb 
969*d9f59799SBjoern A. Zeeb 	/*
970*d9f59799SBjoern A. Zeeb 	 * This is a bandaid for now.  If we went through (*iv_update_bss)()
971*d9f59799SBjoern A. Zeeb 	 * and then removed the lsta we end up here without a lsta and have
972*d9f59799SBjoern A. Zeeb 	 * to manually allocate and link it in as lkpi_ic_node_alloc()/init()
973*d9f59799SBjoern A. Zeeb 	 * would normally do.
974*d9f59799SBjoern A. Zeeb 	 * XXX-BZ I do not like this but currently we have no good way of
975*d9f59799SBjoern A. Zeeb 	 * intercepting the bss swap and state changes and packets going out
976*d9f59799SBjoern A. Zeeb 	 * workflow so live with this.  It is a compat layer after all.
977*d9f59799SBjoern A. Zeeb 	 */
978*d9f59799SBjoern A. Zeeb 	if (ni->ni_drv_data == NULL) {
979*d9f59799SBjoern A. Zeeb 		lsta = lkpi_lsta_alloc(vap, ni->ni_macaddr, hw, ni);
980*d9f59799SBjoern A. Zeeb 		if (lsta == NULL) {
981*d9f59799SBjoern A. Zeeb 			error = ENOMEM;
982*d9f59799SBjoern A. Zeeb 			goto out;
983*d9f59799SBjoern A. Zeeb 		}
984*d9f59799SBjoern A. Zeeb 		lsta->ni = ieee80211_ref_node(ni);
985*d9f59799SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
986*d9f59799SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
987*d9f59799SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
988*d9f59799SBjoern A. Zeeb 	} else {
9896b4cac81SBjoern A. Zeeb 		lsta = ni->ni_drv_data;
990*d9f59799SBjoern A. Zeeb 	}
991*d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
9926b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
9936b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
9946b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
9956b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
9966b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
9976b4cac81SBjoern A. Zeeb 	if (error != 0) {
9986b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
9996b4cac81SBjoern A. Zeeb 		goto out;
10006b4cac81SBjoern A. Zeeb 	}
10016b4cac81SBjoern A. Zeeb #if 0
10026b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
10036b4cac81SBjoern A. Zeeb #endif
10046b4cac81SBjoern A. Zeeb 
10056b4cac81SBjoern A. Zeeb 	/*
10066b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
10076b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
10086b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
10096b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1010*d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1011*d9f59799SBjoern A. Zeeb 	 * for all queues.
10126b4cac81SBjoern A. Zeeb 	 */
10136b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, false);
10146b4cac81SBjoern A. Zeeb 
10156b4cac81SBjoern A. Zeeb 	{
10166b4cac81SBjoern A. Zeeb 		int i, count;
10176b4cac81SBjoern A. Zeeb 
1018500be2e8SBjoern A. Zeeb 		for (i = 3 * (hw->queues + 1); i > 0; i--) {
10196b4cac81SBjoern A. Zeeb 			struct lkpi_txq *ltxq;
10206b4cac81SBjoern A. Zeeb 			int tid;
10216b4cac81SBjoern A. Zeeb 
10226b4cac81SBjoern A. Zeeb 			count = 0;
10236b4cac81SBjoern A. Zeeb 			/* Wake up all queues to know they are allocated in the driver. */
10246b4cac81SBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
10256b4cac81SBjoern A. Zeeb 
10266b4cac81SBjoern A. Zeeb 				if (tid == IEEE80211_NUM_TIDS) {
10276b4cac81SBjoern A. Zeeb 					IMPROVE("station specific?");
10286b4cac81SBjoern A. Zeeb 					if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
10296b4cac81SBjoern A. Zeeb 						continue;
10306b4cac81SBjoern A. Zeeb 				} else if (tid >= hw->queues)
10316b4cac81SBjoern A. Zeeb 					continue;
10326b4cac81SBjoern A. Zeeb 
10336b4cac81SBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
10346b4cac81SBjoern A. Zeeb 					continue;
10356b4cac81SBjoern A. Zeeb 
10366b4cac81SBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10376b4cac81SBjoern A. Zeeb 				if (!ltxq->seen_dequeue)
10386b4cac81SBjoern A. Zeeb 					count++;
10396b4cac81SBjoern A. Zeeb 			}
10406b4cac81SBjoern A. Zeeb 			if (count == 0)
10416b4cac81SBjoern A. Zeeb 				break;
10426b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
10436b4cac81SBjoern A. Zeeb 			if (count > 0)
1044312ba38aSRuslan Makhmatkhanov 				ic_printf(vap->iv_ic, "%s: waiting for %d queues "
10456b4cac81SBjoern A. Zeeb 				    "to be allocated by driver\n", __func__, count);
10466b4cac81SBjoern A. Zeeb #endif
1047500be2e8SBjoern A. Zeeb 			pause("lkpi80211txq", hz/10);
10486b4cac81SBjoern A. Zeeb 		}
10496b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
10506b4cac81SBjoern A. Zeeb 		if (count > 0)
1051312ba38aSRuslan Makhmatkhanov 			ic_printf(vap->iv_ic, "%s: %d queues still not "
10526b4cac81SBjoern A. Zeeb 			    "allocated by driver\n", __func__, count);
10536b4cac81SBjoern A. Zeeb #endif
10546b4cac81SBjoern A. Zeeb 	}
10556b4cac81SBjoern A. Zeeb 
10566b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
10576b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
10586b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
10596b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
10606b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
10616b4cac81SBjoern A. Zeeb 
10626b4cac81SBjoern A. Zeeb 	/*
10636b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
10646b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
10656b4cac81SBjoern A. Zeeb 	 * - event_callback
10666b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
10676b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
10686b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
10696b4cac81SBjoern A. Zeeb 	 */
10706b4cac81SBjoern A. Zeeb 
10716b4cac81SBjoern A. Zeeb out:
10726b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
10736b4cac81SBjoern A. Zeeb 	if (ni != NULL)
10746b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
10756b4cac81SBjoern A. Zeeb 	return (error);
10766b4cac81SBjoern A. Zeeb }
10776b4cac81SBjoern A. Zeeb 
10786b4cac81SBjoern A. Zeeb static int
10796b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10806b4cac81SBjoern A. Zeeb {
10816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10826b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10856b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10866b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10876b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
10886b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10896b4cac81SBjoern A. Zeeb 	int error;
10906b4cac81SBjoern A. Zeeb 
10916b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
10926b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10936b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10946b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10956b4cac81SBjoern A. Zeeb 
10966b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
10976b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
10986b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
10996b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
11006b4cac81SBjoern A. Zeeb 
1101*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
11026b4cac81SBjoern A. Zeeb 
11036b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11046b4cac81SBjoern A. Zeeb 
1105*d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1106*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1107*d9f59799SBjoern A. Zeeb 
11086b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
11096b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
11106b4cac81SBjoern A. Zeeb 
11116b4cac81SBjoern A. Zeeb 	/* flush, no drop */
11126b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
11136b4cac81SBjoern A. Zeeb 
11146b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
11156b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
11166b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
11176b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
11186b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
11196b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
11206b4cac81SBjoern A. Zeeb 	}
11216b4cac81SBjoern A. Zeeb 
11226b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
11236b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
11246b4cac81SBjoern A. Zeeb 
11256b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
11266b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1127*d9f59799SBjoern A. Zeeb 
1128*d9f59799SBjoern A. Zeeb 	/* Take the station down. */
11296b4cac81SBjoern A. Zeeb 
11306b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
11316b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
11326b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1133*d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
11346b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
11356b4cac81SBjoern A. Zeeb 	if (error != 0) {
11366b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
11376b4cac81SBjoern A. Zeeb 		goto out;
11386b4cac81SBjoern A. Zeeb 	}
11396b4cac81SBjoern A. Zeeb #if 0
11406b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
11416b4cac81SBjoern A. Zeeb #endif
11426b4cac81SBjoern A. Zeeb 
1143*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
11446b4cac81SBjoern A. Zeeb 
1145*d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1146*d9f59799SBjoern A. Zeeb 
1147*d9f59799SBjoern A. Zeeb 	/* conf_tx */
1148*d9f59799SBjoern A. Zeeb 
1149*d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
11506b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
11516b4cac81SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
11526b4cac81SBjoern A. Zeeb 
11536b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
11546b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
11556b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
11566b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
11576b4cac81SBjoern A. Zeeb 
11586b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
11596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
11606b4cac81SBjoern A. Zeeb 		free(conf, M_LKPI80211);
11616b4cac81SBjoern A. Zeeb 	}
11626b4cac81SBjoern A. Zeeb 
11636b4cac81SBjoern A. Zeeb out:
11646b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
11656b4cac81SBjoern A. Zeeb 	if (ni != NULL)
11666b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
11676b4cac81SBjoern A. Zeeb 	return (error);
11686b4cac81SBjoern A. Zeeb }
11696b4cac81SBjoern A. Zeeb 
11706b4cac81SBjoern A. Zeeb static int
11716b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11726b4cac81SBjoern A. Zeeb {
11736b4cac81SBjoern A. Zeeb 	int error;
11746b4cac81SBjoern A. Zeeb 
11756b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
11766b4cac81SBjoern A. Zeeb 	if (error == 0)
11776b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
11786b4cac81SBjoern A. Zeeb 	return (error);
11796b4cac81SBjoern A. Zeeb }
11806b4cac81SBjoern A. Zeeb 
11816b4cac81SBjoern A. Zeeb static int
11826b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11836b4cac81SBjoern A. Zeeb {
11846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11856b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11866b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
11876b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11886b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11896b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11906b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
11916b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
11926b4cac81SBjoern A. Zeeb 	int error;
11936b4cac81SBjoern A. Zeeb 
11946b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11956b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11966b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11976b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11986b4cac81SBjoern A. Zeeb 
11996b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
12006b4cac81SBjoern A. Zeeb 	ni = NULL;
12016b4cac81SBjoern A. Zeeb 
12026b4cac81SBjoern A. Zeeb 	/* Finish auth. */
12036b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
12046b4cac81SBjoern A. Zeeb 
12056b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
12066b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
12076b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
12086b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12096b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
12106b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
12116b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
12126b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
12136b4cac81SBjoern A. Zeeb 	if (error != 0)
12146b4cac81SBjoern A. Zeeb 		goto out;
12156b4cac81SBjoern A. Zeeb 
12166b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
12176b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
12186b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12196b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
12206b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
12216b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
12226b4cac81SBjoern A. Zeeb 	}
12236b4cac81SBjoern A. Zeeb 
12246b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
12256b4cac81SBjoern A. Zeeb 
12266b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
12276b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
12286b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12296b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
12306b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
12316b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
12326b4cac81SBjoern A. Zeeb 	}
12336b4cac81SBjoern A. Zeeb 
12346b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
12356b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
12366b4cac81SBjoern A. Zeeb 
12376b4cac81SBjoern A. Zeeb 	/*
12386b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
12396b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
12406b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
12416b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
12426b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
12436b4cac81SBjoern A. Zeeb 	 * - event_callback
12446b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
12456b4cac81SBjoern A. Zeeb 	 */
12466b4cac81SBjoern A. Zeeb 
12476b4cac81SBjoern A. Zeeb out:
12486b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
12496b4cac81SBjoern A. Zeeb 	if (ni != NULL)
12506b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
12516b4cac81SBjoern A. Zeeb 	return (error);
12526b4cac81SBjoern A. Zeeb }
12536b4cac81SBjoern A. Zeeb 
12546b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
12556b4cac81SBjoern A. Zeeb static int
12566b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
12576b4cac81SBjoern A. Zeeb {
12586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12596b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12606b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
12616b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12626b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12636b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12646b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
12656b4cac81SBjoern A. Zeeb 
12666b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
12676b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
12686b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
12696b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
12706b4cac81SBjoern A. Zeeb 
12716b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
12726b4cac81SBjoern A. Zeeb 
12736b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
12746b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
12756b4cac81SBjoern A. Zeeb 
12766b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
12776b4cac81SBjoern A. Zeeb 
12786b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
12796b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
12806b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12816b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
12826b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
12836b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
12846b4cac81SBjoern A. Zeeb 	}
12856b4cac81SBjoern A. Zeeb 
12866b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
12876b4cac81SBjoern A. Zeeb 
12886b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
12896b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
12906b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12916b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
12926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
12936b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
12946b4cac81SBjoern A. Zeeb 	}
12956b4cac81SBjoern A. Zeeb 
12966b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
12976b4cac81SBjoern A. Zeeb 	if (ni != NULL)
12986b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
12996b4cac81SBjoern A. Zeeb 
13006b4cac81SBjoern A. Zeeb 	return (0);
13016b4cac81SBjoern A. Zeeb }
13026b4cac81SBjoern A. Zeeb 
13036b4cac81SBjoern A. Zeeb static int
1304*d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13056b4cac81SBjoern A. Zeeb {
13066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13076b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13086b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13096b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13106b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13126b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13136b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1314*d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
13156b4cac81SBjoern A. Zeeb 	int error;
13166b4cac81SBjoern A. Zeeb 
13176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13216b4cac81SBjoern A. Zeeb 
13226b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
13236b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
13246b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
13256b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13266b4cac81SBjoern A. Zeeb 
1327*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1328*d9f59799SBjoern A. Zeeb 
1329*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1330*d9f59799SBjoern A. Zeeb 
1331*d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1332*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1333*d9f59799SBjoern A. Zeeb 
1334*d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1335*d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1336*d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1337*d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1338*d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1339*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1340*d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1341*d9f59799SBjoern A. Zeeb 	}
1342*d9f59799SBjoern A. Zeeb 
1343*d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1344*d9f59799SBjoern A. Zeeb 
1345*d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1346*d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1347*d9f59799SBjoern A. Zeeb 	if (error != 0)
1348*d9f59799SBjoern A. Zeeb 		goto outni;
1349*d9f59799SBjoern A. Zeeb 
1350*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1351*d9f59799SBjoern A. Zeeb 
1352*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1353*d9f59799SBjoern A. Zeeb 
1354*d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1355*d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1356*d9f59799SBjoern A. Zeeb 
1357*d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1358*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1359*d9f59799SBjoern A. Zeeb 
13606b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
13616b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
13626b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13636b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
13646b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
13656b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
13666b4cac81SBjoern A. Zeeb 	}
13676b4cac81SBjoern A. Zeeb 
1368*d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1369*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1370*d9f59799SBjoern A. Zeeb 
1371*d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1372*d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1373*d9f59799SBjoern A. Zeeb 
1374*d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1375*d9f59799SBjoern A. Zeeb 
13766b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
13776b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13786b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
13796b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
13806b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
13816b4cac81SBjoern A. Zeeb 	if (error != 0)
13826b4cac81SBjoern A. Zeeb 		goto out;
13836b4cac81SBjoern A. Zeeb 
1384*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
13856b4cac81SBjoern A. Zeeb 
1386*d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1387*d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1388*d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1389*d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1390*d9f59799SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1391*d9f59799SBjoern A. Zeeb 	if (error != 0) {
1392*d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1393*d9f59799SBjoern A. Zeeb 		goto out;
1394*d9f59799SBjoern A. Zeeb 	}
1395*d9f59799SBjoern A. Zeeb #if 0
1396*d9f59799SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
1397*d9f59799SBjoern A. Zeeb #endif
1398*d9f59799SBjoern A. Zeeb 
1399*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1400*d9f59799SBjoern A. Zeeb 
1401*d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1402*d9f59799SBjoern A. Zeeb 	/* We need to do this now, can only do after sta is IEEE80211_STA_NOTEXIST. */
1403*d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1404*d9f59799SBjoern A. Zeeb 
1405*d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1406*d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1407*d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1408*d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1409*d9f59799SBjoern A. Zeeb 	vif->bss_conf.ssid_len = 0;
1410*d9f59799SBjoern A. Zeeb 	memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1411*d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1412*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1413*d9f59799SBjoern A. Zeeb 
1414*d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1415*d9f59799SBjoern A. Zeeb 
1416*d9f59799SBjoern A. Zeeb 	/* conf_tx */
1417*d9f59799SBjoern A. Zeeb 
1418*d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1419*d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1420*d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1421*d9f59799SBjoern A. Zeeb 
1422*d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1423*d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
1424*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1425*d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1426*d9f59799SBjoern A. Zeeb 
1427*d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1428*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1429*d9f59799SBjoern A. Zeeb 		free(conf, M_LKPI80211);
1430*d9f59799SBjoern A. Zeeb 	}
1431*d9f59799SBjoern A. Zeeb 
1432*d9f59799SBjoern A. Zeeb 	error = EALREADY;
14336b4cac81SBjoern A. Zeeb out:
14346b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1435*d9f59799SBjoern A. Zeeb outni:
14366b4cac81SBjoern A. Zeeb 	if (ni != NULL)
14376b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
14386b4cac81SBjoern A. Zeeb 	return (error);
14396b4cac81SBjoern A. Zeeb }
14406b4cac81SBjoern A. Zeeb 
14416b4cac81SBjoern A. Zeeb static int
1442*d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1443*d9f59799SBjoern A. Zeeb {
1444*d9f59799SBjoern A. Zeeb 	int error;
1445*d9f59799SBjoern A. Zeeb 
1446*d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1447*d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1448*d9f59799SBjoern A. Zeeb 		return (error);
1449*d9f59799SBjoern A. Zeeb 
1450*d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1451*d9f59799SBjoern A. Zeeb 
1452*d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1453*d9f59799SBjoern A. Zeeb 	return (error);
1454*d9f59799SBjoern A. Zeeb }
1455*d9f59799SBjoern A. Zeeb 
1456*d9f59799SBjoern A. Zeeb static int
14576b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14586b4cac81SBjoern A. Zeeb {
14596b4cac81SBjoern A. Zeeb 	int error;
14606b4cac81SBjoern A. Zeeb 
1461*d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
14626b4cac81SBjoern A. Zeeb 	return (error);
14636b4cac81SBjoern A. Zeeb }
14646b4cac81SBjoern A. Zeeb 
14656b4cac81SBjoern A. Zeeb static int
14666b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14676b4cac81SBjoern A. Zeeb {
14686b4cac81SBjoern A. Zeeb 	int error;
14696b4cac81SBjoern A. Zeeb 
1470*d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
14716b4cac81SBjoern A. Zeeb 	return (error);
14726b4cac81SBjoern A. Zeeb }
14736b4cac81SBjoern A. Zeeb 
14746b4cac81SBjoern A. Zeeb static int
14756b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14766b4cac81SBjoern A. Zeeb {
14776b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14786b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14796b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14806b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14816b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
14826b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14836b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
14846b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14856b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
14866b4cac81SBjoern A. Zeeb 	int error;
14876b4cac81SBjoern A. Zeeb 
14886b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14896b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14906b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14916b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14926b4cac81SBjoern A. Zeeb 
14936b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14946b4cac81SBjoern A. Zeeb 	ni = NULL;
14956b4cac81SBjoern A. Zeeb 
14966b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
14976b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
14986b4cac81SBjoern A. Zeeb 
14996b4cac81SBjoern A. Zeeb 	/* Finish assoc. */
15006b4cac81SBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
15016b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
15026b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
15036b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15046b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
15056b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
15066b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
15076b4cac81SBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
15086b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
15096b4cac81SBjoern A. Zeeb 	if (error != 0)
15106b4cac81SBjoern 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;
15166b4cac81SBjoern A. Zeeb 	if (!vif->bss_conf.assoc || vif->bss_conf.aid != IEEE80211_NODE_AID(ni)) {
15176b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = true;
15186b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = IEEE80211_NODE_AID(ni);
15196b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
15206b4cac81SBjoern A. Zeeb 	}
15216b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
15226b4cac81SBjoern A. Zeeb 	vif->bss_conf.ssid_len = ni->ni_esslen;
15236b4cac81SBjoern A. Zeeb 	memcpy(vif->bss_conf.ssid, ni->ni_essid, ni->ni_esslen);
15246b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
15256b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
15266b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
15276b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
15286b4cac81SBjoern A. Zeeb 	}
15296b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
15306b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
15316b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
15326b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
15336b4cac81SBjoern A. Zeeb 	}
15346b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
15356b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
15366b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
15376b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
15386b4cac81SBjoern A. Zeeb 	}
1539fa8f007dSBjoern A. Zeeb 
1540fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1541fa8f007dSBjoern A. Zeeb 
15426b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
15436b4cac81SBjoern A. Zeeb 
15446b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
15456b4cac81SBjoern A. Zeeb 	 * - event_callback
15466b4cac81SBjoern A. Zeeb 	 */
15476b4cac81SBjoern A. Zeeb 
15486b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
15496b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
15506b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15516b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
15526b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
15536b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
15546b4cac81SBjoern A. Zeeb 	}
15556b4cac81SBjoern A. Zeeb 
1556086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1557086be6a8SBjoern A. Zeeb 
15586b4cac81SBjoern A. Zeeb 	/*
15596b4cac81SBjoern A. Zeeb 	 * And then:
15606b4cac81SBjoern A. Zeeb 	 * - (more packets)?
15616b4cac81SBjoern A. Zeeb 	 * - set_key
15626b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
15636b4cac81SBjoern A. Zeeb 	 * - set_key (?)
15646b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
15656b4cac81SBjoern A. Zeeb 	 */
15666b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
15676b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
15686b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
15696b4cac81SBjoern A. Zeeb 
15706b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
15716b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
15726b4cac81SBjoern A. Zeeb 	}
15736b4cac81SBjoern A. Zeeb 
15746b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
15756b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
15766b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
15776b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15786b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
15796b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
15806b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
15816b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, 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;
1627*d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1628*d9f59799SBjoern A. Zeeb #if 0
1629*d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1630*d9f59799SBjoern 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 
1643*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1644*d9f59799SBjoern A. Zeeb 
1645*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1646*d9f59799SBjoern A. Zeeb 
1647*d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1648*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1649*d9f59799SBjoern A. Zeeb 
1650*d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1651*d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1652*d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1653*d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1654*d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1655*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1656*d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1657*d9f59799SBjoern A. Zeeb 	}
1658*d9f59799SBjoern A. Zeeb 
1659*d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1660*d9f59799SBjoern A. Zeeb 
1661*d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1662*d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1663*d9f59799SBjoern A. Zeeb 	if (error != 0)
1664*d9f59799SBjoern A. Zeeb 		goto outni;
1665*d9f59799SBjoern A. Zeeb 
1666*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1667*d9f59799SBjoern A. Zeeb 
1668*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1669*d9f59799SBjoern A. Zeeb 
1670*d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1671*d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1672*d9f59799SBjoern A. Zeeb 
1673*d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1674*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1675*d9f59799SBjoern A. Zeeb 
1676*d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
1677*d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
1678*d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1679*d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
1680*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1681*d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
1682*d9f59799SBjoern A. Zeeb 	}
1683*d9f59799SBjoern A. Zeeb 
1684*d9f59799SBjoern A. Zeeb #if 0
1685*d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1686*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1687*d9f59799SBjoern A. Zeeb 
1688*d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1689*d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1690*d9f59799SBjoern A. Zeeb #endif
1691*d9f59799SBjoern A. Zeeb 
1692*d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1693*d9f59799SBjoern 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));
16986b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
16996b4cac81SBjoern A. Zeeb 	if (error != 0)
17006b4cac81SBjoern A. Zeeb 		goto out;
17016b4cac81SBjoern A. Zeeb 
1702*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __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));
17086b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
17096b4cac81SBjoern A. Zeeb 	if (error != 0)
17106b4cac81SBjoern A. Zeeb 		goto out;
17116b4cac81SBjoern A. Zeeb 
1712*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
17136b4cac81SBjoern A. Zeeb 
1714*d9f59799SBjoern A. Zeeb #if 0
1715*d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1716*d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1717*d9f59799SBjoern A. Zeeb #endif
1718*d9f59799SBjoern A. Zeeb 
1719*d9f59799SBjoern A. Zeeb 	error = EALREADY;
17206b4cac81SBjoern A. Zeeb out:
17216b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1722*d9f59799SBjoern 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
1729*d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1730*d9f59799SBjoern A. Zeeb {
1731*d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1732*d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
1733*d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
1734*d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
1735*d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
1736*d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
1737*d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
1738*d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1739*d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1740*d9f59799SBjoern A. Zeeb 	int error;
1741*d9f59799SBjoern A. Zeeb 
1742*d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
1743*d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1744*d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1745*d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
1746*d9f59799SBjoern A. Zeeb 
1747*d9f59799SBjoern A. Zeeb 	/* Keep ni around. */
1748*d9f59799SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
1749*d9f59799SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1750*d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
1751*d9f59799SBjoern A. Zeeb 
1752*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1753*d9f59799SBjoern A. Zeeb 
1754*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1755*d9f59799SBjoern A. Zeeb 
1756*d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1757*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1758*d9f59799SBjoern A. Zeeb 
1759*d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1760*d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1761*d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1762*d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1763*d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1764*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1765*d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1766*d9f59799SBjoern A. Zeeb 	}
1767*d9f59799SBjoern A. Zeeb 
1768*d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1769*d9f59799SBjoern A. Zeeb 
1770*d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1771*d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1772*d9f59799SBjoern A. Zeeb 	if (error != 0)
1773*d9f59799SBjoern A. Zeeb 		goto outni;
1774*d9f59799SBjoern A. Zeeb 
1775*d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1776*d9f59799SBjoern A. Zeeb 
1777*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1778*d9f59799SBjoern A. Zeeb 
1779*d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1780*d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1781*d9f59799SBjoern A. Zeeb 
1782*d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1783*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1784*d9f59799SBjoern A. Zeeb 
1785*d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
1786*d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
1787*d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1788*d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
1789*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1790*d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
1791*d9f59799SBjoern A. Zeeb 	}
1792*d9f59799SBjoern A. Zeeb 
1793*d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1794*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1795*d9f59799SBjoern A. Zeeb 
1796*d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1797*d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1798*d9f59799SBjoern A. Zeeb 
1799*d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1800*d9f59799SBjoern A. Zeeb 
1801*d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1802*d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1803*d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1804*d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1805*d9f59799SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1806*d9f59799SBjoern A. Zeeb 	if (error != 0)
1807*d9f59799SBjoern A. Zeeb 		goto out;
1808*d9f59799SBjoern A. Zeeb 
1809*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1810*d9f59799SBjoern A. Zeeb 
1811*d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
1812*d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1813*d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1814*d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1815*d9f59799SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1816*d9f59799SBjoern A. Zeeb 	if (error != 0)
1817*d9f59799SBjoern A. Zeeb 		goto out;
1818*d9f59799SBjoern A. Zeeb 
1819*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1820*d9f59799SBjoern A. Zeeb 
1821*d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
1822*d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1823*d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1824*d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1825*d9f59799SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1826*d9f59799SBjoern A. Zeeb 	if (error != 0)
1827*d9f59799SBjoern A. Zeeb 		goto out;
1828*d9f59799SBjoern A. Zeeb 
1829*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1830*d9f59799SBjoern A. Zeeb 
1831*d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1832*d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1833*d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1834*d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1835*d9f59799SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1836*d9f59799SBjoern A. Zeeb 	if (error != 0) {
1837*d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1838*d9f59799SBjoern A. Zeeb 		goto out;
1839*d9f59799SBjoern A. Zeeb 	}
1840*d9f59799SBjoern A. Zeeb #if 0
1841*d9f59799SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
1842*d9f59799SBjoern A. Zeeb #endif
1843*d9f59799SBjoern A. Zeeb 
1844*d9f59799SBjoern A. Zeeb 	lkpi_dump_lsta(lsta, __func__, __LINE__);
1845*d9f59799SBjoern A. Zeeb 
1846*d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
1847*d9f59799SBjoern A. Zeeb 	/*
1848*d9f59799SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
1849*d9f59799SBjoern A. Zeeb 	 * See comment there; removes the sta from fw.
1850*d9f59799SBjoern A. Zeeb 	 */
1851*d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
1852*d9f59799SBjoern A. Zeeb 
1853*d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1854*d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1855*d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1856*d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1857*d9f59799SBjoern A. Zeeb 	vif->bss_conf.ssid_len = 0;
1858*d9f59799SBjoern A. Zeeb 	memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1859*d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1860*d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1861*d9f59799SBjoern A. Zeeb 
1862*d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1863*d9f59799SBjoern A. Zeeb 
1864*d9f59799SBjoern A. Zeeb 	/* conf_tx */
1865*d9f59799SBjoern A. Zeeb 
1866*d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1867*d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1868*d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1869*d9f59799SBjoern A. Zeeb 
1870*d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1871*d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
1872*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1873*d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1874*d9f59799SBjoern A. Zeeb 
1875*d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1876*d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1877*d9f59799SBjoern A. Zeeb 		free(conf, M_LKPI80211);
1878*d9f59799SBjoern A. Zeeb 	}
1879*d9f59799SBjoern A. Zeeb 
1880*d9f59799SBjoern A. Zeeb 	error = EALREADY;
1881*d9f59799SBjoern A. Zeeb out:
1882*d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1883*d9f59799SBjoern A. Zeeb outni:
1884*d9f59799SBjoern A. Zeeb 	if (ni != NULL)
1885*d9f59799SBjoern A. Zeeb 		ieee80211_free_node(ni);
1886*d9f59799SBjoern A. Zeeb 	return (error);
1887*d9f59799SBjoern A. Zeeb }
1888*d9f59799SBjoern A. Zeeb 
1889*d9f59799SBjoern A. Zeeb static int
1890*d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1891*d9f59799SBjoern A. Zeeb {
1892*d9f59799SBjoern A. Zeeb 
1893*d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
1894*d9f59799SBjoern A. Zeeb }
1895*d9f59799SBjoern A. Zeeb 
1896*d9f59799SBjoern 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 
1901*d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
1902*d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1903*d9f59799SBjoern A. Zeeb 		return (error);
1904*d9f59799SBjoern A. Zeeb 
1905*d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1906*d9f59799SBjoern A. Zeeb 
1907*d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
19086b4cac81SBjoern A. Zeeb 	return (error);
19096b4cac81SBjoern A. Zeeb }
19106b4cac81SBjoern A. Zeeb 
1911*d9f59799SBjoern 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() */
1927*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
1928*d9f59799SBjoern 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 },
1934*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
19356b4cac81SBjoern A. Zeeb 
1936*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1937*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
1938*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
1939*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
1940*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
19416b4cac81SBjoern A. Zeeb 
1942*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
1943*d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
1944*d9f59799SBjoern 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 
19696b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
19706b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
19716b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
19726b4cac81SBjoern A. Zeeb 
19736b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
19746b4cac81SBjoern A. Zeeb 
19756b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
19766b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
19776b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
19786b4cac81SBjoern A. Zeeb 
19796b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
19806b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
19816b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
19826b4cac81SBjoern A. Zeeb 
19836b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
19846b4cac81SBjoern A. Zeeb 
19856b4cac81SBjoern A. Zeeb 	} else {
19866b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
19876b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
19886b4cac81SBjoern A. Zeeb 		return (ENOSYS);
19896b4cac81SBjoern A. Zeeb 	}
19906b4cac81SBjoern A. Zeeb 
19916b4cac81SBjoern A. Zeeb 	error = 0;
19926b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
19936b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
1994*d9f59799SBjoern A. Zeeb 			if (debug_80211 & D80211_TRACE)
1995*d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
1996*d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
1997*d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
1998*d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
19996b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
20006b4cac81SBjoern A. Zeeb 			break;
20016b4cac81SBjoern A. Zeeb 		}
20026b4cac81SBjoern A. Zeeb 	}
20036b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
20046b4cac81SBjoern A. Zeeb 
20056b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2006*d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
20076b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
20086b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
20096b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
20106b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
20116b4cac81SBjoern A. Zeeb 		return (ENOSYS);
20126b4cac81SBjoern A. Zeeb 	}
20136b4cac81SBjoern A. Zeeb 
20146b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
2015*d9f59799SBjoern A. Zeeb 		if (debug_80211 & D80211_TRACE)
2016*d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2017*d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2018*d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2019*d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
20206b4cac81SBjoern A. Zeeb 		return (0);
20216b4cac81SBjoern A. Zeeb 	}
20226b4cac81SBjoern A. Zeeb 
20236b4cac81SBjoern A. Zeeb 	if (error != 0) {
20246b4cac81SBjoern A. Zeeb 		/* XXX-BZ currently expected so ignore. */
20256b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
20266b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
20276b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
20286b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
20296b4cac81SBjoern A. Zeeb 		/* return (error); */
20306b4cac81SBjoern A. Zeeb 	}
20316b4cac81SBjoern A. Zeeb 
20326b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
2033*d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2034*d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
20356b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
20366b4cac81SBjoern A. Zeeb 
20376b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
20386b4cac81SBjoern A. Zeeb }
20396b4cac81SBjoern A. Zeeb 
20406b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
20416b4cac81SBjoern A. Zeeb 
2042*d9f59799SBjoern A. Zeeb /*
2043*d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2044*d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2045*d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2046*d9f59799SBjoern A. Zeeb  */
2047*d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2048*d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2049*d9f59799SBjoern A. Zeeb {
2050*d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2051*d9f59799SBjoern A. Zeeb 	struct ieee80211_node *obss;
2052*d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2053*d9f59799SBjoern A. Zeeb 
2054*d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2055*d9f59799SBjoern A. Zeeb 	obss = vap->iv_bss;
2056*d9f59799SBjoern A. Zeeb 
2057*d9f59799SBjoern A. Zeeb 	/* Nothing to copy from.  Just return. */
2058*d9f59799SBjoern A. Zeeb 	if (obss == NULL || obss->ni_drv_data == NULL)
2059*d9f59799SBjoern A. Zeeb 		goto out;
2060*d9f59799SBjoern A. Zeeb 
2061*d9f59799SBjoern A. Zeeb 	/* Nothing to copy to.  Just return. */
2062*d9f59799SBjoern A. Zeeb 	IMPROVE("clearing the obss might still be needed?");
2063*d9f59799SBjoern A. Zeeb 	if (ni == NULL)
2064*d9f59799SBjoern A. Zeeb 		goto out;
2065*d9f59799SBjoern A. Zeeb 
2066*d9f59799SBjoern A. Zeeb 	/* Nothing changed? panic? */
2067*d9f59799SBjoern A. Zeeb 	if (obss == ni)
2068*d9f59799SBjoern A. Zeeb 		goto out;
2069*d9f59799SBjoern A. Zeeb 
2070*d9f59799SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
2071*d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
2072*d9f59799SBjoern A. Zeeb 		    "ni %p ni_drv_data %p\n", __func__,
2073*d9f59799SBjoern A. Zeeb 		    obss, (obss != NULL) ? obss->ni_drv_data : NULL,
2074*d9f59799SBjoern A. Zeeb 		    ni, (ni != NULL) ? ni->ni_drv_data : NULL);
2075*d9f59799SBjoern A. Zeeb 
2076*d9f59799SBjoern A. Zeeb 	lsta = obss->ni_drv_data;
2077*d9f59799SBjoern A. Zeeb 	obss->ni_drv_data = ni->ni_drv_data;
2078*d9f59799SBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2079*d9f59799SBjoern A. Zeeb 	if (lsta != NULL)
2080*d9f59799SBjoern A. Zeeb 		lsta->ni = ni;
2081*d9f59799SBjoern A. Zeeb 	lsta = obss->ni_drv_data;
2082*d9f59799SBjoern A. Zeeb 	if (lsta != NULL)
2083*d9f59799SBjoern A. Zeeb 		lsta->ni = obss;
2084*d9f59799SBjoern A. Zeeb 
2085*d9f59799SBjoern A. Zeeb out:
2086*d9f59799SBjoern A. Zeeb 	return (lvif->iv_update_bss(vap, ni));
2087*d9f59799SBjoern A. Zeeb }
2088*d9f59799SBjoern A. Zeeb 
20896b4cac81SBjoern A. Zeeb static int
20906b4cac81SBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
20916b4cac81SBjoern A. Zeeb {
20926b4cac81SBjoern A. Zeeb 	/* This needs queuing and go at the right moment. */
20936b4cac81SBjoern A. Zeeb #ifdef WITH_WME_UPDATE
20946b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
20956b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20966b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20976b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20986b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20996b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
21006b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
21016b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
21026b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
21036b4cac81SBjoern A. Zeeb 	int error;
21046b4cac81SBjoern A. Zeeb 	uint16_t ac;
21056b4cac81SBjoern A. Zeeb #endif
21066b4cac81SBjoern A. Zeeb 
21076b4cac81SBjoern A. Zeeb 	IMPROVE();
21086b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
21096b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
21106b4cac81SBjoern A. Zeeb 
21116b4cac81SBjoern A. Zeeb #ifdef WITH_WME_UPDATE
21126b4cac81SBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
21136b4cac81SBjoern A. Zeeb 	if (vap == NULL)
21146b4cac81SBjoern A. Zeeb 		return (0);
21156b4cac81SBjoern A. Zeeb 
21166b4cac81SBjoern A. Zeeb 	/* We should factor this out into per-vap (*wme_update). */
21176b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
21186b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
21196b4cac81SBjoern A. Zeeb 		return (0);
21206b4cac81SBjoern A. Zeeb 
21216b4cac81SBjoern A. Zeeb 	/* XXX-BZ check amount of hw queues */
21226b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21236b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21246b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21256b4cac81SBjoern A. Zeeb 
21266b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
21276b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
21286b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
21296b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
21306b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
21316b4cac81SBjoern A. Zeeb 
21326b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
21336b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21346b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
21356b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
21366b4cac81SBjoern A. Zeeb 
21376b4cac81SBjoern A. Zeeb 		/* XXX-BZ should keep this in lvif? */
21386b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
21396b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
21406b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
21416b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
21426b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
21436b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
21446b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
21456b4cac81SBjoern A. Zeeb 		if (error != 0)
21466b4cac81SBjoern A. Zeeb 			printf("%s: conf_tx ac %u failed %d\n",
21476b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
21486b4cac81SBjoern A. Zeeb 	}
21496b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21506b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
21516b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
21526b4cac81SBjoern A. Zeeb #endif
21536b4cac81SBjoern A. Zeeb 
21546b4cac81SBjoern A. Zeeb 	return (0);
21556b4cac81SBjoern A. Zeeb }
21566b4cac81SBjoern A. Zeeb 
21576b4cac81SBjoern A. Zeeb static struct ieee80211vap *
21586b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
21596b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
21606b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
21616b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
21626b4cac81SBjoern A. Zeeb {
21636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21666b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
21676b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21686b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
21696b4cac81SBjoern A. Zeeb 	size_t len;
21706b4cac81SBjoern A. Zeeb 	int error;
21716b4cac81SBjoern A. Zeeb 
21726b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
21736b4cac81SBjoern A. Zeeb 		return (NULL);
21746b4cac81SBjoern A. Zeeb 
21756b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
21766b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21776b4cac81SBjoern A. Zeeb 
21786b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
21796b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
21806b4cac81SBjoern A. Zeeb 
21816b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
21826b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
21836b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
21846b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
21856b4cac81SBjoern A. Zeeb 
21866b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21876b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
21886b4cac81SBjoern A. Zeeb 	vif->p2p = false;
21896b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
21906b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
21916b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
21926b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
21936b4cac81SBjoern A. Zeeb 	IMPROVE();
21946b4cac81SBjoern A. Zeeb 
21956b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
21966b4cac81SBjoern A. Zeeb #if 1
21976b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
21986b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = true;
21996b4cac81SBjoern A. Zeeb 	vif->bss_conf.ps = false;
22006b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
22016b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
22026b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
22036b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
22046b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
22056b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
22066b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
22076b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
22086b4cac81SBjoern A. Zeeb #endif
22096b4cac81SBjoern A. Zeeb #if 0
22106b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
22116b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
22126b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
22136b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
22146b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
22156b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
22166b4cac81SBjoern A. Zeeb #endif
22176b4cac81SBjoern A. Zeeb 	IMPROVE();
22186b4cac81SBjoern A. Zeeb 
22196b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
22206b4cac81SBjoern A. Zeeb 	if (error != 0) {
22216b4cac81SBjoern A. Zeeb 		printf("%s: failed to start hw: %d\n", __func__, error);
22226b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
22236b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
22246b4cac81SBjoern A. Zeeb 		return (NULL);
22256b4cac81SBjoern A. Zeeb 	}
22266b4cac81SBjoern A. Zeeb 
22276b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
22286b4cac81SBjoern A. Zeeb 	if (error != 0) {
22296b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
22306b4cac81SBjoern A. Zeeb 		printf("%s: failed to add interface: %d\n", __func__, error);
22316b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
22326b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
22336b4cac81SBjoern A. Zeeb 		return (NULL);
22346b4cac81SBjoern A. Zeeb 	}
22356b4cac81SBjoern A. Zeeb 
22366b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22376b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
22386b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22396b4cac81SBjoern A. Zeeb 
22406b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
22416b4cac81SBjoern A. Zeeb 	changed = 0;
22426b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
22436b4cac81SBjoern A. Zeeb 
22446b4cac81SBjoern A. Zeeb 	/* conf_tx setup; default WME? */
22456b4cac81SBjoern A. Zeeb 
22466b4cac81SBjoern A. Zeeb 	/* Force MC init. */
22476b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
22486b4cac81SBjoern A. Zeeb 
22496b4cac81SBjoern A. Zeeb 	IMPROVE();
22506b4cac81SBjoern A. Zeeb 
22516b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
22526b4cac81SBjoern A. Zeeb 
22536b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
22546b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
22556b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2256*d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2257*d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
22586b4cac81SBjoern A. Zeeb 
22596b4cac81SBjoern A. Zeeb 	/* Key management. */
22606b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
22616b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
22626b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
22636b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
22646b4cac81SBjoern A. Zeeb #endif
22656b4cac81SBjoern A. Zeeb 	}
22666b4cac81SBjoern A. Zeeb 
22676b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
22686b4cac81SBjoern A. Zeeb 
22696b4cac81SBjoern A. Zeeb 	/* Complete setup. */
22706b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
22716b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
22726b4cac81SBjoern A. Zeeb 
22736b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
22746b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
22756b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
22766b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
22776b4cac81SBjoern A. Zeeb 
22786b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
22796b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
22806b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
22816b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
22826b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
22836b4cac81SBjoern A. Zeeb 	/* any others? */
22846b4cac81SBjoern A. Zeeb 	IMPROVE();
22856b4cac81SBjoern A. Zeeb 
22866b4cac81SBjoern A. Zeeb 	return (vap);
22876b4cac81SBjoern A. Zeeb }
22886b4cac81SBjoern A. Zeeb 
22896b4cac81SBjoern A. Zeeb static void
22906b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
22916b4cac81SBjoern A. Zeeb {
22926b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
22936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22976b4cac81SBjoern A. Zeeb 
22986b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22996b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23006b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
23016b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
23026b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23036b4cac81SBjoern A. Zeeb 
23046b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
23056b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
23066b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
23076b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
23086b4cac81SBjoern A. Zeeb 
23096b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
23106b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
23116b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
23126b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
23136b4cac81SBjoern A. Zeeb }
23146b4cac81SBjoern A. Zeeb 
23156b4cac81SBjoern A. Zeeb static void
23166b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
23176b4cac81SBjoern A. Zeeb {
23186b4cac81SBjoern A. Zeeb 
23196b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
23206b4cac81SBjoern A. Zeeb 	TRACEOK();
23216b4cac81SBjoern A. Zeeb }
23226b4cac81SBjoern A. Zeeb 
23236b4cac81SBjoern A. Zeeb static void
23246b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
23256b4cac81SBjoern A. Zeeb {
23266b4cac81SBjoern A. Zeeb 
23276b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
23286b4cac81SBjoern A. Zeeb }
23296b4cac81SBjoern A. Zeeb 
23306b4cac81SBjoern A. Zeeb static void
23316b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
23326b4cac81SBjoern A. Zeeb {
23336b4cac81SBjoern A. Zeeb 
23346b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
23356b4cac81SBjoern A. Zeeb }
23366b4cac81SBjoern A. Zeeb 
23376b4cac81SBjoern A. Zeeb /* Start / stop device. */
23386b4cac81SBjoern A. Zeeb static void
23396b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
23406b4cac81SBjoern A. Zeeb {
23416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23426b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23436b4cac81SBjoern A. Zeeb 	int error;
23446b4cac81SBjoern A. Zeeb 	bool start_all;
23456b4cac81SBjoern A. Zeeb 
23466b4cac81SBjoern A. Zeeb 	IMPROVE();
23476b4cac81SBjoern A. Zeeb 
23486b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
23496b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23506b4cac81SBjoern A. Zeeb 	start_all = false;
23516b4cac81SBjoern A. Zeeb 
23526b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
23536b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
23546b4cac81SBjoern A. Zeeb 		if (error == 0)
23556b4cac81SBjoern A. Zeeb 			start_all = true;
23566b4cac81SBjoern A. Zeeb 	} else {
23576b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
23586b4cac81SBjoern A. Zeeb 	}
23596b4cac81SBjoern A. Zeeb 
23606b4cac81SBjoern A. Zeeb 	if (start_all)
23616b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
23626b4cac81SBjoern A. Zeeb }
23636b4cac81SBjoern A. Zeeb 
23646b4cac81SBjoern A. Zeeb bool
23656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
23666b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
23676b4cac81SBjoern A. Zeeb {
23686b4cac81SBjoern A. Zeeb 	int i;
23696b4cac81SBjoern A. Zeeb 
23706b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
23716b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
23726b4cac81SBjoern A. Zeeb 			return (true);
23736b4cac81SBjoern A. Zeeb 	}
23746b4cac81SBjoern A. Zeeb 
23756b4cac81SBjoern A. Zeeb 	return (false);
23766b4cac81SBjoern A. Zeeb }
23776b4cac81SBjoern A. Zeeb 
23786b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
23796b4cac81SBjoern A. Zeeb bool
23806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
23816b4cac81SBjoern A. Zeeb {
23826b4cac81SBjoern A. Zeeb 	size_t x;
23836b4cac81SBjoern A. Zeeb 	uint8_t l;
23846b4cac81SBjoern A. Zeeb 
23856b4cac81SBjoern A. Zeeb 	x = *xp;
23866b4cac81SBjoern A. Zeeb 
23876b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
23886b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
23896b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
23906b4cac81SBjoern A. Zeeb 	x += 2 + l;
23916b4cac81SBjoern A. Zeeb 
23926b4cac81SBjoern A. Zeeb 	if (x > ies_len)
23936b4cac81SBjoern A. Zeeb 		return (false);
23946b4cac81SBjoern A. Zeeb 
23956b4cac81SBjoern A. Zeeb 	*xp = x;
23966b4cac81SBjoern A. Zeeb 	return (true);
23976b4cac81SBjoern A. Zeeb }
23986b4cac81SBjoern A. Zeeb 
23996b4cac81SBjoern A. Zeeb static int
24006b4cac81SBjoern A. Zeeb lkpi_ieee80211_probereq_ie_alloc(struct ieee80211vap *vap,
24016b4cac81SBjoern A. Zeeb     struct ieee80211com *ic, struct ieee80211_scan_ies *scan_ies,
24026b4cac81SBjoern A. Zeeb     const uint8_t *ssid, size_t ssidlen)
24036b4cac81SBjoern A. Zeeb {
24046b4cac81SBjoern A. Zeeb 
24056b4cac81SBjoern A. Zeeb 	return (ieee80211_probereq_ie(vap, ic,
24066b4cac81SBjoern A. Zeeb 	    &scan_ies->common_ies, &scan_ies->common_ie_len,
24076b4cac81SBjoern A. Zeeb 	    ssid, ssidlen, true));
24086b4cac81SBjoern A. Zeeb }
24096b4cac81SBjoern A. Zeeb 
24106b4cac81SBjoern A. Zeeb static void
24116b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
24126b4cac81SBjoern A. Zeeb {
24136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24146b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24166b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24176b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
24186b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
24196b4cac81SBjoern A. Zeeb 	int error;
24206b4cac81SBjoern A. Zeeb 
24216b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
24226b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0) {
24236b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
24246b4cac81SBjoern A. Zeeb 		return;
24256b4cac81SBjoern A. Zeeb 	}
24266b4cac81SBjoern A. Zeeb 
24276b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
24286b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
24296b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
2430d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
24316b4cac81SBjoern A. Zeeb 		return;
24326b4cac81SBjoern A. Zeeb 	}
24336b4cac81SBjoern A. Zeeb 
24346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24356b4cac81SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) {
2436d3ef7fb4SBjoern A. Zeeb sw_scan:
24376b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
24386b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
2439086be6a8SBjoern A. Zeeb 
2440086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
2441086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
2442086be6a8SBjoern A. Zeeb 
24436b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
24446b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
24456b4cac81SBjoern A. Zeeb 		IMPROVE();
24466b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
24476b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
24486b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
24496b4cac81SBjoern A. Zeeb 
24506b4cac81SBjoern A. Zeeb 	} else {
24516b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
24526b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
24536b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
24546b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
24556b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
24566b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
24576b4cac81SBjoern A. Zeeb 		int i;
24586b4cac81SBjoern A. Zeeb 
24596b4cac81SBjoern A. Zeeb 		ssids_len = ss->ss_nssid * sizeof(*ssids);;
24606b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
24616b4cac81SBjoern A. Zeeb 
24626b4cac81SBjoern A. Zeeb 		nchan = 0;
24636b4cac81SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++)
24646b4cac81SBjoern A. Zeeb 			nchan++;
24656b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
24666b4cac81SBjoern A. Zeeb 
24676b4cac81SBjoern A. Zeeb 		KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
24686b4cac81SBjoern A. Zeeb 		    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
24696b4cac81SBjoern A. Zeeb 		lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
24706b4cac81SBjoern A. Zeeb 		    s6ghzlen + chan_len, M_LKPI80211, M_WAITOK | M_ZERO);
24716b4cac81SBjoern A. Zeeb 
24726b4cac81SBjoern A. Zeeb 		error = lkpi_ieee80211_probereq_ie_alloc(vap, ic,
24736b4cac81SBjoern A. Zeeb 		    &hw_req->ies, NULL, -1);
24746b4cac81SBjoern A. Zeeb 		if (error != 0)
24756b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: probereq_ie returned %d\n",
24766b4cac81SBjoern A. Zeeb 			    __func__, error);
24776b4cac81SBjoern A. Zeeb 
24786b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
24796b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
24806b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
24816b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
24826b4cac81SBjoern A. Zeeb #if 0
24836b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
24846b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
24856b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
24866b4cac81SBjoern A. Zeeb #endif
24876b4cac81SBjoern A. Zeeb #ifdef __notyet__
24886b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
24896b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
24906b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
24916b4cac81SBjoern A. Zeeb #endif
24926b4cac81SBjoern A. Zeeb #if 0
24936b4cac81SBjoern A. Zeeb 		hw_req->req.ie_len = ;
24946b4cac81SBjoern A. Zeeb 		hw_req->req.ie = ;
24956b4cac81SBjoern A. Zeeb #endif
2496d3ef7fb4SBjoern A. Zeeb #if 0
2497d3ef7fb4SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len
2498d3ef7fb4SBjoern A. Zeeb 		hw->wiphy->max_scan_ssids
2499d3ef7fb4SBjoern A. Zeeb #endif
25006b4cac81SBjoern A. Zeeb 
25016b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
25026b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
25036b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
25046b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
25056b4cac81SBjoern A. Zeeb 			*(cpp + i) =
25066b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
25076b4cac81SBjoern A. Zeeb 		}
25086b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
25096b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
25106b4cac81SBjoern A. Zeeb 
25116b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
25126b4cac81SBjoern A. Zeeb 			lc->center_freq = c->ic_freq;
25136b4cac81SBjoern A. Zeeb 			/* lc->flags */
25146b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
25156b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
25166b4cac81SBjoern A. Zeeb 			/* lc-> ... */
25176b4cac81SBjoern A. Zeeb 			lc++;
25186b4cac81SBjoern A. Zeeb 		}
25196b4cac81SBjoern A. Zeeb 
25206b4cac81SBjoern A. Zeeb 		hw_req->req.n_ssids = ss->ss_nssid;
25216b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
25226b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
25236b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
25246b4cac81SBjoern A. Zeeb 			for (i = 0; i < ss->ss_nssid; i++) {
25256b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
25266b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
25276b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
25286b4cac81SBjoern A. Zeeb 				ssids++;
25296b4cac81SBjoern A. Zeeb 			}
25306b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
25316b4cac81SBjoern A. Zeeb 		} else {
25326b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
25336b4cac81SBjoern A. Zeeb 		}
25346b4cac81SBjoern A. Zeeb 
25356b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
25366b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
25376b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
25386b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
25396b4cac81SBjoern A. Zeeb 		/* s6gp->... */
25406b4cac81SBjoern A. Zeeb 
25416b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25426b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
25436b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
25446b4cac81SBjoern A. Zeeb 		if (error != 0) {
25456b4cac81SBjoern A. Zeeb 			free(hw_req->ies.common_ies, M_80211_VAP);
25466b4cac81SBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
25476b4cac81SBjoern A. Zeeb 			lhw->hw_req = NULL;
2548d3ef7fb4SBjoern A. Zeeb 
2549d3ef7fb4SBjoern A. Zeeb 			/*
2550d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
2551d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
2552d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
2553d3ef7fb4SBjoern A. Zeeb 			 */
2554d3ef7fb4SBjoern A. Zeeb 			if (error == 1)
2555d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
2556d3ef7fb4SBjoern A. Zeeb 
2557d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2558d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
2559d3ef7fb4SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
25606b4cac81SBjoern A. Zeeb 		}
25616b4cac81SBjoern A. Zeeb 	}
25626b4cac81SBjoern A. Zeeb }
25636b4cac81SBjoern A. Zeeb 
25646b4cac81SBjoern A. Zeeb static void
25656b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
25666b4cac81SBjoern A. Zeeb {
25676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25686b4cac81SBjoern A. Zeeb 
25696b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
25706b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
25716b4cac81SBjoern A. Zeeb 		return;
25726b4cac81SBjoern A. Zeeb 	}
25736b4cac81SBjoern A. Zeeb 
25746b4cac81SBjoern A. Zeeb 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
25756b4cac81SBjoern A. Zeeb 		/* Nothing to do. */
25766b4cac81SBjoern A. Zeeb 	} else {
25776b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
25786b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
25796b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
25806b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
25816b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
25826b4cac81SBjoern A. Zeeb 
25836b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
25846b4cac81SBjoern A. Zeeb 		ss = ic->ic_scan;
25856b4cac81SBjoern A. Zeeb 		vap = ss->ss_vap;
25866b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25876b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
25886b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
25896b4cac81SBjoern A. Zeeb 
25906b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
2591086be6a8SBjoern A. Zeeb 
2592086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
2593086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
25946b4cac81SBjoern A. Zeeb 	}
25956b4cac81SBjoern A. Zeeb }
25966b4cac81SBjoern A. Zeeb 
25976b4cac81SBjoern A. Zeeb static void
25986b4cac81SBjoern A. Zeeb lkpi_ic_scan_curchan_nada(struct ieee80211_scan_state *ss __unused,
25996b4cac81SBjoern A. Zeeb     unsigned long maxdwell __unused)
26006b4cac81SBjoern A. Zeeb {
26016b4cac81SBjoern A. Zeeb }
26026b4cac81SBjoern A. Zeeb 
26036b4cac81SBjoern A. Zeeb static void
26046b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
26056b4cac81SBjoern A. Zeeb {
26066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26076b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26086b4cac81SBjoern A. Zeeb 	int error;
26096b4cac81SBjoern A. Zeeb 
26106b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
26116b4cac81SBjoern A. Zeeb #ifdef __no_longer__
26126b4cac81SBjoern A. Zeeb 	/* For now only be concerned if scanning. */
26136b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
26146b4cac81SBjoern A. Zeeb 		IMPROVE();
26156b4cac81SBjoern A. Zeeb 		return;
26166b4cac81SBjoern A. Zeeb 	}
26176b4cac81SBjoern A. Zeeb #endif
26186b4cac81SBjoern A. Zeeb 
26196b4cac81SBjoern A. Zeeb 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
26206b4cac81SBjoern A. Zeeb 		/*
26216b4cac81SBjoern A. Zeeb 		 * AP scanning is taken care of by firmware, so only switch
26226b4cac81SBjoern A. Zeeb 		 * channels in monitor mode (maybe, maybe not; to be
26236b4cac81SBjoern A. Zeeb 		 * investigated at the right time).
26246b4cac81SBjoern A. Zeeb 		 */
26256b4cac81SBjoern A. Zeeb 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
26266b4cac81SBjoern A. Zeeb 			UNIMPLEMENTED;
26276b4cac81SBjoern A. Zeeb 		}
26286b4cac81SBjoern A. Zeeb 	} else {
26296b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c = ic->ic_curchan;
26306b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
26316b4cac81SBjoern A. Zeeb 		struct cfg80211_chan_def chandef;
26326b4cac81SBjoern A. Zeeb 
26336b4cac81SBjoern A. Zeeb 		if (c == NULL || c == IEEE80211_CHAN_ANYC ||
26346b4cac81SBjoern A. Zeeb 		    lhw->ops->config == NULL) {
26356b4cac81SBjoern A. Zeeb 			ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
26366b4cac81SBjoern A. Zeeb 			    c, lhw->ops->config);
26376b4cac81SBjoern A. Zeeb 			return;
26386b4cac81SBjoern A. Zeeb 		}
26396b4cac81SBjoern A. Zeeb 
26406b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
26416b4cac81SBjoern A. Zeeb 		if (chan == NULL) {
26426b4cac81SBjoern A. Zeeb 			ic_printf(ic, "%s: c %p chan %p\n", __func__,
26436b4cac81SBjoern A. Zeeb 			    c, chan);
26446b4cac81SBjoern A. Zeeb 			return;
26456b4cac81SBjoern A. Zeeb 		}
26466b4cac81SBjoern A. Zeeb 
26476b4cac81SBjoern A. Zeeb 		memset(&chandef, 0, sizeof(chandef));
26486b4cac81SBjoern A. Zeeb 		chandef.chan = chan;
26496b4cac81SBjoern A. Zeeb 		chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
26506b4cac81SBjoern A. Zeeb 		chandef.center_freq1 = chandef.chan->center_freq;
26516b4cac81SBjoern A. Zeeb 
26526b4cac81SBjoern A. Zeeb 		/* XXX max power for scanning? */
26536b4cac81SBjoern A. Zeeb 		IMPROVE();
26546b4cac81SBjoern A. Zeeb 
26556b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
26566b4cac81SBjoern A. Zeeb 		hw->conf.chandef = chandef;
26576b4cac81SBjoern A. Zeeb 
26586b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
26596b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP) {
26606b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
26616b4cac81SBjoern A. Zeeb 			    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
26626b4cac81SBjoern A. Zeeb 			/* XXX should we unroll to the previous chandef? */
26636b4cac81SBjoern A. Zeeb 			IMPROVE();
26646b4cac81SBjoern A. Zeeb 		} else {
26656b4cac81SBjoern A. Zeeb 			/* Update radiotap channels as well. */
26666b4cac81SBjoern A. Zeeb 			lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
26676b4cac81SBjoern A. Zeeb 			lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
26686b4cac81SBjoern A. Zeeb 			lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
26696b4cac81SBjoern A. Zeeb 			lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
26706b4cac81SBjoern A. Zeeb 		}
26716b4cac81SBjoern A. Zeeb 
26726b4cac81SBjoern A. Zeeb 		/* Currently PS is hard coded off! Not sure it belongs here. */
26736b4cac81SBjoern A. Zeeb 		IMPROVE();
26746b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
26756b4cac81SBjoern A. Zeeb 		    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
26766b4cac81SBjoern A. Zeeb 			hw->conf.flags &= ~IEEE80211_CONF_PS;
26776b4cac81SBjoern A. Zeeb 			error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
26786b4cac81SBjoern A. Zeeb 			if (error != 0 && error != EOPNOTSUPP)
26796b4cac81SBjoern A. Zeeb 				ic_printf(ic, "ERROR: %s: config %#0x returned "
26806b4cac81SBjoern A. Zeeb 				    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
26816b4cac81SBjoern A. Zeeb 				    error);
26826b4cac81SBjoern A. Zeeb 		}
26836b4cac81SBjoern A. Zeeb 	}
26846b4cac81SBjoern A. Zeeb }
26856b4cac81SBjoern A. Zeeb 
26866b4cac81SBjoern A. Zeeb static struct ieee80211_node *
26876b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
26886b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
26896b4cac81SBjoern A. Zeeb {
26906b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
26916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26924f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
26936b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26946b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26956b4cac81SBjoern A. Zeeb 
26966b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
26976b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
26986b4cac81SBjoern A. Zeeb 
26996b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
27004f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
27014f61ef8bSBjoern A. Zeeb 		return (NULL);
27024f61ef8bSBjoern A. Zeeb 
27036b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
27046b4cac81SBjoern A. Zeeb 	if (ni == NULL)
27056b4cac81SBjoern A. Zeeb 		return (NULL);
27066b4cac81SBjoern A. Zeeb 
27076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27084f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
27096b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
27106b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
27116b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
27126b4cac81SBjoern A. Zeeb 		return (NULL);
27136b4cac81SBjoern A. Zeeb 	}
27146b4cac81SBjoern A. Zeeb 
27156b4cac81SBjoern A. Zeeb 	return (ni);
27166b4cac81SBjoern A. Zeeb }
27176b4cac81SBjoern A. Zeeb 
27186b4cac81SBjoern A. Zeeb static int
27196b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
27206b4cac81SBjoern A. Zeeb {
27216b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27236b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27246b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27256b4cac81SBjoern A. Zeeb 	int error;
27266b4cac81SBjoern A. Zeeb 
27276b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
27286b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
27296b4cac81SBjoern A. Zeeb 
27306b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
27316b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
27326b4cac81SBjoern A. Zeeb 		if (error != 0)
27336b4cac81SBjoern A. Zeeb 			return (error);
27346b4cac81SBjoern A. Zeeb 	}
27356b4cac81SBjoern A. Zeeb 
27366b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
27376b4cac81SBjoern A. Zeeb 
27386b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
27396b4cac81SBjoern A. Zeeb 
27406b4cac81SBjoern A. Zeeb 	/* Now take the reference before linking it to the table. */
27416b4cac81SBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
27426b4cac81SBjoern A. Zeeb 
27436b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27446b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
27456b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27466b4cac81SBjoern A. Zeeb 
27476b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
27486b4cac81SBjoern A. Zeeb 	IMPROVE();
27496b4cac81SBjoern A. Zeeb 
27506b4cac81SBjoern A. Zeeb 	return (0);
27516b4cac81SBjoern A. Zeeb }
27526b4cac81SBjoern A. Zeeb 
27536b4cac81SBjoern A. Zeeb static void
27546b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
27556b4cac81SBjoern A. Zeeb {
27566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27586b4cac81SBjoern A. Zeeb 
27596b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
27606b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
27616b4cac81SBjoern A. Zeeb 
27626b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
27636b4cac81SBjoern A. Zeeb 	IMPROVE();
27646b4cac81SBjoern A. Zeeb 
27656b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
27666b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
27676b4cac81SBjoern A. Zeeb }
27686b4cac81SBjoern A. Zeeb 
27696b4cac81SBjoern A. Zeeb static void
27706b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
27716b4cac81SBjoern A. Zeeb {
27726b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27746b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27756b4cac81SBjoern A. Zeeb 
27766b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
27776b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
27786b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2779*d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
2780*d9f59799SBjoern A. Zeeb 		goto out;
27816b4cac81SBjoern A. Zeeb 
27826b4cac81SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
27836b4cac81SBjoern A. Zeeb 	IMPROVE();
27846b4cac81SBjoern A. Zeeb 
27856b4cac81SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
27866b4cac81SBjoern A. Zeeb #ifdef __notyet__
27876b4cac81SBjoern A. Zeeb 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
27886b4cac81SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
27896b4cac81SBjoern A. Zeeb #endif
27906b4cac81SBjoern A. Zeeb 	/* Drain taskq. */
27916b4cac81SBjoern A. Zeeb 
27926b4cac81SBjoern A. Zeeb 	/* Drain sta->txq[] */
27936b4cac81SBjoern A. Zeeb 	mtx_destroy(&lsta->txq_mtx);
27946b4cac81SBjoern A. Zeeb 
27956b4cac81SBjoern A. Zeeb 	/* Remove lsta if added_to_drv. */
27966b4cac81SBjoern A. Zeeb 	/* Remove lsta from vif */
27976b4cac81SBjoern A. Zeeb 
27986b4cac81SBjoern A. Zeeb 	/* remove ref from lsta node... */
27996b4cac81SBjoern A. Zeeb 
2800*d9f59799SBjoern A. Zeeb 	/* Free lsta. */
2801*d9f59799SBjoern A. Zeeb 
2802*d9f59799SBjoern A. Zeeb out:
28036b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
28046b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
28056b4cac81SBjoern A. Zeeb }
28066b4cac81SBjoern A. Zeeb 
28076b4cac81SBjoern A. Zeeb static int
28086b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
28096b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
28106b4cac81SBjoern A. Zeeb {
28116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28126b4cac81SBjoern A. Zeeb 
28136b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
28146b4cac81SBjoern A. Zeeb 
28156b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
28166b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
28176b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
28186b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
28196b4cac81SBjoern A. Zeeb 
28206b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
28216b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
28226b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
28236b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
28246b4cac81SBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
28266b4cac81SBjoern A. Zeeb 	return (0);
28276b4cac81SBjoern A. Zeeb }
28286b4cac81SBjoern A. Zeeb 
28296b4cac81SBjoern A. Zeeb static void
28306b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
28316b4cac81SBjoern A. Zeeb {
28326b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
28336b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
28346b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
28356b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
28366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28406b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28416b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
28426b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
28436b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
28446b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28456b4cac81SBjoern A. Zeeb 	void *buf;
28466b4cac81SBjoern A. Zeeb 	int ac;
28476b4cac81SBjoern A. Zeeb 
28486b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
28496b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28506b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX_DUMP)
28516b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
28526b4cac81SBjoern A. Zeeb #endif
28536b4cac81SBjoern A. Zeeb 
28546b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
28556b4cac81SBjoern A. Zeeb #ifndef TRY_HW_CRYPTO
28566b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
28576b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
28586b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
28596b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
28606b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
28616b4cac81SBjoern A. Zeeb 		if (k == NULL) {
28626b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
28636b4cac81SBjoern A. Zeeb 			m_freem(m);
28646b4cac81SBjoern A. Zeeb 			return;
28656b4cac81SBjoern A. Zeeb 		}
28666b4cac81SBjoern A. Zeeb 	}
28676b4cac81SBjoern A. Zeeb #endif
28686b4cac81SBjoern A. Zeeb 
28696b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
28706b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28716b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28726b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
28736b4cac81SBjoern A. Zeeb 
28746b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
28756b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
28766b4cac81SBjoern A. Zeeb 
28776b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
28786b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
28796b4cac81SBjoern A. Zeeb 		if (k != NULL)
28806b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
28816b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
28826b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
28836b4cac81SBjoern A. Zeeb 		IMPROVE();
28846b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
28856b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
28866b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
28876b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
28886b4cac81SBjoern A. Zeeb 		}
28896b4cac81SBjoern A. Zeeb 
28906b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
28916b4cac81SBjoern A. Zeeb 	}
28926b4cac81SBjoern A. Zeeb 
28936b4cac81SBjoern A. Zeeb 	/*
28946b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
28956b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
28963d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
28973d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
28986b4cac81SBjoern A. Zeeb 	 */
28996b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
29006b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
29016b4cac81SBjoern A. Zeeb 		printf("XXX ERROR %s: skb alloc failed\n", __func__);
29026b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
29036b4cac81SBjoern A. Zeeb 		m_freem(m);
29046b4cac81SBjoern A. Zeeb 		return;
29056b4cac81SBjoern A. Zeeb 	}
29066b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
29076b4cac81SBjoern A. Zeeb 
29086b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
29096b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
29106b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
29116b4cac81SBjoern A. Zeeb 	skb->m = m;
29126b4cac81SBjoern A. Zeeb #if 0
29136b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
29146b4cac81SBjoern A. Zeeb #else
29156b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
29166b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
29176b4cac81SBjoern A. Zeeb #endif
29186b4cac81SBjoern A. Zeeb 	/* Save the ni. */
29196b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
29206b4cac81SBjoern A. Zeeb 
29216b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
29226b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29236b4cac81SBjoern A. Zeeb 
29246b4cac81SBjoern A. Zeeb 	/* XXX-BZ review this at some point [4] vs. [8] vs. [16](TID). */
29256b4cac81SBjoern A. Zeeb 	ac = M_WME_GETAC(m);
29266b4cac81SBjoern A. Zeeb 	skb->priority = WME_AC_TO_TID(ac);
29276b4cac81SBjoern A. Zeeb 	ac = lkpi_ac_net_to_l80211(ac);
29286b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
29296b4cac81SBjoern A. Zeeb 
29306b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
29316b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
29326b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
29336b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
29346b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
29356b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
29366b4cac81SBjoern A. Zeeb 	info->hw_queue = ac;		/* XXX-BZ is this correct? */
29376b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
29386b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
29396b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
29406b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
29416b4cac81SBjoern A. Zeeb 
29426b4cac81SBjoern A. Zeeb 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
29436b4cac81SBjoern A. Zeeb 	if (lsta != NULL) {
29446b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
29456b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
29466b4cac81SBjoern A. Zeeb 		info->control.hw_key = lsta->kc;
29476b4cac81SBjoern A. Zeeb #endif
29486b4cac81SBjoern A. Zeeb 	} else {
29496b4cac81SBjoern A. Zeeb 		sta = NULL;
29506b4cac81SBjoern A. Zeeb 	}
29516b4cac81SBjoern A. Zeeb 
29526b4cac81SBjoern A. Zeeb 	IMPROVE();
29536b4cac81SBjoern A. Zeeb 
29546b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
29556b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
29566b4cac81SBjoern A. Zeeb 
29576b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[ac]);	/* XXX-BZ re-check */
29586b4cac81SBjoern A. Zeeb 		/*
29596b4cac81SBjoern A. Zeeb 		 * We currently do not use queues but do direct TX.
29606b4cac81SBjoern A. Zeeb 		 * The exception to the rule is initial packets, as we cannot
29616b4cac81SBjoern A. Zeeb 		 * TX until queues are allocated (at least for iwlwifi).
29626b4cac81SBjoern A. Zeeb 		 * So we wake_tx_queue in newstate and register any dequeue
29636b4cac81SBjoern A. Zeeb 		 * calls.  In the time until then we queue packets and
29646b4cac81SBjoern A. Zeeb 		 * let the driver deal with them.
29656b4cac81SBjoern A. Zeeb 		 */
29666b4cac81SBjoern A. Zeeb 		if (!ltxq->seen_dequeue) {
29676b4cac81SBjoern A. Zeeb 
29686b4cac81SBjoern A. Zeeb 			/* Prevent an ordering problem, likely other issues. */
29696b4cac81SBjoern A. Zeeb 			while (!skb_queue_empty(&ltxq->skbq)) {
29706b4cac81SBjoern A. Zeeb 				struct sk_buff *skb2;
29716b4cac81SBjoern A. Zeeb 
29726b4cac81SBjoern A. Zeeb 				skb2 = skb_dequeue(&ltxq->skbq);
29736b4cac81SBjoern A. Zeeb 				if (skb2 != NULL) {
29746b4cac81SBjoern A. Zeeb 					memset(&control, 0, sizeof(control));
29756b4cac81SBjoern A. Zeeb 					control.sta = sta;
29766b4cac81SBjoern A. Zeeb 					lkpi_80211_mo_tx(hw, &control, skb2);
29776b4cac81SBjoern A. Zeeb 				}
29786b4cac81SBjoern A. Zeeb 			}
29796b4cac81SBjoern A. Zeeb 			goto ops_tx;
29806b4cac81SBjoern A. Zeeb 		}
29816b4cac81SBjoern A. Zeeb 		if (0 && ltxq->seen_dequeue && skb_queue_empty(&ltxq->skbq))
29826b4cac81SBjoern A. Zeeb 			goto ops_tx;
29836b4cac81SBjoern A. Zeeb 
29846b4cac81SBjoern A. Zeeb 		skb_queue_tail(&ltxq->skbq, skb);
29856b4cac81SBjoern A. Zeeb 		if (debug_80211 & D80211_TRACE_TX)
29866b4cac81SBjoern A. Zeeb 			printf("%s:%d lsta %p sta %p ni %p %6D skb %p lxtq %p "
29876b4cac81SBjoern A. Zeeb 			    "qlen %u WAKE_TX_Q ac %d prio %u qmap %u\n",
29886b4cac81SBjoern A. Zeeb 			    __func__, __LINE__, lsta, sta, ni,
29896b4cac81SBjoern A. Zeeb 			    ni->ni_macaddr, ":", skb, ltxq,
29906b4cac81SBjoern A. Zeeb 			    skb_queue_len(&ltxq->skbq), ac,
29916b4cac81SBjoern A. Zeeb 			    skb->priority, skb->qmap);
29926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[ac]);	/* XXX-BZ */
29936b4cac81SBjoern A. Zeeb 		return;
29946b4cac81SBjoern A. Zeeb 	}
29956b4cac81SBjoern A. Zeeb 
29966b4cac81SBjoern A. Zeeb ops_tx:
29976b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
29986b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p sta %p ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
29996b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
30006b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
30016b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
30026b4cac81SBjoern A. Zeeb 	control.sta = sta;
30036b4cac81SBjoern A. Zeeb 
30046b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
30056b4cac81SBjoern A. Zeeb 	return;
30066b4cac81SBjoern A. Zeeb }
30076b4cac81SBjoern A. Zeeb 
30086b4cac81SBjoern A. Zeeb static void
30096b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
30106b4cac81SBjoern A. Zeeb {
30116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
30126b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
30136b4cac81SBjoern A. Zeeb 	struct mbufq mq;
30146b4cac81SBjoern A. Zeeb 	struct mbuf *m;
30156b4cac81SBjoern A. Zeeb 
30166b4cac81SBjoern A. Zeeb 	lsta = ctx;
30176b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
30186b4cac81SBjoern A. Zeeb 
30196b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
30206b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
30216b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
30226b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
30236b4cac81SBjoern A. Zeeb 
30246b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
30256b4cac81SBjoern A. Zeeb 
30266b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
30276b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
30286b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
30296b4cac81SBjoern A. Zeeb 
30306b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
30316b4cac81SBjoern A. Zeeb 	while (m != NULL) {
30326b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
30336b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
30346b4cac81SBjoern A. Zeeb 	}
30356b4cac81SBjoern A. Zeeb }
30366b4cac81SBjoern A. Zeeb 
30376b4cac81SBjoern A. Zeeb static int
30386b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
30396b4cac81SBjoern A. Zeeb {
30406b4cac81SBjoern A. Zeeb 
30416b4cac81SBjoern A. Zeeb 	/* XXX TODO */
30426b4cac81SBjoern A. Zeeb 	IMPROVE();
30436b4cac81SBjoern A. Zeeb 
30446b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
30456b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
30466b4cac81SBjoern A. Zeeb 
30476b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
30486b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
30496b4cac81SBjoern A. Zeeb }
30506b4cac81SBjoern A. Zeeb 
30516b4cac81SBjoern A. Zeeb static void
30526b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
30536b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
30546b4cac81SBjoern A. Zeeb {
30556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30576b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
30586b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
30596b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
30606b4cac81SBjoern A. Zeeb 
30616b4cac81SBjoern A. Zeeb 	/* Channels */
30626b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30646b4cac81SBjoern A. Zeeb 
30656b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
30666b4cac81SBjoern A. Zeeb 	nchans = 0;
30676b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
30686b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
30696b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
30706b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
30716b4cac81SBjoern A. Zeeb 		chan_flags = 0;
30726b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
30736b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
30746b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
30756b4cac81SBjoern A. Zeeb #ifdef __notyet__
30766b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
30776b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NG);
30786b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
30796b4cac81SBjoern A. Zeeb 		}
30806b4cac81SBjoern A. Zeeb #endif
30816b4cac81SBjoern A. Zeeb 
30826b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3083cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
30846b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
30856b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
30866b4cac81SBjoern A. Zeeb 
30876b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
30886b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
30896b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
30906b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
30916b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
30926b4cac81SBjoern A. Zeeb 				continue;
30936b4cac81SBjoern A. Zeeb 			}
30946b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
30956b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
30966b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
30976b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
30986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
30996b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
31006b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
31016b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
31026b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
31036b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
31046b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
31056b4cac81SBjoern A. Zeeb 
31066b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
31076b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
31086b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
31096b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
3110cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
3111cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
31126b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
31136b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
31146b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
31156b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
31166b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
3117cee56e77SBjoern A. Zeeb 			if (error != 0)
31186b4cac81SBjoern A. Zeeb 				break;
31196b4cac81SBjoern A. Zeeb 		}
31206b4cac81SBjoern A. Zeeb 	}
31216b4cac81SBjoern A. Zeeb 
31226b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
31236b4cac81SBjoern A. Zeeb 	nchans = 0;
31246b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
31256b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
31266b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
31276b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
31286b4cac81SBjoern A. Zeeb 		chan_flags = 0;
31296b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
31306b4cac81SBjoern A. Zeeb #ifdef __not_yet__
31316b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
31326b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NA);
31336b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
31346b4cac81SBjoern A. Zeeb 		}
31356b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
31366b4cac81SBjoern A. Zeeb 
31376b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
31386b4cac81SBjoern A. Zeeb 			ic->ic_vhtcaps =
31396b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
31406b4cac81SBjoern A. Zeeb 
31416b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
31426b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
31436b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
31446b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
31456b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
31466b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
31476b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
31486b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
31496b4cac81SBjoern A. Zeeb 		}
31506b4cac81SBjoern A. Zeeb #endif
31516b4cac81SBjoern A. Zeeb 
31526b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3153cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
31546b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
31556b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
31566b4cac81SBjoern A. Zeeb 
31576b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
31586b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
31596b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
31606b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
31616b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
31626b4cac81SBjoern A. Zeeb 				continue;
31636b4cac81SBjoern A. Zeeb 			}
31646b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
31656b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
31666b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
31676b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
31686b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
31696b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
31706b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
31716b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
31726b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
31736b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
31746b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
31756b4cac81SBjoern A. Zeeb 
31766b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
31776b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
31786b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
31796b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
3180cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
3181cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
31826b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
31836b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
31846b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
31856b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
31866b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
3187cee56e77SBjoern A. Zeeb 			if (error != 0)
31886b4cac81SBjoern A. Zeeb 				break;
31896b4cac81SBjoern A. Zeeb 		}
31906b4cac81SBjoern A. Zeeb 	}
31916b4cac81SBjoern A. Zeeb }
31926b4cac81SBjoern A. Zeeb 
31936b4cac81SBjoern A. Zeeb static void *
31946b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
31956b4cac81SBjoern A. Zeeb {
31966b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
31976b4cac81SBjoern A. Zeeb 
31986b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
31996b4cac81SBjoern A. Zeeb 	if (ic == NULL)
32006b4cac81SBjoern A. Zeeb 		return (NULL);
32016b4cac81SBjoern A. Zeeb 
32026b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
32036b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
32046b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
32056b4cac81SBjoern A. Zeeb 
32066b4cac81SBjoern A. Zeeb 	return (ic);
32076b4cac81SBjoern A. Zeeb }
32086b4cac81SBjoern A. Zeeb 
32096b4cac81SBjoern A. Zeeb struct ieee80211_hw *
32106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
32116b4cac81SBjoern A. Zeeb {
32126b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32146b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
32156b4cac81SBjoern A. Zeeb 
32166b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
32176b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
32186b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
32196b4cac81SBjoern A. Zeeb 		return (NULL);
32206b4cac81SBjoern A. Zeeb 
32216b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
32226b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
3223652e22d3SBjoern A. Zeeb 
32246b4cac81SBjoern A. Zeeb 	mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
32256b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
32266b4cac81SBjoern A. Zeeb 
32276b4cac81SBjoern A. Zeeb 	/*
32286b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
32296b4cac81SBjoern A. Zeeb 	 * not initialized.
32306b4cac81SBjoern A. Zeeb 	 */
32316b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32326b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
3233086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
32346b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
32356b4cac81SBjoern A. Zeeb 
32366b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
32376b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
32386b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
32396b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
32406b4cac81SBjoern A. Zeeb 		return (NULL);
32416b4cac81SBjoern A. Zeeb 	}
32426b4cac81SBjoern A. Zeeb 
32436b4cac81SBjoern A. Zeeb 	IMPROVE();
32446b4cac81SBjoern A. Zeeb 
32456b4cac81SBjoern A. Zeeb 	return (hw);
32466b4cac81SBjoern A. Zeeb }
32476b4cac81SBjoern A. Zeeb 
32486b4cac81SBjoern A. Zeeb void
32496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
32506b4cac81SBjoern A. Zeeb {
32516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32526b4cac81SBjoern A. Zeeb 
32536b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
32546b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
32556b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
32566b4cac81SBjoern A. Zeeb 
32576b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
32586b4cac81SBjoern A. Zeeb 	mtx_destroy(&lhw->mtx);
32596b4cac81SBjoern A. Zeeb 	IMPROVE();
32606b4cac81SBjoern A. Zeeb }
32616b4cac81SBjoern A. Zeeb 
32626b4cac81SBjoern A. Zeeb void
32636b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
32646b4cac81SBjoern A. Zeeb {
32656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32666b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
32676b4cac81SBjoern A. Zeeb 
32686b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
32696b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
32706b4cac81SBjoern A. Zeeb 
32716b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
32726b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
32736b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
32746b4cac81SBjoern A. Zeeb 
32756b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
32766b4cac81SBjoern A. Zeeb }
32776b4cac81SBjoern A. Zeeb 
32786b4cac81SBjoern A. Zeeb struct ieee80211_hw *
32796b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
32806b4cac81SBjoern A. Zeeb {
32816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32826b4cac81SBjoern A. Zeeb 
32836b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
32846b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
32856b4cac81SBjoern A. Zeeb }
32866b4cac81SBjoern A. Zeeb 
32876b4cac81SBjoern A. Zeeb static void
32886b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
32896b4cac81SBjoern A. Zeeb {
32906b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
32916b4cac81SBjoern A. Zeeb 
32926b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
32936b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
32946b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
32956b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
32966b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
32976b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
32986b4cac81SBjoern A. Zeeb }
32996b4cac81SBjoern A. Zeeb 
33002e183d99SBjoern A. Zeeb int
33016b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
33026b4cac81SBjoern A. Zeeb {
33036b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3305c5b96b3eSBjoern A. Zeeb 	int band, i;
33066b4cac81SBjoern A. Zeeb 
33076b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
33086b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
33096b4cac81SBjoern A. Zeeb 
3310652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
3311652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3312652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
3313652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
3314652e22d3SBjoern A. Zeeb 
33156b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
33166b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
33176b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
33186b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
33196b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
33206b4cac81SBjoern A. Zeeb 		/* We take the first one. */
33216b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
33226b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
33236b4cac81SBjoern A. Zeeb 	} else {
33246b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
33256b4cac81SBjoern A. Zeeb 	}
33266b4cac81SBjoern A. Zeeb 
33273d09d310SBjoern A. Zeeb #ifdef __not_yet__
33283d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
33296b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
33303d09d310SBjoern A. Zeeb #endif
33316b4cac81SBjoern A. Zeeb 
33326b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
33336b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
33346b4cac81SBjoern A. Zeeb 
33356b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
33366b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
33376b4cac81SBjoern A. Zeeb 	ic->ic_caps =
33386b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
33396b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
33406b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
33416b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
33426b4cac81SBjoern A. Zeeb #if 0
33436b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
33446b4cac81SBjoern A. Zeeb #endif
33456b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
33466b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
33476b4cac81SBjoern A. Zeeb 	    ;
33486b4cac81SBjoern A. Zeeb #if 0
33496b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
33506b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
33516b4cac81SBjoern A. Zeeb #endif
3352cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
3353cc4e78d5SBjoern A. Zeeb 		/*
3354cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
3355cc4e78d5SBjoern A. Zeeb 		 *
3356cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3357cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
3358cc4e78d5SBjoern A. Zeeb 		 * the flag.
3359cc4e78d5SBjoern A. Zeeb 		 */
33606b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
33616b4cac81SBjoern A. Zeeb 	}
33626b4cac81SBjoern A. Zeeb 
33636b4cac81SBjoern A. Zeeb #ifdef __notyet__
33646b4cac81SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
33656b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
33666b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
33676b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_MAXAMSDU_3839
33686b4cac81SBjoern A. Zeeb 						/* max A-MSDU length */
33696b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
33706b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
33716b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
33726b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
33736b4cac81SBjoern A. Zeeb #endif
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
33766b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
33776b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
33786b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
33796b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
33806b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
33816b4cac81SBjoern A. Zeeb 	}
33826b4cac81SBjoern A. Zeeb #endif
33836b4cac81SBjoern A. Zeeb 
33846b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
33856b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
33866b4cac81SBjoern A. Zeeb 
33876b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
33886b4cac81SBjoern A. Zeeb 
33896b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
33906b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
33916b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
33926b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
33936b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
33946b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
33956b4cac81SBjoern A. Zeeb 	if (lhw->ops->hw_scan &&
33966b4cac81SBjoern A. Zeeb 	    ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS))
33976b4cac81SBjoern A. Zeeb 		ic->ic_scan_curchan = lkpi_ic_scan_curchan_nada;
33986b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
33996b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
34006b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
34016b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
34026b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
34036b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
34046b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
34056b4cac81SBjoern A. Zeeb 
34066b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
34076b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
34086b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
34096b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
34106b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
34116b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
34126b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
34136b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
34146b4cac81SBjoern A. Zeeb 
34156b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
34166b4cac81SBjoern A. Zeeb 
3417c5b96b3eSBjoern A. Zeeb 	/*
3418c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
3419c5b96b3eSBjoern A. Zeeb 	 * expect one.
3420c5b96b3eSBjoern A. Zeeb 	 */
3421c5b96b3eSBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS &&
3422c5b96b3eSBjoern A. Zeeb 	    hw->conf.chandef.chan == NULL; band++) {
3423c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
3424c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
3425c5b96b3eSBjoern A. Zeeb 
3426c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3427c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3428c5b96b3eSBjoern A. Zeeb 			continue;
3429c5b96b3eSBjoern A. Zeeb 
3430c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
3431c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3432c5b96b3eSBjoern A. Zeeb 			struct cfg80211_chan_def chandef;
3433c5b96b3eSBjoern A. Zeeb 
3434c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3435c5b96b3eSBjoern A. Zeeb 				continue;
3436c5b96b3eSBjoern A. Zeeb 
3437c5b96b3eSBjoern A. Zeeb 			memset(&chandef, 0, sizeof(chandef));
3438c5b96b3eSBjoern A. Zeeb 			cfg80211_chandef_create(&chandef, &channels[i],
3439c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
3440c5b96b3eSBjoern A. Zeeb 			hw->conf.chandef = chandef;
3441c5b96b3eSBjoern A. Zeeb 			break;
3442c5b96b3eSBjoern A. Zeeb 		}
3443c5b96b3eSBjoern A. Zeeb 	}
3444c5b96b3eSBjoern A. Zeeb 
34456b4cac81SBjoern A. Zeeb 	if (bootverbose)
34466b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
34472e183d99SBjoern A. Zeeb 
34482e183d99SBjoern A. Zeeb 	return (0);
34496b4cac81SBjoern A. Zeeb }
34506b4cac81SBjoern A. Zeeb 
34516b4cac81SBjoern A. Zeeb void
34526b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
34536b4cac81SBjoern A. Zeeb {
34546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34556b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
34586b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
34596b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
34606b4cac81SBjoern A. Zeeb }
34616b4cac81SBjoern A. Zeeb 
34626b4cac81SBjoern A. Zeeb void
34636b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
34646b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
34656b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
34666b4cac81SBjoern A. Zeeb     void *arg)
34676b4cac81SBjoern A. Zeeb {
34686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34696b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34706b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
347161a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
34726b4cac81SBjoern A. Zeeb 
34736b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
34746b4cac81SBjoern A. Zeeb 
34756b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
34766b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
347761a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
34786b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
34796b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
34806b4cac81SBjoern A. Zeeb 		    __func__, flags);
34816b4cac81SBjoern A. Zeeb 	}
34826b4cac81SBjoern A. Zeeb 
34836b4cac81SBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
34846b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
348561a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
34866b4cac81SBjoern A. Zeeb 
34876b4cac81SBjoern A. Zeeb 	if (atomic)
34886b4cac81SBjoern A. Zeeb 		LKPI_80211_LHW_LOCK(lhw);
34896b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
34906b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
34916b4cac81SBjoern A. Zeeb 
34926b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
34936b4cac81SBjoern A. Zeeb 
34946b4cac81SBjoern A. Zeeb 		/*
34956b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
34966b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
34976b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
34986b4cac81SBjoern A. Zeeb 		 * driver does not know about.
34996b4cac81SBjoern A. Zeeb 		 */
35006b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
35016b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
35026b4cac81SBjoern A. Zeeb 			continue;
35036b4cac81SBjoern A. Zeeb 
35046b4cac81SBjoern A. Zeeb 		/*
350561a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
350661a68e50SBjoern A. Zeeb 		 * if we haven't yet.
350761a68e50SBjoern A. Zeeb 		 */
350861a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
350961a68e50SBjoern A. Zeeb 			continue;
351061a68e50SBjoern A. Zeeb 
351161a68e50SBjoern A. Zeeb 		/*
35126b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
35136b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
35146b4cac81SBjoern A. Zeeb 		 */
35156b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
35166b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
35176b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
35186b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
35196b4cac81SBjoern A. Zeeb 	}
35206b4cac81SBjoern A. Zeeb 	if (atomic)
35216b4cac81SBjoern A. Zeeb 		LKPI_80211_LHW_UNLOCK(lhw);
35226b4cac81SBjoern A. Zeeb }
35236b4cac81SBjoern A. Zeeb 
35246b4cac81SBjoern A. Zeeb void
35256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
35266b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
35276b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
35286b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
35296b4cac81SBjoern A. Zeeb     void *arg)
35306b4cac81SBjoern A. Zeeb {
35316b4cac81SBjoern A. Zeeb 
35326b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35336b4cac81SBjoern A. Zeeb }
35346b4cac81SBjoern A. Zeeb 
35356b4cac81SBjoern A. Zeeb void
35366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
35376b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
35386b4cac81SBjoern A. Zeeb 	void *),
35396b4cac81SBjoern A. Zeeb     void *arg)
35406b4cac81SBjoern A. Zeeb {
35416b4cac81SBjoern A. Zeeb 
35426b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35436b4cac81SBjoern A. Zeeb }
35446b4cac81SBjoern A. Zeeb 
35456b4cac81SBjoern A. Zeeb void
35466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
35476b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
35486b4cac81SBjoern A. Zeeb {
35496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35506b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35516b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35526b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
35536b4cac81SBjoern A. Zeeb 
35546b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
35556b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
35566b4cac81SBjoern A. Zeeb 
35576b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
35586b4cac81SBjoern A. Zeeb 
35596b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
35606b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
35616b4cac81SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
35636b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
35646b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
35656b4cac81SBjoern A. Zeeb 				continue;
35666b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
35676b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
35686b4cac81SBjoern A. Zeeb 		}
35696b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
35706b4cac81SBjoern A. Zeeb 	}
35716b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
35726b4cac81SBjoern A. Zeeb }
35736b4cac81SBjoern A. Zeeb 
35746b4cac81SBjoern A. Zeeb int
35756b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
35766b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
35776b4cac81SBjoern A. Zeeb {
35786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35796b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35806b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
35816b4cac81SBjoern A. Zeeb 
35826b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
35836b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
35846b4cac81SBjoern A. Zeeb 
35856b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
35866b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
35876b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
35886b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
35896b4cac81SBjoern A. Zeeb 	}
35906b4cac81SBjoern A. Zeeb 
35916b4cac81SBjoern A. Zeeb 	TODO();
35926b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
35936b4cac81SBjoern A. Zeeb 
35946b4cac81SBjoern A. Zeeb 	return (0);
35956b4cac81SBjoern A. Zeeb }
35966b4cac81SBjoern A. Zeeb 
35976b4cac81SBjoern A. Zeeb void
35986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
35996b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
36006b4cac81SBjoern A. Zeeb {
36016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36026b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36036b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
36046b4cac81SBjoern A. Zeeb 
36056b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
36066b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
36076b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
36086b4cac81SBjoern A. Zeeb 
36096b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
36106b4cac81SBjoern A. Zeeb 
36116b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
36126b4cac81SBjoern A. Zeeb 	free(lhw->hw_req->ies.common_ies, M_80211_VAP);
36136b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
36146b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
36156b4cac81SBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_SCAN_RUNNING;
36166b4cac81SBjoern A. Zeeb 	wakeup(lhw);
36176b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
36186b4cac81SBjoern A. Zeeb 
36196b4cac81SBjoern A. Zeeb 	return;
36206b4cac81SBjoern A. Zeeb }
36216b4cac81SBjoern A. Zeeb 
36226b4cac81SBjoern A. Zeeb void
36236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
36246b4cac81SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused)
36256b4cac81SBjoern A. Zeeb {
36266b4cac81SBjoern A. Zeeb 	struct epoch_tracker et;
36276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36286b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36296b4cac81SBjoern A. Zeeb 	struct mbuf *m;
36306b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
36316b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
36326b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
36336b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
36346b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36356b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
36366b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
36376b4cac81SBjoern A. Zeeb 	int i, offset, ok, type;
3638c0cadd99SBjoern A. Zeeb 	bool is_beacon;
36396b4cac81SBjoern A. Zeeb 
36406b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
36416b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
36426b4cac81SBjoern A. Zeeb 		IMPROVE();
36436b4cac81SBjoern A. Zeeb 		goto err;
36446b4cac81SBjoern A. Zeeb 	}
36456b4cac81SBjoern A. Zeeb 
36466b4cac81SBjoern A. Zeeb 	/*
36476b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
36486b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
36496b4cac81SBjoern A. Zeeb 	 */
36506b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
36516b4cac81SBjoern A. Zeeb 	if (m == NULL)
36526b4cac81SBjoern A. Zeeb 		goto err;
36536b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
36546b4cac81SBjoern A. Zeeb 
36556b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
36566b4cac81SBjoern A. Zeeb 	offset = m->m_len;
36576b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
36586b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
36596b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
36606b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
36616b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
36626b4cac81SBjoern A. Zeeb 	}
36636b4cac81SBjoern A. Zeeb 
36646b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
36656b4cac81SBjoern A. Zeeb 
36666b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
3667c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
3668c0cadd99SBjoern A. Zeeb 
3669c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
3670c0cadd99SBjoern A. Zeeb 	if (is_beacon && (debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
36716b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
36726b4cac81SBjoern A. Zeeb 
36736b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
36746b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
3675c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
36766b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
36776b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
36786b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
3679c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
36806b4cac81SBjoern A. Zeeb 
36816b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX_DUMP)
36826b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
36836b4cac81SBjoern A. Zeeb 
36846b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
36856b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
3686c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
36876b4cac81SBjoern A. Zeeb 		    "%u band %u, %u %u %u %u, %u, %#x %#x %#x %#x %u %u %u\n",
36886b4cac81SBjoern A. Zeeb 			__func__,
3689c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
3690c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
36916b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
36926b4cac81SBjoern A. Zeeb 			rx_status->flag,
36936b4cac81SBjoern A. Zeeb 			rx_status->freq,
36946b4cac81SBjoern A. Zeeb 			rx_status->bw,
36956b4cac81SBjoern A. Zeeb 			rx_status->encoding,
36966b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
36976b4cac81SBjoern A. Zeeb 			rx_status->band,
36986b4cac81SBjoern A. Zeeb 			rx_status->chains,
36996b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
37006b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
37016b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
37026b4cac81SBjoern A. Zeeb 			rx_status->signal,
37036b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
37046b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
37056b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
37066b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
37076b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
37086b4cac81SBjoern A. Zeeb 			rx_status->nss,
37096b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
37106b4cac81SBjoern A. Zeeb no_trace_beacons:
37116b4cac81SBjoern A. Zeeb #endif
37126b4cac81SBjoern A. Zeeb 
37136b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
37146b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
37156b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
37166b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
37176b4cac81SBjoern A. Zeeb 		rx_stats.c_rssi = rx_status->signal;
37186b4cac81SBjoern A. Zeeb 	else
37196b4cac81SBjoern A. Zeeb 		rx_stats.c_rssi = 0;			/* XXX */
37206b4cac81SBjoern A. Zeeb 	rx_stats.c_nf = -96;				/* XXX */
37216b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
37226b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
37236b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
37246b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
37256b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
37266b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
37276b4cac81SBjoern A. Zeeb 
37286b4cac81SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor. */
37296b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
37306b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
37316b4cac81SBjoern A. Zeeb 
37326b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
37336b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
37346b4cac81SBjoern A. Zeeb 
37356b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
37366b4cac81SBjoern A. Zeeb 	if (ok == 0) {
37376b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
37386b4cac81SBjoern A. Zeeb 		goto err;
37396b4cac81SBjoern A. Zeeb 	}
37406b4cac81SBjoern A. Zeeb 
37416b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
37426b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
37436b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
37446b4cac81SBjoern A. Zeeb 	} else {
3745c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
3746c8dafefaSBjoern A. Zeeb 
37476b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
37486b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
37496b4cac81SBjoern A. Zeeb 		if (ni != NULL)
37506b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
37516b4cac81SBjoern A. Zeeb 	}
37526b4cac81SBjoern A. Zeeb 
37536b4cac81SBjoern A. Zeeb 	if (ni != NULL)
37546b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
37556b4cac81SBjoern A. Zeeb 	else
37566b4cac81SBjoern A. Zeeb 		/*
37576b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
37586b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
37596b4cac81SBjoern A. Zeeb 		 */
37606b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
37616b4cac81SBjoern A. Zeeb 
37626b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
3763c0cadd99SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
3764c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
3765c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
37666b4cac81SBjoern A. Zeeb 
3767c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
3768c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
3769c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
3770c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
3771c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
3772c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
3773c8dafefaSBjoern A. Zeeb 
3774c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
3775c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
3776c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
3777c8dafefaSBjoern A. Zeeb 
3778c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
3779c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3780c8dafefaSBjoern A. Zeeb 
3781c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
3782c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
3783c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
3784c8dafefaSBjoern A. Zeeb 		/*
3785c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
3786c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
3787c8dafefaSBjoern A. Zeeb 		 */
3788c8dafefaSBjoern A. Zeeb 	}
3789c8dafefaSBjoern A. Zeeb skip_device_ts:
3790c8dafefaSBjoern A. Zeeb 
37916b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
37926b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
37936b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
37946b4cac81SBjoern A. Zeeb 
37956b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
37966b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
37976b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
37986b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
37996b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
38006b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
38016b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
38026b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
38036b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
38046b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
38056b4cac81SBjoern A. Zeeb #endif
38066b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
38076b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
38086b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
38096b4cac81SBjoern A. Zeeb 		IMPROVE();
38106b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
38116b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
38126b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
38136b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
38146b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rx_stats.c_rssi;
38156b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
38166b4cac81SBjoern A. Zeeb 	}
38176b4cac81SBjoern A. Zeeb 
38186b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
38196b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
38206b4cac81SBjoern A. Zeeb 
38216b4cac81SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
38226b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
38236b4cac81SBjoern A. Zeeb 		type = ieee80211_input_mimo(ni, m);
38246b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
38256b4cac81SBjoern A. Zeeb 	} else {
38266b4cac81SBjoern A. Zeeb 		type = ieee80211_input_mimo_all(ic, m);
38276b4cac81SBjoern A. Zeeb 	}
38286b4cac81SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
38296b4cac81SBjoern A. Zeeb 
38306b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
38316b4cac81SBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, type);
38326b4cac81SBjoern A. Zeeb 
38336b4cac81SBjoern A. Zeeb 	IMPROVE();
38346b4cac81SBjoern A. Zeeb 
38356b4cac81SBjoern A. Zeeb err:
38366b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
38376b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
38386b4cac81SBjoern A. Zeeb }
38396b4cac81SBjoern A. Zeeb 
38406b4cac81SBjoern A. Zeeb uint8_t
38416b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr)
38426b4cac81SBjoern A. Zeeb {
38436b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
38446b4cac81SBjoern A. Zeeb 
38456b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
38466b4cac81SBjoern A. Zeeb 	return (ieee80211_gettid(wh));
38476b4cac81SBjoern A. Zeeb }
38486b4cac81SBjoern A. Zeeb 
38496b4cac81SBjoern A. Zeeb struct wiphy *
38506b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
38516b4cac81SBjoern A. Zeeb {
38526b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
38536b4cac81SBjoern A. Zeeb 
38546b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
38556b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
38566b4cac81SBjoern A. Zeeb 		return (NULL);
38576b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
38586b4cac81SBjoern A. Zeeb 
38596b4cac81SBjoern A. Zeeb 	/* XXX TODO */
38606b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
38616b4cac81SBjoern A. Zeeb }
38626b4cac81SBjoern A. Zeeb 
38636b4cac81SBjoern A. Zeeb void
38646b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
38656b4cac81SBjoern A. Zeeb {
38666b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
38676b4cac81SBjoern A. Zeeb 
38686b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
38696b4cac81SBjoern A. Zeeb 		return;
38706b4cac81SBjoern A. Zeeb 
38716b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
38726b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
38736b4cac81SBjoern A. Zeeb }
38746b4cac81SBjoern A. Zeeb 
38756b4cac81SBjoern A. Zeeb uint32_t
38766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
38776b4cac81SBjoern A. Zeeb     enum nl80211_band band)
38786b4cac81SBjoern A. Zeeb {
38796b4cac81SBjoern A. Zeeb 
38806b4cac81SBjoern A. Zeeb 	switch (band) {
38816b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
38826b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
38836b4cac81SBjoern A. Zeeb 		break;
38846b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
38856b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
38866b4cac81SBjoern A. Zeeb 		break;
38876b4cac81SBjoern A. Zeeb 	default:
38886b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
38896b4cac81SBjoern A. Zeeb 		break;
38906b4cac81SBjoern A. Zeeb 	}
38916b4cac81SBjoern A. Zeeb 
38926b4cac81SBjoern A. Zeeb 	return (0);
38936b4cac81SBjoern A. Zeeb }
38946b4cac81SBjoern A. Zeeb 
38956b4cac81SBjoern A. Zeeb uint32_t
38966b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
38976b4cac81SBjoern A. Zeeb {
38986b4cac81SBjoern A. Zeeb 
38996b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
39006b4cac81SBjoern A. Zeeb }
39016b4cac81SBjoern A. Zeeb 
39026b4cac81SBjoern A. Zeeb static struct lkpi_sta *
39036b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
39046b4cac81SBjoern A. Zeeb {
39056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
39066b4cac81SBjoern A. Zeeb 
39076b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
39086b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
39096b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
39106b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
39116b4cac81SBjoern A. Zeeb 			return (lsta);
39126b4cac81SBjoern A. Zeeb 		}
39136b4cac81SBjoern A. Zeeb 	}
39146b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
39156b4cac81SBjoern A. Zeeb 
39166b4cac81SBjoern A. Zeeb 	return (NULL);
39176b4cac81SBjoern A. Zeeb }
39186b4cac81SBjoern A. Zeeb 
39196b4cac81SBjoern A. Zeeb struct ieee80211_sta *
39206b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
39216b4cac81SBjoern A. Zeeb {
39226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39236b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
39246b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
39256b4cac81SBjoern A. Zeeb 
39266b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
39276b4cac81SBjoern A. Zeeb 
39286b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
39296b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
39306b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
39316b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
39326b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
39336b4cac81SBjoern A. Zeeb 			return (sta);
39346b4cac81SBjoern A. Zeeb 		}
39356b4cac81SBjoern A. Zeeb 	}
39366b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
39376b4cac81SBjoern A. Zeeb 	return (NULL);
39386b4cac81SBjoern A. Zeeb }
39396b4cac81SBjoern A. Zeeb 
39406b4cac81SBjoern A. Zeeb struct ieee80211_sta *
39412e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
39422e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
39436b4cac81SBjoern A. Zeeb {
39446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39466b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39476b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39486b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
39496b4cac81SBjoern A. Zeeb 
39506b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
39516b4cac81SBjoern A. Zeeb 	sta = NULL;
39526b4cac81SBjoern A. Zeeb 
39536b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
39546b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
39556b4cac81SBjoern A. Zeeb 
39566b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
39576b4cac81SBjoern A. Zeeb 
39586b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
39596b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
39606b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
39616b4cac81SBjoern A. Zeeb 			continue;
39626b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
39636b4cac81SBjoern A. Zeeb 		if (sta != NULL)
39646b4cac81SBjoern A. Zeeb 			break;
39656b4cac81SBjoern A. Zeeb 	}
39666b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
39676b4cac81SBjoern A. Zeeb 
39686b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
39696b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
39706b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
39716b4cac81SBjoern A. Zeeb 			return (NULL);
39726b4cac81SBjoern A. Zeeb 	}
39736b4cac81SBjoern A. Zeeb 
39746b4cac81SBjoern A. Zeeb 	return (sta);
39756b4cac81SBjoern A. Zeeb }
39766b4cac81SBjoern A. Zeeb 
39776b4cac81SBjoern A. Zeeb struct sk_buff *
39786b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
39796b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
39806b4cac81SBjoern A. Zeeb {
39816b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
39826b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
39836b4cac81SBjoern A. Zeeb 
39846b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
39856b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
39866b4cac81SBjoern A. Zeeb 
39876b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
39886b4cac81SBjoern A. Zeeb 
39896b4cac81SBjoern A. Zeeb 	return (skb);
39906b4cac81SBjoern A. Zeeb }
39916b4cac81SBjoern A. Zeeb 
39926b4cac81SBjoern A. Zeeb void
39936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
39946b4cac81SBjoern A. Zeeb     uint64_t *frame_cnt, uint64_t *byte_cnt)
39956b4cac81SBjoern A. Zeeb {
39966b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
39976b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
39986b4cac81SBjoern A. Zeeb 	uint64_t fc, bc;
39996b4cac81SBjoern A. Zeeb 
40006b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
40016b4cac81SBjoern A. Zeeb 
40026b4cac81SBjoern A. Zeeb 	fc = bc = 0;
40036b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
40046b4cac81SBjoern A. Zeeb 		fc++;
40056b4cac81SBjoern A. Zeeb 		bc += skb->len;
40066b4cac81SBjoern A. Zeeb 	}
40076b4cac81SBjoern A. Zeeb 	if (frame_cnt)
40086b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
40096b4cac81SBjoern A. Zeeb 	if (byte_cnt)
40106b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
40116b4cac81SBjoern A. Zeeb 
40126b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
40136b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
40146b4cac81SBjoern A. Zeeb 	IMPROVE();
40156b4cac81SBjoern A. Zeeb }
40166b4cac81SBjoern A. Zeeb 
40176b4cac81SBjoern A. Zeeb /*
40186b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
40196b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
40206b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
40216b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
40226b4cac81SBjoern A. Zeeb  */
40236b4cac81SBjoern A. Zeeb void
40246b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
40256b4cac81SBjoern A. Zeeb     int status)
40266b4cac81SBjoern A. Zeeb {
40276b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
40286b4cac81SBjoern A. Zeeb 	struct mbuf *m;
40296b4cac81SBjoern A. Zeeb 
40306b4cac81SBjoern A. Zeeb 	m = skb->m;
40316b4cac81SBjoern A. Zeeb 	skb->m = NULL;
40326b4cac81SBjoern A. Zeeb 
40336b4cac81SBjoern A. Zeeb 	if (m != NULL) {
40346b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
40356b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
40366b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
40376b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
40386b4cac81SBjoern A. Zeeb 	}
40396b4cac81SBjoern A. Zeeb 
40406b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
40416b4cac81SBjoern A. Zeeb }
40426b4cac81SBjoern A. Zeeb 
40436b4cac81SBjoern A. Zeeb /*
40446b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
40456b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
40466b4cac81SBjoern A. Zeeb  * mbufs this should go away.
40476b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
40486b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
40496b4cac81SBjoern A. Zeeb  */
40506b4cac81SBjoern A. Zeeb static void
40516b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
40526b4cac81SBjoern A. Zeeb {
40536b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
40546b4cac81SBjoern A. Zeeb 	struct mbuf *m;
40556b4cac81SBjoern A. Zeeb 
40566b4cac81SBjoern A. Zeeb 	if (p == NULL)
40576b4cac81SBjoern A. Zeeb 		return;
40586b4cac81SBjoern A. Zeeb 
40596b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
40606b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
40616b4cac81SBjoern A. Zeeb 
40626b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
40636b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
40646b4cac81SBjoern A. Zeeb 	if (ni != NULL)
40656b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
40666b4cac81SBjoern A. Zeeb 	m_freem(m);
40676b4cac81SBjoern A. Zeeb }
40686b4cac81SBjoern A. Zeeb 
40696b4cac81SBjoern A. Zeeb void
40706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
40716b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
40726b4cac81SBjoern A. Zeeb {
40736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40746b4cac81SBjoern A. Zeeb 
40756b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
40766b4cac81SBjoern A. Zeeb 	IMPROVE();
40776b4cac81SBjoern A. Zeeb 
40786b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
40796b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
40806b4cac81SBjoern A. Zeeb }
40816b4cac81SBjoern A. Zeeb 
40826b4cac81SBjoern A. Zeeb void
40836b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
40846b4cac81SBjoern A. Zeeb     struct work_struct *w)
40856b4cac81SBjoern A. Zeeb {
40866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40876b4cac81SBjoern A. Zeeb 
40886b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
40896b4cac81SBjoern A. Zeeb 	IMPROVE();
40906b4cac81SBjoern A. Zeeb 
40916b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
40926b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
40936b4cac81SBjoern A. Zeeb }
40946b4cac81SBjoern A. Zeeb 
40956b4cac81SBjoern A. Zeeb struct sk_buff *
40966b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
40976b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
40986b4cac81SBjoern A. Zeeb {
40996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41006b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
41016b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
41026b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
41036b4cac81SBjoern A. Zeeb 	uint16_t v;
41046b4cac81SBjoern A. Zeeb 
41056b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
41066b4cac81SBjoern A. Zeeb 	if (skb == NULL)
41076b4cac81SBjoern A. Zeeb 		return (NULL);
41086b4cac81SBjoern A. Zeeb 
41096b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
41106b4cac81SBjoern A. Zeeb 
41116b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
41126b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
41136b4cac81SBjoern A. Zeeb 
41146b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
41156b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
41166b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
41176b4cac81SBjoern A. Zeeb 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
41186b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
41196b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
41206b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
41216b4cac81SBjoern A. Zeeb 
41226b4cac81SBjoern A. Zeeb 	return (skb);
41236b4cac81SBjoern A. Zeeb }
41246b4cac81SBjoern A. Zeeb 
41256b4cac81SBjoern A. Zeeb struct sk_buff *
41266b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
41276b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif, bool qos)
41286b4cac81SBjoern A. Zeeb {
41296b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41306b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
41316b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
41326b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
41336b4cac81SBjoern A. Zeeb 
41346b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
41356b4cac81SBjoern A. Zeeb 	if (skb == NULL)
41366b4cac81SBjoern A. Zeeb 		return (NULL);
41376b4cac81SBjoern A. Zeeb 
41386b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
41396b4cac81SBjoern A. Zeeb 
41406b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
41416b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
41426b4cac81SBjoern A. Zeeb 
41436b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
41446b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
41456b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
41466b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
41476b4cac81SBjoern A. Zeeb 
41486b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
41496b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
41506b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
41516b4cac81SBjoern A. Zeeb 
41526b4cac81SBjoern A. Zeeb 	return (skb);
41536b4cac81SBjoern A. Zeeb }
41546b4cac81SBjoern A. Zeeb 
41556b4cac81SBjoern A. Zeeb struct wireless_dev *
41566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
41576b4cac81SBjoern A. Zeeb {
41586b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41596b4cac81SBjoern A. Zeeb 
41606b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
41616b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
41626b4cac81SBjoern A. Zeeb }
41636b4cac81SBjoern A. Zeeb 
41646b4cac81SBjoern A. Zeeb void
41656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
41666b4cac81SBjoern A. Zeeb {
41676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41686b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
41696b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
41706b4cac81SBjoern A. Zeeb 	int arg;
41716b4cac81SBjoern A. Zeeb 
41726b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
41736b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
41746b4cac81SBjoern A. Zeeb 
41756b4cac81SBjoern A. Zeeb 	/*
4176f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
41776b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
41786b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
41796b4cac81SBjoern A. Zeeb 	 */
4180f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
4181bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
41826b4cac81SBjoern A. Zeeb 
41836b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
41846b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
41856b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
41866b4cac81SBjoern A. Zeeb }
41876b4cac81SBjoern A. Zeeb 
4188bb81db90SBjoern A. Zeeb void
4189bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4190bb81db90SBjoern A. Zeeb {
4191bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4192bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
4193bb81db90SBjoern A. Zeeb 	enum ieee80211_state nstate;
4194bb81db90SBjoern A. Zeeb 	int arg;
4195bb81db90SBjoern A. Zeeb 
4196bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
4197bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
4198bb81db90SBjoern A. Zeeb 
4199bb81db90SBjoern A. Zeeb 	/*
4200bb81db90SBjoern A. Zeeb 	 * Go to scan; otherwise we need to elaborately check state and
4201bb81db90SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4202bb81db90SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
4203bb81db90SBjoern A. Zeeb 	 */
4204bb81db90SBjoern A. Zeeb 	nstate = IEEE80211_S_SCAN;
4205bb81db90SBjoern A. Zeeb 	arg = 0;
4206bb81db90SBjoern A. Zeeb 
4207bb81db90SBjoern A. Zeeb 	/* We should be in RUN.  Can we assert that? */
4208bb81db90SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4209bb81db90SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4210bb81db90SBjoern A. Zeeb 		    vif, vap, ieee80211_state_name[vap->iv_state]);
4211bb81db90SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
4212bb81db90SBjoern A. Zeeb }
4213bb81db90SBjoern A. Zeeb 
42146b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
42156b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
42166b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
4217