xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 86bc7259d3d558495511a0a23d137b4a6cd030f3)
16b4cac81SBjoern A. Zeeb /*-
2ac1d519cSBjoern A. Zeeb  * Copyright (c) 2020-2024 The FreeBSD Foundation
3*86bc7259SBjoern A. Zeeb  * Copyright (c) 2020-2024 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/param.h>
436b4cac81SBjoern A. Zeeb #include <sys/types.h>
446b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
456b4cac81SBjoern A. Zeeb #include <sys/errno.h>
466b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
476b4cac81SBjoern A. Zeeb #include <sys/module.h>
486b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
496b4cac81SBjoern A. Zeeb #include <sys/socket.h>
506b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
516b4cac81SBjoern A. Zeeb #include <sys/queue.h>
526b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
53527687a9SBjoern A. Zeeb #include <sys/libkern.h>
546b4cac81SBjoern A. Zeeb 
556b4cac81SBjoern A. Zeeb #include <net/if.h>
566b4cac81SBjoern A. Zeeb #include <net/if_var.h>
576b4cac81SBjoern A. Zeeb #include <net/if_media.h>
586b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
596b4cac81SBjoern A. Zeeb 
606b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
616b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
626b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
649fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
656b4cac81SBjoern A. Zeeb 
666b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
676b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
686b4cac81SBjoern A. Zeeb 
696b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
706b4cac81SBjoern A. Zeeb #include "linux_80211.h"
716b4cac81SBjoern A. Zeeb 
724a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
73b35f6cd0SBjoern A. Zeeb /* #define	LKPI_80211_HW_CRYPTO */
749fb91463SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
759fb91463SBjoern A. Zeeb /* #define	LKPI_80211_HT */
769fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
779fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
789fb91463SBjoern A. Zeeb #endif
79b35f6cd0SBjoern A. Zeeb 
806b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
816b4cac81SBjoern A. Zeeb 
825a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
835a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
845a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
855a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
865a9a0d78SBjoern A. Zeeb } while (0)
875a9a0d78SBjoern A. Zeeb 
886b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
896b4cac81SBjoern A. Zeeb 
909d9ba2b7SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
919d9ba2b7SBjoern A. Zeeb int linuxkpi_debug_80211;
926b4cac81SBjoern A. Zeeb 
936b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
949d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
959d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
969d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
979d9ba2b7SBjoern A. Zeeb 
989d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
999d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1009d9ba2b7SBjoern A. Zeeb 
1019d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1026b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1039d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1046b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1056b4cac81SBjoern A. Zeeb #else
1066b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1076b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1086b4cac81SBjoern A. Zeeb #endif
1096b4cac81SBjoern A. Zeeb 
1106b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1116b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1126b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1136b4cac81SBjoern A. Zeeb #endif
1146b4cac81SBjoern A. Zeeb 
1156b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1166b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1176b4cac81SBjoern A. Zeeb 
118b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
119b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
120b0f73768SBjoern A. Zeeb 
121fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
122fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1234f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1244f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1254f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1264f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1274f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1284f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1294f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1304f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1314f61ef8bSBjoern A. Zeeb #if 0
1324f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1334f61ef8bSBjoern A. Zeeb #endif
1344f61ef8bSBjoern A. Zeeb };
1354f61ef8bSBjoern A. Zeeb 
1366b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1376b4cac81SBjoern A. Zeeb 	/*
1386b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1396b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1406b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1416b4cac81SBjoern A. Zeeb 	 */
1426b4cac81SBjoern A. Zeeb };
1436b4cac81SBjoern A. Zeeb 
144ac867c20SBjoern A. Zeeb #if 0
1456b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1466b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
147ac867c20SBjoern A. Zeeb #endif
14888665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1496b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
150759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1516b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1534a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1544a67f1dfSBjoern A. Zeeb #endif
1556b4cac81SBjoern A. Zeeb 
1569fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
1579fb91463SBjoern A. Zeeb static void
1589fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
1599fb91463SBjoern A. Zeeb {
1609fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
1619fb91463SBjoern A. Zeeb 	uint8_t *ie;
1629fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
1639fb91463SBjoern A. Zeeb 	int i, rx_nss;
1649fb91463SBjoern A. Zeeb 
1659fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
1669fb91463SBjoern A. Zeeb 		return;
1679fb91463SBjoern A. Zeeb 
1689fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
1699fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1709fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
1719fb91463SBjoern A. Zeeb 
1729fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
1739fb91463SBjoern A. Zeeb 
1749fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
1759fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
1769fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1779fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
1789fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
1799fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1809fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
1819fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
1829fb91463SBjoern A. Zeeb 
1839fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
1849fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
1859fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1869fb91463SBjoern A. Zeeb 		ie += 4;
1879fb91463SBjoern A. Zeeb 	ie += 2;
1889fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
1899fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
1909fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
1919fb91463SBjoern A. Zeeb 
1929fb91463SBjoern A. Zeeb 	rx_nss = 0;
1939fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
1949fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
1959fb91463SBjoern A. Zeeb 			rx_nss++;
1969fb91463SBjoern A. Zeeb 	}
1979fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
1989fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
1999fb91463SBjoern A. Zeeb 
2009fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
2019fb91463SBjoern A. Zeeb }
2029fb91463SBjoern A. Zeeb #endif
2039fb91463SBjoern A. Zeeb 
2049fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
2059fb91463SBjoern A. Zeeb static void
2069fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
2079fb91463SBjoern A. Zeeb {
2089fb91463SBjoern A. Zeeb 
2099fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
2109fb91463SBjoern A. Zeeb 		return;
2119fb91463SBjoern A. Zeeb 
2129fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
2139fb91463SBjoern A. Zeeb #ifdef __notyet__
2149fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
2159fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
2169fb91463SBjoern A. Zeeb 		} else
2179fb91463SBjoern A. Zeeb #endif
2189fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2199fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
2209fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2219fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
2229fb91463SBjoern A. Zeeb 	}
2239fb91463SBjoern A. Zeeb 
2249fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
2259fb91463SBjoern A. Zeeb 	return;
2269fb91463SBjoern A. Zeeb }
2279fb91463SBjoern A. Zeeb #endif
2289fb91463SBjoern A. Zeeb 
229d9f59799SBjoern A. Zeeb static void
23067674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
23167674c1cSBjoern A. Zeeb     const char *_f, int _l)
232d9f59799SBjoern A. Zeeb {
233d9f59799SBjoern A. Zeeb 
2349d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2359d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
236d9f59799SBjoern A. Zeeb 		return;
237d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
238d9f59799SBjoern A. Zeeb 		return;
239d9f59799SBjoern A. Zeeb 
240d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
24167674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
24267674c1cSBjoern A. Zeeb 	if (ni != NULL)
24367674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
244d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
245d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
246d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
2479d9ba2b7SBjoern A. Zeeb #endif
248d9f59799SBjoern A. Zeeb }
249d9f59799SBjoern A. Zeeb 
250d9f59799SBjoern A. Zeeb static void
251d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
252d9f59799SBjoern A. Zeeb {
253d9f59799SBjoern A. Zeeb 
254d9f59799SBjoern A. Zeeb 
255d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2560936c648SBjoern A. Zeeb 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
2570936c648SBjoern A. Zeeb 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
2580936c648SBjoern A. Zeeb 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
259d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
260d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
261d9f59799SBjoern A. Zeeb }
262d9f59799SBjoern A. Zeeb 
2634f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
2644f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
2654f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
2664f61ef8bSBjoern A. Zeeb {
2674f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
2684f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
2694f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
2704f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
271b6b352e4SBjoern A. Zeeb 	int band, i, tid;
2729fb91463SBjoern A. Zeeb 	int ht_rx_nss;
2739fb91463SBjoern A. Zeeb 	int vht_rx_nss;
2744f61ef8bSBjoern A. Zeeb 
2754f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2764f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
2774f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2784f61ef8bSBjoern A. Zeeb 		return (NULL);
2794f61ef8bSBjoern A. Zeeb 
2804f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2814f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2824f61ef8bSBjoern A. Zeeb 	/*
2830936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
2840936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
2850936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
2860936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
2870936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
2880936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
2890936c648SBjoern A. Zeeb 	 * two separate allocations.
2904f61ef8bSBjoern A. Zeeb 	 */
2910936c648SBjoern A. Zeeb 	lsta->ni = ni;
2924f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2934f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2944f61ef8bSBjoern A. Zeeb 
2954f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2964f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2974f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2984f61ef8bSBjoern A. Zeeb 
2994f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
300b6b352e4SBjoern A. Zeeb 
301b6b352e4SBjoern A. Zeeb 	/* TXQ */
3024f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
3034f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
3044f61ef8bSBjoern A. Zeeb 
305d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
3064f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
3074f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
3084f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
3094f61ef8bSBjoern A. Zeeb 			goto cleanup;
3104f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
3114f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
312d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
313d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
314d0d29110SBjoern A. Zeeb 				continue;
315d0d29110SBjoern A. Zeeb 			}
316d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
3174f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
3184f61ef8bSBjoern A. Zeeb 		} else {
319fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
3204f61ef8bSBjoern A. Zeeb 		}
321d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
3220cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
323d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
3244f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
3254f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
3260cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
327d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
328eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
3294f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
3304f61ef8bSBjoern A. Zeeb 	}
3314f61ef8bSBjoern A. Zeeb 
332b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
333b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
334b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
335b6b352e4SBjoern A. Zeeb 
336b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
337b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
338b6b352e4SBjoern A. Zeeb 			continue;
339b6b352e4SBjoern A. Zeeb 
340b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
341b6b352e4SBjoern A. Zeeb 
342b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
343b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
344b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
345b6b352e4SBjoern A. Zeeb 		}
346b6b352e4SBjoern A. Zeeb 	}
3479fb91463SBjoern A. Zeeb 
3486ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
3499fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
350f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
3519fb91463SBjoern A. Zeeb 
3529fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
3539fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3549fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
3559fb91463SBjoern A. Zeeb #endif
3569fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
3579fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
3589fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
3599fb91463SBjoern A. Zeeb #endif
3609fb91463SBjoern A. Zeeb 
3619fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
3629fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
3639fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
364b6b352e4SBjoern A. Zeeb 
3656ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
3666ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
3676ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
3686ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
3696ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
3706ffb7bd4SBjoern A. Zeeb 	}
3716ffb7bd4SBjoern A. Zeeb 
3724f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
373fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
3744f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
3754f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
3760936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
3774f61ef8bSBjoern A. Zeeb 
3784f61ef8bSBjoern A. Zeeb 	return (lsta);
3794f61ef8bSBjoern A. Zeeb 
3804f61ef8bSBjoern A. Zeeb cleanup:
381eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
382eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
383eac3646fSBjoern A. Zeeb 
384eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
385eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
3864f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
387eac3646fSBjoern A. Zeeb 	}
3884f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
3894f61ef8bSBjoern A. Zeeb 	return (NULL);
3904f61ef8bSBjoern A. Zeeb }
3914f61ef8bSBjoern A. Zeeb 
3920936c648SBjoern A. Zeeb static void
3930936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
3940936c648SBjoern A. Zeeb {
3950936c648SBjoern A. Zeeb 	struct mbuf *m;
3960936c648SBjoern A. Zeeb 
3970936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
3980936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
3990936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
4000936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
4010936c648SBjoern A. Zeeb 
4020936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
4030936c648SBjoern A. Zeeb 	IMPROVE();
4040936c648SBjoern A. Zeeb 
405fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
406fa4e4257SBjoern A. Zeeb 
407fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4080936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
409fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4100936c648SBjoern A. Zeeb 
4110936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
4120936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
4130936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
4140936c648SBjoern A. Zeeb 
4150936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4160936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
4170936c648SBjoern A. Zeeb 	while (m != NULL) {
4180936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
4190936c648SBjoern A. Zeeb 
4200936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
4210936c648SBjoern A. Zeeb 		if (nim != NULL)
4220936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
4230936c648SBjoern A. Zeeb 		m_freem(m);
4240936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
4250936c648SBjoern A. Zeeb 	}
4260936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
4270936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
428fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
4290936c648SBjoern A. Zeeb 
4300936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
4310936c648SBjoern A. Zeeb 
4320936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
4330936c648SBjoern A. Zeeb 
4340936c648SBjoern A. Zeeb 	/* Free lsta. */
4350936c648SBjoern A. Zeeb 	lsta->ni = NULL;
4360936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
4370936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
4380936c648SBjoern A. Zeeb }
4390936c648SBjoern A. Zeeb 
4400936c648SBjoern A. Zeeb 
4416b4cac81SBjoern A. Zeeb static enum nl80211_band
4426b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
4436b4cac81SBjoern A. Zeeb {
4446b4cac81SBjoern A. Zeeb 
4456b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
4466b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
4476b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
4486b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
4496b4cac81SBjoern A. Zeeb #ifdef __notyet__
4506b4cac81SBjoern A. Zeeb 	else if ()
4516b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
4526b4cac81SBjoern A. Zeeb 	else if ()
4536b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
4546b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
4556b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
4566b4cac81SBjoern A. Zeeb #endif
4576b4cac81SBjoern A. Zeeb 	else
4586b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
4596b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
4606b4cac81SBjoern A. Zeeb }
4616b4cac81SBjoern A. Zeeb 
4626b4cac81SBjoern A. Zeeb static uint32_t
4636b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
4646b4cac81SBjoern A. Zeeb {
4656b4cac81SBjoern A. Zeeb 
4666b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
4676b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
4686b4cac81SBjoern A. Zeeb 	switch (band) {
4696b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
4706b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
4716b4cac81SBjoern A. Zeeb 		break;
4726b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
4736b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
4746b4cac81SBjoern A. Zeeb 		break;
4756b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
4766b4cac81SBjoern A. Zeeb 		break;
4776b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
4786b4cac81SBjoern A. Zeeb 		break;
4796b4cac81SBjoern A. Zeeb 	default:
4806b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
4816b4cac81SBjoern A. Zeeb 		break;
4826b4cac81SBjoern A. Zeeb 	}
4836b4cac81SBjoern A. Zeeb 
4846b4cac81SBjoern A. Zeeb 	IMPROVE();
4856b4cac81SBjoern A. Zeeb 	return (0x00);
4866b4cac81SBjoern A. Zeeb }
4876b4cac81SBjoern A. Zeeb 
488e3a0b120SBjoern A. Zeeb #if 0
4896b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
4906b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
4916b4cac81SBjoern A. Zeeb {
4926b4cac81SBjoern A. Zeeb 
4936b4cac81SBjoern A. Zeeb 	switch (ac) {
4946b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
4956b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
4966b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
4976b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
4986b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
4996b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5006b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
5016b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
5026b4cac81SBjoern A. Zeeb 	default:
5036b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
5046b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5056b4cac81SBjoern A. Zeeb 	}
5066b4cac81SBjoern A. Zeeb }
507e3a0b120SBjoern A. Zeeb #endif
5086b4cac81SBjoern A. Zeeb 
5096b4cac81SBjoern A. Zeeb static enum nl80211_iftype
5106b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
5116b4cac81SBjoern A. Zeeb {
5126b4cac81SBjoern A. Zeeb 
5136b4cac81SBjoern A. Zeeb 	switch (opmode) {
5146b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
5156b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
5166b4cac81SBjoern A. Zeeb 		break;
5176b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
5186b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
5196b4cac81SBjoern A. Zeeb 		break;
5206b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
5216b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
5226b4cac81SBjoern A. Zeeb 		break;
5236b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
5246b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
5256b4cac81SBjoern A. Zeeb 		break;
5266b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
5276b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
5286b4cac81SBjoern A. Zeeb 		break;
5296b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
5306b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
5316b4cac81SBjoern A. Zeeb 		break;
5326b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
5336b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5346b4cac81SBjoern A. Zeeb 	default:
5356b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
5366b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5376b4cac81SBjoern A. Zeeb 	}
5386b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
5396b4cac81SBjoern A. Zeeb }
5406b4cac81SBjoern A. Zeeb 
541b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
5426b4cac81SBjoern A. Zeeb static uint32_t
5436b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
5446b4cac81SBjoern A. Zeeb {
5456b4cac81SBjoern A. Zeeb 
5466b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
5476b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
5486b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5496b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5506b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
5516b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
552906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
5536b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
5546b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
5566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
5576b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
5586b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
5596b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5606b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5616b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5626b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
5636b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5646b4cac81SBjoern A. Zeeb 		break;
5656b4cac81SBjoern A. Zeeb 	default:
5666b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
5676b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5686b4cac81SBjoern A. Zeeb 	}
5696b4cac81SBjoern A. Zeeb 
5706b4cac81SBjoern A. Zeeb 	return (0);
5716b4cac81SBjoern A. Zeeb }
5726b4cac81SBjoern A. Zeeb 
5736b4cac81SBjoern A. Zeeb static uint32_t
5746b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
5756b4cac81SBjoern A. Zeeb {
5766b4cac81SBjoern A. Zeeb 
5776b4cac81SBjoern A. Zeeb 	switch (cipher) {
5786b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
5796b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
5806b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
5816b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
5826b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
5836b4cac81SBjoern A. Zeeb 		if (keylen < 8)
5846b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
5856b4cac81SBjoern A. Zeeb 		else
5866b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
5876b4cac81SBjoern A. Zeeb 		break;
5886b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
5896b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
5906b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
5916b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
5926b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
5936b4cac81SBjoern A. Zeeb 		break;
5946b4cac81SBjoern A. Zeeb 	default:
5956b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
5966b4cac81SBjoern A. Zeeb 	};
5976b4cac81SBjoern A. Zeeb 	return (0);
5986b4cac81SBjoern A. Zeeb }
5996b4cac81SBjoern A. Zeeb #endif
6006b4cac81SBjoern A. Zeeb 
6016b4cac81SBjoern A. Zeeb #ifdef __notyet__
6026b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
6036b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
6046b4cac81SBjoern A. Zeeb {
6056b4cac81SBjoern A. Zeeb 
6066b4cac81SBjoern A. Zeeb 	/*
6076b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
6086b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
6096b4cac81SBjoern A. Zeeb 	 */
6106b4cac81SBjoern A. Zeeb 	switch (state) {
6116b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
6126b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
6136b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
6146b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
6156b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
6166b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
6176b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
6186b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
6196b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
6206b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
6216b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
6226b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
6236b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
6246b4cac81SBjoern A. Zeeb 	default:
6256b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
6266b4cac81SBjoern A. Zeeb 	};
6276b4cac81SBjoern A. Zeeb 
6286b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
6296b4cac81SBjoern A. Zeeb }
6306b4cac81SBjoern A. Zeeb #endif
6316b4cac81SBjoern A. Zeeb 
6326b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6336b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
6346b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
6356b4cac81SBjoern A. Zeeb {
6366b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6376b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
6386b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
6396b4cac81SBjoern A. Zeeb 	int i, nchans;
6406b4cac81SBjoern A. Zeeb 
6416b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6426b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
6436b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
6446b4cac81SBjoern A. Zeeb 		return (NULL);
6456b4cac81SBjoern A. Zeeb 
6466b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
6476b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
6486b4cac81SBjoern A. Zeeb 		return (NULL);
6496b4cac81SBjoern A. Zeeb 
6506b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
6516b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
6526b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
6536b4cac81SBjoern A. Zeeb 			return (&channels[i]);
6546b4cac81SBjoern A. Zeeb 	}
6556b4cac81SBjoern A. Zeeb 
6566b4cac81SBjoern A. Zeeb 	return (NULL);
6576b4cac81SBjoern A. Zeeb }
6586b4cac81SBjoern A. Zeeb 
6592ac8a218SBjoern A. Zeeb #if 0
6606b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6616b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
6626b4cac81SBjoern A. Zeeb {
6636b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6646b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
6656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6666b4cac81SBjoern A. Zeeb 
6676b4cac81SBjoern A. Zeeb 	chan = NULL;
6686b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
6696b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
6706b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
6716b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
6726b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
6736b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
6746b4cac81SBjoern A. Zeeb 	else
6756b4cac81SBjoern A. Zeeb 		c = NULL;
6766b4cac81SBjoern A. Zeeb 
6776b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6786b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
6796b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
6806b4cac81SBjoern A. Zeeb 	}
6816b4cac81SBjoern A. Zeeb 
6826b4cac81SBjoern A. Zeeb 	return (chan);
6836b4cac81SBjoern A. Zeeb }
6842ac8a218SBjoern A. Zeeb #endif
6856b4cac81SBjoern A. Zeeb 
6862e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
6872e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
6882e183d99SBjoern A. Zeeb {
6892e183d99SBjoern A. Zeeb 	enum nl80211_band band;
6902e183d99SBjoern A. Zeeb 
6912e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6922e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6932e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6942e183d99SBjoern A. Zeeb 		int i;
6952e183d99SBjoern A. Zeeb 
6962e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
6972e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6982e183d99SBjoern A. Zeeb 			continue;
6992e183d99SBjoern A. Zeeb 
7002e183d99SBjoern A. Zeeb 		channels = supband->channels;
7012e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
7022e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
7032e183d99SBjoern A. Zeeb 				return (&channels[i]);
7042e183d99SBjoern A. Zeeb 		}
7052e183d99SBjoern A. Zeeb 	}
7062e183d99SBjoern A. Zeeb 
7072e183d99SBjoern A. Zeeb 	return (NULL);
7082e183d99SBjoern A. Zeeb }
7092e183d99SBjoern A. Zeeb 
710b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
7116b4cac81SBjoern A. Zeeb static int
7126b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
7136b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
7146b4cac81SBjoern A. Zeeb {
7156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
7166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7196b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
7206b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
7216b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
7226b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
7236b4cac81SBjoern A. Zeeb 	int error;
7246b4cac81SBjoern A. Zeeb 
7256b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
7266b4cac81SBjoern A. Zeeb 
7276b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
7286b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
7296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7326b4cac81SBjoern A. Zeeb 
7336b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
7346b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
7356b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
7366b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
7376b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
7386b4cac81SBjoern A. Zeeb #if 0
7396b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
7406b4cac81SBjoern A. Zeeb #endif
7416b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
7426b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
7436b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
7446b4cac81SBjoern A. Zeeb 
7456b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
7466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
7476b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
7486b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
7496b4cac81SBjoern A. Zeeb 		break;
7506b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7516b4cac81SBjoern A. Zeeb 	default:
7526b4cac81SBjoern A. Zeeb 		IMPROVE();
7536b4cac81SBjoern A. Zeeb 		return (0);
7546b4cac81SBjoern A. Zeeb 	};
7556b4cac81SBjoern A. Zeeb 
7566b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
7576b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
7586b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
7596b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
7606b4cac81SBjoern A. Zeeb 
7616b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
7626b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
7636b4cac81SBjoern A. Zeeb 	}
7646b4cac81SBjoern A. Zeeb 
7656b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
7666b4cac81SBjoern A. Zeeb 	if (error != 0) {
7676b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
7686b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
7696b4cac81SBjoern A. Zeeb 		return (0);
7706b4cac81SBjoern A. Zeeb 	} else {
7716b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
7726b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
7736b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
7746b4cac81SBjoern A. Zeeb 		return (1);
7756b4cac81SBjoern A. Zeeb 	}
7766b4cac81SBjoern A. Zeeb }
7776b4cac81SBjoern A. Zeeb 
7786b4cac81SBjoern A. Zeeb static int
7796b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
7806b4cac81SBjoern A. Zeeb {
7816b4cac81SBjoern A. Zeeb 
7826b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
7836b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
7846b4cac81SBjoern A. Zeeb }
7856b4cac81SBjoern A. Zeeb static  int
7866b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
7876b4cac81SBjoern A. Zeeb {
7886b4cac81SBjoern A. Zeeb 
7896b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
7906b4cac81SBjoern A. Zeeb }
7916b4cac81SBjoern A. Zeeb #endif
7926b4cac81SBjoern A. Zeeb 
7936b4cac81SBjoern A. Zeeb static u_int
7946b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
7956b4cac81SBjoern A. Zeeb {
7966b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
7976b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7986b4cac81SBjoern A. Zeeb 
7996b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
8006b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
8016b4cac81SBjoern A. Zeeb 
8026b4cac81SBjoern A. Zeeb 	mc_list = arg;
8036b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
8046b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
8056b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
8066b4cac81SBjoern A. Zeeb 			return (0);
8076b4cac81SBjoern A. Zeeb 	}
8086b4cac81SBjoern A. Zeeb 
8096b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
8106b4cac81SBjoern A. Zeeb 	if (addr == NULL)
8116b4cac81SBjoern A. Zeeb 		return (0);
8126b4cac81SBjoern A. Zeeb 
8136b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
8146b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
8156b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
8166b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
8176b4cac81SBjoern A. Zeeb 	mc_list->count++;
8186b4cac81SBjoern A. Zeeb 
8199d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8209d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8216b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
8226b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
8239d9ba2b7SBjoern A. Zeeb #endif
8246b4cac81SBjoern A. Zeeb 
8256b4cac81SBjoern A. Zeeb 	return (1);
8266b4cac81SBjoern A. Zeeb }
8276b4cac81SBjoern A. Zeeb 
8286b4cac81SBjoern A. Zeeb static void
8296b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
8306b4cac81SBjoern A. Zeeb {
8316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8326b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8336b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
8346b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
8356b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
8366b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
8376b4cac81SBjoern A. Zeeb 	u64 mc;
8386b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
8396b4cac81SBjoern A. Zeeb 
8406b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
8416b4cac81SBjoern A. Zeeb 
8426b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
8436b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
8446b4cac81SBjoern A. Zeeb 		return;
8456b4cac81SBjoern A. Zeeb 
8466b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
8476b4cac81SBjoern A. Zeeb 		return;
8486b4cac81SBjoern A. Zeeb 
8496b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
8506b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
8516b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
8526b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
8536b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8546b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
8556b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
8566b4cac81SBjoern A. Zeeb 	} else {
8576b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
8586b4cac81SBjoern A. Zeeb 	}
8596b4cac81SBjoern A. Zeeb 
8606b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8616b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
8626b4cac81SBjoern A. Zeeb 	/*
8636b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
8646b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
8656b4cac81SBjoern A. Zeeb 	 */
8666b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
8676b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
8686b4cac81SBjoern A. Zeeb 
8699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8716b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
8726b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
8739d9ba2b7SBjoern A. Zeeb #endif
8746b4cac81SBjoern A. Zeeb 
8756b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
8766b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
8776b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
8786b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
8796b4cac81SBjoern A. Zeeb 			mc_list.count--;
8806b4cac81SBjoern A. Zeeb 		}
8816b4cac81SBjoern A. Zeeb 	}
8826b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
8836b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
8846b4cac81SBjoern A. Zeeb }
8856b4cac81SBjoern A. Zeeb 
886fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
887fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
888fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
889fa8f007dSBjoern A. Zeeb {
890fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
891fa8f007dSBjoern A. Zeeb 
892fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
893fa8f007dSBjoern A. Zeeb 
8949d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8959d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
896fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
897fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
898ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
899fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
900616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
901fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
902fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
903fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
904fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
905ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
9069d9ba2b7SBjoern A. Zeeb #endif
907fa8f007dSBjoern A. Zeeb 
908fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
909fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
910fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
911fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
912fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
913fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
914fa8f007dSBjoern A. Zeeb 	}
915fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
916fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
917fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
918fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
919fa8f007dSBjoern A. Zeeb 	}
920fa8f007dSBjoern A. Zeeb 
921fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
922fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
923fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
924fa8f007dSBjoern A. Zeeb 
9259d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
9269d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
927fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
928fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
929ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
930fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
931616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
932fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
933fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
934fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
935fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
936ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
9379d9ba2b7SBjoern A. Zeeb #endif
938fa8f007dSBjoern A. Zeeb 
939fa8f007dSBjoern A. Zeeb 	return (bss_changed);
940fa8f007dSBjoern A. Zeeb }
941fa8f007dSBjoern A. Zeeb 
9426b4cac81SBjoern A. Zeeb static void
9436b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
9446b4cac81SBjoern A. Zeeb {
9456b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9466b4cac81SBjoern A. Zeeb 	int error;
9478ac540d3SBjoern A. Zeeb 	bool cancel;
9486b4cac81SBjoern A. Zeeb 
9498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9508ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9528ac540d3SBjoern A. Zeeb 	if (!cancel)
9536b4cac81SBjoern A. Zeeb 		return;
9546b4cac81SBjoern A. Zeeb 
9556b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9566b4cac81SBjoern A. Zeeb 
957bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
958bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
9596b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
9606b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
9618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
9626b4cac81SBjoern A. Zeeb 
9636b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
9648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9658ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
9668ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
9678ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9698ac540d3SBjoern A. Zeeb 
970bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
9716b4cac81SBjoern A. Zeeb 
9728ac540d3SBjoern A. Zeeb 	if (cancel)
9736b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
9746b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
9756b4cac81SBjoern A. Zeeb }
9766b4cac81SBjoern A. Zeeb 
9776b4cac81SBjoern A. Zeeb static void
978086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
979086be6a8SBjoern A. Zeeb {
980086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
981086be6a8SBjoern A. Zeeb 	int error;
982086be6a8SBjoern A. Zeeb 	bool old;
983086be6a8SBjoern A. Zeeb 
984086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
985086be6a8SBjoern A. Zeeb 	if (old == new)
986086be6a8SBjoern A. Zeeb 		return;
987086be6a8SBjoern A. Zeeb 
988086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
989086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
990086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
991086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
992086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
993086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
994086be6a8SBjoern A. Zeeb 	}
995086be6a8SBjoern A. Zeeb }
996086be6a8SBjoern A. Zeeb 
9975a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
9986b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9996b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
10006b4cac81SBjoern A. Zeeb {
10015a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
10025a4d2461SBjoern A. Zeeb 
10035a4d2461SBjoern A. Zeeb 	changed = 0;
10046b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1005616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
10066b4cac81SBjoern A. Zeeb 
10076b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
10086b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
10096b4cac81SBjoern A. Zeeb 
1010616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1011616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
10126b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
10136b4cac81SBjoern A. Zeeb 		IMPROVE();
1014086be6a8SBjoern A. Zeeb 
10155a4d2461SBjoern A. Zeeb 		/*
10165a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
10175a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
10185a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
10195a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
10205a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
10215a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
10225a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
10235a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
10245a4d2461SBjoern A. Zeeb 		 */
10256b4cac81SBjoern A. Zeeb 	}
10265a4d2461SBjoern A. Zeeb 
10275a4d2461SBjoern A. Zeeb 	return (changed);
10286b4cac81SBjoern A. Zeeb }
10296b4cac81SBjoern A. Zeeb 
10306b4cac81SBjoern A. Zeeb static void
10316b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
10326b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
10336b4cac81SBjoern A. Zeeb {
10346b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
10356b4cac81SBjoern A. Zeeb 	int tid;
1036eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
10376b4cac81SBjoern A. Zeeb 
10386b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
10396b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
10406b4cac81SBjoern A. Zeeb 
10416b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
10426b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
10436b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
10446b4cac81SBjoern A. Zeeb 				continue;
10456b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
10466b4cac81SBjoern A. Zeeb 			continue;
10476b4cac81SBjoern A. Zeeb 
10486b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
10496b4cac81SBjoern A. Zeeb 			continue;
10506b4cac81SBjoern A. Zeeb 
10516b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10526b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
10536b4cac81SBjoern A. Zeeb 			continue;
10546b4cac81SBjoern A. Zeeb 
1055eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1056eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1057eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1058eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
10596b4cac81SBjoern A. Zeeb 			continue;
10606b4cac81SBjoern A. Zeeb 
10616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
10626b4cac81SBjoern A. Zeeb 	}
10636b4cac81SBjoern A. Zeeb }
10646b4cac81SBjoern A. Zeeb 
106588665349SBjoern A. Zeeb /*
106688665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
106788665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
106888665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
106988665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
107088665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
107188665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
107288665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
107388665349SBjoern A. Zeeb  * re-used.
107488665349SBjoern A. Zeeb  */
107588665349SBjoern A. Zeeb static void
107688665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
107788665349SBjoern A. Zeeb {
107888665349SBjoern A. Zeeb 	struct mbufq mq;
107988665349SBjoern A. Zeeb 	struct mbuf *m;
108088665349SBjoern A. Zeeb 	int len;
108188665349SBjoern A. Zeeb 
108288665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
108388665349SBjoern A. Zeeb 
108488665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
108588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
108688665349SBjoern A. Zeeb 	lsta->txq_ready = false;
108788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
108888665349SBjoern A. Zeeb 
108988665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
109088665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
109188665349SBjoern A. Zeeb 
109288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
109388665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
109488665349SBjoern A. Zeeb 	if (len <= 0) {
109588665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
109688665349SBjoern A. Zeeb 		return;
109788665349SBjoern A. Zeeb 	}
109888665349SBjoern A. Zeeb 
109988665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
110088665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
110188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
110288665349SBjoern A. Zeeb 
110388665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
110488665349SBjoern A. Zeeb 	while (m != NULL) {
110588665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
110688665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
110788665349SBjoern A. Zeeb 	}
110888665349SBjoern A. Zeeb }
110988665349SBjoern A. Zeeb 
11106b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
11116b4cac81SBjoern A. Zeeb 
11126b4cac81SBjoern A. Zeeb static int
11136b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11146b4cac81SBjoern A. Zeeb {
11156b4cac81SBjoern A. Zeeb 
11166b4cac81SBjoern A. Zeeb 	return (0);
11176b4cac81SBjoern A. Zeeb }
11186b4cac81SBjoern A. Zeeb 
11196b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
11206b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
11216b4cac81SBjoern A. Zeeb 
11226b4cac81SBjoern A. Zeeb static int
11236b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11246b4cac81SBjoern A. Zeeb {
11256b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1126c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1127d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
11286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11296b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11306b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
11316b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11326b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11346b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
11356b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
11366b4cac81SBjoern A. Zeeb 	uint32_t changed;
11376b4cac81SBjoern A. Zeeb 	int error;
11386b4cac81SBjoern A. Zeeb 
11392ac8a218SBjoern A. Zeeb 	/*
11402ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
11412ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
11422ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
11432ac8a218SBjoern A. Zeeb 	 */
11442ac8a218SBjoern A. Zeeb 
11452ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
11462ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
11472ac8a218SBjoern A. Zeeb 		return (EINVAL);
11482ac8a218SBjoern A. Zeeb 	}
11490936c648SBjoern A. Zeeb 	/*
11500936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
11510936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
11520936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
11530936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
11540936c648SBjoern A. Zeeb 	 */
11552ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11562ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
11572ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
11582ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
11590936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11602ac8a218SBjoern A. Zeeb 		return (EINVAL);
11616b4cac81SBjoern A. Zeeb 	}
11626b4cac81SBjoern A. Zeeb 
11636b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11642ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
11652ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
11662ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
11672ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
11680936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11692ac8a218SBjoern A. Zeeb 		return (ESRCH);
11702ac8a218SBjoern A. Zeeb 	}
11712ac8a218SBjoern A. Zeeb 
11726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11736b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11746b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11756b4cac81SBjoern A. Zeeb 
11760936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
11770936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
11780936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
11790936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
11800936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
11810936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
11820936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
11830936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
11840936c648SBjoern A. Zeeb 		return (EBUSY);
11850936c648SBjoern A. Zeeb 	}
11860936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
11870936c648SBjoern A. Zeeb 
11886b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
11906b4cac81SBjoern A. Zeeb 
11916b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
11926b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1193d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1194d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
11956b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
11966b4cac81SBjoern A. Zeeb 	} else {
11976b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1198c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
11996b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1200d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
12016b4cac81SBjoern A. Zeeb 	}
12026b4cac81SBjoern A. Zeeb 
1203d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1204d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1205d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
12066b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1207d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1208d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1209d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1210d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
12119fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
12122ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
12132ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
12149fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
12159fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
12169fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1217d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
12189fb91463SBjoern A. Zeeb 		} else
1219d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
12209fb91463SBjoern A. Zeeb 	}
12219fb91463SBjoern A. Zeeb #endif
12229fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
12239fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
12249fb91463SBjoern A. Zeeb #ifdef __notyet__
12259fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1226d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1227d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
12289fb91463SBjoern A. Zeeb 		} else
12299fb91463SBjoern A. Zeeb #endif
12309fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1231d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
12329fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1233d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
12349fb91463SBjoern A. Zeeb 	}
12359fb91463SBjoern A. Zeeb #endif
12366b4cac81SBjoern A. Zeeb 	/* Responder ... */
1237d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1238d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1239d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1240d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
12419fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
12426b4cac81SBjoern A. Zeeb 
12436ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
12446ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
12456ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
12466ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
12476ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
12486ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
12496ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
12506ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
12516ffb7bd4SBjoern A. Zeeb 
12526ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
12536ffb7bd4SBjoern A. Zeeb 
12546ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
12556ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
12566ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
12576ffb7bd4SBjoern A. Zeeb 
12586ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
12596ffb7bd4SBjoern A. Zeeb 
12606b4cac81SBjoern A. Zeeb 	error = 0;
12616b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
12626b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
12636b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
12646b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
12656b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1266d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
12676b4cac81SBjoern A. Zeeb 	} else {
1268d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
12696b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
12707b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
12717b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
12727b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1273d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
12749fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
12759fb91463SBjoern A. Zeeb 			if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) {
12769fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
12779fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
12789fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 += 10;
12799fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
12809fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 -= 10;
12819fb91463SBjoern A. Zeeb 			}
12829fb91463SBjoern A. Zeeb #endif
12837b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1284d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
12856b4cac81SBjoern A. Zeeb 		} else {
1286018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1287018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
12886b4cac81SBjoern A. Zeeb 			goto out;
12896b4cac81SBjoern A. Zeeb 		}
12906ffb7bd4SBjoern A. Zeeb 
1291d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
12926ffb7bd4SBjoern A. Zeeb 
12936b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
12946b4cac81SBjoern A. Zeeb 		if (error == 0)
129568541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1296d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
12976b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
12986b4cac81SBjoern A. Zeeb 			error = 0;
12996b4cac81SBjoern A. Zeeb 		if (error != 0) {
1300018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1301018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1302d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1303d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1304c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
13056b4cac81SBjoern A. Zeeb 			goto out;
13066b4cac81SBjoern A. Zeeb 		}
13076b4cac81SBjoern A. Zeeb 	}
13086b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
13096b4cac81SBjoern A. Zeeb 
13106b4cac81SBjoern A. Zeeb 	/* RATES */
13116b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
13126b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
13136b4cac81SBjoern A. Zeeb 
1314d9f59799SBjoern A. Zeeb 	/*
13150936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
13160936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
13170936c648SBjoern A. Zeeb 	 * under the hoods.
1318d9f59799SBjoern A. Zeeb 	 */
13190936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
13200936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
13216b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1322e24e8103SBjoern A. Zeeb 
132388665349SBjoern A. Zeeb 	/*
132488665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
132588665349SBjoern A. Zeeb 	 * that we can tx again.
132688665349SBjoern A. Zeeb 	 */
132788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
132888665349SBjoern A. Zeeb 	lsta->txq_ready = true;
132988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
133088665349SBjoern A. Zeeb 
1331e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
13322ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1333e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1334e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1335e24e8103SBjoern A. Zeeb 
1336d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
13376b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13386b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
13396b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1340e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
13416b4cac81SBjoern A. Zeeb 	if (error != 0) {
13426b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1343018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1344018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
13456b4cac81SBjoern A. Zeeb 		goto out;
13466b4cac81SBjoern A. Zeeb 	}
13476b4cac81SBjoern A. Zeeb #if 0
13486b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
13496b4cac81SBjoern A. Zeeb #endif
13506b4cac81SBjoern A. Zeeb 
13519d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13529d9ba2b7SBjoern A. Zeeb 
13531665ef97SBjoern A. Zeeb #if 0
13546b4cac81SBjoern A. Zeeb 	/*
13556b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
13566b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
13576b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
13586b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1359d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1360d9f59799SBjoern A. Zeeb 	 * for all queues.
13616b4cac81SBjoern A. Zeeb 	 */
1362e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
13631665ef97SBjoern A. Zeeb #endif
13646b4cac81SBjoern A. Zeeb 
13656b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
13666b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13676b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
13686b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
13696b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
13706b4cac81SBjoern A. Zeeb 
13716b4cac81SBjoern A. Zeeb 	/*
13726b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
13736b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
13746b4cac81SBjoern A. Zeeb 	 * - event_callback
13756b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
13766b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
13776b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
13786b4cac81SBjoern A. Zeeb 	 */
13796b4cac81SBjoern A. Zeeb 
1380105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1381105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1382105b9df2SBjoern A. Zeeb 
1383105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1384105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1385105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1386105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1387105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1388105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1389105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1390105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1391105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1392105b9df2SBjoern A. Zeeb 
1393105b9df2SBjoern A. Zeeb 	/*
1394105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1395105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1396105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1397105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1398105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1399105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1400105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1401105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1402105b9df2SBjoern A. Zeeb 	 */
1403105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1404105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1405105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1406105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1407105b9df2SBjoern A. Zeeb 	} else {
1408105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1409105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1410105b9df2SBjoern A. Zeeb 		/*
1411105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1412105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1413105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1414105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1415105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1416105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1417105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1418105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1419105b9df2SBjoern A. Zeeb 		 */
1420105b9df2SBjoern A. Zeeb 	}
1421105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1422105b9df2SBjoern A. Zeeb 	goto out_relocked;
1423105b9df2SBjoern A. Zeeb 
14246b4cac81SBjoern A. Zeeb out:
14258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14266b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1427105b9df2SBjoern A. Zeeb out_relocked:
14280936c648SBjoern A. Zeeb 	/*
1429105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
14300936c648SBjoern A. Zeeb 	 * during the work of this function.
14310936c648SBjoern A. Zeeb 	 */
14326b4cac81SBjoern A. Zeeb 	if (ni != NULL)
14336b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
14346b4cac81SBjoern A. Zeeb 	return (error);
14356b4cac81SBjoern A. Zeeb }
14366b4cac81SBjoern A. Zeeb 
14376b4cac81SBjoern A. Zeeb static int
14386b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14396b4cac81SBjoern A. Zeeb {
14406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14446b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
14456b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14466b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
14476b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14486b4cac81SBjoern A. Zeeb 	int error;
14496b4cac81SBjoern A. Zeeb 
14506b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14516b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14526b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14536b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14546b4cac81SBjoern A. Zeeb 
14552ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
14562ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14572ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
14582ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
14592ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
14602ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
14612ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
14622ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
14632ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
14642ac8a218SBjoern A. Zeeb #endif
14652ac8a218SBjoern A. Zeeb 
14662ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
14672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
14682ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
14692ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
14702ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
14712ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
14726b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
14736b4cac81SBjoern A. Zeeb 
147467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
14756b4cac81SBjoern A. Zeeb 
14766b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14786b4cac81SBjoern A. Zeeb 
1479d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1480d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1481d9f59799SBjoern A. Zeeb 
14826b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
148388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
14846b4cac81SBjoern A. Zeeb 
14856b4cac81SBjoern A. Zeeb 	/* flush, no drop */
14866b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
14876b4cac81SBjoern A. Zeeb 
14886b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
14896b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
14906b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14916b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
14926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
14936b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
14946b4cac81SBjoern A. Zeeb 	}
14956b4cac81SBjoern A. Zeeb 
14966b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
14976b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
14986b4cac81SBjoern A. Zeeb 
14996b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
15006b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1501d9f59799SBjoern A. Zeeb 
1502d9f59799SBjoern A. Zeeb 	/* Take the station down. */
15036b4cac81SBjoern A. Zeeb 
15046b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
15056b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15066b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1507d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1508e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
15096b4cac81SBjoern A. Zeeb 	if (error != 0) {
15106b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1511018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1512018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
15136b4cac81SBjoern A. Zeeb 		goto out;
15146b4cac81SBjoern A. Zeeb 	}
15156b4cac81SBjoern A. Zeeb #if 0
15166b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
15176b4cac81SBjoern A. Zeeb #endif
15186b4cac81SBjoern A. Zeeb 
151967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
15206b4cac81SBjoern A. Zeeb 
15212ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15222ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
15232ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
15242ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
15252ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1526d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
15270936c648SBjoern A. Zeeb 	/*
15280936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
15290936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
15300936c648SBjoern A. Zeeb 	 * and potentially freed.
15310936c648SBjoern A. Zeeb 	 */
15320936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1533d9f59799SBjoern A. Zeeb 
1534d9f59799SBjoern A. Zeeb 	/* conf_tx */
1535d9f59799SBjoern A. Zeeb 
1536d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
15376b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1538c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1539d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
15406b4cac81SBjoern A. Zeeb 
1541d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
15426b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
154368541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
15446b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
15456b4cac81SBjoern A. Zeeb 
15465a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
15475a4d2461SBjoern A. Zeeb 
15486b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
1549d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1550d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1551c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
15526b4cac81SBjoern A. Zeeb 	}
15536b4cac81SBjoern A. Zeeb 
15546b4cac81SBjoern A. Zeeb out:
15558ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15566b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15576b4cac81SBjoern A. Zeeb 	return (error);
15586b4cac81SBjoern A. Zeeb }
15596b4cac81SBjoern A. Zeeb 
15606b4cac81SBjoern A. Zeeb static int
15616b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15626b4cac81SBjoern A. Zeeb {
15636b4cac81SBjoern A. Zeeb 	int error;
15646b4cac81SBjoern A. Zeeb 
15656b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
15666b4cac81SBjoern A. Zeeb 	if (error == 0)
15676b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
15686b4cac81SBjoern A. Zeeb 	return (error);
15696b4cac81SBjoern A. Zeeb }
15706b4cac81SBjoern A. Zeeb 
15716b4cac81SBjoern A. Zeeb static int
15726b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15736b4cac81SBjoern A. Zeeb {
15746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15756b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15766b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15776b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15786b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15796b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
15806b4cac81SBjoern A. Zeeb 	int error;
15816b4cac81SBjoern A. Zeeb 
15826b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15836b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15846b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15856b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15866b4cac81SBjoern A. Zeeb 
15876b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
15888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15892ac8a218SBjoern A. Zeeb 
15902ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15912ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
15922ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
15932ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15942ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
15952ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
15962ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
15972ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
15982ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
15992ac8a218SBjoern A. Zeeb #endif
16002ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16012ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16022ac8a218SBjoern A. Zeeb 		goto out;
16032ac8a218SBjoern A. Zeeb 	}
16042ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16052ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16062ac8a218SBjoern A. Zeeb 
16072ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
16086b4cac81SBjoern A. Zeeb 
16096b4cac81SBjoern A. Zeeb 	/* Finish auth. */
16106b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
16116b4cac81SBjoern A. Zeeb 
16126b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
16136b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
16146b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1615e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1616018d93ecSBjoern A. Zeeb 	if (error != 0) {
1617018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1618018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
16196b4cac81SBjoern A. Zeeb 		goto out;
1620018d93ecSBjoern A. Zeeb 	}
16216b4cac81SBjoern A. Zeeb 
16226b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16236b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16246b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16256b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
16266b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16276b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16286b4cac81SBjoern A. Zeeb 	}
16296b4cac81SBjoern A. Zeeb 
16306b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
16316b4cac81SBjoern A. Zeeb 
16326b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
16336b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
16346b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16356b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
16366b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
16376b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
16386b4cac81SBjoern A. Zeeb 	}
16396b4cac81SBjoern A. Zeeb 
16406b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
164188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
16426b4cac81SBjoern A. Zeeb 
16436b4cac81SBjoern A. Zeeb 	/*
16446b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
16456b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
16466b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
16476b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
16486b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
16496b4cac81SBjoern A. Zeeb 	 * - event_callback
16506b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
16516b4cac81SBjoern A. Zeeb 	 */
16526b4cac81SBjoern A. Zeeb 
16536b4cac81SBjoern A. Zeeb out:
16548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16556b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16566b4cac81SBjoern A. Zeeb 	return (error);
16576b4cac81SBjoern A. Zeeb }
16586b4cac81SBjoern A. Zeeb 
16596b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
16606b4cac81SBjoern A. Zeeb static int
16616b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16626b4cac81SBjoern A. Zeeb {
16636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16666b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16676b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16686b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16692ac8a218SBjoern A. Zeeb 	int error;
16706b4cac81SBjoern A. Zeeb 
16716b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16736b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16746b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16756b4cac81SBjoern A. Zeeb 
16766b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16782ac8a218SBjoern A. Zeeb 
16792ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16802ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
16812ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
16822ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16832ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16842ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16852ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16862ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16872ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16882ac8a218SBjoern A. Zeeb #endif
16892ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16902ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16912ac8a218SBjoern A. Zeeb 		goto out;
16922ac8a218SBjoern A. Zeeb 	}
16932ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16942ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16952ac8a218SBjoern A. Zeeb 
16962ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
16972ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
16986b4cac81SBjoern A. Zeeb 
16996b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
17006b4cac81SBjoern A. Zeeb 
17016b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
17026b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
17036b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17046b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
17056b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
17066b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
17076b4cac81SBjoern A. Zeeb 	}
17086b4cac81SBjoern A. Zeeb 
17096b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
17106b4cac81SBjoern A. Zeeb 
17116b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
17126b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
17136b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17146b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
17156b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
17166b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
17176b4cac81SBjoern A. Zeeb 	}
17186b4cac81SBjoern A. Zeeb 
17192ac8a218SBjoern A. Zeeb 	error = 0;
17202ac8a218SBjoern A. Zeeb out:
17218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17226b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
17236b4cac81SBjoern A. Zeeb 
17242ac8a218SBjoern A. Zeeb 	return (error);
17256b4cac81SBjoern A. Zeeb }
17266b4cac81SBjoern A. Zeeb 
17276b4cac81SBjoern A. Zeeb static int
1728d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17296b4cac81SBjoern A. Zeeb {
17306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17326b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17336b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17346b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
17356b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
17366b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
17376b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1738d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
17396b4cac81SBjoern A. Zeeb 	int error;
17406b4cac81SBjoern A. Zeeb 
17416b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17426b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17436b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17446b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17456b4cac81SBjoern A. Zeeb 
17462ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17472ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17482ac8a218SBjoern A. Zeeb 
17492ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17502ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17512ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
17522ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
17532ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17542ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17552ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17562ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17572ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
17582ac8a218SBjoern A. Zeeb #endif
17592ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
17602ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17612ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
17622ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
17632ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
17642ac8a218SBjoern A. Zeeb 
17652ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
17666b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
17676b4cac81SBjoern A. Zeeb 
176867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1769d9f59799SBjoern A. Zeeb 
1770d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1771d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1772d9f59799SBjoern A. Zeeb 
1773d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1774d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1775d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1776d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1777d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1778ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
1779d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1780d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1781d9f59799SBjoern A. Zeeb 	}
1782d9f59799SBjoern A. Zeeb 
17838ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1784d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1785d9f59799SBjoern A. Zeeb 
178688665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
1787d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1788018d93ecSBjoern A. Zeeb 	if (error != 0) {
1789018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1790018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1791d9f59799SBjoern A. Zeeb 		goto outni;
1792018d93ecSBjoern A. Zeeb 	}
1793d9f59799SBjoern A. Zeeb 
1794d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
179588665349SBjoern A. Zeeb 
179688665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
179788665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
179888665349SBjoern A. Zeeb 
17998ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1800d9f59799SBjoern A. Zeeb 
180167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1802d9f59799SBjoern A. Zeeb 
1803d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
180488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
1805d9f59799SBjoern A. Zeeb 
1806d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1807d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1808d9f59799SBjoern A. Zeeb 
18096b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
18106b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
18116b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18126b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
1813ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
18146b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
18156b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
18166b4cac81SBjoern A. Zeeb 	}
18176b4cac81SBjoern A. Zeeb 
1818d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1819d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1820d9f59799SBjoern A. Zeeb 
1821d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1822d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1823d9f59799SBjoern A. Zeeb 
1824d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1825d9f59799SBjoern A. Zeeb 
18266b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
18276b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18286b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
18296b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1830e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1831018d93ecSBjoern A. Zeeb 	if (error != 0) {
1832018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1833018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18346b4cac81SBjoern A. Zeeb 		goto out;
1835018d93ecSBjoern A. Zeeb 	}
18366b4cac81SBjoern A. Zeeb 
18375a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
18385a4d2461SBjoern A. Zeeb 	bss_changed = 0;
18395a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
18406b4cac81SBjoern A. Zeeb 
18415a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
184216e688b2SBjoern A. Zeeb 
1843d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1844d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1845d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1846d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1847e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1848d9f59799SBjoern A. Zeeb 	if (error != 0) {
1849d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1850018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1851018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
1852d9f59799SBjoern A. Zeeb 		goto out;
1853d9f59799SBjoern A. Zeeb 	}
1854d9f59799SBjoern A. Zeeb 
185516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1856d9f59799SBjoern A. Zeeb 
1857d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1858d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1859d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1860616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
1861616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1862d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1863d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1864d9f59799SBjoern A. Zeeb 
18652ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18662ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
18672ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
18682ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
18692ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1870d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
18710936c648SBjoern A. Zeeb 	/*
18720936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
18730936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
18740936c648SBjoern A. Zeeb 	 * and potentially freed.
18750936c648SBjoern A. Zeeb 	 */
18760936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1877d9f59799SBjoern A. Zeeb 
1878d9f59799SBjoern A. Zeeb 	/* conf_tx */
1879d9f59799SBjoern A. Zeeb 
1880d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1881d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1882c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1883d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
1884d9f59799SBjoern A. Zeeb 
1885d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1886d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
188768541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1888d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1889d9f59799SBjoern A. Zeeb 
18905a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
18915a4d2461SBjoern A. Zeeb 
1892d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1893d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1894d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1895c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
1896d9f59799SBjoern A. Zeeb 	}
1897d9f59799SBjoern A. Zeeb 
1898d9f59799SBjoern A. Zeeb 	error = EALREADY;
18996b4cac81SBjoern A. Zeeb out:
19008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1902d9f59799SBjoern A. Zeeb outni:
19036b4cac81SBjoern A. Zeeb 	return (error);
19046b4cac81SBjoern A. Zeeb }
19056b4cac81SBjoern A. Zeeb 
19066b4cac81SBjoern A. Zeeb static int
1907d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1908d9f59799SBjoern A. Zeeb {
1909d9f59799SBjoern A. Zeeb 	int error;
1910d9f59799SBjoern A. Zeeb 
1911d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1912d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1913d9f59799SBjoern A. Zeeb 		return (error);
1914d9f59799SBjoern A. Zeeb 
1915d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1916d9f59799SBjoern A. Zeeb 
1917d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1918d9f59799SBjoern A. Zeeb 	return (error);
1919d9f59799SBjoern A. Zeeb }
1920d9f59799SBjoern A. Zeeb 
1921d9f59799SBjoern A. Zeeb static int
19226b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19236b4cac81SBjoern A. Zeeb {
19246b4cac81SBjoern A. Zeeb 	int error;
19256b4cac81SBjoern A. Zeeb 
1926d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
19276b4cac81SBjoern A. Zeeb 	return (error);
19286b4cac81SBjoern A. Zeeb }
19296b4cac81SBjoern A. Zeeb 
19306b4cac81SBjoern A. Zeeb static int
19316b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19326b4cac81SBjoern A. Zeeb {
19336b4cac81SBjoern A. Zeeb 	int error;
19346b4cac81SBjoern A. Zeeb 
1935d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
19366b4cac81SBjoern A. Zeeb 	return (error);
19376b4cac81SBjoern A. Zeeb }
19386b4cac81SBjoern A. Zeeb 
19396b4cac81SBjoern A. Zeeb static int
19406b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19416b4cac81SBjoern A. Zeeb {
19426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19436b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19446b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19456b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19466b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19476b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19486b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19496b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19506b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
19516b4cac81SBjoern A. Zeeb 	int error;
19526b4cac81SBjoern A. Zeeb 
19536b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19546b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19556b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19566b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19576b4cac81SBjoern A. Zeeb 
19586b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
19598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19602ac8a218SBjoern A. Zeeb 
19612ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19622ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19632ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
19642ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19652ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19662ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19672ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19682ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19692ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19702ac8a218SBjoern A. Zeeb #endif
19712ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
19722ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
19732ac8a218SBjoern A. Zeeb 		goto out;
19742ac8a218SBjoern A. Zeeb 	}
19752ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19762ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19772ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19782ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19792ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19802ac8a218SBjoern A. Zeeb 
19812ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
19826b4cac81SBjoern A. Zeeb 
19836b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
19846b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
19856b4cac81SBjoern A. Zeeb 
19869597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
19879597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
19889597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
19899597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
19906b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19919597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
19924a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19934a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
19944a67f1dfSBjoern A. Zeeb 		sta->wme = true;
19954a67f1dfSBjoern A. Zeeb #endif
1996e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1997018d93ecSBjoern A. Zeeb 	if (error != 0) {
1998018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1999018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20009597f7cbSBjoern A. Zeeb 		goto out;
2001018d93ecSBjoern A. Zeeb 	}
20026b4cac81SBjoern A. Zeeb 
20036b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
20046b4cac81SBjoern A. Zeeb 
20056b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
20066b4cac81SBjoern A. Zeeb 	bss_changed = 0;
20074a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
20084a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
20094a67f1dfSBjoern A. Zeeb #endif
2010616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2011616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2012616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
20136b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
20146b4cac81SBjoern A. Zeeb 	}
20156b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2016616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2017616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
20186b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
20196b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
20206b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
20216b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
20226b4cac81SBjoern A. Zeeb 	}
20236b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
20246b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
20256b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
20266b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
20276b4cac81SBjoern A. Zeeb 	}
20286b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
20296b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
20306b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
20316b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
20326b4cac81SBjoern A. Zeeb 	}
2033fa8f007dSBjoern A. Zeeb 
2034fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2035fa8f007dSBjoern A. Zeeb 
20366b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
20376b4cac81SBjoern A. Zeeb 
20386b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
20396b4cac81SBjoern A. Zeeb 	 * - event_callback
20406b4cac81SBjoern A. Zeeb 	 */
20416b4cac81SBjoern A. Zeeb 
20426b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20436b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20446b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20456b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
20466b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20476b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20486b4cac81SBjoern A. Zeeb 	}
20496b4cac81SBjoern A. Zeeb 
2050086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2051086be6a8SBjoern A. Zeeb 
20526b4cac81SBjoern A. Zeeb 	/*
20536b4cac81SBjoern A. Zeeb 	 * And then:
20546b4cac81SBjoern A. Zeeb 	 * - (more packets)?
20556b4cac81SBjoern A. Zeeb 	 * - set_key
20566b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
20576b4cac81SBjoern A. Zeeb 	 * - set_key (?)
20586b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
20596b4cac81SBjoern A. Zeeb 	 */
20606b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
20616b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
20626b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
20636b4cac81SBjoern A. Zeeb 
20646b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
20656b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
20666b4cac81SBjoern A. Zeeb 	}
20676b4cac81SBjoern A. Zeeb 
20689fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
20699fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
20709fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
20719fb91463SBjoern A. Zeeb #endif
20729fb91463SBjoern A. Zeeb 
20736b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
20746b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20756b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
20766b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2077e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
20786b4cac81SBjoern A. Zeeb 	if (error != 0) {
20796b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2080018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2081018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20826b4cac81SBjoern A. Zeeb 		goto out;
20836b4cac81SBjoern A. Zeeb 	}
20846b4cac81SBjoern A. Zeeb 
20856b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
20866b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
20876b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
20886b4cac81SBjoern A. Zeeb 	 *
20896b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
20906b4cac81SBjoern A. Zeeb 	 */
20916b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
20926b4cac81SBjoern A. Zeeb 
2093fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2094fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2095fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2096fa8f007dSBjoern A. Zeeb 
20976b4cac81SBjoern A. Zeeb out:
20988ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20996b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21006b4cac81SBjoern A. Zeeb 	return (error);
21016b4cac81SBjoern A. Zeeb }
21026b4cac81SBjoern A. Zeeb 
21036b4cac81SBjoern A. Zeeb static int
21046b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21056b4cac81SBjoern A. Zeeb {
21066b4cac81SBjoern A. Zeeb 	int error;
21076b4cac81SBjoern A. Zeeb 
21086b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
21096b4cac81SBjoern A. Zeeb 	if (error == 0)
21106b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
21116b4cac81SBjoern A. Zeeb 	return (error);
21126b4cac81SBjoern A. Zeeb }
21136b4cac81SBjoern A. Zeeb 
21146b4cac81SBjoern A. Zeeb static int
21156b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21166b4cac81SBjoern A. Zeeb {
21176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21186b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21196b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21206b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21216b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
21226b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21236b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2124d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2125d9f59799SBjoern A. Zeeb #if 0
2126d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2127d9f59799SBjoern A. Zeeb #endif
21286b4cac81SBjoern A. Zeeb 	int error;
21296b4cac81SBjoern A. Zeeb 
21306b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21316b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21326b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21336b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21346b4cac81SBjoern A. Zeeb 
21352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21362ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21372ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
21382ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
21392ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21402ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21412ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21422ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21432ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21442ac8a218SBjoern A. Zeeb #endif
21452ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21472ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
21482ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
21492ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
21502ac8a218SBjoern A. Zeeb 
21512ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
21526b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
21536b4cac81SBjoern A. Zeeb 
215467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2155d9f59799SBjoern A. Zeeb 
2156d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2158d9f59799SBjoern A. Zeeb 
2159d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2160d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2161d9f59799SBjoern A. Zeeb 
2162d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2163d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2164d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2165d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2166d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2167ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2168d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2169d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2170d9f59799SBjoern A. Zeeb 	}
2171d9f59799SBjoern A. Zeeb 
21728ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2173d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2174d9f59799SBjoern A. Zeeb 
2175d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2176d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2177018d93ecSBjoern A. Zeeb 	if (error != 0) {
2178018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2179018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2180d9f59799SBjoern A. Zeeb 		goto outni;
2181018d93ecSBjoern A. Zeeb 	}
2182d9f59799SBjoern A. Zeeb 
2183d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
218488665349SBjoern A. Zeeb 
218588665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
218688665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
218788665349SBjoern A. Zeeb 
21888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2189d9f59799SBjoern A. Zeeb 
219067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2191d9f59799SBjoern A. Zeeb 
2192d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
219388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2194d9f59799SBjoern A. Zeeb 
2195d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2196d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2197d9f59799SBjoern A. Zeeb 
2198d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2199d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2200d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2201d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2202ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2203d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2204d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2205d9f59799SBjoern A. Zeeb 	}
2206d9f59799SBjoern A. Zeeb 
2207d9f59799SBjoern A. Zeeb #if 0
2208d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2209d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2210d9f59799SBjoern A. Zeeb 
2211d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2212d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2213d9f59799SBjoern A. Zeeb #endif
2214d9f59799SBjoern A. Zeeb 
2215d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2216d9f59799SBjoern A. Zeeb 
22176b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
22186b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22196b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
22206b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2221e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2222018d93ecSBjoern A. Zeeb 	if (error != 0) {
2223018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2224018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22256b4cac81SBjoern A. Zeeb 		goto out;
2226018d93ecSBjoern A. Zeeb 	}
22276b4cac81SBjoern A. Zeeb 
222867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22296b4cac81SBjoern A. Zeeb 
22306b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
22316b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22326b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
22336b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2234e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2235018d93ecSBjoern A. Zeeb 	if (error != 0) {
2236018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2237018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22386b4cac81SBjoern A. Zeeb 		goto out;
2239018d93ecSBjoern A. Zeeb 	}
22406b4cac81SBjoern A. Zeeb 
224167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22426b4cac81SBjoern A. Zeeb 
2243d9f59799SBjoern A. Zeeb #if 0
2244d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2245d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2246d9f59799SBjoern A. Zeeb #endif
2247d9f59799SBjoern A. Zeeb 
2248d9f59799SBjoern A. Zeeb 	error = EALREADY;
22496b4cac81SBjoern A. Zeeb out:
22508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22516b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2252d9f59799SBjoern A. Zeeb outni:
22536b4cac81SBjoern A. Zeeb 	return (error);
22546b4cac81SBjoern A. Zeeb }
22556b4cac81SBjoern A. Zeeb 
22566b4cac81SBjoern A. Zeeb static int
2257d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2258d9f59799SBjoern A. Zeeb {
2259d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2260d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2261d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2262d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2263d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2264d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2265d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2266d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2267d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2268d9f59799SBjoern A. Zeeb 	int error;
2269d9f59799SBjoern A. Zeeb 
2270d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2271d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2272d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2273d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2274d9f59799SBjoern A. Zeeb 
22752ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22762ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22772ac8a218SBjoern A. Zeeb 
22782ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22792ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22802ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22812ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22822ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22832ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22842ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22852ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22862ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22872ac8a218SBjoern A. Zeeb #endif
22882ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22892ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22902ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22912ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22922ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22932ac8a218SBjoern A. Zeeb 
22942ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2295d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2296d9f59799SBjoern A. Zeeb 
229767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2298d9f59799SBjoern A. Zeeb 
2299d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2300d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2301d9f59799SBjoern A. Zeeb 
2302d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2303d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2304d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2305d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2306d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2307ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2308d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2309d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2310d9f59799SBjoern A. Zeeb 	}
2311d9f59799SBjoern A. Zeeb 
23128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2313d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2314d9f59799SBjoern A. Zeeb 
2315d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2316d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2317018d93ecSBjoern A. Zeeb 	if (error != 0) {
2318018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2319018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2320d9f59799SBjoern A. Zeeb 		goto outni;
2321018d93ecSBjoern A. Zeeb 	}
2322d9f59799SBjoern A. Zeeb 
2323d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
232488665349SBjoern A. Zeeb 
232588665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
232688665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
232788665349SBjoern A. Zeeb 
23288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2329d9f59799SBjoern A. Zeeb 
233067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2331d9f59799SBjoern A. Zeeb 
2332d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
233388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2334d9f59799SBjoern A. Zeeb 
2335d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2336d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2337d9f59799SBjoern A. Zeeb 
2338d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2339d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2340d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2341d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2342ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2343d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2344d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2345d9f59799SBjoern A. Zeeb 	}
2346d9f59799SBjoern A. Zeeb 
2347d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2348d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2349d9f59799SBjoern A. Zeeb 
2350d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2351d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2352d9f59799SBjoern A. Zeeb 
2353d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2354d9f59799SBjoern A. Zeeb 
2355d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2356d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2357d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2358d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2359e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2360018d93ecSBjoern A. Zeeb 	if (error != 0) {
2361018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2362018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2363d9f59799SBjoern A. Zeeb 		goto out;
2364018d93ecSBjoern A. Zeeb 	}
2365d9f59799SBjoern A. Zeeb 
236667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2367d9f59799SBjoern A. Zeeb 
2368d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2369d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2370d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2371d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2372e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2373018d93ecSBjoern A. Zeeb 	if (error != 0) {
2374018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2375018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2376d9f59799SBjoern A. Zeeb 		goto out;
2377018d93ecSBjoern A. Zeeb 	}
2378d9f59799SBjoern A. Zeeb 
237967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2380d9f59799SBjoern A. Zeeb 
2381d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2382d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2383d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2384d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2385e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2386018d93ecSBjoern A. Zeeb 	if (error != 0) {
2387018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2388018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2389d9f59799SBjoern A. Zeeb 		goto out;
2390018d93ecSBjoern A. Zeeb 	}
2391d9f59799SBjoern A. Zeeb 
23925a4d2461SBjoern A. Zeeb 	bss_changed = 0;
239316e688b2SBjoern A. Zeeb 	/*
23945a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
23955a4d2461SBjoern A. Zeeb 	 *
239616e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
23975a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
23985a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
23995a4d2461SBjoern A. Zeeb 	 *
24005a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
24015a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
24025a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
24035a4d2461SBjoern A. Zeeb 	 * a fw assert.
24045a4d2461SBjoern A. Zeeb 	 *
24055a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
24065a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
24075a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
24085a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
24095a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
24105a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
24115a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
24125a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
24135a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
24145a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
241516e688b2SBjoern A. Zeeb 	 */
24165a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
24175a4d2461SBjoern A. Zeeb 
24185a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
241916e688b2SBjoern A. Zeeb 
2420d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2421d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2422d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2423d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2424e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2425d9f59799SBjoern A. Zeeb 	if (error != 0) {
2426d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2427018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2428018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2429d9f59799SBjoern A. Zeeb 		goto out;
2430d9f59799SBjoern A. Zeeb 	}
2431d9f59799SBjoern A. Zeeb 
24325a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24335a4d2461SBjoern A. Zeeb 
243416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2435d9f59799SBjoern A. Zeeb 
2436d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2437d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2438d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2439616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2440616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2441d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
24425a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
24435a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
24445a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2445d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2446d9f59799SBjoern A. Zeeb 
24472ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24482ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24492ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24502ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24520936c648SBjoern A. Zeeb 	/*
24530936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24540936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24550936c648SBjoern A. Zeeb 	 * and potentially freed.
24560936c648SBjoern A. Zeeb 	 */
24570936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2458d9f59799SBjoern A. Zeeb 
2459d9f59799SBjoern A. Zeeb 	/* conf_tx */
2460d9f59799SBjoern A. Zeeb 
2461d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2462d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2463c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2464d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2465d9f59799SBjoern A. Zeeb 
2466d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2467d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
246868541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2469d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2470d9f59799SBjoern A. Zeeb 
24715a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24725a4d2461SBjoern A. Zeeb 
2473d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2474d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2475d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2476c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2477d9f59799SBjoern A. Zeeb 	}
2478d9f59799SBjoern A. Zeeb 
2479d9f59799SBjoern A. Zeeb 	error = EALREADY;
2480d9f59799SBjoern A. Zeeb out:
24818ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2482d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2483d9f59799SBjoern A. Zeeb outni:
2484d9f59799SBjoern A. Zeeb 	return (error);
2485d9f59799SBjoern A. Zeeb }
2486d9f59799SBjoern A. Zeeb 
2487d9f59799SBjoern A. Zeeb static int
2488d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2489d9f59799SBjoern A. Zeeb {
2490d9f59799SBjoern A. Zeeb 
2491d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2492d9f59799SBjoern A. Zeeb }
2493d9f59799SBjoern A. Zeeb 
2494d9f59799SBjoern A. Zeeb static int
24956b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24966b4cac81SBjoern A. Zeeb {
24976b4cac81SBjoern A. Zeeb 	int error;
24986b4cac81SBjoern A. Zeeb 
2499d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2500d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2501d9f59799SBjoern A. Zeeb 		return (error);
2502d9f59799SBjoern A. Zeeb 
2503d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2504d9f59799SBjoern A. Zeeb 
2505d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
25066b4cac81SBjoern A. Zeeb 	return (error);
25076b4cac81SBjoern A. Zeeb }
25086b4cac81SBjoern A. Zeeb 
2509d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
25106b4cac81SBjoern A. Zeeb 
25116b4cac81SBjoern A. Zeeb /*
25126b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
25136b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
25146b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
25156b4cac81SBjoern A. Zeeb  */
25166b4cac81SBjoern A. Zeeb struct fsm_state {
25176b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
25186b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
25196b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
25206b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
25216b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
25226b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
25236b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
25246b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2525d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2526d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
25276b4cac81SBjoern A. Zeeb 
25286b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
25296b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
25306b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
25316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2532d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
25336b4cac81SBjoern A. Zeeb 
2534d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2535d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2536d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2537d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2538d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
25396b4cac81SBjoern A. Zeeb 
2540d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2541d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2542d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
25436b4cac81SBjoern A. Zeeb 
25446b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
25456b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
25466b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
25476b4cac81SBjoern A. Zeeb 
25486b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
25496b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
25506b4cac81SBjoern A. Zeeb };
25516b4cac81SBjoern A. Zeeb 
25526b4cac81SBjoern A. Zeeb static int
25536b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25546b4cac81SBjoern A. Zeeb {
25556b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
25566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25586b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25596b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
25606b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
25616b4cac81SBjoern A. Zeeb 	int error;
25626b4cac81SBjoern A. Zeeb 
25636b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
25646b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
25656b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
25666b4cac81SBjoern A. Zeeb 
25679d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25689d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
25696b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
25706b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
25719d9ba2b7SBjoern A. Zeeb #endif
25726b4cac81SBjoern A. Zeeb 
25736b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
25746b4cac81SBjoern A. Zeeb 
25756b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
25766b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25776b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
25786b4cac81SBjoern A. Zeeb 
25796b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
25806b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
25816b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
25826b4cac81SBjoern A. Zeeb 
25836b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
25846b4cac81SBjoern A. Zeeb 
25856b4cac81SBjoern A. Zeeb 	} else {
25866b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
25876b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
25886b4cac81SBjoern A. Zeeb 		return (ENOSYS);
25896b4cac81SBjoern A. Zeeb 	}
25906b4cac81SBjoern A. Zeeb 
25916b4cac81SBjoern A. Zeeb 	error = 0;
25926b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
25936b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
25949d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25959d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2596d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2597d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2598d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2599d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
26009d9ba2b7SBjoern A. Zeeb #endif
26016b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
26026b4cac81SBjoern A. Zeeb 			break;
26036b4cac81SBjoern A. Zeeb 		}
26046b4cac81SBjoern A. Zeeb 	}
26056b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
26066b4cac81SBjoern A. Zeeb 
26076b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2608d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
26096b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
26106b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
26116b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
26126b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
26136b4cac81SBjoern A. Zeeb 		return (ENOSYS);
26146b4cac81SBjoern A. Zeeb 	}
26156b4cac81SBjoern A. Zeeb 
26166b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
26179d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26189d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2619d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2620d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2621d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2622d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
26239d9ba2b7SBjoern A. Zeeb #endif
26246b4cac81SBjoern A. Zeeb 		return (0);
26256b4cac81SBjoern A. Zeeb 	}
26266b4cac81SBjoern A. Zeeb 
26276b4cac81SBjoern A. Zeeb 	if (error != 0) {
26286b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
26296b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
26306b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
26316b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
263245c27ad5SBjoern A. Zeeb 		return (error);
26336b4cac81SBjoern A. Zeeb 	}
26346b4cac81SBjoern A. Zeeb 
26359d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26369d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2637d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2638d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
26396b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
26409d9ba2b7SBjoern A. Zeeb #endif
26416b4cac81SBjoern A. Zeeb 
26426b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
26436b4cac81SBjoern A. Zeeb }
26446b4cac81SBjoern A. Zeeb 
26456b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
26466b4cac81SBjoern A. Zeeb 
2647d9f59799SBjoern A. Zeeb /*
2648d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2649d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2650d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2651d9f59799SBjoern A. Zeeb  */
2652d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2653d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2654d9f59799SBjoern A. Zeeb {
2655d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26562ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2657d9f59799SBjoern A. Zeeb 
26582ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2659d9f59799SBjoern A. Zeeb 
2660ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26612ac8a218SBjoern A. Zeeb 
26622ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26632ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26642ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26652ac8a218SBjoern A. Zeeb 
26662ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
26672ac8a218SBjoern A. Zeeb 	return (rni);
2668d9f59799SBjoern A. Zeeb }
2669d9f59799SBjoern A. Zeeb 
26704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26716b4cac81SBjoern A. Zeeb static int
26724a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
26736b4cac81SBjoern A. Zeeb {
26744a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
26756b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26766b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26776b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26786b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
26796b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
26806b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
26816b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
26826b4cac81SBjoern A. Zeeb 	int error;
26836b4cac81SBjoern A. Zeeb 	uint16_t ac;
26846b4cac81SBjoern A. Zeeb 
26856b4cac81SBjoern A. Zeeb 	IMPROVE();
26866b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
26876b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
26886b4cac81SBjoern A. Zeeb 
26896b4cac81SBjoern A. Zeeb 	if (vap == NULL)
26906b4cac81SBjoern A. Zeeb 		return (0);
26916b4cac81SBjoern A. Zeeb 
26924a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
26934a67f1dfSBjoern A. Zeeb 		return (0);
26944a67f1dfSBjoern A. Zeeb 
26956b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
26966b4cac81SBjoern A. Zeeb 		return (0);
26976b4cac81SBjoern A. Zeeb 
26984a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
26994a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
27004a67f1dfSBjoern A. Zeeb 		return (0);
27014a67f1dfSBjoern A. Zeeb 	}
27024a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
27036b4cac81SBjoern A. Zeeb 
27044a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
27056b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
27066b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
27076b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
27086b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
27096b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
27106b4cac81SBjoern A. Zeeb 
27114a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27124a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27134a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27144a67f1dfSBjoern A. Zeeb 
27156b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
27166b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
27176b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
27186b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
27196b4cac81SBjoern A. Zeeb 
27206b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
27216b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
27226b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
27236b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
27246b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
27256b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
272668541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
27276b4cac81SBjoern A. Zeeb 		if (error != 0)
27283f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
27296b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
27306b4cac81SBjoern A. Zeeb 	}
27316b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27326b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
27334a67f1dfSBjoern A. Zeeb 	if (!planned)
27346b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
27354a67f1dfSBjoern A. Zeeb 
27364a67f1dfSBjoern A. Zeeb 	return (changed);
27374a67f1dfSBjoern A. Zeeb }
27386b4cac81SBjoern A. Zeeb #endif
27396b4cac81SBjoern A. Zeeb 
27404a67f1dfSBjoern A. Zeeb static int
27414a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
27424a67f1dfSBjoern A. Zeeb {
27434a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27444a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
27454a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
27464a67f1dfSBjoern A. Zeeb 
27474a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
27484a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
27494a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
27506b4cac81SBjoern A. Zeeb 		return (0);
27514a67f1dfSBjoern A. Zeeb 
27524a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
27534a67f1dfSBjoern A. Zeeb 
27544a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
27554a67f1dfSBjoern A. Zeeb #endif
27564a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
27576b4cac81SBjoern A. Zeeb }
27586b4cac81SBjoern A. Zeeb 
27594aff4048SBjoern A. Zeeb /*
27604aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
27614aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
27624aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
27634aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
27644aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
27654aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
27664aff4048SBjoern A. Zeeb  */
27674aff4048SBjoern A. Zeeb static void
27684aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
27694aff4048SBjoern A. Zeeb {
27704aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
27714aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27724aff4048SBjoern A. Zeeb 
27734aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
27744aff4048SBjoern A. Zeeb 	/* NB: identify vap's by if_init; left as an extra check. */
27754aff4048SBjoern A. Zeeb 	if (ifp->if_init != ieee80211_init || (ifp->if_flags & IFF_UP) != 0) {
27764aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
27774aff4048SBjoern A. Zeeb 		return;
27784aff4048SBjoern A. Zeeb 	}
27794aff4048SBjoern A. Zeeb 
27804aff4048SBjoern A. Zeeb 	vif = arg;
27814aff4048SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, IF_LLADDR(ifp));
27824aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
27834aff4048SBjoern A. Zeeb }
27844aff4048SBjoern A. Zeeb 
27856b4cac81SBjoern A. Zeeb static struct ieee80211vap *
27866b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
27876b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
27886b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
27896b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
27906b4cac81SBjoern A. Zeeb {
27916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27936b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27946b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
27956b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2796a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
27976b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
27986b4cac81SBjoern A. Zeeb 	size_t len;
2799e3a0b120SBjoern A. Zeeb 	int error, i;
2800a6042e17SBjoern A. Zeeb 	uint16_t ac;
28016b4cac81SBjoern A. Zeeb 
28026b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
28036b4cac81SBjoern A. Zeeb 		return (NULL);
28046b4cac81SBjoern A. Zeeb 
28056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28076b4cac81SBjoern A. Zeeb 
28086b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
28096b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
28106b4cac81SBjoern A. Zeeb 
28116b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
28126b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
28136b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
28142ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
28152ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
28166b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
28176b4cac81SBjoern A. Zeeb 
28186b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28196b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
28206b4cac81SBjoern A. Zeeb 	vif->p2p = false;
28216b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
28226b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
28236b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
28246b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
28256b4cac81SBjoern A. Zeeb 	IMPROVE();
28266b4cac81SBjoern A. Zeeb 
28276b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
28286b4cac81SBjoern A. Zeeb #if 1
28296b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
2830adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
28316ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
28326ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
28334aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
28344aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
28356ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
28367b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
28376b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
28386b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
28396b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
28406b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
28416b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2842616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
2843616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
2844616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
2845616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
28466ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2847caaa79c3SBjoern A. Zeeb 	/*
2848caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2849caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2850caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2851caaa79c3SBjoern A. Zeeb 	 */
28526ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
28536b4cac81SBjoern A. Zeeb #endif
28546b4cac81SBjoern A. Zeeb #if 0
28556b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
28566b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
28576b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
28586b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
28596b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
28606b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
28616b4cac81SBjoern A. Zeeb #endif
2862e3a0b120SBjoern A. Zeeb 
28636ffb7bd4SBjoern A. Zeeb 	/* Link Config */
28646ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
28656ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
28666ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
28676ffb7bd4SBjoern A. Zeeb 	}
28686ffb7bd4SBjoern A. Zeeb 
2869e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2870e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2871e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2872e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2873e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2874e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2875e3a0b120SBjoern A. Zeeb 		else
2876e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
28770cbcfa19SBjoern A. Zeeb 
28780cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
28790cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
2880e3a0b120SBjoern A. Zeeb 	}
2881e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2882e3a0b120SBjoern A. Zeeb 
28836b4cac81SBjoern A. Zeeb 	IMPROVE();
28846b4cac81SBjoern A. Zeeb 
28856b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
28866b4cac81SBjoern A. Zeeb 	if (error != 0) {
28873f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
28886b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
28896b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
28906b4cac81SBjoern A. Zeeb 		return (NULL);
28916b4cac81SBjoern A. Zeeb 	}
28926b4cac81SBjoern A. Zeeb 
28936b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
28946b4cac81SBjoern A. Zeeb 	if (error != 0) {
28956b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
28963f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
28976b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
28986b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
28996b4cac81SBjoern A. Zeeb 		return (NULL);
29006b4cac81SBjoern A. Zeeb 	}
29016b4cac81SBjoern A. Zeeb 
29028891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
29036b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
29048891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
29056b4cac81SBjoern A. Zeeb 
29066b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
29076b4cac81SBjoern A. Zeeb 	changed = 0;
29086b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
29096b4cac81SBjoern A. Zeeb 
2910a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2911a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2912a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2913a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2914a6042e17SBjoern A. Zeeb 
2915a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
2916a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
2917a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
2918a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
2919a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
2920a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2921a6042e17SBjoern A. Zeeb 		if (error != 0)
2922a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2923a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
2924a6042e17SBjoern A. Zeeb 	}
2925a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2926a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
2927a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
29286b4cac81SBjoern A. Zeeb 
29296b4cac81SBjoern A. Zeeb 	/* Force MC init. */
29306b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
29316b4cac81SBjoern A. Zeeb 
29326b4cac81SBjoern A. Zeeb 	IMPROVE();
29336b4cac81SBjoern A. Zeeb 
29346b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
29356b4cac81SBjoern A. Zeeb 
29366b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
29376b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
29386b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2939d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2940d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
29416b4cac81SBjoern A. Zeeb 
29426b4cac81SBjoern A. Zeeb 	/* Key management. */
29436b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2944b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
29456b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
29466b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
29476b4cac81SBjoern A. Zeeb #endif
29486b4cac81SBjoern A. Zeeb 	}
29496b4cac81SBjoern A. Zeeb 
29509fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
29519fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
29529fb91463SBjoern A. Zeeb #endif
29539fb91463SBjoern A. Zeeb 
29546b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
29556b4cac81SBjoern A. Zeeb 
29566b4cac81SBjoern A. Zeeb 	/* Complete setup. */
29576b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
29586b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
29596b4cac81SBjoern A. Zeeb 
29606b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
29616b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
29626b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
29636b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
29646b4cac81SBjoern A. Zeeb 
29656b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
29666b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
29676b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
29686b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
29696b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
29706b4cac81SBjoern A. Zeeb 	/* any others? */
29716b4cac81SBjoern A. Zeeb 	IMPROVE();
29726b4cac81SBjoern A. Zeeb 
29736b4cac81SBjoern A. Zeeb 	return (vap);
29746b4cac81SBjoern A. Zeeb }
29756b4cac81SBjoern A. Zeeb 
29764b0af114SBjoern A. Zeeb void
29774b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
29784b0af114SBjoern A. Zeeb {
29794b0af114SBjoern A. Zeeb 
29804b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
29814b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
29824b0af114SBjoern A. Zeeb 
29834b0af114SBjoern A. Zeeb 	IMPROVE();
29844b0af114SBjoern A. Zeeb }
29854b0af114SBjoern A. Zeeb 
29864b0af114SBjoern A. Zeeb void
29874b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
29884b0af114SBjoern A. Zeeb {
29894b0af114SBjoern A. Zeeb 
29904b0af114SBjoern A. Zeeb 	TODO();
29914b0af114SBjoern A. Zeeb }
29924b0af114SBjoern A. Zeeb 
29936b4cac81SBjoern A. Zeeb static void
29946b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
29956b4cac81SBjoern A. Zeeb {
29966b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
29976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29986b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30006b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30016b4cac81SBjoern A. Zeeb 
30026b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
30036b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
30046b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
30056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30076b4cac81SBjoern A. Zeeb 
30084aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
30094aff4048SBjoern A. Zeeb 
30108891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
30116b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
30128891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
30136b4cac81SBjoern A. Zeeb 
30146b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
30156b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3016dbf76919SBjoern A. Zeeb 
3017dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3018dbf76919SBjoern A. Zeeb 
3019dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3020dbf76919SBjoern A. Zeeb 
30216c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
30227b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
30236c38c6b1SBjoern A. Zeeb 
30246b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
30256b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
30266b4cac81SBjoern A. Zeeb }
30276b4cac81SBjoern A. Zeeb 
30286b4cac81SBjoern A. Zeeb static void
30296b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
30306b4cac81SBjoern A. Zeeb {
30316b4cac81SBjoern A. Zeeb 
30326b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
30336b4cac81SBjoern A. Zeeb 	TRACEOK();
30346b4cac81SBjoern A. Zeeb }
30356b4cac81SBjoern A. Zeeb 
30366b4cac81SBjoern A. Zeeb static void
30376b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
30386b4cac81SBjoern A. Zeeb {
30396b4cac81SBjoern A. Zeeb 
30406b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30416b4cac81SBjoern A. Zeeb }
30426b4cac81SBjoern A. Zeeb 
30436b4cac81SBjoern A. Zeeb static void
30446b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
30456b4cac81SBjoern A. Zeeb {
30466b4cac81SBjoern A. Zeeb 
30476b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30486b4cac81SBjoern A. Zeeb }
30496b4cac81SBjoern A. Zeeb 
30506b4cac81SBjoern A. Zeeb /* Start / stop device. */
30516b4cac81SBjoern A. Zeeb static void
30526b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
30536b4cac81SBjoern A. Zeeb {
30546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30558d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30576b4cac81SBjoern A. Zeeb 	int error;
30588d58a057SBjoern A. Zeeb #endif
30596b4cac81SBjoern A. Zeeb 	bool start_all;
30606b4cac81SBjoern A. Zeeb 
30616b4cac81SBjoern A. Zeeb 	IMPROVE();
30626b4cac81SBjoern A. Zeeb 
30636b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30648d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30656b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30668d58a057SBjoern A. Zeeb #endif
30676b4cac81SBjoern A. Zeeb 	start_all = false;
30686b4cac81SBjoern A. Zeeb 
30698ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
30708ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
30716b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
30728d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30736b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
30746b4cac81SBjoern A. Zeeb 		if (error == 0)
30758d58a057SBjoern A. Zeeb #endif
30766b4cac81SBjoern A. Zeeb 			start_all = true;
30776b4cac81SBjoern A. Zeeb 	} else {
30788d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30797b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
30808d58a057SBjoern A. Zeeb #endif
30816b4cac81SBjoern A. Zeeb 	}
30828ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
30838ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
30846b4cac81SBjoern A. Zeeb 
30856b4cac81SBjoern A. Zeeb 	if (start_all)
30866b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
30876b4cac81SBjoern A. Zeeb }
30886b4cac81SBjoern A. Zeeb 
30896b4cac81SBjoern A. Zeeb bool
30906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
30916b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
30926b4cac81SBjoern A. Zeeb {
30936b4cac81SBjoern A. Zeeb 	int i;
30946b4cac81SBjoern A. Zeeb 
30956b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
30966b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
30976b4cac81SBjoern A. Zeeb 			return (true);
30986b4cac81SBjoern A. Zeeb 	}
30996b4cac81SBjoern A. Zeeb 
31006b4cac81SBjoern A. Zeeb 	return (false);
31016b4cac81SBjoern A. Zeeb }
31026b4cac81SBjoern A. Zeeb 
31036b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
31046b4cac81SBjoern A. Zeeb bool
31056b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
31066b4cac81SBjoern A. Zeeb {
31076b4cac81SBjoern A. Zeeb 	size_t x;
31086b4cac81SBjoern A. Zeeb 	uint8_t l;
31096b4cac81SBjoern A. Zeeb 
31106b4cac81SBjoern A. Zeeb 	x = *xp;
31116b4cac81SBjoern A. Zeeb 
31126b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
31136b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
31146b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
31156b4cac81SBjoern A. Zeeb 	x += 2 + l;
31166b4cac81SBjoern A. Zeeb 
31176b4cac81SBjoern A. Zeeb 	if (x > ies_len)
31186b4cac81SBjoern A. Zeeb 		return (false);
31196b4cac81SBjoern A. Zeeb 
31206b4cac81SBjoern A. Zeeb 	*xp = x;
31216b4cac81SBjoern A. Zeeb 	return (true);
31226b4cac81SBjoern A. Zeeb }
31236b4cac81SBjoern A. Zeeb 
3124d9945d78SBjoern A. Zeeb static uint8_t *
3125d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3126d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
31276b4cac81SBjoern A. Zeeb {
3128d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3129d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
31303dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3131d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3132d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3133d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3134d9945d78SBjoern A. Zeeb 	int band, i;
31356b4cac81SBjoern A. Zeeb 
31363dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3137d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3138d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3139d9945d78SBjoern A. Zeeb 			continue;
3140d9945d78SBjoern A. Zeeb 
3141d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3142d9945d78SBjoern A. Zeeb 		/*
3143d9945d78SBjoern A. Zeeb 		 * This should not happen;
3144d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3145d9945d78SBjoern A. Zeeb 		 */
3146d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3147d9945d78SBjoern A. Zeeb 			continue;
3148d9945d78SBjoern A. Zeeb 
3149d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3150d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3151d9945d78SBjoern A. Zeeb 		chan = NULL;
3152d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3153d9945d78SBjoern A. Zeeb 
3154d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3155d9945d78SBjoern A. Zeeb 				continue;
3156d9945d78SBjoern A. Zeeb 
31573dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3158d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3159d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3160d9945d78SBjoern A. Zeeb 				break;
3161d9945d78SBjoern A. Zeeb 		}
3162d9945d78SBjoern A. Zeeb 
3163d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3164d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3165d9945d78SBjoern A. Zeeb 			continue;
3166d9945d78SBjoern A. Zeeb 
3167d9945d78SBjoern A. Zeeb 		pb = p;
31683dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3169d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3170d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3171d9945d78SBjoern A. Zeeb 
31723dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
31733dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
31743dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
31753dd98026SBjoern A. Zeeb 
31763dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
31773dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
31783dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
31793dd98026SBjoern A. Zeeb 		}
31803dd98026SBjoern A. Zeeb #endif
31813dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
31823dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
31833dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
31843dd98026SBjoern A. Zeeb 
31853dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
31863dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
31873dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
31883dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
31893dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
31903dd98026SBjoern A. Zeeb 		}
31913dd98026SBjoern A. Zeeb #endif
31923dd98026SBjoern A. Zeeb 
3193d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3194d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3195d9945d78SBjoern A. Zeeb 	}
3196d9945d78SBjoern A. Zeeb 
3197d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3198d9945d78SBjoern A. Zeeb 	pb = p;
3199d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3200d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3201d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3202d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3203d9945d78SBjoern A. Zeeb 	}
3204d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3205d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3206d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3207d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3208d9945d78SBjoern A. Zeeb 	}
3209d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3210d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3211d9945d78SBjoern A. Zeeb 
3212d9945d78SBjoern A. Zeeb 	return (p);
32136b4cac81SBjoern A. Zeeb }
32146b4cac81SBjoern A. Zeeb 
32156b4cac81SBjoern A. Zeeb static void
32166b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
32176b4cac81SBjoern A. Zeeb {
32186b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32196b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32206b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32216b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32226b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
32236b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
32246b4cac81SBjoern A. Zeeb 	int error;
32258ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
32266b4cac81SBjoern A. Zeeb 
32276b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3229a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
32306b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
32318ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32326b4cac81SBjoern A. Zeeb 		return;
32336b4cac81SBjoern A. Zeeb 	}
32348ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
32358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32366b4cac81SBjoern A. Zeeb 
32376b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
32386b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
32396b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3240d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
32416b4cac81SBjoern A. Zeeb 		return;
32426b4cac81SBjoern A. Zeeb 	}
32436b4cac81SBjoern A. Zeeb 
32446b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32458ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3246a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3247a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3248d3ef7fb4SBjoern A. Zeeb sw_scan:
32496b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32506b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3251086be6a8SBjoern A. Zeeb 
3252086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3253086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3254086be6a8SBjoern A. Zeeb 
32556b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
32566b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
32576b4cac81SBjoern A. Zeeb 		IMPROVE();
32586b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
32596b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
32606b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
32616b4cac81SBjoern A. Zeeb 
32626b4cac81SBjoern A. Zeeb 	} else {
32636b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
32646b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
32656b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
32666b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
32676b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
32686b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3269d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3270d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3271d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
32723206587aSBjoern A. Zeeb 		bool running;
3273d9945d78SBjoern A. Zeeb 
3274d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3275d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
32766b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
32776b4cac81SBjoern A. Zeeb 
3278d9945d78SBjoern A. Zeeb 		band_mask = 0;
32796b4cac81SBjoern A. Zeeb 		nchan = 0;
3280d9945d78SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++) {
32816b4cac81SBjoern A. Zeeb 			nchan++;
3282d9945d78SBjoern A. Zeeb 			band = lkpi_net80211_chan_to_nl80211_band(
3283d9945d78SBjoern A. Zeeb 			    ss->ss_chans[ss->ss_next + i]);
3284d9945d78SBjoern A. Zeeb 			band_mask |= (1 << band);
3285d9945d78SBjoern A. Zeeb 		}
32863206587aSBjoern A. Zeeb 
32873206587aSBjoern A. Zeeb 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
32883206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
32893206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
32903206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
32913206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
32923206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
32933206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
32943206587aSBjoern A. Zeeb 			 */
32953206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
32963206587aSBjoern A. Zeeb 		}
32973206587aSBjoern A. Zeeb 
32986b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
32996b4cac81SBjoern A. Zeeb 
3300d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3301d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3302d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3303d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3304d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3305d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3306d9945d78SBjoern A. Zeeb 
3307d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3308d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3309d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3310d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3311d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3312d9945d78SBjoern A. Zeeb 		}
3313d9945d78SBjoern A. Zeeb 
33143206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3315d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3316d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
33176b4cac81SBjoern A. Zeeb 
33186b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
33196b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
33206b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
33216b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
33226b4cac81SBjoern A. Zeeb #if 0
33236b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
33246b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
33256b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
33266b4cac81SBjoern A. Zeeb #endif
33276b4cac81SBjoern A. Zeeb #ifdef __notyet__
33286b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
33296b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
33306b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
33316b4cac81SBjoern A. Zeeb #endif
3332e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
33336b4cac81SBjoern A. Zeeb 
33346b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
33356b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
33366b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
33376b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
33386b4cac81SBjoern A. Zeeb 			*(cpp + i) =
33396b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
33406b4cac81SBjoern A. Zeeb 		}
33416b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
33426b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
33436b4cac81SBjoern A. Zeeb 
33446b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
33453206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
33466b4cac81SBjoern A. Zeeb 			/* lc->flags */
33476b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
33486b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
33496b4cac81SBjoern A. Zeeb 			/* lc-> ... */
33506b4cac81SBjoern A. Zeeb 			lc++;
33516b4cac81SBjoern A. Zeeb 		}
33526b4cac81SBjoern A. Zeeb 
3353d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
33546b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
33556b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
33566b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3357d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
33586b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
33596b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
33606b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
33616b4cac81SBjoern A. Zeeb 				ssids++;
33626b4cac81SBjoern A. Zeeb 			}
33636b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
33646b4cac81SBjoern A. Zeeb 		} else {
33656b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
33666b4cac81SBjoern A. Zeeb 		}
33676b4cac81SBjoern A. Zeeb 
33686b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
33696b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
33706b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
33716b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
33726b4cac81SBjoern A. Zeeb 		/* s6gp->... */
33736b4cac81SBjoern A. Zeeb 
3374d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3375d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3376d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3377d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3378d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3379d9945d78SBjoern A. Zeeb 
33806b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33816b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
33823206587aSBjoern A. Zeeb 
33833206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
33843206587aSBjoern A. Zeeb 		/* Re-check under lock. */
33853206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
33863206587aSBjoern A. Zeeb 		if (!running) {
33873206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
33883206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
33893206587aSBjoern A. Zeeb 
33903206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
33913206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
33923206587aSBjoern A. Zeeb 		}
33933206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33943206587aSBjoern A. Zeeb 		if (running) {
33953206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
33963206587aSBjoern A. Zeeb 			return;
33973206587aSBjoern A. Zeeb 		}
33983206587aSBjoern A. Zeeb 
33996b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
34006b4cac81SBjoern A. Zeeb 		if (error != 0) {
3401d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3402d9945d78SBjoern A. Zeeb 
34033206587aSBjoern A. Zeeb 			/*
34043206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
34053206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
34063206587aSBjoern A. Zeeb 			 * and only there.
34073206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
34083206587aSBjoern A. Zeeb 			 * behave differently:
34093206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
34103206587aSBjoern A. Zeeb 			 *        and can then still return an error.
34113206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
34123206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
34133206587aSBjoern A. Zeeb 			 * ...
34143206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
34153206587aSBjoern A. Zeeb 			 * and balance between both code paths.
34163206587aSBjoern A. Zeeb 			 */
34173206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
34183206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
34193206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
34206b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
34213206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
34223206587aSBjoern A. Zeeb 			}
34233206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3424d3ef7fb4SBjoern A. Zeeb 
3425d3ef7fb4SBjoern A. Zeeb 			/*
3426d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3427d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3428d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3429d3ef7fb4SBjoern A. Zeeb 			 */
3430196cfd0bSBjoern A. Zeeb 			if (error == 1) {
34318ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3432a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
34338ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34343206587aSBjoern A. Zeeb 				/*
34353206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
34363206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
34373206587aSBjoern A. Zeeb 				 * currently not re-enable it?
34383206587aSBjoern A. Zeeb 				 */
34393206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3440196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3441196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3442196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3443196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3444196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3445196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3446196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3447196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3448d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3449196cfd0bSBjoern A. Zeeb 			}
3450d3ef7fb4SBjoern A. Zeeb 
3451d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3452d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
34536b4cac81SBjoern A. Zeeb 		}
34546b4cac81SBjoern A. Zeeb 	}
34556b4cac81SBjoern A. Zeeb }
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb static void
34586b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
34596b4cac81SBjoern A. Zeeb {
34606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34618ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
34626b4cac81SBjoern A. Zeeb 
34636b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3465a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
34668ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34676b4cac81SBjoern A. Zeeb 		return;
34686b4cac81SBjoern A. Zeeb 	}
34698ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
34708ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34716b4cac81SBjoern A. Zeeb 
34728ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3473a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3474a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
34756b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
34766b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
34776b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
34786b4cac81SBjoern A. Zeeb 
3479a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3480a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
34816b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
34826b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
34836b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3484a486fbbdSBjoern A. Zeeb 
34856b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
34866b4cac81SBjoern A. Zeeb 
34876b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3488086be6a8SBjoern A. Zeeb 
3489086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3490086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
34916b4cac81SBjoern A. Zeeb 	}
34926b4cac81SBjoern A. Zeeb }
34936b4cac81SBjoern A. Zeeb 
34946b4cac81SBjoern A. Zeeb static void
3495a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3496a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3497a486fbbdSBjoern A. Zeeb {
3498a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
34998ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3500a486fbbdSBjoern A. Zeeb 
3501a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
35028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35038ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
35048ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35058ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3506a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3507a486fbbdSBjoern A. Zeeb }
3508a486fbbdSBjoern A. Zeeb 
3509a486fbbdSBjoern A. Zeeb static void
3510a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3511a486fbbdSBjoern A. Zeeb {
3512a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
35138ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3514a486fbbdSBjoern A. Zeeb 
3515a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
35168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35178ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
35188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35198ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3520a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3521a486fbbdSBjoern A. Zeeb }
3522a486fbbdSBjoern A. Zeeb 
3523a486fbbdSBjoern A. Zeeb static void
35246b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
35256b4cac81SBjoern A. Zeeb {
35266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35276b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3528b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3529b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
35306b4cac81SBjoern A. Zeeb 	int error;
35318ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
35326b4cac81SBjoern A. Zeeb 
35336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3534b2cf3c21SBjoern A. Zeeb 
3535b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3536b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
35376b4cac81SBjoern A. Zeeb 		return;
35386b4cac81SBjoern A. Zeeb 
3539a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
35408ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35418ac540d3SBjoern A. Zeeb 	hw_scan_running =
35428ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
35438ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
35448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35458ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3546a486fbbdSBjoern A. Zeeb 		return;
3547a486fbbdSBjoern A. Zeeb 
3548b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3549b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
35506b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
35516b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
35526b4cac81SBjoern A. Zeeb 		return;
35536b4cac81SBjoern A. Zeeb 	}
35546b4cac81SBjoern A. Zeeb 
35556b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
35566b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
35576b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
35586b4cac81SBjoern A. Zeeb 		    c, chan);
35596b4cac81SBjoern A. Zeeb 		return;
35606b4cac81SBjoern A. Zeeb 	}
35616b4cac81SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
35636b4cac81SBjoern A. Zeeb 	IMPROVE();
35646b4cac81SBjoern A. Zeeb 
35656b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35664a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
35679fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
35689fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
35699fb91463SBjoern A. Zeeb #endif
35704a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
35716b4cac81SBjoern A. Zeeb 
35726b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
35736b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
35746b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
35756b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
35766b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
35776b4cac81SBjoern A. Zeeb 		IMPROVE();
35786b4cac81SBjoern A. Zeeb 	} else {
35796b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
35806b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
35816b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
35826b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
35836b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
35846b4cac81SBjoern A. Zeeb 	}
35856b4cac81SBjoern A. Zeeb 
35866b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
35876b4cac81SBjoern A. Zeeb 	IMPROVE();
35886b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
35896b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
35906b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
35916b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
35926b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
35936b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
35946b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
35956b4cac81SBjoern A. Zeeb 			    error);
35966b4cac81SBjoern A. Zeeb 	}
35976b4cac81SBjoern A. Zeeb }
35986b4cac81SBjoern A. Zeeb 
35996b4cac81SBjoern A. Zeeb static struct ieee80211_node *
36006b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
36016b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
36026b4cac81SBjoern A. Zeeb {
36036b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36054f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
36066b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
36086b4cac81SBjoern A. Zeeb 
36096b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
36106b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36116b4cac81SBjoern A. Zeeb 
36126b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
36134f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
36144f61ef8bSBjoern A. Zeeb 		return (NULL);
36154f61ef8bSBjoern A. Zeeb 
36166b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
36176b4cac81SBjoern A. Zeeb 	if (ni == NULL)
36186b4cac81SBjoern A. Zeeb 		return (NULL);
36196b4cac81SBjoern A. Zeeb 
36206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36214f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
36226b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
36236b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
36246b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
36256b4cac81SBjoern A. Zeeb 		return (NULL);
36266b4cac81SBjoern A. Zeeb 	}
36276b4cac81SBjoern A. Zeeb 
36286b4cac81SBjoern A. Zeeb 	return (ni);
36296b4cac81SBjoern A. Zeeb }
36306b4cac81SBjoern A. Zeeb 
36316b4cac81SBjoern A. Zeeb static int
36326b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
36336b4cac81SBjoern A. Zeeb {
36346b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36366b4cac81SBjoern A. Zeeb 	int error;
36376b4cac81SBjoern A. Zeeb 
36386b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36396b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36406b4cac81SBjoern A. Zeeb 
36416b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
36426b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
36436b4cac81SBjoern A. Zeeb 		if (error != 0)
36446b4cac81SBjoern A. Zeeb 			return (error);
36456b4cac81SBjoern A. Zeeb 	}
36466b4cac81SBjoern A. Zeeb 
36476b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
36486b4cac81SBjoern A. Zeeb 	IMPROVE();
36496b4cac81SBjoern A. Zeeb 
36506b4cac81SBjoern A. Zeeb 	return (0);
36516b4cac81SBjoern A. Zeeb }
36526b4cac81SBjoern A. Zeeb 
36536b4cac81SBjoern A. Zeeb static void
36546b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
36556b4cac81SBjoern A. Zeeb {
36566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36586b4cac81SBjoern A. Zeeb 
36596b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36606b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36616b4cac81SBjoern A. Zeeb 
36626b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
36636b4cac81SBjoern A. Zeeb 	IMPROVE();
36646b4cac81SBjoern A. Zeeb 
36656b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
36666b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
36676b4cac81SBjoern A. Zeeb }
36686b4cac81SBjoern A. Zeeb 
36696b4cac81SBjoern A. Zeeb static void
36706b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
36716b4cac81SBjoern A. Zeeb {
36726b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36746b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
36756b4cac81SBjoern A. Zeeb 
36766b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36776b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36786b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
36796b4cac81SBjoern A. Zeeb 
36800936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
36816b4cac81SBjoern A. Zeeb 
36820936c648SBjoern A. Zeeb 	/*
36830936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
36840936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
36850936c648SBjoern A. Zeeb 	 */
36860936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
36876b4cac81SBjoern A. Zeeb 
36886b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
36896b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
36906b4cac81SBjoern A. Zeeb }
36916b4cac81SBjoern A. Zeeb 
36926b4cac81SBjoern A. Zeeb static int
36936b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
36946b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
36956b4cac81SBjoern A. Zeeb {
36966b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
36976b4cac81SBjoern A. Zeeb 
36986b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3699fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
370088665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
3701fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3702fa4e4257SBjoern A. Zeeb 		/*
3703fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3704fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
3705fa4e4257SBjoern A. Zeeb 		 */
37060936c648SBjoern A. Zeeb 		m_free(m);
37070936c648SBjoern A. Zeeb 		return (ENETDOWN);
37080936c648SBjoern A. Zeeb 	}
37096b4cac81SBjoern A. Zeeb 
37106b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
37116b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
3712fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3713fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
37146b4cac81SBjoern A. Zeeb 
37159d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37169d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
37176b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
37186b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
37196b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
37209d9ba2b7SBjoern A. Zeeb #endif
37216b4cac81SBjoern A. Zeeb 
37226b4cac81SBjoern A. Zeeb 	return (0);
37236b4cac81SBjoern A. Zeeb }
37246b4cac81SBjoern A. Zeeb 
37256b4cac81SBjoern A. Zeeb static void
37266b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
37276b4cac81SBjoern A. Zeeb {
37286b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3729b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
37306b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3731b35f6cd0SBjoern A. Zeeb #endif
37326b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
37336b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
37346b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
37356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37366b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37376b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37386b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
37396b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
37406b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
37416b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
37426b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3743e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
3744ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
37456b4cac81SBjoern A. Zeeb 	void *buf;
3746e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
37476b4cac81SBjoern A. Zeeb 
37486b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
37496b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
37516b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
37526b4cac81SBjoern A. Zeeb #endif
37536b4cac81SBjoern A. Zeeb 
37546b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3755b35f6cd0SBjoern A. Zeeb 	k = NULL;
3756b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
37576b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
37586b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
37596b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
37606b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
37616b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
37626b4cac81SBjoern A. Zeeb 		if (k == NULL) {
37636b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
37646b4cac81SBjoern A. Zeeb 			m_freem(m);
37656b4cac81SBjoern A. Zeeb 			return;
37666b4cac81SBjoern A. Zeeb 		}
37676b4cac81SBjoern A. Zeeb 	}
37686b4cac81SBjoern A. Zeeb #endif
37696b4cac81SBjoern A. Zeeb 
37706b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
37716b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37736b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
37746b4cac81SBjoern A. Zeeb 
37756b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
37766b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
37776b4cac81SBjoern A. Zeeb 
37786b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
37796b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
37806b4cac81SBjoern A. Zeeb 		if (k != NULL)
37816b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
37826b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
37836b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
37846b4cac81SBjoern A. Zeeb 		IMPROVE();
37856b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
37866b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
37876b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
37886b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
37896b4cac81SBjoern A. Zeeb 		}
37906b4cac81SBjoern A. Zeeb 
37916b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
37926b4cac81SBjoern A. Zeeb 	}
37936b4cac81SBjoern A. Zeeb 
37946b4cac81SBjoern A. Zeeb 	/*
37956b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
37966b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
37973d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
37983d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
37996b4cac81SBjoern A. Zeeb 	 */
38006b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
38016b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
38023f0083c4SBjoern A. Zeeb 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
38036b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
38046b4cac81SBjoern A. Zeeb 		m_freem(m);
38056b4cac81SBjoern A. Zeeb 		return;
38066b4cac81SBjoern A. Zeeb 	}
38076b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
38086b4cac81SBjoern A. Zeeb 
38096b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
38106b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
38116b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
38126b4cac81SBjoern A. Zeeb 	skb->m = m;
38136b4cac81SBjoern A. Zeeb #if 0
38146b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
38156b4cac81SBjoern A. Zeeb #else
38166b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
38176b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
38186b4cac81SBjoern A. Zeeb #endif
38196b4cac81SBjoern A. Zeeb 	/* Save the ni. */
38206b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
38216b4cac81SBjoern A. Zeeb 
38226b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
38236b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
38246b4cac81SBjoern A. Zeeb 
3825e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3826e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3827e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
38281665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
38291665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
38301665ef97SBjoern A. Zeeb 			skb->priority = 7;
38311665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
38321665ef97SBjoern A. Zeeb 		} else {
38331665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
38341665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
3835e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
3836e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
38371665ef97SBjoern A. Zeeb 		}
3838e3a0b120SBjoern A. Zeeb 	} else {
3839e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3840fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
3841e3a0b120SBjoern A. Zeeb 	}
38426b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
38436b4cac81SBjoern A. Zeeb 
38446b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
38456b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
38466b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
38476b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
38486b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
38496b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3850e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
38516b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
38526b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
38536b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
38546b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
38559fb91463SBjoern A. Zeeb #ifdef __notyet__
38569fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38579fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
38589fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
38599fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
38609fb91463SBjoern A. Zeeb #endif
38619fb91463SBjoern A. Zeeb #endif
38626b4cac81SBjoern A. Zeeb 
38636b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3864b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
38656b4cac81SBjoern A. Zeeb 	info->control.hw_key = lsta->kc;
38666b4cac81SBjoern A. Zeeb #endif
38676b4cac81SBjoern A. Zeeb 
38686b4cac81SBjoern A. Zeeb 	IMPROVE();
38696b4cac81SBjoern A. Zeeb 
3870e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
3871e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
3872e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
3873e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
3874e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3875d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3876e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
3877e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
3878e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3879e3a0b120SBjoern A. Zeeb 	}
3880e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
3881d0d29110SBjoern A. Zeeb 		goto ops_tx;
3882e3a0b120SBjoern A. Zeeb 
3883d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3884d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3885d0d29110SBjoern A. Zeeb 
3886eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
38876b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
38889d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
38899d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3890d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3891d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3892d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3893fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
3894d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
3895d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3896d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3897d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
38989d9ba2b7SBjoern A. Zeeb #endif
3899eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
390045bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3901d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
390245bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
39036b4cac81SBjoern A. Zeeb 	return;
39046b4cac81SBjoern A. Zeeb 
39056b4cac81SBjoern A. Zeeb ops_tx:
39069d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39079d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3908d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3909d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
39106b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
39116b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
39129d9ba2b7SBjoern A. Zeeb #endif
39136b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
39146b4cac81SBjoern A. Zeeb 	control.sta = sta;
391545bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
39166b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
391745bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
39186b4cac81SBjoern A. Zeeb }
39196b4cac81SBjoern A. Zeeb 
39206b4cac81SBjoern A. Zeeb static void
39216b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
39226b4cac81SBjoern A. Zeeb {
39236b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39246b4cac81SBjoern A. Zeeb 	struct mbufq mq;
39256b4cac81SBjoern A. Zeeb 	struct mbuf *m;
392688665349SBjoern A. Zeeb 	bool shall_tx;
39276b4cac81SBjoern A. Zeeb 
39286b4cac81SBjoern A. Zeeb 	lsta = ctx;
39296b4cac81SBjoern A. Zeeb 
39309d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39319d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
39326b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
39339d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
39346b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
39359d9ba2b7SBjoern A. Zeeb #endif
39366b4cac81SBjoern A. Zeeb 
39376b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
39386b4cac81SBjoern A. Zeeb 
3939fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3940fa4e4257SBjoern A. Zeeb 	/*
3941fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
394288665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
394388665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
394488665349SBjoern A. Zeeb 	 * point to try to TX.
394588665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
394688665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
3947fa4e4257SBjoern A. Zeeb 	 */
394888665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
394988665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
39506b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
395188665349SBjoern A. Zeeb 	/*
395288665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
395388665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
395488665349SBjoern A. Zeeb 	 */
3955fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
39566b4cac81SBjoern A. Zeeb 
39576b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
39586b4cac81SBjoern A. Zeeb 	while (m != NULL) {
39596b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
39606b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
39616b4cac81SBjoern A. Zeeb 	}
39626b4cac81SBjoern A. Zeeb }
39636b4cac81SBjoern A. Zeeb 
39646b4cac81SBjoern A. Zeeb static int
39656b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
39666b4cac81SBjoern A. Zeeb {
39676b4cac81SBjoern A. Zeeb 
39686b4cac81SBjoern A. Zeeb 	/* XXX TODO */
39696b4cac81SBjoern A. Zeeb 	IMPROVE();
39706b4cac81SBjoern A. Zeeb 
39716b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
39726b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
39736b4cac81SBjoern A. Zeeb 
39746b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
39756b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
39766b4cac81SBjoern A. Zeeb }
39776b4cac81SBjoern A. Zeeb 
39789fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
39799fb91463SBjoern A. Zeeb static int
39809fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
39819fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
39829fb91463SBjoern A. Zeeb {
39839fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39849fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39859fb91463SBjoern A. Zeeb 
39869fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39879fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39889fb91463SBjoern A. Zeeb 
39899fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39909fb91463SBjoern A. Zeeb 
39919fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
39929fb91463SBjoern A. Zeeb }
39939fb91463SBjoern A. Zeeb 
39949fb91463SBjoern A. Zeeb static int
39959fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
39969fb91463SBjoern A. Zeeb {
39979fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39989fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39999fb91463SBjoern A. Zeeb 
40009fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40019fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40029fb91463SBjoern A. Zeeb 
40039fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40049fb91463SBjoern A. Zeeb 
40059fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
40069fb91463SBjoern A. Zeeb }
40079fb91463SBjoern A. Zeeb 
40089fb91463SBjoern A. Zeeb 
40099fb91463SBjoern A. Zeeb static int
40109fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
40119fb91463SBjoern A. Zeeb {
40129fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40139fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40149fb91463SBjoern A. Zeeb 
40159fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40169fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40179fb91463SBjoern A. Zeeb 
40189fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40199fb91463SBjoern A. Zeeb 
40209fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
40219fb91463SBjoern A. Zeeb }
40229fb91463SBjoern A. Zeeb 
40239fb91463SBjoern A. Zeeb static int
40249fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
40259fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
40269fb91463SBjoern A. Zeeb {
40279fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40289fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40299fb91463SBjoern A. Zeeb 
40309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40329fb91463SBjoern A. Zeeb 
40339fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40349fb91463SBjoern A. Zeeb 
40359fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
40369fb91463SBjoern A. Zeeb }
40379fb91463SBjoern A. Zeeb 
40389fb91463SBjoern A. Zeeb static int
40399fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
40409fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
40419fb91463SBjoern A. Zeeb {
40429fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40439fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40449fb91463SBjoern A. Zeeb 
40459fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40469fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40479fb91463SBjoern A. Zeeb 
40489fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40499fb91463SBjoern A. Zeeb 
40509fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
40519fb91463SBjoern A. Zeeb }
40529fb91463SBjoern A. Zeeb 
40539fb91463SBjoern A. Zeeb static void
40549fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
40559fb91463SBjoern A. Zeeb {
40569fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40579fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40589fb91463SBjoern A. Zeeb 
40599fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40609fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40619fb91463SBjoern A. Zeeb 
40629fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40639fb91463SBjoern A. Zeeb 
40649fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
40659fb91463SBjoern A. Zeeb }
40669fb91463SBjoern A. Zeeb 
40679fb91463SBjoern A. Zeeb static void
40689fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
40699fb91463SBjoern A. Zeeb {
40709fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40719fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40729fb91463SBjoern A. Zeeb 
40739fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40749fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40759fb91463SBjoern A. Zeeb 
40769fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40779fb91463SBjoern A. Zeeb 
40789fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
40799fb91463SBjoern A. Zeeb }
40809fb91463SBjoern A. Zeeb 
40819fb91463SBjoern A. Zeeb static void
40829fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
40839fb91463SBjoern A. Zeeb     int status)
40849fb91463SBjoern A. Zeeb {
40859fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40869fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40879fb91463SBjoern A. Zeeb 
40889fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40899fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40909fb91463SBjoern A. Zeeb 
40919fb91463SBjoern A. Zeeb 	IMPROVE_HT();
40929fb91463SBjoern A. Zeeb 
40939fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
40949fb91463SBjoern A. Zeeb }
40959fb91463SBjoern A. Zeeb 
40969fb91463SBjoern A. Zeeb static int
40979fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
40989fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
40999fb91463SBjoern A. Zeeb {
41009fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
41019fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41029fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41039fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
41049fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41059fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
41069fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
41079fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
41089fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
41099fb91463SBjoern A. Zeeb 	int error;
41109fb91463SBjoern A. Zeeb 
41119fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
41129fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
41139fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41149fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
41159fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
41169fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
41179fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
41189fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
41199fb91463SBjoern A. Zeeb 
41209fb91463SBjoern A. Zeeb 	params.sta = sta;
41219fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
41229fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
41239fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
41249fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
41259fb91463SBjoern A. Zeeb 	else
41269fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
41279fb91463SBjoern A. Zeeb 	if (params.buf_size > hw->max_rx_aggregation_subframes)
41289fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
41299fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
41309fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
41319fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
41329fb91463SBjoern A. Zeeb 	params.amsdu = false;
41339fb91463SBjoern A. Zeeb 
41349fb91463SBjoern A. Zeeb 	IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
41359fb91463SBjoern A. Zeeb 
41369fb91463SBjoern A. Zeeb 	/* This may call kalloc.  Make sure we can sleep. */
41379fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
41389fb91463SBjoern A. Zeeb 	if (error != 0) {
41399fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
41409fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
41419fb91463SBjoern A. Zeeb 		return (error);
41429fb91463SBjoern A. Zeeb 	}
41439fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
41449fb91463SBjoern A. Zeeb 
41459fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
41469fb91463SBjoern A. Zeeb 	return (error);
41479fb91463SBjoern A. Zeeb }
41489fb91463SBjoern A. Zeeb 
41499fb91463SBjoern A. Zeeb static void
41509fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
41519fb91463SBjoern A. Zeeb {
41529fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
41539fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41549fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41559fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
41569fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41579fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
41589fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
41599fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
41609fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
41619fb91463SBjoern A. Zeeb 	int error;
41629fb91463SBjoern A. Zeeb 	uint8_t tid;
41639fb91463SBjoern A. Zeeb 
41649fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
41659fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
41669fb91463SBjoern A. Zeeb 
41679fb91463SBjoern A. Zeeb 	/*
41689fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
41699fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
41709fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
41719fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
41729fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
41739fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
41749fb91463SBjoern A. Zeeb 	 * track some state itself.
41759fb91463SBjoern A. Zeeb 	 */
41769fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
41779fb91463SBjoern A. Zeeb 		goto net80211_only;
41789fb91463SBjoern A. Zeeb 
41799fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41809fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
41819fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
41829fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
41839fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
41849fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
41859fb91463SBjoern A. Zeeb 
41869fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
41879fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
41889fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
41899fb91463SBjoern A. Zeeb 			break;
41909fb91463SBjoern A. Zeeb 	}
41919fb91463SBjoern A. Zeeb 
41929fb91463SBjoern A. Zeeb 	params.sta = sta;
41939fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
41949fb91463SBjoern A. Zeeb 	params.buf_size = 0;
41959fb91463SBjoern A. Zeeb 	params.timeout = 0;
41969fb91463SBjoern A. Zeeb 	params.ssn = 0;
41979fb91463SBjoern A. Zeeb 	params.tid = tid;
41989fb91463SBjoern A. Zeeb 	params.amsdu = false;
41999fb91463SBjoern A. Zeeb 
42009fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
42019fb91463SBjoern A. Zeeb 	if (error != 0)
42029fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
42039fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
42049fb91463SBjoern A. Zeeb 
42059fb91463SBjoern A. Zeeb net80211_only:
42069fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
42079fb91463SBjoern A. Zeeb }
42089fb91463SBjoern A. Zeeb #endif
42099fb91463SBjoern A. Zeeb 
42109fb91463SBjoern A. Zeeb static void
42119fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
42129fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
42139fb91463SBjoern A. Zeeb {
42149fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
42159fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
42169fb91463SBjoern A. Zeeb 
42179fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
42189fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
42199fb91463SBjoern A. Zeeb 		return;
42209fb91463SBjoern A. Zeeb 
42219fb91463SBjoern A. Zeeb 	switch (band) {
42229fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
42239fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
42249fb91463SBjoern A. Zeeb 		break;
42259fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
42269fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
42279fb91463SBjoern A. Zeeb 		break;
42289fb91463SBjoern A. Zeeb 	default:
42299fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
42309fb91463SBjoern A. Zeeb 		return;
42319fb91463SBjoern A. Zeeb 	}
42329fb91463SBjoern A. Zeeb 
42339fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
42349fb91463SBjoern A. Zeeb 
42359fb91463SBjoern A. Zeeb 	/*
42369fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
42379fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
42389fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
42399fb91463SBjoern A. Zeeb 	 */
42409fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
42419fb91463SBjoern A. Zeeb 
42429fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
42439fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
42449fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
42459fb91463SBjoern A. Zeeb #ifdef __notyet__
42469fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
42479fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
42489fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
42499fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
42509fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
42519fb91463SBjoern A. Zeeb #endif
42529fb91463SBjoern A. Zeeb 
42539fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
42549fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
42559fb91463SBjoern A. Zeeb 
42569fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
42579fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
42589fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
42599fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
42609fb91463SBjoern A. Zeeb #endif
42619fb91463SBjoern A. Zeeb }
42629fb91463SBjoern A. Zeeb 
42636b4cac81SBjoern A. Zeeb static void
42646b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
42656b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
42666b4cac81SBjoern A. Zeeb {
42676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42686b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
42696b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
42706b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
42716b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
42726b4cac81SBjoern A. Zeeb 
42736b4cac81SBjoern A. Zeeb 	/* Channels */
42746b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42756b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42766b4cac81SBjoern A. Zeeb 
42776b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
42786b4cac81SBjoern A. Zeeb 	nchans = 0;
42796b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
42806b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
42816b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
42826b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
42836b4cac81SBjoern A. Zeeb 		chan_flags = 0;
42846b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
42856b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
42869fb91463SBjoern A. Zeeb 
42879fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
42886b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
42899fb91463SBjoern A. Zeeb 
42909fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
42919fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
42926b4cac81SBjoern A. Zeeb 
42936b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4294cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
42956b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
42966b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
42976b4cac81SBjoern A. Zeeb 
42986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
42993f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
43003f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
43016b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
43026b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
43036b4cac81SBjoern A. Zeeb 				continue;
43046b4cac81SBjoern A. Zeeb 			}
43056b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
43066b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
43076b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
43086b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
43096b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
43106b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
43116b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
43126b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
43136b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
43146b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
43156b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
43166b4cac81SBjoern A. Zeeb 
43176b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
43186b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
43196b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
43205856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4321cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4322cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
43233f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
43243f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
43256b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
43266b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
43276b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4328cee56e77SBjoern A. Zeeb 			if (error != 0)
43296b4cac81SBjoern A. Zeeb 				break;
43306b4cac81SBjoern A. Zeeb 		}
43316b4cac81SBjoern A. Zeeb 	}
43326b4cac81SBjoern A. Zeeb 
43336b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
43346b4cac81SBjoern A. Zeeb 	nchans = 0;
43356b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
43366b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
43376b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
43386b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
43396b4cac81SBjoern A. Zeeb 		chan_flags = 0;
43406b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
43419fb91463SBjoern A. Zeeb 
43429fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
43439fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
43449fb91463SBjoern A. Zeeb 
43459fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
43466b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
43476b4cac81SBjoern A. Zeeb 
43486b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4349562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
43506b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
43516b4cac81SBjoern A. Zeeb 
43526b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
43536b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
43546b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4355562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
43566b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
43576b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4358562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
43596b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
43606b4cac81SBjoern A. Zeeb 		}
43616b4cac81SBjoern A. Zeeb #endif
43626b4cac81SBjoern A. Zeeb 
43636b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4364cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
43656b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
43666b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
43676b4cac81SBjoern A. Zeeb 
43686b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
43693f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
43703f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
43716b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
43726b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
43736b4cac81SBjoern A. Zeeb 				continue;
43746b4cac81SBjoern A. Zeeb 			}
43756b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
43766b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
43776b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
43786b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
43796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
43806b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
43816b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
43826b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
43836b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
43846b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
43856b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
43866b4cac81SBjoern A. Zeeb 
43876b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
43886b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
43896b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
43905856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4391cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4392cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
43933f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
43943f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
43956b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
43966b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
43976b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4398cee56e77SBjoern A. Zeeb 			if (error != 0)
43996b4cac81SBjoern A. Zeeb 				break;
44006b4cac81SBjoern A. Zeeb 		}
44016b4cac81SBjoern A. Zeeb 	}
44026b4cac81SBjoern A. Zeeb }
44036b4cac81SBjoern A. Zeeb 
44046b4cac81SBjoern A. Zeeb static void *
44056b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
44066b4cac81SBjoern A. Zeeb {
44076b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44086b4cac81SBjoern A. Zeeb 
44096b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
44106b4cac81SBjoern A. Zeeb 
44116b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
44126b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
44136b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
44146b4cac81SBjoern A. Zeeb 
44156b4cac81SBjoern A. Zeeb 	return (ic);
44166b4cac81SBjoern A. Zeeb }
44176b4cac81SBjoern A. Zeeb 
44186b4cac81SBjoern A. Zeeb struct ieee80211_hw *
44196b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
44206b4cac81SBjoern A. Zeeb {
44216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44236b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4424a5ae63edSBjoern A. Zeeb 	int ac;
44256b4cac81SBjoern A. Zeeb 
44266b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
44276b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
44286b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
44296b4cac81SBjoern A. Zeeb 		return (NULL);
44306b4cac81SBjoern A. Zeeb 
44316b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
44326b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4433652e22d3SBjoern A. Zeeb 
44348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
44358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4436eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
44378891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
44386b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4439a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4440a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4441a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4442a5ae63edSBjoern A. Zeeb 	}
44436b4cac81SBjoern A. Zeeb 
4444759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
4445759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4446759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4447759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4448759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
4449759a996dSBjoern A. Zeeb 
44506b4cac81SBjoern A. Zeeb 	/*
44516b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
44526b4cac81SBjoern A. Zeeb 	 * not initialized.
44536b4cac81SBjoern A. Zeeb 	 */
44546b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44556b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4456086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
44576b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
44586b4cac81SBjoern A. Zeeb 
44596b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
44606b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
44616b4cac81SBjoern A. Zeeb 
44626b4cac81SBjoern A. Zeeb 	IMPROVE();
44636b4cac81SBjoern A. Zeeb 
44646b4cac81SBjoern A. Zeeb 	return (hw);
44656b4cac81SBjoern A. Zeeb }
44666b4cac81SBjoern A. Zeeb 
44676b4cac81SBjoern A. Zeeb void
44686b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
44696b4cac81SBjoern A. Zeeb {
44706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4471759a996dSBjoern A. Zeeb 	struct mbuf *m;
44726b4cac81SBjoern A. Zeeb 
44736b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
44746b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
44756b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
44766b4cac81SBjoern A. Zeeb 
4477759a996dSBjoern A. Zeeb 	/*
4478759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
4479759a996dSBjoern A. Zeeb 	 */
4480759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4481759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
4482759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4483759a996dSBjoern A. Zeeb 
4484759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4485759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4486759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4487759a996dSBjoern A. Zeeb 
4488759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4489759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
4490759a996dSBjoern A. Zeeb 	while (m != NULL) {
4491759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
4492759a996dSBjoern A. Zeeb 
4493759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4494759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
4495759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
4496759a996dSBjoern A. Zeeb 
4497759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4498759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
4499759a996dSBjoern A. Zeeb 		}
4500759a996dSBjoern A. Zeeb 		m_freem(m);
4501759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
4502759a996dSBjoern A. Zeeb 	}
4503759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4504759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4505759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4506759a996dSBjoern A. Zeeb 
45076b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4508eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
45098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4510eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4511eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
45126b4cac81SBjoern A. Zeeb 	IMPROVE();
45136b4cac81SBjoern A. Zeeb }
45146b4cac81SBjoern A. Zeeb 
45156b4cac81SBjoern A. Zeeb void
45166b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
45176b4cac81SBjoern A. Zeeb {
45186b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45196b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45206b4cac81SBjoern A. Zeeb 
45216b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
45226b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
45236b4cac81SBjoern A. Zeeb 
45246b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
45256b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
45266b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
45276b4cac81SBjoern A. Zeeb 
45286b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
45296b4cac81SBjoern A. Zeeb }
45306b4cac81SBjoern A. Zeeb 
45316b4cac81SBjoern A. Zeeb struct ieee80211_hw *
45326b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
45336b4cac81SBjoern A. Zeeb {
45346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45356b4cac81SBjoern A. Zeeb 
45366b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
45376b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
45386b4cac81SBjoern A. Zeeb }
45396b4cac81SBjoern A. Zeeb 
45406b4cac81SBjoern A. Zeeb static void
45416b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
45426b4cac81SBjoern A. Zeeb {
45436b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45446b4cac81SBjoern A. Zeeb 
45456b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
45466b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
45476b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
45486b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
45496b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
45506b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
45516b4cac81SBjoern A. Zeeb }
45526b4cac81SBjoern A. Zeeb 
45532e183d99SBjoern A. Zeeb int
45546b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
45556b4cac81SBjoern A. Zeeb {
45566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4558c5b96b3eSBjoern A. Zeeb 	int band, i;
45596b4cac81SBjoern A. Zeeb 
45606b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
45616b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
45626b4cac81SBjoern A. Zeeb 
4563652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
4564652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4565652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
4566652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
4567652e22d3SBjoern A. Zeeb 
45686b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
45696b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
45706b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
45716b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
45726b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
45736b4cac81SBjoern A. Zeeb 		/* We take the first one. */
45746b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
45756b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
45766b4cac81SBjoern A. Zeeb 	} else {
45776b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
45786b4cac81SBjoern A. Zeeb 	}
45796b4cac81SBjoern A. Zeeb 
45803d09d310SBjoern A. Zeeb #ifdef __not_yet__
45813d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
45826b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
45833d09d310SBjoern A. Zeeb #endif
45846b4cac81SBjoern A. Zeeb 
45856b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
45866b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
45876b4cac81SBjoern A. Zeeb 
45886b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
45896b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
45906b4cac81SBjoern A. Zeeb 	ic->ic_caps =
45916b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
45926b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
45936b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
45944a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
45956b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
45964a67f1dfSBjoern A. Zeeb #endif
45976b4cac81SBjoern A. Zeeb #if 0
45986b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
45996b4cac81SBjoern A. Zeeb #endif
46006b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
46016b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
46026b4cac81SBjoern A. Zeeb 	    ;
46036b4cac81SBjoern A. Zeeb #if 0
46046b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
46056b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
46066b4cac81SBjoern A. Zeeb #endif
4607cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
4608cc4e78d5SBjoern A. Zeeb 		/*
4609cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
4610cc4e78d5SBjoern A. Zeeb 		 *
4611cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4612cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
4613cc4e78d5SBjoern A. Zeeb 		 * the flag.
4614cc4e78d5SBjoern A. Zeeb 		 */
46156b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4616a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
46176b4cac81SBjoern A. Zeeb 	}
46186b4cac81SBjoern A. Zeeb 
4619527687a9SBjoern A. Zeeb 	/*
4620527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
4621527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
4622527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
4623527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4624527687a9SBjoern A. Zeeb 	 */
4625527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
4626527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
4627527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
4628527687a9SBjoern A. Zeeb 
4629527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4630527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
4631527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
4632527687a9SBjoern A. Zeeb 		}
4633527687a9SBjoern A. Zeeb 	}
4634527687a9SBjoern A. Zeeb 
46356b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
4636b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
46376b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
46386b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
46396b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
46406b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
46416b4cac81SBjoern A. Zeeb 	}
46426b4cac81SBjoern A. Zeeb #endif
46436b4cac81SBjoern A. Zeeb 
46446b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
46456b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
46466b4cac81SBjoern A. Zeeb 
46476b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
46486b4cac81SBjoern A. Zeeb 
46496b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
46506b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
46516b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
46526b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
46536b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
46546b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
46556b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
46566b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
46576b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
46586b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
46596b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
46606b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
46616b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
46626b4cac81SBjoern A. Zeeb 
4663a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4664a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4665a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4666a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4667a486fbbdSBjoern A. Zeeb 
46686b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
46696b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
46706b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
46716b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
46726b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
46736b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
46746b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
46756b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
46766b4cac81SBjoern A. Zeeb 
46779fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
4678*86bc7259SBjoern A. Zeeb 	/*
4679*86bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
4680*86bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
4681*86bc7259SBjoern A. Zeeb 	 */
4682*86bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
46839fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
46849fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
46859fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
46869fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
46879fb91463SBjoern A. Zeeb 
46889fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
46899fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
46909fb91463SBjoern A. Zeeb 
46919fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
46929fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
46939fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
46949fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
46959fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
46969fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
46979fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
46989fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
46999fb91463SBjoern A. Zeeb 
47009fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
47019fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
47029fb91463SBjoern A. Zeeb 
47039fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
47049fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
47059fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
47069fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
4707*86bc7259SBjoern A. Zeeb 	}
47089fb91463SBjoern A. Zeeb #endif
47099fb91463SBjoern A. Zeeb 
47106b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
47116b4cac81SBjoern A. Zeeb 
4712c5b96b3eSBjoern A. Zeeb 	/*
4713c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
4714c5b96b3eSBjoern A. Zeeb 	 * expect one.
4715d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
4716d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4717c5b96b3eSBjoern A. Zeeb 	 */
4718d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
4719f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4720c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
4721c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
4722c5b96b3eSBjoern A. Zeeb 
4723c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4724c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4725c5b96b3eSBjoern A. Zeeb 			continue;
4726c5b96b3eSBjoern A. Zeeb 
4727d9945d78SBjoern A. Zeeb 		lhw->supbands++;
4728d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4729d9945d78SBjoern A. Zeeb 
4730f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
4731f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
4732f454a4a1SBjoern A. Zeeb 			continue;
4733f454a4a1SBjoern A. Zeeb 
4734c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
4735c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4736c5b96b3eSBjoern A. Zeeb 
4737c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4738c5b96b3eSBjoern A. Zeeb 				continue;
4739c5b96b3eSBjoern A. Zeeb 
47404a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
47419fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
47429fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
47439fb91463SBjoern A. Zeeb #endif
4744c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
4745c5b96b3eSBjoern A. Zeeb 			break;
4746c5b96b3eSBjoern A. Zeeb 		}
4747c5b96b3eSBjoern A. Zeeb 	}
4748c5b96b3eSBjoern A. Zeeb 
4749d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4750d9945d78SBjoern A. Zeeb 
4751d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
4752d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4753d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4754d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4755d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4756d9945d78SBjoern A. Zeeb 	}
4757d9945d78SBjoern A. Zeeb 
4758d9945d78SBjoern A. Zeeb 	/*
4759d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
4760d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
4761d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
4762d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
4763d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
4764d9945d78SBjoern A. Zeeb 	 */
4765d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4766d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4767d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
476878ca45dfSBjoern A. Zeeb 
476978ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4770d9945d78SBjoern A. Zeeb 		/*
477178ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
477278ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
477378ca45dfSBjoern A. Zeeb 		 * space in.
4774d9945d78SBjoern A. Zeeb 		 */
4775d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
477678ca45dfSBjoern A. Zeeb 	}
4777d9945d78SBjoern A. Zeeb 
47789fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
47799fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
47809fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
47819fb91463SBjoern A. Zeeb #endif
47829fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
47839fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
47849fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
47859fb91463SBjoern A. Zeeb #endif
47869fb91463SBjoern A. Zeeb 
4787d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
478878ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
478978ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
479078ca45dfSBjoern A. Zeeb 			goto err;
4791d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
479278ca45dfSBjoern A. Zeeb 	}
4793d9945d78SBjoern A. Zeeb 
47946b4cac81SBjoern A. Zeeb 	if (bootverbose)
47956b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
47962e183d99SBjoern A. Zeeb 
47972e183d99SBjoern A. Zeeb 	return (0);
479878ca45dfSBjoern A. Zeeb err:
479978ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
480078ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
48016b4cac81SBjoern A. Zeeb }
48026b4cac81SBjoern A. Zeeb 
48036b4cac81SBjoern A. Zeeb void
48046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
48056b4cac81SBjoern A. Zeeb {
48066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48076b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48086b4cac81SBjoern A. Zeeb 
48096b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
48106b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48116b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
48126b4cac81SBjoern A. Zeeb }
48136b4cac81SBjoern A. Zeeb 
48146b4cac81SBjoern A. Zeeb void
48156b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
48166b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
48176b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
48186b4cac81SBjoern A. Zeeb     void *arg)
48196b4cac81SBjoern A. Zeeb {
48206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48216b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
48226b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
482361a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
48246b4cac81SBjoern A. Zeeb 
48256b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
48266b4cac81SBjoern A. Zeeb 
48276b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
48286b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
482961a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4830adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
48316b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
48326b4cac81SBjoern A. Zeeb 		    __func__, flags);
48336b4cac81SBjoern A. Zeeb 	}
48346b4cac81SBjoern A. Zeeb 
4835adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
48366b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
483761a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
48386b4cac81SBjoern A. Zeeb 
48396b4cac81SBjoern A. Zeeb 	if (atomic)
48408891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
48416b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
48426b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
48436b4cac81SBjoern A. Zeeb 
48446b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
48456b4cac81SBjoern A. Zeeb 
48466b4cac81SBjoern A. Zeeb 		/*
48476b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
48486b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
48496b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
48506b4cac81SBjoern A. Zeeb 		 * driver does not know about.
48516b4cac81SBjoern A. Zeeb 		 */
48526b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
48536b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
48546b4cac81SBjoern A. Zeeb 			continue;
48556b4cac81SBjoern A. Zeeb 
48566b4cac81SBjoern A. Zeeb 		/*
485761a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
485861a68e50SBjoern A. Zeeb 		 * if we haven't yet.
485961a68e50SBjoern A. Zeeb 		 */
486061a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
486161a68e50SBjoern A. Zeeb 			continue;
486261a68e50SBjoern A. Zeeb 
486361a68e50SBjoern A. Zeeb 		/*
48646b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
48656b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
48666b4cac81SBjoern A. Zeeb 		 */
48676b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
48686b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
48696b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
48706b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
48716b4cac81SBjoern A. Zeeb 	}
48726b4cac81SBjoern A. Zeeb 	if (atomic)
48738891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
48746b4cac81SBjoern A. Zeeb }
48756b4cac81SBjoern A. Zeeb 
48766b4cac81SBjoern A. Zeeb void
48776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
48786b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
48796b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
48806b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
48816b4cac81SBjoern A. Zeeb     void *arg)
48826b4cac81SBjoern A. Zeeb {
48836b4cac81SBjoern A. Zeeb 
48846b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
48856b4cac81SBjoern A. Zeeb }
48866b4cac81SBjoern A. Zeeb 
48876b4cac81SBjoern A. Zeeb void
48886b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
48896b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
48906b4cac81SBjoern A. Zeeb 	void *),
48916b4cac81SBjoern A. Zeeb     void *arg)
48926b4cac81SBjoern A. Zeeb {
4893c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4894c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4895c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4896c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
48976b4cac81SBjoern A. Zeeb 
4898c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
4899c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4900c5e25798SBjoern A. Zeeb 
4901c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
4902c5e25798SBjoern A. Zeeb 
4903c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
4904c5e25798SBjoern A. Zeeb 
4905c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4906c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4907c5e25798SBjoern A. Zeeb 
4908c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4909c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
4910c5e25798SBjoern A. Zeeb 			continue;
4911c5e25798SBjoern A. Zeeb 
4912c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4913c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
4914c5e25798SBjoern A. Zeeb 			continue;
4915c5e25798SBjoern A. Zeeb 
4916d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
4917c5e25798SBjoern A. Zeeb 	}
4918c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
49196b4cac81SBjoern A. Zeeb }
49206b4cac81SBjoern A. Zeeb 
49216b4cac81SBjoern A. Zeeb void
49226b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
49236b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
49246b4cac81SBjoern A. Zeeb {
49256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
49276b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49286b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
49296b4cac81SBjoern A. Zeeb 
49306b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
49316b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
49326b4cac81SBjoern A. Zeeb 
49336b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
49346b4cac81SBjoern A. Zeeb 
49358891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
49366b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
49376b4cac81SBjoern A. Zeeb 
49386b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
49396b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
49406b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
49416b4cac81SBjoern A. Zeeb 				continue;
49426b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
49436b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
49446b4cac81SBjoern A. Zeeb 		}
49456b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
49466b4cac81SBjoern A. Zeeb 	}
49478891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
49486b4cac81SBjoern A. Zeeb }
49496b4cac81SBjoern A. Zeeb 
4950673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
4951673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4952673d62fcSBjoern A. Zeeb {
4953673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
4954673d62fcSBjoern A. Zeeb 
4955673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4956673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
4957673d62fcSBjoern A. Zeeb 	return (regd);
4958673d62fcSBjoern A. Zeeb }
4959673d62fcSBjoern A. Zeeb 
49606b4cac81SBjoern A. Zeeb int
49616b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
49626b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
49636b4cac81SBjoern A. Zeeb {
49646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49656b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49666b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
49676b4cac81SBjoern A. Zeeb 
49686b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
49696b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
49706b4cac81SBjoern A. Zeeb 
49716b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
49726b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
49736b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
49746b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
49756b4cac81SBjoern A. Zeeb 	}
49766b4cac81SBjoern A. Zeeb 
49776b4cac81SBjoern A. Zeeb 	TODO();
49786b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
49796b4cac81SBjoern A. Zeeb 
49806b4cac81SBjoern A. Zeeb 	return (0);
49816b4cac81SBjoern A. Zeeb }
49826b4cac81SBjoern A. Zeeb 
49836b4cac81SBjoern A. Zeeb void
49846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
49856b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
49866b4cac81SBjoern A. Zeeb {
49876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49886b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49896b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
49906b4cac81SBjoern A. Zeeb 
49916b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
49926b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
49936b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
49946b4cac81SBjoern A. Zeeb 
49956b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
49966b4cac81SBjoern A. Zeeb 
49978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
49986b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
49996b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5000a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
50016b4cac81SBjoern A. Zeeb 	wakeup(lhw);
50028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
50036b4cac81SBjoern A. Zeeb 
50046b4cac81SBjoern A. Zeeb 	return;
50056b4cac81SBjoern A. Zeeb }
50066b4cac81SBjoern A. Zeeb 
5007759a996dSBjoern A. Zeeb static void
5008759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5009759a996dSBjoern A. Zeeb {
5010759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5011759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5012759a996dSBjoern A. Zeeb 	int ok;
5013759a996dSBjoern A. Zeeb 
5014759a996dSBjoern A. Zeeb 	ni = NULL;
5015759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5016759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5017759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5018759a996dSBjoern A. Zeeb 
5019759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5020759a996dSBjoern A. Zeeb 		ni = rxni->ni;
5021759a996dSBjoern A. Zeeb 	}
5022759a996dSBjoern A. Zeeb 
5023759a996dSBjoern A. Zeeb 	if (ni != NULL) {
5024759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
5025759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
5026759a996dSBjoern A. Zeeb 		if (ok < 0)
5027759a996dSBjoern A. Zeeb 			m_freem(m);
5028759a996dSBjoern A. Zeeb 	} else {
5029759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5030759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
5031759a996dSBjoern A. Zeeb 	}
5032759a996dSBjoern A. Zeeb 
5033759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5034759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5035bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5036759a996dSBjoern A. Zeeb #endif
5037759a996dSBjoern A. Zeeb }
5038759a996dSBjoern A. Zeeb 
5039759a996dSBjoern A. Zeeb static void
5040759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5041759a996dSBjoern A. Zeeb {
5042759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5043759a996dSBjoern A. Zeeb 	struct mbufq mq;
5044759a996dSBjoern A. Zeeb 	struct mbuf *m;
5045759a996dSBjoern A. Zeeb 
5046759a996dSBjoern A. Zeeb 	lhw = ctx;
5047759a996dSBjoern A. Zeeb 
5048759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5049759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5050bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5051bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5052759a996dSBjoern A. Zeeb #endif
5053759a996dSBjoern A. Zeeb 
5054759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
5055759a996dSBjoern A. Zeeb 
5056759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5057759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
5058759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5059759a996dSBjoern A. Zeeb 
5060759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
5061759a996dSBjoern A. Zeeb 	while (m != NULL) {
5062759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5063759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
5064759a996dSBjoern A. Zeeb 	}
5065759a996dSBjoern A. Zeeb }
5066759a996dSBjoern A. Zeeb 
5067e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
50686b4cac81SBjoern A. Zeeb void
50696b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
5070e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
5071e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
50726b4cac81SBjoern A. Zeeb {
50736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50746b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
50756b4cac81SBjoern A. Zeeb 	struct mbuf *m;
50766b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
50776b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
50786b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
50796b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
50806b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
50816b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
50826b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50839d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
5084170acccfSBjoern A. Zeeb 	int8_t rssi;
5085c0cadd99SBjoern A. Zeeb 	bool is_beacon;
50866b4cac81SBjoern A. Zeeb 
50876b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
50886b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
50896b4cac81SBjoern A. Zeeb 		IMPROVE();
50906b4cac81SBjoern A. Zeeb 		goto err;
50916b4cac81SBjoern A. Zeeb 	}
50926b4cac81SBjoern A. Zeeb 
50936b4cac81SBjoern A. Zeeb 	/*
50946b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
50956b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
50966b4cac81SBjoern A. Zeeb 	 */
50976b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
50986b4cac81SBjoern A. Zeeb 	if (m == NULL)
50996b4cac81SBjoern A. Zeeb 		goto err;
51006b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
51016b4cac81SBjoern A. Zeeb 
51026b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
51036b4cac81SBjoern A. Zeeb 	offset = m->m_len;
51046b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
51056b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
51066b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
51076b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
51086b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
51096b4cac81SBjoern A. Zeeb 	}
51106b4cac81SBjoern A. Zeeb 
51116b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
51126b4cac81SBjoern A. Zeeb 
51136b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
5114c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
5115c0cadd99SBjoern A. Zeeb 
5116c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51179d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
51186b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
51196b4cac81SBjoern A. Zeeb 
51209d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
51216b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5122c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
51236b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
51246b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
51256b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
5126c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
51276b4cac81SBjoern A. Zeeb 
51289d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
51296b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
51306b4cac81SBjoern A. Zeeb 
51316b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
51329d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5133bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
513460970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
51356b4cac81SBjoern A. Zeeb 			__func__,
5136c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
5137c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
51386b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
51396b4cac81SBjoern A. Zeeb 			rx_status->flag,
51406b4cac81SBjoern A. Zeeb 			rx_status->freq,
51416b4cac81SBjoern A. Zeeb 			rx_status->bw,
51426b4cac81SBjoern A. Zeeb 			rx_status->encoding,
51436b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
51446b4cac81SBjoern A. Zeeb 			rx_status->band,
51456b4cac81SBjoern A. Zeeb 			rx_status->chains,
51466b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
51476b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
51486b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
514960970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
51506b4cac81SBjoern A. Zeeb 			rx_status->signal,
51516b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
51526b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
51536b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
51546b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
51556b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
51566b4cac81SBjoern A. Zeeb 			rx_status->nss,
51576b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
51586b4cac81SBjoern A. Zeeb no_trace_beacons:
51596b4cac81SBjoern A. Zeeb #endif
51606b4cac81SBjoern A. Zeeb 
51616b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
51626b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
516360970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
516460970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
51656b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
51666b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5167170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
51686b4cac81SBjoern A. Zeeb 	else
5169170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
5170170acccfSBjoern A. Zeeb 	/*
5171170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
5172170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
5173170acccfSBjoern A. Zeeb 	 */
5174170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
5175170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
51766b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
51776b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
51786b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
51796b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
51806b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
51816b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
51826b4cac81SBjoern A. Zeeb 
51836b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
51846b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
51856b4cac81SBjoern A. Zeeb 
51866b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
51876b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
51886b4cac81SBjoern A. Zeeb 
51896b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
51906b4cac81SBjoern A. Zeeb 	if (ok == 0) {
5191dbc06dd9SBjoern A. Zeeb 		m_freem(m);
51926b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
51936b4cac81SBjoern A. Zeeb 		goto err;
51946b4cac81SBjoern A. Zeeb 	}
51956b4cac81SBjoern A. Zeeb 
519658246901SBjoern A. Zeeb 	lsta = NULL;
51976b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
51986b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
51996b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
52006b4cac81SBjoern A. Zeeb 	} else {
5201c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
5202c8dafefaSBjoern A. Zeeb 
52036b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
52046b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
52056b4cac81SBjoern A. Zeeb 		if (ni != NULL)
52066b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
52076b4cac81SBjoern A. Zeeb 	}
52086b4cac81SBjoern A. Zeeb 
52096b4cac81SBjoern A. Zeeb 	if (ni != NULL)
52106b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
52116b4cac81SBjoern A. Zeeb 	else
52126b4cac81SBjoern A. Zeeb 		/*
52136b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
52146b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
52156b4cac81SBjoern A. Zeeb 		 */
52166b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
52176b4cac81SBjoern A. Zeeb 
52189d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
52199d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5220bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5221c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5222c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
52239d9ba2b7SBjoern A. Zeeb #endif
52246b4cac81SBjoern A. Zeeb 
5225c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
5226c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
5227c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5228c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
5229c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
5230c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
5231c8dafefaSBjoern A. Zeeb 
5232c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
5233c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5234c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
5235c8dafefaSBjoern A. Zeeb 
5236c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
5237c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5238c8dafefaSBjoern A. Zeeb 
5239c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
5240c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
5241c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5242c8dafefaSBjoern A. Zeeb 		/*
5243c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
5244c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5245c8dafefaSBjoern A. Zeeb 		 */
5246c8dafefaSBjoern A. Zeeb skip_device_ts:
52479fb91463SBjoern A. Zeeb 		;
52489fb91463SBjoern A. Zeeb 	}
5249c8dafefaSBjoern A. Zeeb 
52506b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
52516b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
52526b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
52536b4cac81SBjoern A. Zeeb 
52546b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
52556b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
52566b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
52576b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
52586b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
52596b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
52606b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
52616b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
52626b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
52636b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
52646b4cac81SBjoern A. Zeeb #endif
52656b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
52666b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
52676b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
52686b4cac81SBjoern A. Zeeb 		IMPROVE();
52696b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
52706b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
52716b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
52726b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5273170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
52746b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
52756b4cac81SBjoern A. Zeeb 	}
52766b4cac81SBjoern A. Zeeb 
52776b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
52786b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
52796b4cac81SBjoern A. Zeeb 
5280e30e05d3SBjoern A. Zeeb #if 0
5281e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
5282e30e05d3SBjoern A. Zeeb 		/*
5283e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
5284e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5285e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
5286e30e05d3SBjoern A. Zeeb 		*/
5287e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5288e30e05d3SBjoern A. Zeeb 	}
5289e30e05d3SBjoern A. Zeeb #endif
5290e30e05d3SBjoern A. Zeeb 
5291759a996dSBjoern A. Zeeb 	/*
5292759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
5293759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
5294759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
5295759a996dSBjoern A. Zeeb 	 */
52966b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
5297759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5298759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5299759a996dSBjoern A. Zeeb 
5300759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5301759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5302759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5303759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5304759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
5305759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
5306759a996dSBjoern A. Zeeb 		}
53076b4cac81SBjoern A. Zeeb 	}
53086b4cac81SBjoern A. Zeeb 
5309759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5310759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
5311759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5312759a996dSBjoern A. Zeeb 		m_freem(m);
5313759a996dSBjoern A. Zeeb 		goto err;
5314759a996dSBjoern A. Zeeb 	}
5315759a996dSBjoern A. Zeeb 
5316759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
5317759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5318759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
53196b4cac81SBjoern A. Zeeb 
53206b4cac81SBjoern A. Zeeb 	IMPROVE();
53216b4cac81SBjoern A. Zeeb 
53226b4cac81SBjoern A. Zeeb err:
53236b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
53246b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
53256b4cac81SBjoern A. Zeeb }
53266b4cac81SBjoern A. Zeeb 
53276b4cac81SBjoern A. Zeeb uint8_t
5328ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
53296b4cac81SBjoern A. Zeeb {
53306b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
5331ec190d91SBjoern A. Zeeb 	uint8_t tid;
5332ec190d91SBjoern A. Zeeb 
5333ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
5334ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5335ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5336ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
53376b4cac81SBjoern A. Zeeb 
53386b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
5339ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
5340ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5341ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5342ec190d91SBjoern A. Zeeb 
5343ec190d91SBjoern A. Zeeb 	return (tid);
53446b4cac81SBjoern A. Zeeb }
53456b4cac81SBjoern A. Zeeb 
5346ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
5347ac1d519cSBjoern A. Zeeb 
5348ac1d519cSBjoern A. Zeeb static void
5349ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
5350ac1d519cSBjoern A. Zeeb {
5351ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5352ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
5353ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
5354ac1d519cSBjoern A. Zeeb 
5355ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
5356ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5357ac1d519cSBjoern A. Zeeb 
5358ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
5359ac1d519cSBjoern A. Zeeb 
5360ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5361ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
5362ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
5363ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
5364ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5365ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
5366ac1d519cSBjoern A. Zeeb 		return;
5367ac1d519cSBjoern A. Zeeb 	}
5368ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
5369ac1d519cSBjoern A. Zeeb 
5370ac1d519cSBjoern A. Zeeb 	/* More work to do? */
5371ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
5372ac1d519cSBjoern A. Zeeb 		schedule_work(work);
5373ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5374ac1d519cSBjoern A. Zeeb 
5375ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
5376ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
5377ac1d519cSBjoern A. Zeeb 
5378ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
5379ac1d519cSBjoern A. Zeeb }
5380ac1d519cSBjoern A. Zeeb 
5381ac1d519cSBjoern A. Zeeb void
5382ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
5383ac1d519cSBjoern A. Zeeb {
5384ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5385ac1d519cSBjoern A. Zeeb 
5386ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5387ac1d519cSBjoern A. Zeeb 
5388ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5389ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
5390ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
5391ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
5392ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5393ac1d519cSBjoern A. Zeeb 
5394ac1d519cSBjoern A. Zeeb 	/*
5395ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
5396ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
5397ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
5398ac1d519cSBjoern A. Zeeb 	 */
5399ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
5400ac1d519cSBjoern A. Zeeb }
5401ac1d519cSBjoern A. Zeeb 
5402ac1d519cSBjoern A. Zeeb void
5403ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
5404ac1d519cSBjoern A. Zeeb {
5405ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5406ac1d519cSBjoern A. Zeeb 
5407ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5408ac1d519cSBjoern A. Zeeb 
5409ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5410ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
5411ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
5412ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
5413ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5414ac1d519cSBjoern A. Zeeb }
5415ac1d519cSBjoern A. Zeeb 
5416ac1d519cSBjoern A. Zeeb void
5417ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
5418ac1d519cSBjoern A. Zeeb {
5419ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5420ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
5421ac1d519cSBjoern A. Zeeb 
5422ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5423ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5424ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
5425ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
5426ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5427ac1d519cSBjoern A. Zeeb 		return;
5428ac1d519cSBjoern A. Zeeb 	}
5429ac1d519cSBjoern A. Zeeb 
5430ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
5431ac1d519cSBjoern A. Zeeb 
5432ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
5433ac1d519cSBjoern A. Zeeb 		    entry);
5434ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
5435ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5436ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
5437ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5438ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
5439ac1d519cSBjoern A. Zeeb 			break;
5440ac1d519cSBjoern A. Zeeb 	}
5441ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5442ac1d519cSBjoern A. Zeeb }
5443ac1d519cSBjoern A. Zeeb 
5444ac1d519cSBjoern A. Zeeb void
5445ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
5446ac1d519cSBjoern A. Zeeb {
5447ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
5448ac1d519cSBjoern A. Zeeb 
5449ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
5450ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
5451ac1d519cSBjoern A. Zeeb }
5452ac1d519cSBjoern A. Zeeb 
5453ac1d519cSBjoern A. Zeeb void
5454ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
5455ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
5456ac1d519cSBjoern A. Zeeb {
5457ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
5458ac1d519cSBjoern A. Zeeb 		/* Run right away. */
5459ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
5460ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
5461ac1d519cSBjoern A. Zeeb 	} else {
5462ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
5463ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
5464ac1d519cSBjoern A. Zeeb 	}
5465ac1d519cSBjoern A. Zeeb }
5466ac1d519cSBjoern A. Zeeb 
5467ac1d519cSBjoern A. Zeeb void
5468ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
5469ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
5470ac1d519cSBjoern A. Zeeb {
5471ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
5472ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
5473ac1d519cSBjoern A. Zeeb }
5474ac1d519cSBjoern A. Zeeb 
5475ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
5476ac1d519cSBjoern A. Zeeb 
54776b4cac81SBjoern A. Zeeb struct wiphy *
54786b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
54796b4cac81SBjoern A. Zeeb {
54806b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5481ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
54826b4cac81SBjoern A. Zeeb 
54836b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
54846b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
54856b4cac81SBjoern A. Zeeb 		return (NULL);
54866b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
54876b4cac81SBjoern A. Zeeb 
5488ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
5489ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
5490ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
5491ac1d519cSBjoern A. Zeeb 
5492ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5493ac1d519cSBjoern A. Zeeb 
5494ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
5495ac1d519cSBjoern A. Zeeb 	TODO();
5496ac1d519cSBjoern A. Zeeb 
5497ac1d519cSBjoern A. Zeeb 	return (wiphy);
54986b4cac81SBjoern A. Zeeb }
54996b4cac81SBjoern A. Zeeb 
55006b4cac81SBjoern A. Zeeb void
55016b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
55026b4cac81SBjoern A. Zeeb {
55036b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
55046b4cac81SBjoern A. Zeeb 
55056b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
55066b4cac81SBjoern A. Zeeb 		return;
55076b4cac81SBjoern A. Zeeb 
5508ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
5509ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
5510ac1d519cSBjoern A. Zeeb 
55116b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5512ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
5513ac1d519cSBjoern A. Zeeb 
55146b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
55156b4cac81SBjoern A. Zeeb }
55166b4cac81SBjoern A. Zeeb 
55176b4cac81SBjoern A. Zeeb uint32_t
55186b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
55196b4cac81SBjoern A. Zeeb     enum nl80211_band band)
55206b4cac81SBjoern A. Zeeb {
55216b4cac81SBjoern A. Zeeb 
55226b4cac81SBjoern A. Zeeb 	switch (band) {
55236b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
55246b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
55256b4cac81SBjoern A. Zeeb 		break;
55266b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
55276b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
55286b4cac81SBjoern A. Zeeb 		break;
55296b4cac81SBjoern A. Zeeb 	default:
55306b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
55316b4cac81SBjoern A. Zeeb 		break;
55326b4cac81SBjoern A. Zeeb 	}
55336b4cac81SBjoern A. Zeeb 
55346b4cac81SBjoern A. Zeeb 	return (0);
55356b4cac81SBjoern A. Zeeb }
55366b4cac81SBjoern A. Zeeb 
55376b4cac81SBjoern A. Zeeb uint32_t
55386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
55396b4cac81SBjoern A. Zeeb {
55406b4cac81SBjoern A. Zeeb 
55416b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
55426b4cac81SBjoern A. Zeeb }
55436b4cac81SBjoern A. Zeeb 
5544ac867c20SBjoern A. Zeeb #if 0
55456b4cac81SBjoern A. Zeeb static struct lkpi_sta *
55466b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
55476b4cac81SBjoern A. Zeeb {
55486b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
55496b4cac81SBjoern A. Zeeb 
55506b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
55516b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
55526b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
55536b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
55546b4cac81SBjoern A. Zeeb 			return (lsta);
55556b4cac81SBjoern A. Zeeb 		}
55566b4cac81SBjoern A. Zeeb 	}
55576b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
55586b4cac81SBjoern A. Zeeb 
55596b4cac81SBjoern A. Zeeb 	return (NULL);
55606b4cac81SBjoern A. Zeeb }
5561ac867c20SBjoern A. Zeeb #endif
55626b4cac81SBjoern A. Zeeb 
55636b4cac81SBjoern A. Zeeb struct ieee80211_sta *
55646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
55656b4cac81SBjoern A. Zeeb {
55666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55676b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
55686b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
55696b4cac81SBjoern A. Zeeb 
55706b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
55716b4cac81SBjoern A. Zeeb 
55726b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
55736b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
55746b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
55756b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
55766b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
55776b4cac81SBjoern A. Zeeb 			return (sta);
55786b4cac81SBjoern A. Zeeb 		}
55796b4cac81SBjoern A. Zeeb 	}
55806b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
55816b4cac81SBjoern A. Zeeb 	return (NULL);
55826b4cac81SBjoern A. Zeeb }
55836b4cac81SBjoern A. Zeeb 
55846b4cac81SBjoern A. Zeeb struct ieee80211_sta *
55852e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
55862e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
55876b4cac81SBjoern A. Zeeb {
55886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55906b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
55916b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
55926b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
55936b4cac81SBjoern A. Zeeb 
55946b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
55956b4cac81SBjoern A. Zeeb 	sta = NULL;
55966b4cac81SBjoern A. Zeeb 
55978891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
55986b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
55996b4cac81SBjoern A. Zeeb 
56006b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
56016b4cac81SBjoern A. Zeeb 
56026b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
56036b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
56046b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
56056b4cac81SBjoern A. Zeeb 			continue;
56066b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
56076b4cac81SBjoern A. Zeeb 		if (sta != NULL)
56086b4cac81SBjoern A. Zeeb 			break;
56096b4cac81SBjoern A. Zeeb 	}
56108891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
56116b4cac81SBjoern A. Zeeb 
56126b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
56136b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
56146b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
56156b4cac81SBjoern A. Zeeb 			return (NULL);
56166b4cac81SBjoern A. Zeeb 	}
56176b4cac81SBjoern A. Zeeb 
56186b4cac81SBjoern A. Zeeb 	return (sta);
56196b4cac81SBjoern A. Zeeb }
56206b4cac81SBjoern A. Zeeb 
56216b4cac81SBjoern A. Zeeb struct sk_buff *
56226b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
56236b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
56246b4cac81SBjoern A. Zeeb {
56256b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
56260cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56276b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
56286b4cac81SBjoern A. Zeeb 
56290cbcfa19SBjoern A. Zeeb 	skb = NULL;
56306b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
56316b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
56326b4cac81SBjoern A. Zeeb 
56330cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
56340cbcfa19SBjoern A. Zeeb 		goto stopped;
56350cbcfa19SBjoern A. Zeeb 
56360cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
56370cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
56380cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
56390cbcfa19SBjoern A. Zeeb 		goto stopped;
56400cbcfa19SBjoern A. Zeeb 	}
56410cbcfa19SBjoern A. Zeeb 
5642eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
5643eac3646fSBjoern A. Zeeb 
5644eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
56456b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
5646eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
56476b4cac81SBjoern A. Zeeb 
56480cbcfa19SBjoern A. Zeeb stopped:
56496b4cac81SBjoern A. Zeeb 	return (skb);
56506b4cac81SBjoern A. Zeeb }
56516b4cac81SBjoern A. Zeeb 
56526b4cac81SBjoern A. Zeeb void
56536b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
565486220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
56556b4cac81SBjoern A. Zeeb {
56566b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
56576b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
565886220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
56596b4cac81SBjoern A. Zeeb 
56606b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
56616b4cac81SBjoern A. Zeeb 
56626b4cac81SBjoern A. Zeeb 	fc = bc = 0;
5663eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
56646b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
56656b4cac81SBjoern A. Zeeb 		fc++;
56666b4cac81SBjoern A. Zeeb 		bc += skb->len;
56676b4cac81SBjoern A. Zeeb 	}
5668eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
56696b4cac81SBjoern A. Zeeb 	if (frame_cnt)
56706b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
56716b4cac81SBjoern A. Zeeb 	if (byte_cnt)
56726b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
56736b4cac81SBjoern A. Zeeb 
56746b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
56756b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
56766b4cac81SBjoern A. Zeeb 	IMPROVE();
56776b4cac81SBjoern A. Zeeb }
56786b4cac81SBjoern A. Zeeb 
56796b4cac81SBjoern A. Zeeb /*
56806b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
56816b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
56826b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
56836b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
56846b4cac81SBjoern A. Zeeb  */
5685a8397571SBjoern A. Zeeb static void
5686a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
56876b4cac81SBjoern A. Zeeb     int status)
56886b4cac81SBjoern A. Zeeb {
56896b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
56906b4cac81SBjoern A. Zeeb 	struct mbuf *m;
56916b4cac81SBjoern A. Zeeb 
56926b4cac81SBjoern A. Zeeb 	m = skb->m;
56936b4cac81SBjoern A. Zeeb 	skb->m = NULL;
56946b4cac81SBjoern A. Zeeb 
56956b4cac81SBjoern A. Zeeb 	if (m != NULL) {
56966b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
56976b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
56986b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
56996b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
57006b4cac81SBjoern A. Zeeb 	}
5701a8397571SBjoern A. Zeeb }
57026b4cac81SBjoern A. Zeeb 
5703a8397571SBjoern A. Zeeb void
5704a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5705a8397571SBjoern A. Zeeb     int status)
5706a8397571SBjoern A. Zeeb {
5707a8397571SBjoern A. Zeeb 
5708a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
57096b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
57106b4cac81SBjoern A. Zeeb }
57116b4cac81SBjoern A. Zeeb 
5712383b3e8fSBjoern A. Zeeb void
5713a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5714a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
5715383b3e8fSBjoern A. Zeeb {
5716a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
5717383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
5718383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
5719383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
5720383b3e8fSBjoern A. Zeeb 	int status;
5721383b3e8fSBjoern A. Zeeb 
5722a8397571SBjoern A. Zeeb 	skb = txstat->skb;
5723383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
5724383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
5725383b3e8fSBjoern A. Zeeb 
5726383b3e8fSBjoern A. Zeeb 		m = skb->m;
5727383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
5728383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
5729383b3e8fSBjoern A. Zeeb 	} else {
5730383b3e8fSBjoern A. Zeeb 		ni = NULL;
5731383b3e8fSBjoern A. Zeeb 	}
5732383b3e8fSBjoern A. Zeeb 
5733a8397571SBjoern A. Zeeb 	info = txstat->info;
5734383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5735383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
5736383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5737383b3e8fSBjoern A. Zeeb 	} else {
5738383b3e8fSBjoern A. Zeeb 		status = 1;
5739383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5740383b3e8fSBjoern A. Zeeb 	}
5741383b3e8fSBjoern A. Zeeb 
5742383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
5743e2c5ab09SJohn Baldwin 		int ridx __unused;
5744383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5745383b3e8fSBjoern A. Zeeb 		int old_rate;
5746383b3e8fSBjoern A. Zeeb 
5747383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5748383b3e8fSBjoern A. Zeeb #endif
5749383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
5750383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5751383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
5752383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5753383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5754383b3e8fSBjoern A. Zeeb 		}
5755383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
5756a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5757383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
5758383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5759383b3e8fSBjoern A. Zeeb #endif
5760adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5761383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5762383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5763383b3e8fSBjoern A. Zeeb 		}
5764383b3e8fSBjoern A. Zeeb 
5765383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5766383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
5767383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5768383b3e8fSBjoern A. Zeeb 
5769383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5770383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5771383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5772383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
5773383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5774383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
5775383b3e8fSBjoern A. Zeeb 		}
5776383b3e8fSBjoern A. Zeeb #endif
5777383b3e8fSBjoern A. Zeeb 	}
5778383b3e8fSBjoern A. Zeeb 
5779383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5780383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5781383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5782383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
5783383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5784383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5785adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
5786383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
5787383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
5788383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
5789383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
5790383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
5791383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
5792383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
5793383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
5794383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
5795383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
5796383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
5797383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
5798383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
5799adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
5800383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
5801383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
5802383b3e8fSBjoern A. Zeeb #endif
5803383b3e8fSBjoern A. Zeeb 
5804a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
5805a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5806a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
5807a8397571SBjoern A. Zeeb 	} else {
5808383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5809383b3e8fSBjoern A. Zeeb 	}
5810a8397571SBjoern A. Zeeb }
5811a8397571SBjoern A. Zeeb 
5812a8397571SBjoern A. Zeeb void
5813a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5814a8397571SBjoern A. Zeeb {
5815a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
5816a8397571SBjoern A. Zeeb 
5817a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
5818a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
5819a8397571SBjoern A. Zeeb 	status.skb = skb;
5820a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
5821a8397571SBjoern A. Zeeb 
5822a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
5823a8397571SBjoern A. Zeeb }
5824383b3e8fSBjoern A. Zeeb 
58256b4cac81SBjoern A. Zeeb /*
58266b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
58276b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
58286b4cac81SBjoern A. Zeeb  * mbufs this should go away.
58296b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
58306b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
58316b4cac81SBjoern A. Zeeb  */
58326b4cac81SBjoern A. Zeeb static void
58336b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
58346b4cac81SBjoern A. Zeeb {
58356b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
58366b4cac81SBjoern A. Zeeb 	struct mbuf *m;
58376b4cac81SBjoern A. Zeeb 
58386b4cac81SBjoern A. Zeeb 	if (p == NULL)
58396b4cac81SBjoern A. Zeeb 		return;
58406b4cac81SBjoern A. Zeeb 
58416b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
58426b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
58436b4cac81SBjoern A. Zeeb 
58446b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
58456b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
58466b4cac81SBjoern A. Zeeb 	if (ni != NULL)
58476b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
58486b4cac81SBjoern A. Zeeb 	m_freem(m);
58496b4cac81SBjoern A. Zeeb }
58506b4cac81SBjoern A. Zeeb 
58516b4cac81SBjoern A. Zeeb void
58526b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
58536b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
58546b4cac81SBjoern A. Zeeb {
58556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58566b4cac81SBjoern A. Zeeb 
58576b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
58586b4cac81SBjoern A. Zeeb 	IMPROVE();
58596b4cac81SBjoern A. Zeeb 
58606b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58616b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
58626b4cac81SBjoern A. Zeeb }
58636b4cac81SBjoern A. Zeeb 
58646b4cac81SBjoern A. Zeeb void
58656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
58666b4cac81SBjoern A. Zeeb     struct work_struct *w)
58676b4cac81SBjoern A. Zeeb {
58686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58696b4cac81SBjoern A. Zeeb 
58706b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
58716b4cac81SBjoern A. Zeeb 	IMPROVE();
58726b4cac81SBjoern A. Zeeb 
58736b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58746b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
58756b4cac81SBjoern A. Zeeb }
58766b4cac81SBjoern A. Zeeb 
58776b4cac81SBjoern A. Zeeb struct sk_buff *
5878ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5879ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5880ade774b1SBjoern A. Zeeb {
5881ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
5882ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
5883ade774b1SBjoern A. Zeeb 	uint8_t *p;
5884ade774b1SBjoern A. Zeeb 	size_t len;
5885ade774b1SBjoern A. Zeeb 
5886ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
5887ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
5888ade774b1SBjoern A. Zeeb 
5889ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5890ade774b1SBjoern A. Zeeb 	if (skb == NULL)
5891ade774b1SBjoern A. Zeeb 		return (NULL);
5892ade774b1SBjoern A. Zeeb 
5893ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
5894ade774b1SBjoern A. Zeeb 
5895ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
5896ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5897ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5898ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5899ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5900ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5901ade774b1SBjoern A. Zeeb 
5902ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
5903ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
5904ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
5905ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
5906ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
5907ade774b1SBjoern A. Zeeb 
5908ade774b1SBjoern A. Zeeb 	return (skb);
5909ade774b1SBjoern A. Zeeb }
5910ade774b1SBjoern A. Zeeb 
5911ade774b1SBjoern A. Zeeb struct sk_buff *
59126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
59136b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
59146b4cac81SBjoern A. Zeeb {
59156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59166b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
59176b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
59186b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
59196b4cac81SBjoern A. Zeeb 	uint16_t v;
59206b4cac81SBjoern A. Zeeb 
59216b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
59226b4cac81SBjoern A. Zeeb 	if (skb == NULL)
59236b4cac81SBjoern A. Zeeb 		return (NULL);
59246b4cac81SBjoern A. Zeeb 
59256b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
59266b4cac81SBjoern A. Zeeb 
59276b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
59286b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
59296b4cac81SBjoern A. Zeeb 
59306b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
59316b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
59326b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5933616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
59346b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
59356b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
59366b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
59376b4cac81SBjoern A. Zeeb 
59386b4cac81SBjoern A. Zeeb 	return (skb);
59396b4cac81SBjoern A. Zeeb }
59406b4cac81SBjoern A. Zeeb 
59416b4cac81SBjoern A. Zeeb struct sk_buff *
59426b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5943adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
59446b4cac81SBjoern A. Zeeb {
59456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59466b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
59476b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
59486b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
59496b4cac81SBjoern A. Zeeb 
5950adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
5951adff403fSBjoern A. Zeeb 
59526b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
59536b4cac81SBjoern A. Zeeb 	if (skb == NULL)
59546b4cac81SBjoern A. Zeeb 		return (NULL);
59556b4cac81SBjoern A. Zeeb 
59566b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
59576b4cac81SBjoern A. Zeeb 
59586b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
59596b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
59606b4cac81SBjoern A. Zeeb 
59616b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
59626b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
59636b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
59646b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
59656b4cac81SBjoern A. Zeeb 
59666b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
59676b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
59686b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
59696b4cac81SBjoern A. Zeeb 
59706b4cac81SBjoern A. Zeeb 	return (skb);
59716b4cac81SBjoern A. Zeeb }
59726b4cac81SBjoern A. Zeeb 
59736b4cac81SBjoern A. Zeeb struct wireless_dev *
59746b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
59756b4cac81SBjoern A. Zeeb {
59766b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59776b4cac81SBjoern A. Zeeb 
59786b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
59796b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
59806b4cac81SBjoern A. Zeeb }
59816b4cac81SBjoern A. Zeeb 
59826b4cac81SBjoern A. Zeeb void
59836b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
59846b4cac81SBjoern A. Zeeb {
59856b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59866b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
59876b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
59886b4cac81SBjoern A. Zeeb 	int arg;
59896b4cac81SBjoern A. Zeeb 
59906b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
59916b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
59926b4cac81SBjoern A. Zeeb 
59936b4cac81SBjoern A. Zeeb 	/*
5994f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
59956b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
59966b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
59976b4cac81SBjoern A. Zeeb 	 */
5998f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
5999bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
60006b4cac81SBjoern A. Zeeb 
6001018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6002018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
60036b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
60046b4cac81SBjoern A. Zeeb }
60056b4cac81SBjoern A. Zeeb 
6006bb81db90SBjoern A. Zeeb void
6007bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
6008bb81db90SBjoern A. Zeeb {
6009bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6010bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
6011bb81db90SBjoern A. Zeeb 
6012bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
6013bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
6014bb81db90SBjoern A. Zeeb 
6015bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6016bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
60173540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
6018bb81db90SBjoern A. Zeeb }
6019bb81db90SBjoern A. Zeeb 
60205edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
60215edde07cSBjoern A. Zeeb 
60225a9a0d78SBjoern A. Zeeb void
60235a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
60245a9a0d78SBjoern A. Zeeb {
60255a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
60265a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
60275a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
60285a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
60295a9a0d78SBjoern A. Zeeb 
60305a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
60315a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
60325a9a0d78SBjoern A. Zeeb 
60335a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
60345a9a0d78SBjoern A. Zeeb 
60355a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
60365a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
60375a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
60385a9a0d78SBjoern A. Zeeb 	else
60395a9a0d78SBjoern A. Zeeb 		ac_count = 1;
60405a9a0d78SBjoern A. Zeeb 
60415a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
60425a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
60435a9a0d78SBjoern A. Zeeb 
60445a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
60455a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
60465a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
60475a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
60480d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
60495a9a0d78SBjoern A. Zeeb 				/*
60505a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
60515a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
60525a9a0d78SBjoern A. Zeeb 				 */
60530d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
60540d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
60555a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
60565a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
60575a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
60585a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
60590d2cb6a6SBjoern A. Zeeb #endif
60605a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
60615a9a0d78SBjoern A. Zeeb 			}
60625a9a0d78SBjoern A. Zeeb 		}
60635a9a0d78SBjoern A. Zeeb 	}
60645a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
60655a9a0d78SBjoern A. Zeeb }
60665a9a0d78SBjoern A. Zeeb 
60675a9a0d78SBjoern A. Zeeb void
60685a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
60695a9a0d78SBjoern A. Zeeb {
60705a9a0d78SBjoern A. Zeeb 	int i;
60715a9a0d78SBjoern A. Zeeb 
60725a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
60735a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
60745a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
60755a9a0d78SBjoern A. Zeeb }
60765a9a0d78SBjoern A. Zeeb 
60775a9a0d78SBjoern A. Zeeb 
60785a9a0d78SBjoern A. Zeeb static void
60795a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
60805a9a0d78SBjoern A. Zeeb {
60815a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
60825a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
60835a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
60845a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
60855a9a0d78SBjoern A. Zeeb 
60865a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
60875a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
60885a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
60895a9a0d78SBjoern A. Zeeb 	else
60905a9a0d78SBjoern A. Zeeb 		ac_count = 1;
60915a9a0d78SBjoern A. Zeeb 
60925a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
60935a9a0d78SBjoern A. Zeeb 
60945a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
60955a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
60965a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
60975a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
60985a9a0d78SBjoern A. Zeeb 
60995a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
61005a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
61015a9a0d78SBjoern A. Zeeb 
61025a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
61035a9a0d78SBjoern A. Zeeb 
61045a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
61055a9a0d78SBjoern A. Zeeb 
61060d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
61075a9a0d78SBjoern A. Zeeb 				/*
61085a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
61095a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
61105a9a0d78SBjoern A. Zeeb 				 */
61110d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
61120d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
61135a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
61145a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
61155a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
61165a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
61170d2cb6a6SBjoern A. Zeeb #endif
61185a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
61195a9a0d78SBjoern A. Zeeb 
61205a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
61215a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
61225a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
61235a9a0d78SBjoern A. Zeeb 
61245a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
61255a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
61265a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
61275a9a0d78SBjoern A. Zeeb 
61285a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
61295a9a0d78SBjoern A. Zeeb 							continue;
61305a9a0d78SBjoern A. Zeeb 
61315a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
61325a9a0d78SBjoern A. Zeeb 							continue;
61335a9a0d78SBjoern A. Zeeb 
61345a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
61355a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
61365a9a0d78SBjoern A. Zeeb 							continue;
61375a9a0d78SBjoern A. Zeeb 
61385a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
61395a9a0d78SBjoern A. Zeeb 
61405a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
61415a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
61425a9a0d78SBjoern A. Zeeb 					}
61435a9a0d78SBjoern A. Zeeb 				}
61445a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
61455a9a0d78SBjoern A. Zeeb 			}
61465a9a0d78SBjoern A. Zeeb 		}
61475a9a0d78SBjoern A. Zeeb 	}
61485a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
61495a9a0d78SBjoern A. Zeeb }
61505a9a0d78SBjoern A. Zeeb 
61515a9a0d78SBjoern A. Zeeb void
61525a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
61535a9a0d78SBjoern A. Zeeb {
61545a9a0d78SBjoern A. Zeeb 	int i;
61555a9a0d78SBjoern A. Zeeb 
61565a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
61575a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
61585a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
61595a9a0d78SBjoern A. Zeeb }
61605a9a0d78SBjoern A. Zeeb 
61615a9a0d78SBjoern A. Zeeb void
61625a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
61635a9a0d78SBjoern A. Zeeb {
61645a9a0d78SBjoern A. Zeeb 
61655a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
61665a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
61675a9a0d78SBjoern A. Zeeb 
61685a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
61695a9a0d78SBjoern A. Zeeb }
61705a9a0d78SBjoern A. Zeeb 
61715a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
61725a9a0d78SBjoern A. Zeeb void
61735a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
61745a9a0d78SBjoern A. Zeeb {
61755a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61765a9a0d78SBjoern A. Zeeb 
61775a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61785a9a0d78SBjoern A. Zeeb 
61795a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
61805a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
61815a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
61825a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
61835a9a0d78SBjoern A. Zeeb }
61845a9a0d78SBjoern A. Zeeb 
61855a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
61865a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
61875a9a0d78SBjoern A. Zeeb {
61885a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61895a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
61905a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
61915a9a0d78SBjoern A. Zeeb 
61925a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61935a9a0d78SBjoern A. Zeeb 	txq = NULL;
61945a9a0d78SBjoern A. Zeeb 
61955a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
61965a9a0d78SBjoern A. Zeeb 
61975a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
61985a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
61995a9a0d78SBjoern A. Zeeb 		goto out;
62005a9a0d78SBjoern A. Zeeb 
62015a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
62025a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
62035a9a0d78SBjoern A. Zeeb 		goto out;
62045a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
62055a9a0d78SBjoern A. Zeeb 		goto out;
62065a9a0d78SBjoern A. Zeeb 
62075a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
62085a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
62095a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
62105a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
62115a9a0d78SBjoern A. Zeeb 
62125a9a0d78SBjoern A. Zeeb out:
62135a9a0d78SBjoern A. Zeeb 	return (txq);
62145a9a0d78SBjoern A. Zeeb }
62155a9a0d78SBjoern A. Zeeb 
62165a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
62175a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
62185a9a0d78SBjoern A. Zeeb {
62195a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62205a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
6221eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
62225a9a0d78SBjoern A. Zeeb 
62235a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
62245a9a0d78SBjoern A. Zeeb 
62255a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
62265a9a0d78SBjoern A. Zeeb 
62275a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
6228eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
6229eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
6230eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
6231eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
62325a9a0d78SBjoern A. Zeeb 		goto out;
62335a9a0d78SBjoern A. Zeeb 
623441b746e0SAustin Shafer 	/*
623541b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
623641b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
623741b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
623841b746e0SAustin Shafer 	 */
623941b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
62405a9a0d78SBjoern A. Zeeb 		goto out;
62415a9a0d78SBjoern A. Zeeb 
62425a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62435a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
62445a9a0d78SBjoern A. Zeeb out:
62455a9a0d78SBjoern A. Zeeb 	return;
62465a9a0d78SBjoern A. Zeeb }
62475a9a0d78SBjoern A. Zeeb 
6248eac3646fSBjoern A. Zeeb void
6249eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
6250eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
6251eac3646fSBjoern A. Zeeb {
6252eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6253eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
6254eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
6255eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
6256eac3646fSBjoern A. Zeeb 
6257eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6258eac3646fSBjoern A. Zeeb 
6259eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
6260eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
6261eac3646fSBjoern A. Zeeb 	do {
6262eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
6263eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
6264eac3646fSBjoern A. Zeeb 			break;
6265eac3646fSBjoern A. Zeeb 
6266eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
6267eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
6268eac3646fSBjoern A. Zeeb 		do {
6269eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6270eac3646fSBjoern A. Zeeb 			if (skb == NULL)
6271eac3646fSBjoern A. Zeeb 				break;
6272eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
6273eac3646fSBjoern A. Zeeb 		} while(1);
6274eac3646fSBjoern A. Zeeb 
6275eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
6276eac3646fSBjoern A. Zeeb 	} while (1);
6277eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
6278eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6279eac3646fSBjoern A. Zeeb }
6280eac3646fSBjoern A. Zeeb 
62815a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
62825a9a0d78SBjoern A. Zeeb 
62835edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
62845edde07cSBjoern A. Zeeb 	u_int refcnt;
62855edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
62865edde07cSBjoern A. Zeeb };
62875edde07cSBjoern A. Zeeb 
62885edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
62895edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
62905edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
62915edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
62925edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
62935edde07cSBjoern A. Zeeb 	size_t ssid_len;
62945edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
62955edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
62965edde07cSBjoern A. Zeeb 
62975edde07cSBjoern A. Zeeb 	/*
62985edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
62995edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
63005edde07cSBjoern A. Zeeb 	 */
63015edde07cSBjoern A. Zeeb 	bool match;
63025edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
63035edde07cSBjoern A. Zeeb };
63045edde07cSBjoern A. Zeeb 
63055edde07cSBjoern A. Zeeb static void
63065edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
63075edde07cSBjoern A. Zeeb {
63085edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
63095edde07cSBjoern A. Zeeb 	size_t ielen;
63105edde07cSBjoern A. Zeeb 
63115edde07cSBjoern A. Zeeb 	lookup = arg;
63125edde07cSBjoern A. Zeeb 
63135edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
63145edde07cSBjoern A. Zeeb 	if (lookup->match)
63155edde07cSBjoern A. Zeeb 		return;
63165edde07cSBjoern A. Zeeb 
63175edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
63185edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
63195edde07cSBjoern A. Zeeb 		return;
63205edde07cSBjoern A. Zeeb 
63215edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
63225edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
63235edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
63245edde07cSBjoern A. Zeeb 		return;
63255edde07cSBjoern A. Zeeb 	}
63265edde07cSBjoern A. Zeeb 
63275edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
63285edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
63295edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
63305edde07cSBjoern A. Zeeb 		return;
63315edde07cSBjoern A. Zeeb 	}
63325edde07cSBjoern A. Zeeb 
63335edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
63345edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
63355edde07cSBjoern A. Zeeb 
63365edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
63375edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
63385edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
63395edde07cSBjoern A. Zeeb 			return;
63405edde07cSBjoern A. Zeeb 	}
63415edde07cSBjoern A. Zeeb 
63425edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
63435edde07cSBjoern A. Zeeb 		return;
63445edde07cSBjoern A. Zeeb 
63455edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
63465edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
63475edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
63485edde07cSBjoern A. Zeeb 			return;
63495edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
63505edde07cSBjoern A. Zeeb 			return;
63515edde07cSBjoern A. Zeeb 	}
63525edde07cSBjoern A. Zeeb 
63535edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
63545edde07cSBjoern A. Zeeb 
63555edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
63565edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
63575edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
63585edde07cSBjoern A. Zeeb 		return;
63595edde07cSBjoern A. Zeeb 
63605edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
63615edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
63625edde07cSBjoern A. Zeeb 	if (ielen)
63635edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
63645edde07cSBjoern A. Zeeb 
63655edde07cSBjoern A. Zeeb 	lookup->match = true;
63665edde07cSBjoern A. Zeeb }
63675edde07cSBjoern A. Zeeb 
63685edde07cSBjoern A. Zeeb struct cfg80211_bss *
63695edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
63705edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
63715edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
63725edde07cSBjoern A. Zeeb {
63735edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
63745edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
63755edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
63765edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
63775edde07cSBjoern A. Zeeb 
63785edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
63795edde07cSBjoern A. Zeeb 
63805edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
63815edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
63825edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
63835edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
63845edde07cSBjoern A. Zeeb 		return (NULL);
63855edde07cSBjoern A. Zeeb 	}
63865edde07cSBjoern A. Zeeb 
63875edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
63885edde07cSBjoern A. Zeeb 	lookup.chan = chan;
63895edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
63905edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
63915edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
63925edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
63935edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
63945edde07cSBjoern A. Zeeb 	lookup.match = false;
63955edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
63965edde07cSBjoern A. Zeeb 
63975edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
63985edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
63995edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
64005edde07cSBjoern A. Zeeb 	if (!lookup.match) {
64015edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
64025edde07cSBjoern A. Zeeb 		return (NULL);
64035edde07cSBjoern A. Zeeb 	}
64045edde07cSBjoern A. Zeeb 
64055edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
64065edde07cSBjoern A. Zeeb 	return (&lbss->bss);
64075edde07cSBjoern A. Zeeb }
64085edde07cSBjoern A. Zeeb 
64095edde07cSBjoern A. Zeeb void
64105edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
64115edde07cSBjoern A. Zeeb {
64125edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
64135edde07cSBjoern A. Zeeb 
64145edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
64155edde07cSBjoern A. Zeeb 
64165edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
64175edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
64185edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
64195edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
64205edde07cSBjoern A. Zeeb 	}
64215edde07cSBjoern A. Zeeb }
64225edde07cSBjoern A. Zeeb 
64235edde07cSBjoern A. Zeeb void
64245edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
64255edde07cSBjoern A. Zeeb {
64265edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
64275edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
64285edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
64295edde07cSBjoern A. Zeeb 
64305edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
64315edde07cSBjoern A. Zeeb 	ic = lhw->ic;
64325edde07cSBjoern A. Zeeb 
64335edde07cSBjoern A. Zeeb 	/*
64345edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
64355edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
64365edde07cSBjoern A. Zeeb 	 */
64375edde07cSBjoern A. Zeeb 	if (ic == NULL ||
64385edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
64395edde07cSBjoern A. Zeeb 		return;
64405edde07cSBjoern A. Zeeb 
64415edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
64425edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
64435edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
64445edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
64455edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
64465edde07cSBjoern A. Zeeb }
64475edde07cSBjoern A. Zeeb 
64485edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
64495edde07cSBjoern A. Zeeb 
6450ac1d519cSBjoern A. Zeeb /*
6451ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
6452ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
6453ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
6454ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
6455ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
6456ac1d519cSBjoern A. Zeeb  */
6457ac1d519cSBjoern A. Zeeb 
6458ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
6459ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
6460ac1d519cSBjoern A. Zeeb {
6461ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
6462ac1d519cSBjoern A. Zeeb 	uint32_t changed;
6463ac1d519cSBjoern A. Zeeb 	int error;
6464ac1d519cSBjoern A. Zeeb 
6465ac1d519cSBjoern A. Zeeb 	changed = 0;
6466ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
6467ac1d519cSBjoern A. Zeeb 		cd = NULL;
6468ac1d519cSBjoern A. Zeeb 	else
6469ac1d519cSBjoern A. Zeeb 		cd = &new->def;
6470ac1d519cSBjoern A. Zeeb 
6471ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
6472ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
6473ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
6474ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
6475ac1d519cSBjoern A. Zeeb 	}
6476ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
6477ac1d519cSBjoern A. Zeeb 
6478ac1d519cSBjoern A. Zeeb 	if (changed == 0)
6479ac1d519cSBjoern A. Zeeb 		return (0);
6480ac1d519cSBjoern A. Zeeb 
6481ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
6482ac1d519cSBjoern A. Zeeb 	return (error);
6483ac1d519cSBjoern A. Zeeb }
6484ac1d519cSBjoern A. Zeeb 
6485ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6486ac1d519cSBjoern A. Zeeb 
64876b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
64886b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
64896b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6490