xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 105b9df26ee0286f3a5a7d191075e068dee1c4a2)
16b4cac81SBjoern A. Zeeb /*-
28ac540d3SBjoern A. Zeeb  * Copyright (c) 2020-2023 The FreeBSD Foundation
3086be6a8SBjoern A. Zeeb  * Copyright (c) 2020-2022 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
426b4cac81SBjoern A. Zeeb #include <sys/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
1486b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
149759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1506b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1514a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1524a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1534a67f1dfSBjoern A. Zeeb #endif
1546b4cac81SBjoern A. Zeeb 
1559fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
1569fb91463SBjoern A. Zeeb static void
1579fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
1589fb91463SBjoern A. Zeeb {
1599fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
1609fb91463SBjoern A. Zeeb 	uint8_t *ie;
1619fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
1629fb91463SBjoern A. Zeeb 	int i, rx_nss;
1639fb91463SBjoern A. Zeeb 
1649fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
1659fb91463SBjoern A. Zeeb 		return;
1669fb91463SBjoern A. Zeeb 
1679fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
1689fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1699fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
1709fb91463SBjoern A. Zeeb 
1719fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
1729fb91463SBjoern A. Zeeb 
1739fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
1749fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
1759fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1769fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
1779fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
1789fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1799fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
1809fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
1819fb91463SBjoern A. Zeeb 
1829fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
1839fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
1849fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1859fb91463SBjoern A. Zeeb 		ie += 4;
1869fb91463SBjoern A. Zeeb 	ie += 2;
1879fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
1889fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
1899fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
1909fb91463SBjoern A. Zeeb 
1919fb91463SBjoern A. Zeeb 	rx_nss = 0;
1929fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
1939fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
1949fb91463SBjoern A. Zeeb 			rx_nss++;
1959fb91463SBjoern A. Zeeb 	}
1969fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
1979fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
1989fb91463SBjoern A. Zeeb 
1999fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
2009fb91463SBjoern A. Zeeb }
2019fb91463SBjoern A. Zeeb #endif
2029fb91463SBjoern A. Zeeb 
2039fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
2049fb91463SBjoern A. Zeeb static void
2059fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
2069fb91463SBjoern A. Zeeb {
2079fb91463SBjoern A. Zeeb 
2089fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
2099fb91463SBjoern A. Zeeb 		return;
2109fb91463SBjoern A. Zeeb 
2119fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
2129fb91463SBjoern A. Zeeb #ifdef __notyet__
2139fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
2149fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
2159fb91463SBjoern A. Zeeb 		} else
2169fb91463SBjoern A. Zeeb #endif
2179fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2189fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
2199fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2209fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
2219fb91463SBjoern A. Zeeb 	}
2229fb91463SBjoern A. Zeeb 
2239fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
2249fb91463SBjoern A. Zeeb 	return;
2259fb91463SBjoern A. Zeeb }
2269fb91463SBjoern A. Zeeb #endif
2279fb91463SBjoern A. Zeeb 
228d9f59799SBjoern A. Zeeb static void
22967674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
23067674c1cSBjoern A. Zeeb     const char *_f, int _l)
231d9f59799SBjoern A. Zeeb {
232d9f59799SBjoern A. Zeeb 
2339d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2349d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
235d9f59799SBjoern A. Zeeb 		return;
236d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
237d9f59799SBjoern A. Zeeb 		return;
238d9f59799SBjoern A. Zeeb 
239d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
24067674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
24167674c1cSBjoern A. Zeeb 	if (ni != NULL)
24267674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
243d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
244d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
245d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
2469d9ba2b7SBjoern A. Zeeb #endif
247d9f59799SBjoern A. Zeeb }
248d9f59799SBjoern A. Zeeb 
249d9f59799SBjoern A. Zeeb static void
250d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
251d9f59799SBjoern A. Zeeb {
252d9f59799SBjoern A. Zeeb 
253d9f59799SBjoern A. Zeeb 
254d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2550936c648SBjoern A. Zeeb 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
2560936c648SBjoern A. Zeeb 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
2570936c648SBjoern A. Zeeb 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
258d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
259d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
260d9f59799SBjoern A. Zeeb }
261d9f59799SBjoern A. Zeeb 
2624f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
2634f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
2644f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
2654f61ef8bSBjoern A. Zeeb {
2664f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
2674f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
2684f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
2694f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
270b6b352e4SBjoern A. Zeeb 	int band, i, tid;
2719fb91463SBjoern A. Zeeb 	int ht_rx_nss;
2729fb91463SBjoern A. Zeeb 	int vht_rx_nss;
2734f61ef8bSBjoern A. Zeeb 
2744f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2754f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
2764f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2774f61ef8bSBjoern A. Zeeb 		return (NULL);
2784f61ef8bSBjoern A. Zeeb 
2794f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2804f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2814f61ef8bSBjoern A. Zeeb 	/*
2820936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
2830936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
2840936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
2850936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
2860936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
2870936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
2880936c648SBjoern A. Zeeb 	 * two separate allocations.
2894f61ef8bSBjoern A. Zeeb 	 */
2900936c648SBjoern A. Zeeb 	lsta->ni = ni;
2914f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2924f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2934f61ef8bSBjoern A. Zeeb 
2944f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2954f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2964f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2974f61ef8bSBjoern A. Zeeb 
2984f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
299b6b352e4SBjoern A. Zeeb 
300b6b352e4SBjoern A. Zeeb 	/* TXQ */
3014f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
3024f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
3034f61ef8bSBjoern A. Zeeb 
304d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
3054f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
3064f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
3074f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
3084f61ef8bSBjoern A. Zeeb 			goto cleanup;
3094f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
3104f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
311d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
312d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
313d0d29110SBjoern A. Zeeb 				continue;
314d0d29110SBjoern A. Zeeb 			}
315d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
3164f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
3174f61ef8bSBjoern A. Zeeb 		} else {
318fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
3194f61ef8bSBjoern A. Zeeb 		}
320d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
3210cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
322d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
3234f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
3244f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
3250cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
326d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
327eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
3284f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
3294f61ef8bSBjoern A. Zeeb 	}
3304f61ef8bSBjoern A. Zeeb 
331b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
332b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
333b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
334b6b352e4SBjoern A. Zeeb 
335b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
336b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
337b6b352e4SBjoern A. Zeeb 			continue;
338b6b352e4SBjoern A. Zeeb 
339b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
340b6b352e4SBjoern A. Zeeb 
341b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
342b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
343b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
344b6b352e4SBjoern A. Zeeb 		}
345b6b352e4SBjoern A. Zeeb 	}
3469fb91463SBjoern A. Zeeb 
3476ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
3489fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
3499fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = 0;
3509fb91463SBjoern A. Zeeb 
3519fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
3529fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3539fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
3549fb91463SBjoern A. Zeeb #endif
3559fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
3569fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
3579fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
3589fb91463SBjoern A. Zeeb #endif
3599fb91463SBjoern A. Zeeb 
3609fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
3619fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
3629fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
363b6b352e4SBjoern A. Zeeb 
3646ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
3656ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
3666ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
3676ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
3686ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
3696ffb7bd4SBjoern A. Zeeb 	}
3706ffb7bd4SBjoern A. Zeeb 
3714f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
372fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
3734f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
3744f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
3750936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
3764f61ef8bSBjoern A. Zeeb 
3774f61ef8bSBjoern A. Zeeb 	return (lsta);
3784f61ef8bSBjoern A. Zeeb 
3794f61ef8bSBjoern A. Zeeb cleanup:
380eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
381eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
382eac3646fSBjoern A. Zeeb 
383eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
384eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
3854f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
386eac3646fSBjoern A. Zeeb 	}
3874f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
3884f61ef8bSBjoern A. Zeeb 	return (NULL);
3894f61ef8bSBjoern A. Zeeb }
3904f61ef8bSBjoern A. Zeeb 
3910936c648SBjoern A. Zeeb static void
3920936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
3930936c648SBjoern A. Zeeb {
3940936c648SBjoern A. Zeeb 	struct mbuf *m;
3950936c648SBjoern A. Zeeb 
3960936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
3970936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
3980936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
3990936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
4000936c648SBjoern A. Zeeb 
4010936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
4020936c648SBjoern A. Zeeb 	IMPROVE();
4030936c648SBjoern A. Zeeb 
404fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
405fa4e4257SBjoern A. Zeeb 
406fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4070936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
408fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4090936c648SBjoern A. Zeeb 
4100936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
4110936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
4120936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
4130936c648SBjoern A. Zeeb 
4140936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4150936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
4160936c648SBjoern A. Zeeb 	while (m != NULL) {
4170936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
4180936c648SBjoern A. Zeeb 
4190936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
4200936c648SBjoern A. Zeeb 		if (nim != NULL)
4210936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
4220936c648SBjoern A. Zeeb 		m_freem(m);
4230936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
4240936c648SBjoern A. Zeeb 	}
4250936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
4260936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
427fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
4280936c648SBjoern A. Zeeb 
4290936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
4300936c648SBjoern A. Zeeb 
4310936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
4320936c648SBjoern A. Zeeb 
4330936c648SBjoern A. Zeeb 	/* Free lsta. */
4340936c648SBjoern A. Zeeb 	lsta->ni = NULL;
4350936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
4360936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
4370936c648SBjoern A. Zeeb }
4380936c648SBjoern A. Zeeb 
4390936c648SBjoern A. Zeeb 
4406b4cac81SBjoern A. Zeeb static enum nl80211_band
4416b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
4426b4cac81SBjoern A. Zeeb {
4436b4cac81SBjoern A. Zeeb 
4446b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
4456b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
4466b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
4476b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
4486b4cac81SBjoern A. Zeeb #ifdef __notyet__
4496b4cac81SBjoern A. Zeeb 	else if ()
4506b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
4516b4cac81SBjoern A. Zeeb 	else if ()
4526b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
4536b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
4546b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
4556b4cac81SBjoern A. Zeeb #endif
4566b4cac81SBjoern A. Zeeb 	else
4576b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
4586b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
4596b4cac81SBjoern A. Zeeb }
4606b4cac81SBjoern A. Zeeb 
4616b4cac81SBjoern A. Zeeb static uint32_t
4626b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
4636b4cac81SBjoern A. Zeeb {
4646b4cac81SBjoern A. Zeeb 
4656b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
4666b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
4676b4cac81SBjoern A. Zeeb 	switch (band) {
4686b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
4696b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
4706b4cac81SBjoern A. Zeeb 		break;
4716b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
4726b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
4736b4cac81SBjoern A. Zeeb 		break;
4746b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
4756b4cac81SBjoern A. Zeeb 		break;
4766b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
4776b4cac81SBjoern A. Zeeb 		break;
4786b4cac81SBjoern A. Zeeb 	default:
4796b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
4806b4cac81SBjoern A. Zeeb 		break;
4816b4cac81SBjoern A. Zeeb 	}
4826b4cac81SBjoern A. Zeeb 
4836b4cac81SBjoern A. Zeeb 	IMPROVE();
4846b4cac81SBjoern A. Zeeb 	return (0x00);
4856b4cac81SBjoern A. Zeeb }
4866b4cac81SBjoern A. Zeeb 
487e3a0b120SBjoern A. Zeeb #if 0
4886b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
4896b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
4906b4cac81SBjoern A. Zeeb {
4916b4cac81SBjoern A. Zeeb 
4926b4cac81SBjoern A. Zeeb 	switch (ac) {
4936b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
4946b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
4956b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
4966b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
4976b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
4986b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
4996b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
5006b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
5016b4cac81SBjoern A. Zeeb 	default:
5026b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
5036b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5046b4cac81SBjoern A. Zeeb 	}
5056b4cac81SBjoern A. Zeeb }
506e3a0b120SBjoern A. Zeeb #endif
5076b4cac81SBjoern A. Zeeb 
5086b4cac81SBjoern A. Zeeb static enum nl80211_iftype
5096b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
5106b4cac81SBjoern A. Zeeb {
5116b4cac81SBjoern A. Zeeb 
5126b4cac81SBjoern A. Zeeb 	switch (opmode) {
5136b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
5146b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
5156b4cac81SBjoern A. Zeeb 		break;
5166b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
5176b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
5186b4cac81SBjoern A. Zeeb 		break;
5196b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
5206b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
5216b4cac81SBjoern A. Zeeb 		break;
5226b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
5236b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
5246b4cac81SBjoern A. Zeeb 		break;
5256b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
5266b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
5276b4cac81SBjoern A. Zeeb 		break;
5286b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
5296b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
5306b4cac81SBjoern A. Zeeb 		break;
5316b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
5326b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5336b4cac81SBjoern A. Zeeb 	default:
5346b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
5356b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5366b4cac81SBjoern A. Zeeb 	}
5376b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
5386b4cac81SBjoern A. Zeeb }
5396b4cac81SBjoern A. Zeeb 
540b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
5416b4cac81SBjoern A. Zeeb static uint32_t
5426b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
5436b4cac81SBjoern A. Zeeb {
5446b4cac81SBjoern A. Zeeb 
5456b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
5466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
5476b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5486b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5496b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
5506b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
551906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
5526b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
5536b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5546b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
5556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
5566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
5576b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
5586b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5596b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5606b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5616b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
5626b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5636b4cac81SBjoern A. Zeeb 		break;
5646b4cac81SBjoern A. Zeeb 	default:
5656b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
5666b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5676b4cac81SBjoern A. Zeeb 	}
5686b4cac81SBjoern A. Zeeb 
5696b4cac81SBjoern A. Zeeb 	return (0);
5706b4cac81SBjoern A. Zeeb }
5716b4cac81SBjoern A. Zeeb 
5726b4cac81SBjoern A. Zeeb static uint32_t
5736b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
5746b4cac81SBjoern A. Zeeb {
5756b4cac81SBjoern A. Zeeb 
5766b4cac81SBjoern A. Zeeb 	switch (cipher) {
5776b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
5786b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
5796b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
5806b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
5816b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
5826b4cac81SBjoern A. Zeeb 		if (keylen < 8)
5836b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
5846b4cac81SBjoern A. Zeeb 		else
5856b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
5866b4cac81SBjoern A. Zeeb 		break;
5876b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
5886b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
5896b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
5906b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
5916b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
5926b4cac81SBjoern A. Zeeb 		break;
5936b4cac81SBjoern A. Zeeb 	default:
5946b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
5956b4cac81SBjoern A. Zeeb 	};
5966b4cac81SBjoern A. Zeeb 	return (0);
5976b4cac81SBjoern A. Zeeb }
5986b4cac81SBjoern A. Zeeb #endif
5996b4cac81SBjoern A. Zeeb 
6006b4cac81SBjoern A. Zeeb #ifdef __notyet__
6016b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
6026b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
6036b4cac81SBjoern A. Zeeb {
6046b4cac81SBjoern A. Zeeb 
6056b4cac81SBjoern A. Zeeb 	/*
6066b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
6076b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
6086b4cac81SBjoern A. Zeeb 	 */
6096b4cac81SBjoern A. Zeeb 	switch (state) {
6106b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
6116b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
6126b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
6136b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
6146b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
6156b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
6166b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
6176b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
6186b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
6196b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
6206b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
6216b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
6226b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
6236b4cac81SBjoern A. Zeeb 	default:
6246b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
6256b4cac81SBjoern A. Zeeb 	};
6266b4cac81SBjoern A. Zeeb 
6276b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
6286b4cac81SBjoern A. Zeeb }
6296b4cac81SBjoern A. Zeeb #endif
6306b4cac81SBjoern A. Zeeb 
6316b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6326b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
6336b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
6346b4cac81SBjoern A. Zeeb {
6356b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6366b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
6376b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
6386b4cac81SBjoern A. Zeeb 	int i, nchans;
6396b4cac81SBjoern A. Zeeb 
6406b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6416b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
6426b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
6436b4cac81SBjoern A. Zeeb 		return (NULL);
6446b4cac81SBjoern A. Zeeb 
6456b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
6466b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
6476b4cac81SBjoern A. Zeeb 		return (NULL);
6486b4cac81SBjoern A. Zeeb 
6496b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
6506b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
6516b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
6526b4cac81SBjoern A. Zeeb 			return (&channels[i]);
6536b4cac81SBjoern A. Zeeb 	}
6546b4cac81SBjoern A. Zeeb 
6556b4cac81SBjoern A. Zeeb 	return (NULL);
6566b4cac81SBjoern A. Zeeb }
6576b4cac81SBjoern A. Zeeb 
6582ac8a218SBjoern A. Zeeb #if 0
6596b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6606b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
6616b4cac81SBjoern A. Zeeb {
6626b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6636b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
6646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6656b4cac81SBjoern A. Zeeb 
6666b4cac81SBjoern A. Zeeb 	chan = NULL;
6676b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
6686b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
6696b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
6706b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
6716b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
6726b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
6736b4cac81SBjoern A. Zeeb 	else
6746b4cac81SBjoern A. Zeeb 		c = NULL;
6756b4cac81SBjoern A. Zeeb 
6766b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6776b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
6786b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
6796b4cac81SBjoern A. Zeeb 	}
6806b4cac81SBjoern A. Zeeb 
6816b4cac81SBjoern A. Zeeb 	return (chan);
6826b4cac81SBjoern A. Zeeb }
6832ac8a218SBjoern A. Zeeb #endif
6846b4cac81SBjoern A. Zeeb 
6852e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
6862e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
6872e183d99SBjoern A. Zeeb {
6882e183d99SBjoern A. Zeeb 	enum nl80211_band band;
6892e183d99SBjoern A. Zeeb 
6902e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6912e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6922e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6932e183d99SBjoern A. Zeeb 		int i;
6942e183d99SBjoern A. Zeeb 
6952e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
6962e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6972e183d99SBjoern A. Zeeb 			continue;
6982e183d99SBjoern A. Zeeb 
6992e183d99SBjoern A. Zeeb 		channels = supband->channels;
7002e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
7012e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
7022e183d99SBjoern A. Zeeb 				return (&channels[i]);
7032e183d99SBjoern A. Zeeb 		}
7042e183d99SBjoern A. Zeeb 	}
7052e183d99SBjoern A. Zeeb 
7062e183d99SBjoern A. Zeeb 	return (NULL);
7072e183d99SBjoern A. Zeeb }
7082e183d99SBjoern A. Zeeb 
709b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
7106b4cac81SBjoern A. Zeeb static int
7116b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
7126b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
7136b4cac81SBjoern A. Zeeb {
7146b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
7156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7166b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7176b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7186b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
7196b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
7206b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
7216b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
7226b4cac81SBjoern A. Zeeb 	int error;
7236b4cac81SBjoern A. Zeeb 
7246b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
7256b4cac81SBjoern A. Zeeb 
7266b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
7276b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
7286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7296b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7306b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7316b4cac81SBjoern A. Zeeb 
7326b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
7336b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
7346b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
7356b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
7366b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
7376b4cac81SBjoern A. Zeeb #if 0
7386b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
7396b4cac81SBjoern A. Zeeb #endif
7406b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
7416b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
7426b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
7436b4cac81SBjoern A. Zeeb 
7446b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
7456b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
7466b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
7476b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
7486b4cac81SBjoern A. Zeeb 		break;
7496b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7506b4cac81SBjoern A. Zeeb 	default:
7516b4cac81SBjoern A. Zeeb 		IMPROVE();
7526b4cac81SBjoern A. Zeeb 		return (0);
7536b4cac81SBjoern A. Zeeb 	};
7546b4cac81SBjoern A. Zeeb 
7556b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
7566b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
7576b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
7586b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
7596b4cac81SBjoern A. Zeeb 
7606b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
7616b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
7626b4cac81SBjoern A. Zeeb 	}
7636b4cac81SBjoern A. Zeeb 
7646b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
7656b4cac81SBjoern A. Zeeb 	if (error != 0) {
7666b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
7676b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
7686b4cac81SBjoern A. Zeeb 		return (0);
7696b4cac81SBjoern A. Zeeb 	} else {
7706b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
7716b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
7726b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
7736b4cac81SBjoern A. Zeeb 		return (1);
7746b4cac81SBjoern A. Zeeb 	}
7756b4cac81SBjoern A. Zeeb }
7766b4cac81SBjoern A. Zeeb 
7776b4cac81SBjoern A. Zeeb static int
7786b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
7796b4cac81SBjoern A. Zeeb {
7806b4cac81SBjoern A. Zeeb 
7816b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
7826b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
7836b4cac81SBjoern A. Zeeb }
7846b4cac81SBjoern A. Zeeb static  int
7856b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
7866b4cac81SBjoern A. Zeeb {
7876b4cac81SBjoern A. Zeeb 
7886b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
7896b4cac81SBjoern A. Zeeb }
7906b4cac81SBjoern A. Zeeb #endif
7916b4cac81SBjoern A. Zeeb 
7926b4cac81SBjoern A. Zeeb static u_int
7936b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
7946b4cac81SBjoern A. Zeeb {
7956b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
7966b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7976b4cac81SBjoern A. Zeeb 
7986b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
7996b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
8006b4cac81SBjoern A. Zeeb 
8016b4cac81SBjoern A. Zeeb 	mc_list = arg;
8026b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
8036b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
8046b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
8056b4cac81SBjoern A. Zeeb 			return (0);
8066b4cac81SBjoern A. Zeeb 	}
8076b4cac81SBjoern A. Zeeb 
8086b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
8096b4cac81SBjoern A. Zeeb 	if (addr == NULL)
8106b4cac81SBjoern A. Zeeb 		return (0);
8116b4cac81SBjoern A. Zeeb 
8126b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
8136b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
8146b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
8156b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
8166b4cac81SBjoern A. Zeeb 	mc_list->count++;
8176b4cac81SBjoern A. Zeeb 
8189d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8199d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8206b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
8216b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
8229d9ba2b7SBjoern A. Zeeb #endif
8236b4cac81SBjoern A. Zeeb 
8246b4cac81SBjoern A. Zeeb 	return (1);
8256b4cac81SBjoern A. Zeeb }
8266b4cac81SBjoern A. Zeeb 
8276b4cac81SBjoern A. Zeeb static void
8286b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
8296b4cac81SBjoern A. Zeeb {
8306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8326b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
8336b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
8346b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
8356b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
8366b4cac81SBjoern A. Zeeb 	u64 mc;
8376b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
8386b4cac81SBjoern A. Zeeb 
8396b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
8406b4cac81SBjoern A. Zeeb 
8416b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
8426b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
8436b4cac81SBjoern A. Zeeb 		return;
8446b4cac81SBjoern A. Zeeb 
8456b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
8466b4cac81SBjoern A. Zeeb 		return;
8476b4cac81SBjoern A. Zeeb 
8486b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
8496b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
8506b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
8516b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
8526b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8536b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
8546b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
8556b4cac81SBjoern A. Zeeb 	} else {
8566b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
8576b4cac81SBjoern A. Zeeb 	}
8586b4cac81SBjoern A. Zeeb 
8596b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8606b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
8616b4cac81SBjoern A. Zeeb 	/*
8626b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
8636b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
8646b4cac81SBjoern A. Zeeb 	 */
8656b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
8666b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
8676b4cac81SBjoern A. Zeeb 
8689d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8699d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8706b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
8716b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
8729d9ba2b7SBjoern A. Zeeb #endif
8736b4cac81SBjoern A. Zeeb 
8746b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
8756b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
8766b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
8776b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
8786b4cac81SBjoern A. Zeeb 			mc_list.count--;
8796b4cac81SBjoern A. Zeeb 		}
8806b4cac81SBjoern A. Zeeb 	}
8816b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
8826b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
8836b4cac81SBjoern A. Zeeb }
8846b4cac81SBjoern A. Zeeb 
885fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
886fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
887fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
888fa8f007dSBjoern A. Zeeb {
889fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
890fa8f007dSBjoern A. Zeeb 
891fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
892fa8f007dSBjoern A. Zeeb 
8939d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
895fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
896fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
897fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
898fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
899616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
900fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
901fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
902fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
903fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
904fa8f007dSBjoern A. Zeeb 			bss_changed);
9059d9ba2b7SBjoern A. Zeeb #endif
906fa8f007dSBjoern A. Zeeb 
907fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
908fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
909fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
910fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
911fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
912fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
913fa8f007dSBjoern A. Zeeb 	}
914fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
915fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
916fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
917fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
918fa8f007dSBjoern A. Zeeb 	}
919fa8f007dSBjoern A. Zeeb 
920fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
921fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
922fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
923fa8f007dSBjoern A. Zeeb 
9249d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
9259d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
926fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
927fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
928fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
929fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
930616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
931fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
932fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
933fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
934fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
935fa8f007dSBjoern A. Zeeb 			bss_changed);
9369d9ba2b7SBjoern A. Zeeb #endif
937fa8f007dSBjoern A. Zeeb 
938fa8f007dSBjoern A. Zeeb 	return (bss_changed);
939fa8f007dSBjoern A. Zeeb }
940fa8f007dSBjoern A. Zeeb 
9416b4cac81SBjoern A. Zeeb static void
9426b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
9436b4cac81SBjoern A. Zeeb {
9446b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9456b4cac81SBjoern A. Zeeb 	int error;
9468ac540d3SBjoern A. Zeeb 	bool cancel;
9476b4cac81SBjoern A. Zeeb 
9488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9498ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9518ac540d3SBjoern A. Zeeb 	if (!cancel)
9526b4cac81SBjoern A. Zeeb 		return;
9536b4cac81SBjoern A. Zeeb 
9546b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9556b4cac81SBjoern A. Zeeb 
956bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
957bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
9586b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
9596b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
9608ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
9616b4cac81SBjoern A. Zeeb 
9626b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
9638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9648ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
9658ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
9668ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9688ac540d3SBjoern A. Zeeb 
969bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
9706b4cac81SBjoern A. Zeeb 
9718ac540d3SBjoern A. Zeeb 	if (cancel)
9726b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
9736b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
9746b4cac81SBjoern A. Zeeb }
9756b4cac81SBjoern A. Zeeb 
9766b4cac81SBjoern A. Zeeb static void
977086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
978086be6a8SBjoern A. Zeeb {
979086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
980086be6a8SBjoern A. Zeeb 	int error;
981086be6a8SBjoern A. Zeeb 	bool old;
982086be6a8SBjoern A. Zeeb 
983086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
984086be6a8SBjoern A. Zeeb 	if (old == new)
985086be6a8SBjoern A. Zeeb 		return;
986086be6a8SBjoern A. Zeeb 
987086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
988086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
989086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
990086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
991086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
992086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
993086be6a8SBjoern A. Zeeb 	}
994086be6a8SBjoern A. Zeeb }
995086be6a8SBjoern A. Zeeb 
9965a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
9976b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9986b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
9996b4cac81SBjoern A. Zeeb {
10005a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
10015a4d2461SBjoern A. Zeeb 
10025a4d2461SBjoern A. Zeeb 	changed = 0;
10036b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1004616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
10056b4cac81SBjoern A. Zeeb 
10066b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
10076b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
10086b4cac81SBjoern A. Zeeb 
1009616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1010616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
10116b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
10126b4cac81SBjoern A. Zeeb 		IMPROVE();
1013086be6a8SBjoern A. Zeeb 
10145a4d2461SBjoern A. Zeeb 		/*
10155a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
10165a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
10175a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
10185a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
10195a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
10205a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
10215a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
10225a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
10235a4d2461SBjoern A. Zeeb 		 */
10246b4cac81SBjoern A. Zeeb 	}
10255a4d2461SBjoern A. Zeeb 
10265a4d2461SBjoern A. Zeeb 	return (changed);
10276b4cac81SBjoern A. Zeeb }
10286b4cac81SBjoern A. Zeeb 
10296b4cac81SBjoern A. Zeeb static void
10306b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
10316b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
10326b4cac81SBjoern A. Zeeb {
10336b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
10346b4cac81SBjoern A. Zeeb 	int tid;
1035eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
10366b4cac81SBjoern A. Zeeb 
10376b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
10386b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
10396b4cac81SBjoern A. Zeeb 
10406b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
10416b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
10426b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
10436b4cac81SBjoern A. Zeeb 				continue;
10446b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
10456b4cac81SBjoern A. Zeeb 			continue;
10466b4cac81SBjoern A. Zeeb 
10476b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
10486b4cac81SBjoern A. Zeeb 			continue;
10496b4cac81SBjoern A. Zeeb 
10506b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10516b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
10526b4cac81SBjoern A. Zeeb 			continue;
10536b4cac81SBjoern A. Zeeb 
1054eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1055eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1056eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1057eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
10586b4cac81SBjoern A. Zeeb 			continue;
10596b4cac81SBjoern A. Zeeb 
10606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
10616b4cac81SBjoern A. Zeeb 	}
10626b4cac81SBjoern A. Zeeb }
10636b4cac81SBjoern A. Zeeb 
10646b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
10656b4cac81SBjoern A. Zeeb 
10666b4cac81SBjoern A. Zeeb static int
10676b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10686b4cac81SBjoern A. Zeeb {
10696b4cac81SBjoern A. Zeeb 
10706b4cac81SBjoern A. Zeeb 	return (0);
10716b4cac81SBjoern A. Zeeb }
10726b4cac81SBjoern A. Zeeb 
10736b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
10746b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
10756b4cac81SBjoern A. Zeeb 
10766b4cac81SBjoern A. Zeeb static int
10776b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10786b4cac81SBjoern A. Zeeb {
10796b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1080c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1081d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
10826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10846b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10856b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10866b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10876b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10886b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
10896b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10906b4cac81SBjoern A. Zeeb 	uint32_t changed;
10916b4cac81SBjoern A. Zeeb 	int error;
10926b4cac81SBjoern A. Zeeb 
10932ac8a218SBjoern A. Zeeb 	/*
10942ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
10952ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
10962ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
10972ac8a218SBjoern A. Zeeb 	 */
10982ac8a218SBjoern A. Zeeb 
10992ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
11002ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
11012ac8a218SBjoern A. Zeeb 		return (EINVAL);
11022ac8a218SBjoern A. Zeeb 	}
11030936c648SBjoern A. Zeeb 	/*
11040936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
11050936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
11060936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
11070936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
11080936c648SBjoern A. Zeeb 	 */
11092ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11102ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
11112ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
11122ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
11130936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11142ac8a218SBjoern A. Zeeb 		return (EINVAL);
11156b4cac81SBjoern A. Zeeb 	}
11166b4cac81SBjoern A. Zeeb 
11176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11182ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
11192ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
11202ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
11212ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
11220936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11232ac8a218SBjoern A. Zeeb 		return (ESRCH);
11242ac8a218SBjoern A. Zeeb 	}
11252ac8a218SBjoern A. Zeeb 
11266b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11276b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11286b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11296b4cac81SBjoern A. Zeeb 
11300936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
11310936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
11320936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
11330936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
11340936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
11350936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
11360936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
11370936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
11380936c648SBjoern A. Zeeb 		return (EBUSY);
11390936c648SBjoern A. Zeeb 	}
11400936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
11410936c648SBjoern A. Zeeb 
11426b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
11446b4cac81SBjoern A. Zeeb 
11456b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
11466b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1147d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1148d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
11496b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
11506b4cac81SBjoern A. Zeeb 	} else {
11516b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1152c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
11536b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1154d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
11556b4cac81SBjoern A. Zeeb 	}
11566b4cac81SBjoern A. Zeeb 
1157d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1158d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1159d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
11606b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1161d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1162d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1163d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1164d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
11659fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
11662ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
11672ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
11689fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
11699fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
11709fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1171d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
11729fb91463SBjoern A. Zeeb 		} else
1173d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
11749fb91463SBjoern A. Zeeb 	}
11759fb91463SBjoern A. Zeeb #endif
11769fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
11779fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
11789fb91463SBjoern A. Zeeb #ifdef __notyet__
11799fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1180d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1181d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
11829fb91463SBjoern A. Zeeb 		} else
11839fb91463SBjoern A. Zeeb #endif
11849fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1185d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
11869fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1187d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
11889fb91463SBjoern A. Zeeb 	}
11899fb91463SBjoern A. Zeeb #endif
11906b4cac81SBjoern A. Zeeb 	/* Responder ... */
1191d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1192d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1193d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1194d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
11959fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
11966b4cac81SBjoern A. Zeeb 
11976ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
11986ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
11996ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
12006ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
12016ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
12026ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
12036ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
12046ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
12056ffb7bd4SBjoern A. Zeeb 
12066ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
12076ffb7bd4SBjoern A. Zeeb 
12086ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
12096ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
12106ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
12116ffb7bd4SBjoern A. Zeeb 
12126ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
12136ffb7bd4SBjoern A. Zeeb 
12146b4cac81SBjoern A. Zeeb 	error = 0;
12156b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
12166b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
12176b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
12186b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
12196b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1220d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
12216b4cac81SBjoern A. Zeeb 	} else {
1222d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
12236b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
1224d1af434dSBjoern A. Zeeb 			vif->bss_conf.chandef.chan = chanctx_conf->def.chan;
1225d1af434dSBjoern A. Zeeb 			vif->bss_conf.chandef.width = chanctx_conf->def.width;
12266b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
1227d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
12289fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
12299fb91463SBjoern A. Zeeb 			if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) {
12309fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
12319fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
12329fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 += 10;
12339fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
12349fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 -= 10;
12359fb91463SBjoern A. Zeeb 			}
12369fb91463SBjoern A. Zeeb #endif
12376b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
1238d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
12396b4cac81SBjoern A. Zeeb 		} else {
1240018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1241018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
12426b4cac81SBjoern A. Zeeb 			goto out;
12436b4cac81SBjoern A. Zeeb 		}
12446ffb7bd4SBjoern A. Zeeb 
1245d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
12466ffb7bd4SBjoern A. Zeeb 
12476b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
12486b4cac81SBjoern A. Zeeb 		if (error == 0)
124968541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1250d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
12516b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
12526b4cac81SBjoern A. Zeeb 			error = 0;
12536b4cac81SBjoern A. Zeeb 		if (error != 0) {
1254018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1255018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1256d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1257d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1258c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
12596b4cac81SBjoern A. Zeeb 			goto out;
12606b4cac81SBjoern A. Zeeb 		}
12616b4cac81SBjoern A. Zeeb 	}
12626b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
12636b4cac81SBjoern A. Zeeb 
12646b4cac81SBjoern A. Zeeb 	/* RATES */
12656b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
12666b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
12676b4cac81SBjoern A. Zeeb 
1268d9f59799SBjoern A. Zeeb 	/*
12690936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
12700936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
12710936c648SBjoern A. Zeeb 	 * under the hoods.
1272d9f59799SBjoern A. Zeeb 	 */
12730936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
12740936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
12756b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1276e24e8103SBjoern A. Zeeb 
1277e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
12782ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1279e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1280e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1281e24e8103SBjoern A. Zeeb 
1282d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
12836b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12846b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
12856b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1286e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
12876b4cac81SBjoern A. Zeeb 	if (error != 0) {
12886b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1289018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1290018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
12916b4cac81SBjoern A. Zeeb 		goto out;
12926b4cac81SBjoern A. Zeeb 	}
12936b4cac81SBjoern A. Zeeb #if 0
12946b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
12956b4cac81SBjoern A. Zeeb #endif
12966b4cac81SBjoern A. Zeeb 
12979d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
12989d9ba2b7SBjoern A. Zeeb 
12991665ef97SBjoern A. Zeeb #if 0
13006b4cac81SBjoern A. Zeeb 	/*
13016b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
13026b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
13036b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
13046b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1305d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1306d9f59799SBjoern A. Zeeb 	 * for all queues.
13076b4cac81SBjoern A. Zeeb 	 */
1308e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
13091665ef97SBjoern A. Zeeb #endif
13106b4cac81SBjoern A. Zeeb 
13116b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
13126b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13136b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
13146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
13156b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
13166b4cac81SBjoern A. Zeeb 
13176b4cac81SBjoern A. Zeeb 	/*
13186b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
13196b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
13206b4cac81SBjoern A. Zeeb 	 * - event_callback
13216b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
13226b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
13236b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
13246b4cac81SBjoern A. Zeeb 	 */
13256b4cac81SBjoern A. Zeeb 
1326*105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1327*105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1328*105b9df2SBjoern A. Zeeb 
1329*105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1330*105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1331*105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1332*105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1333*105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1334*105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1335*105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1336*105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1337*105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1338*105b9df2SBjoern A. Zeeb 
1339*105b9df2SBjoern A. Zeeb 	/*
1340*105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1341*105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1342*105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1343*105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1344*105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1345*105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1346*105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1347*105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1348*105b9df2SBjoern A. Zeeb 	 */
1349*105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1350*105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1351*105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1352*105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1353*105b9df2SBjoern A. Zeeb 	} else {
1354*105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1355*105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1356*105b9df2SBjoern A. Zeeb 		/*
1357*105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1358*105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1359*105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1360*105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1361*105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1362*105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1363*105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1364*105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1365*105b9df2SBjoern A. Zeeb 		 */
1366*105b9df2SBjoern A. Zeeb 	}
1367*105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1368*105b9df2SBjoern A. Zeeb 	goto out_relocked;
1369*105b9df2SBjoern A. Zeeb 
13706b4cac81SBjoern A. Zeeb out:
13718ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
13726b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1373*105b9df2SBjoern A. Zeeb out_relocked:
13740936c648SBjoern A. Zeeb 	/*
1375*105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
13760936c648SBjoern A. Zeeb 	 * during the work of this function.
13770936c648SBjoern A. Zeeb 	 */
13786b4cac81SBjoern A. Zeeb 	if (ni != NULL)
13796b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
13806b4cac81SBjoern A. Zeeb 	return (error);
13816b4cac81SBjoern A. Zeeb }
13826b4cac81SBjoern A. Zeeb 
13836b4cac81SBjoern A. Zeeb static int
13846b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13856b4cac81SBjoern A. Zeeb {
13866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13876b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13886b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13896b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13906b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13916b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13926b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13936b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
13946b4cac81SBjoern A. Zeeb 	int error;
13956b4cac81SBjoern A. Zeeb 
13966b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13986b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13996b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14006b4cac81SBjoern A. Zeeb 
14012ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
14022ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14032ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
14042ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
14052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
14062ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
14072ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
14082ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
14092ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
14102ac8a218SBjoern A. Zeeb #endif
14112ac8a218SBjoern A. Zeeb 
14122ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
14132ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
14142ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
14152ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
14162ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
14172ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
14186b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
14196b4cac81SBjoern A. Zeeb 
142067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
14216b4cac81SBjoern A. Zeeb 
14226b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14246b4cac81SBjoern A. Zeeb 
1425d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1426d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1427d9f59799SBjoern A. Zeeb 
14286b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
14296b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
14306b4cac81SBjoern A. Zeeb 
14316b4cac81SBjoern A. Zeeb 	/* flush, no drop */
14326b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
14336b4cac81SBjoern A. Zeeb 
14346b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
14356b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
14366b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14376b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
14386b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
14396b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
14406b4cac81SBjoern A. Zeeb 	}
14416b4cac81SBjoern A. Zeeb 
14426b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
14436b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
14446b4cac81SBjoern A. Zeeb 
14456b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
14466b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1447d9f59799SBjoern A. Zeeb 
1448d9f59799SBjoern A. Zeeb 	/* Take the station down. */
14496b4cac81SBjoern A. Zeeb 
14506b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
14516b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
14526b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1453d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1454e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
14556b4cac81SBjoern A. Zeeb 	if (error != 0) {
14566b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1457018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1458018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
14596b4cac81SBjoern A. Zeeb 		goto out;
14606b4cac81SBjoern A. Zeeb 	}
14616b4cac81SBjoern A. Zeeb #if 0
14626b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
14636b4cac81SBjoern A. Zeeb #endif
14646b4cac81SBjoern A. Zeeb 
146567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
14666b4cac81SBjoern A. Zeeb 
14672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
14682ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
14692ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
14702ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
14712ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1472d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
14730936c648SBjoern A. Zeeb 	/*
14740936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
14750936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
14760936c648SBjoern A. Zeeb 	 * and potentially freed.
14770936c648SBjoern A. Zeeb 	 */
14780936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1479d9f59799SBjoern A. Zeeb 
1480d9f59799SBjoern A. Zeeb 	/* conf_tx */
1481d9f59799SBjoern A. Zeeb 
1482d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
14836b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1484c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1485d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
14866b4cac81SBjoern A. Zeeb 
1487d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
14886b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
148968541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
14906b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
14916b4cac81SBjoern A. Zeeb 
14925a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
14935a4d2461SBjoern A. Zeeb 
14946b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
1495d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1496d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1497c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
14986b4cac81SBjoern A. Zeeb 	}
14996b4cac81SBjoern A. Zeeb 
15006b4cac81SBjoern A. Zeeb out:
15018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15026b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15036b4cac81SBjoern A. Zeeb 	return (error);
15046b4cac81SBjoern A. Zeeb }
15056b4cac81SBjoern A. Zeeb 
15066b4cac81SBjoern A. Zeeb static int
15076b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15086b4cac81SBjoern A. Zeeb {
15096b4cac81SBjoern A. Zeeb 	int error;
15106b4cac81SBjoern A. Zeeb 
15116b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
15126b4cac81SBjoern A. Zeeb 	if (error == 0)
15136b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
15146b4cac81SBjoern A. Zeeb 	return (error);
15156b4cac81SBjoern A. Zeeb }
15166b4cac81SBjoern A. Zeeb 
15176b4cac81SBjoern A. Zeeb static int
15186b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15196b4cac81SBjoern A. Zeeb {
15206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15246b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15256b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
15266b4cac81SBjoern A. Zeeb 	int error;
15276b4cac81SBjoern A. Zeeb 
15286b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15326b4cac81SBjoern A. Zeeb 
15336b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
15348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15352ac8a218SBjoern A. Zeeb 
15362ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15372ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
15382ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
15392ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15402ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
15412ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
15422ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
15432ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
15442ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
15452ac8a218SBjoern A. Zeeb #endif
15462ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
15472ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
15482ac8a218SBjoern A. Zeeb 		goto out;
15492ac8a218SBjoern A. Zeeb 	}
15502ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
15512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
15522ac8a218SBjoern A. Zeeb 
15532ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
15546b4cac81SBjoern A. Zeeb 
15556b4cac81SBjoern A. Zeeb 	/* Finish auth. */
15566b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
15576b4cac81SBjoern A. Zeeb 
15586b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
15596b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
15606b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1561e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1562018d93ecSBjoern A. Zeeb 	if (error != 0) {
1563018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1564018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
15656b4cac81SBjoern A. Zeeb 		goto out;
1566018d93ecSBjoern A. Zeeb 	}
15676b4cac81SBjoern A. Zeeb 
15686b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
15696b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
15706b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15716b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
15726b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
15736b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
15746b4cac81SBjoern A. Zeeb 	}
15756b4cac81SBjoern A. Zeeb 
15766b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
15776b4cac81SBjoern A. Zeeb 
15786b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
15796b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
15806b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15816b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
15826b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
15836b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
15846b4cac81SBjoern A. Zeeb 	}
15856b4cac81SBjoern A. Zeeb 
15866b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
1587e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), true, true);
15886b4cac81SBjoern A. Zeeb 
15896b4cac81SBjoern A. Zeeb 	/*
15906b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
15916b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
15926b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
15936b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
15946b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
15956b4cac81SBjoern A. Zeeb 	 * - event_callback
15966b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
15976b4cac81SBjoern A. Zeeb 	 */
15986b4cac81SBjoern A. Zeeb 
15996b4cac81SBjoern A. Zeeb out:
16008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16026b4cac81SBjoern A. Zeeb 	return (error);
16036b4cac81SBjoern A. Zeeb }
16046b4cac81SBjoern A. Zeeb 
16056b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
16066b4cac81SBjoern A. Zeeb static int
16076b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16086b4cac81SBjoern A. Zeeb {
16096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16116b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16126b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16136b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16146b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16152ac8a218SBjoern A. Zeeb 	int error;
16166b4cac81SBjoern A. Zeeb 
16176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16216b4cac81SBjoern A. Zeeb 
16226b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16242ac8a218SBjoern A. Zeeb 
16252ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16262ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
16272ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
16282ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16292ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16302ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16312ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16322ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16332ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16342ac8a218SBjoern A. Zeeb #endif
16352ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16362ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16372ac8a218SBjoern A. Zeeb 		goto out;
16382ac8a218SBjoern A. Zeeb 	}
16392ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16402ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16412ac8a218SBjoern A. Zeeb 
16422ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
16432ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
16446b4cac81SBjoern A. Zeeb 
16456b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
16466b4cac81SBjoern A. Zeeb 
16476b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16486b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16496b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16506b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
16516b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16526b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16536b4cac81SBjoern A. Zeeb 	}
16546b4cac81SBjoern A. Zeeb 
16556b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
16566b4cac81SBjoern A. Zeeb 
16576b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
16586b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
16596b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16606b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
16616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
16626b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
16636b4cac81SBjoern A. Zeeb 	}
16646b4cac81SBjoern A. Zeeb 
16652ac8a218SBjoern A. Zeeb 	error = 0;
16662ac8a218SBjoern A. Zeeb out:
16678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16686b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16696b4cac81SBjoern A. Zeeb 
16702ac8a218SBjoern A. Zeeb 	return (error);
16716b4cac81SBjoern A. Zeeb }
16726b4cac81SBjoern A. Zeeb 
16736b4cac81SBjoern A. Zeeb static int
1674d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16756b4cac81SBjoern A. Zeeb {
16766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16806b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16826b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
16836b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1684d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
16856b4cac81SBjoern A. Zeeb 	int error;
16866b4cac81SBjoern A. Zeeb 
16876b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16896b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16906b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16916b4cac81SBjoern A. Zeeb 
16922ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16932ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16942ac8a218SBjoern A. Zeeb 
16952ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16962ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16972ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
16982ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
16992ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17002ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17012ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17022ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17032ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
17042ac8a218SBjoern A. Zeeb #endif
17052ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
17062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17072ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
17082ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
17092ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
17102ac8a218SBjoern A. Zeeb 
17112ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
17126b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
17136b4cac81SBjoern A. Zeeb 
171467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1715d9f59799SBjoern A. Zeeb 
1716d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1717d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1718d9f59799SBjoern A. Zeeb 
1719d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1720d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1721d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1722d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1723d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1724d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1725d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1726d9f59799SBjoern A. Zeeb 	}
1727d9f59799SBjoern A. Zeeb 
17288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1729d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1730d9f59799SBjoern A. Zeeb 
1731d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1732d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1733018d93ecSBjoern A. Zeeb 	if (error != 0) {
1734018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1735018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1736d9f59799SBjoern A. Zeeb 		goto outni;
1737018d93ecSBjoern A. Zeeb 	}
1738d9f59799SBjoern A. Zeeb 
1739d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17408ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1741d9f59799SBjoern A. Zeeb 
174267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1743d9f59799SBjoern A. Zeeb 
1744d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1745d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1746d9f59799SBjoern A. Zeeb 
1747d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1748d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1749d9f59799SBjoern A. Zeeb 
17506b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
17516b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
17526b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17536b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
17546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
17556b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
17566b4cac81SBjoern A. Zeeb 	}
17576b4cac81SBjoern A. Zeeb 
1758d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1759d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1760d9f59799SBjoern A. Zeeb 
1761d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1762d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1763d9f59799SBjoern A. Zeeb 
1764d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1765d9f59799SBjoern A. Zeeb 
17666b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
17676b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
17686b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
17696b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1770e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1771018d93ecSBjoern A. Zeeb 	if (error != 0) {
1772018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1773018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
17746b4cac81SBjoern A. Zeeb 		goto out;
1775018d93ecSBjoern A. Zeeb 	}
17766b4cac81SBjoern A. Zeeb 
17775a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
17785a4d2461SBjoern A. Zeeb 	bss_changed = 0;
17795a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
17806b4cac81SBjoern A. Zeeb 
17815a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
178216e688b2SBjoern A. Zeeb 
1783d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1784d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1785d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1786d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1787e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1788d9f59799SBjoern A. Zeeb 	if (error != 0) {
1789d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1790018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1791018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
1792d9f59799SBjoern A. Zeeb 		goto out;
1793d9f59799SBjoern A. Zeeb 	}
1794d9f59799SBjoern A. Zeeb 
179516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1796d9f59799SBjoern A. Zeeb 
1797d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1798d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1799d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1800616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
1801616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1802d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1803d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1804d9f59799SBjoern A. Zeeb 
18052ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18062ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
18072ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
18082ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
18092ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1810d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
18110936c648SBjoern A. Zeeb 	/*
18120936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
18130936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
18140936c648SBjoern A. Zeeb 	 * and potentially freed.
18150936c648SBjoern A. Zeeb 	 */
18160936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1817d9f59799SBjoern A. Zeeb 
1818d9f59799SBjoern A. Zeeb 	/* conf_tx */
1819d9f59799SBjoern A. Zeeb 
1820d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1821d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1822c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1823d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
1824d9f59799SBjoern A. Zeeb 
1825d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1826d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
182768541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1828d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1829d9f59799SBjoern A. Zeeb 
18305a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
18315a4d2461SBjoern A. Zeeb 
1832d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1833d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1834d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1835c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
1836d9f59799SBjoern A. Zeeb 	}
1837d9f59799SBjoern A. Zeeb 
1838d9f59799SBjoern A. Zeeb 	error = EALREADY;
18396b4cac81SBjoern A. Zeeb out:
18408ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
18416b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1842d9f59799SBjoern A. Zeeb outni:
18436b4cac81SBjoern A. Zeeb 	return (error);
18446b4cac81SBjoern A. Zeeb }
18456b4cac81SBjoern A. Zeeb 
18466b4cac81SBjoern A. Zeeb static int
1847d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1848d9f59799SBjoern A. Zeeb {
1849d9f59799SBjoern A. Zeeb 	int error;
1850d9f59799SBjoern A. Zeeb 
1851d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1852d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1853d9f59799SBjoern A. Zeeb 		return (error);
1854d9f59799SBjoern A. Zeeb 
1855d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1856d9f59799SBjoern A. Zeeb 
1857d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1858d9f59799SBjoern A. Zeeb 	return (error);
1859d9f59799SBjoern A. Zeeb }
1860d9f59799SBjoern A. Zeeb 
1861d9f59799SBjoern A. Zeeb static int
18626b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18636b4cac81SBjoern A. Zeeb {
18646b4cac81SBjoern A. Zeeb 	int error;
18656b4cac81SBjoern A. Zeeb 
1866d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
18676b4cac81SBjoern A. Zeeb 	return (error);
18686b4cac81SBjoern A. Zeeb }
18696b4cac81SBjoern A. Zeeb 
18706b4cac81SBjoern A. Zeeb static int
18716b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18726b4cac81SBjoern A. Zeeb {
18736b4cac81SBjoern A. Zeeb 	int error;
18746b4cac81SBjoern A. Zeeb 
1875d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
18766b4cac81SBjoern A. Zeeb 	return (error);
18776b4cac81SBjoern A. Zeeb }
18786b4cac81SBjoern A. Zeeb 
18796b4cac81SBjoern A. Zeeb static int
18806b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18816b4cac81SBjoern A. Zeeb {
18826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18846b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18856b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18866b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
18876b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
18886b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
18896b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
18906b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
18916b4cac81SBjoern A. Zeeb 	int error;
18926b4cac81SBjoern A. Zeeb 
18936b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
18946b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18956b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18966b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
18976b4cac81SBjoern A. Zeeb 
18986b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
18998ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19002ac8a218SBjoern A. Zeeb 
19012ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19022ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19032ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
19042ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19062ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19072ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19082ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19092ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19102ac8a218SBjoern A. Zeeb #endif
19112ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
19122ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
19132ac8a218SBjoern A. Zeeb 		goto out;
19142ac8a218SBjoern A. Zeeb 	}
19152ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19162ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19172ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19182ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19192ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19202ac8a218SBjoern A. Zeeb 
19212ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
19226b4cac81SBjoern A. Zeeb 
19236b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
19246b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
19256b4cac81SBjoern A. Zeeb 
19269597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
19279597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
19289597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
19299597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
19306b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19319597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
19324a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19334a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
19344a67f1dfSBjoern A. Zeeb 		sta->wme = true;
19354a67f1dfSBjoern A. Zeeb #endif
1936e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1937018d93ecSBjoern A. Zeeb 	if (error != 0) {
1938018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1939018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19409597f7cbSBjoern A. Zeeb 		goto out;
1941018d93ecSBjoern A. Zeeb 	}
19426b4cac81SBjoern A. Zeeb 
19436b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
19446b4cac81SBjoern A. Zeeb 
19456b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
19466b4cac81SBjoern A. Zeeb 	bss_changed = 0;
19474a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19484a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
19494a67f1dfSBjoern A. Zeeb #endif
1950616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
1951616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
1952616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
19536b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
19546b4cac81SBjoern A. Zeeb 	}
19556b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
1956616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
1957616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
19586b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
19596b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
19606b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
19616b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
19626b4cac81SBjoern A. Zeeb 	}
19636b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
19646b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
19656b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
19666b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
19676b4cac81SBjoern A. Zeeb 	}
19686b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
19696b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
19706b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
19716b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
19726b4cac81SBjoern A. Zeeb 	}
1973fa8f007dSBjoern A. Zeeb 
1974fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1975fa8f007dSBjoern A. Zeeb 
19766b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
19776b4cac81SBjoern A. Zeeb 
19786b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
19796b4cac81SBjoern A. Zeeb 	 * - event_callback
19806b4cac81SBjoern A. Zeeb 	 */
19816b4cac81SBjoern A. Zeeb 
19826b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
19836b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
19846b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19856b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
19866b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
19876b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
19886b4cac81SBjoern A. Zeeb 	}
19896b4cac81SBjoern A. Zeeb 
1990086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1991086be6a8SBjoern A. Zeeb 
19926b4cac81SBjoern A. Zeeb 	/*
19936b4cac81SBjoern A. Zeeb 	 * And then:
19946b4cac81SBjoern A. Zeeb 	 * - (more packets)?
19956b4cac81SBjoern A. Zeeb 	 * - set_key
19966b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
19976b4cac81SBjoern A. Zeeb 	 * - set_key (?)
19986b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
19996b4cac81SBjoern A. Zeeb 	 */
20006b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
20016b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
20026b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
20036b4cac81SBjoern A. Zeeb 
20046b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
20056b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
20066b4cac81SBjoern A. Zeeb 	}
20076b4cac81SBjoern A. Zeeb 
20089fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
20099fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
20109fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
20119fb91463SBjoern A. Zeeb #endif
20129fb91463SBjoern A. Zeeb 
20136b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
20146b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20156b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
20166b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2017e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
20186b4cac81SBjoern A. Zeeb 	if (error != 0) {
20196b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2020018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2021018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20226b4cac81SBjoern A. Zeeb 		goto out;
20236b4cac81SBjoern A. Zeeb 	}
20246b4cac81SBjoern A. Zeeb 
20256b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
20266b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
20276b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
20286b4cac81SBjoern A. Zeeb 	 *
20296b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
20306b4cac81SBjoern A. Zeeb 	 */
20316b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
20326b4cac81SBjoern A. Zeeb 
2033fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2034fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2035fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2036fa8f007dSBjoern A. Zeeb 
20376b4cac81SBjoern A. Zeeb out:
20388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20396b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
20406b4cac81SBjoern A. Zeeb 	return (error);
20416b4cac81SBjoern A. Zeeb }
20426b4cac81SBjoern A. Zeeb 
20436b4cac81SBjoern A. Zeeb static int
20446b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20456b4cac81SBjoern A. Zeeb {
20466b4cac81SBjoern A. Zeeb 	int error;
20476b4cac81SBjoern A. Zeeb 
20486b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
20496b4cac81SBjoern A. Zeeb 	if (error == 0)
20506b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
20516b4cac81SBjoern A. Zeeb 	return (error);
20526b4cac81SBjoern A. Zeeb }
20536b4cac81SBjoern A. Zeeb 
20546b4cac81SBjoern A. Zeeb static int
20556b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20566b4cac81SBjoern A. Zeeb {
20576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20586b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20606b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20616b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20626b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20636b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2064d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2065d9f59799SBjoern A. Zeeb #if 0
2066d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2067d9f59799SBjoern A. Zeeb #endif
20686b4cac81SBjoern A. Zeeb 	int error;
20696b4cac81SBjoern A. Zeeb 
20706b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20716b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20726b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20736b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20746b4cac81SBjoern A. Zeeb 
20752ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20762ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20772ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
20782ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
20792ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20802ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20812ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20822ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20832ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20842ac8a218SBjoern A. Zeeb #endif
20852ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20862ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20872ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
20882ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
20892ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
20902ac8a218SBjoern A. Zeeb 
20912ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
20926b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
20936b4cac81SBjoern A. Zeeb 
209467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2095d9f59799SBjoern A. Zeeb 
2096d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2098d9f59799SBjoern A. Zeeb 
2099d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2100d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2101d9f59799SBjoern A. Zeeb 
2102d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2103d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2104d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2105d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2106d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2107d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2108d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2109d9f59799SBjoern A. Zeeb 	}
2110d9f59799SBjoern A. Zeeb 
21118ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2112d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2113d9f59799SBjoern A. Zeeb 
2114d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2115d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2116018d93ecSBjoern A. Zeeb 	if (error != 0) {
2117018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2118018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2119d9f59799SBjoern A. Zeeb 		goto outni;
2120018d93ecSBjoern A. Zeeb 	}
2121d9f59799SBjoern A. Zeeb 
2122d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2124d9f59799SBjoern A. Zeeb 
212567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2126d9f59799SBjoern A. Zeeb 
2127d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2128d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2129d9f59799SBjoern A. Zeeb 
2130d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2131d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2132d9f59799SBjoern A. Zeeb 
2133d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2134d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2135d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2136d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2137d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2138d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2139d9f59799SBjoern A. Zeeb 	}
2140d9f59799SBjoern A. Zeeb 
2141d9f59799SBjoern A. Zeeb #if 0
2142d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2143d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2144d9f59799SBjoern A. Zeeb 
2145d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2146d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2147d9f59799SBjoern A. Zeeb #endif
2148d9f59799SBjoern A. Zeeb 
2149d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2150d9f59799SBjoern A. Zeeb 
21516b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
21526b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21536b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
21546b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2155e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2156018d93ecSBjoern A. Zeeb 	if (error != 0) {
2157018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2158018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21596b4cac81SBjoern A. Zeeb 		goto out;
2160018d93ecSBjoern A. Zeeb 	}
21616b4cac81SBjoern A. Zeeb 
216267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21636b4cac81SBjoern A. Zeeb 
21646b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
21656b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21666b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
21676b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2168e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2169018d93ecSBjoern A. Zeeb 	if (error != 0) {
2170018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2171018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21726b4cac81SBjoern A. Zeeb 		goto out;
2173018d93ecSBjoern A. Zeeb 	}
21746b4cac81SBjoern A. Zeeb 
217567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21766b4cac81SBjoern A. Zeeb 
2177d9f59799SBjoern A. Zeeb #if 0
2178d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2179d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2180d9f59799SBjoern A. Zeeb #endif
2181d9f59799SBjoern A. Zeeb 
2182d9f59799SBjoern A. Zeeb 	error = EALREADY;
21836b4cac81SBjoern A. Zeeb out:
21848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21856b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2186d9f59799SBjoern A. Zeeb outni:
21876b4cac81SBjoern A. Zeeb 	return (error);
21886b4cac81SBjoern A. Zeeb }
21896b4cac81SBjoern A. Zeeb 
21906b4cac81SBjoern A. Zeeb static int
2191d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2192d9f59799SBjoern A. Zeeb {
2193d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2194d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2195d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2196d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2197d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2198d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2199d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2200d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2201d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2202d9f59799SBjoern A. Zeeb 	int error;
2203d9f59799SBjoern A. Zeeb 
2204d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2205d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2206d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2207d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2208d9f59799SBjoern A. Zeeb 
22092ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22102ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22112ac8a218SBjoern A. Zeeb 
22122ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22132ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22142ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22152ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22162ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22172ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22182ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22192ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22202ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22212ac8a218SBjoern A. Zeeb #endif
22222ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22232ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22242ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22252ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22262ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22272ac8a218SBjoern A. Zeeb 
22282ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2229d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2230d9f59799SBjoern A. Zeeb 
223167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2232d9f59799SBjoern A. Zeeb 
2233d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2234d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2235d9f59799SBjoern A. Zeeb 
2236d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2237d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2238d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2239d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2240d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2241d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2242d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2243d9f59799SBjoern A. Zeeb 	}
2244d9f59799SBjoern A. Zeeb 
22458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2246d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2247d9f59799SBjoern A. Zeeb 
2248d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2249d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2250018d93ecSBjoern A. Zeeb 	if (error != 0) {
2251018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2252018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2253d9f59799SBjoern A. Zeeb 		goto outni;
2254018d93ecSBjoern A. Zeeb 	}
2255d9f59799SBjoern A. Zeeb 
2256d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2258d9f59799SBjoern A. Zeeb 
225967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2260d9f59799SBjoern A. Zeeb 
2261d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2262d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2263d9f59799SBjoern A. Zeeb 
2264d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2265d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2266d9f59799SBjoern A. Zeeb 
2267d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2268d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2269d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2270d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2271d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2272d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2273d9f59799SBjoern A. Zeeb 	}
2274d9f59799SBjoern A. Zeeb 
2275d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2276d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2277d9f59799SBjoern A. Zeeb 
2278d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2279d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2280d9f59799SBjoern A. Zeeb 
2281d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2282d9f59799SBjoern A. Zeeb 
2283d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2284d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2285d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2286d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2287e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2288018d93ecSBjoern A. Zeeb 	if (error != 0) {
2289018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2290018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2291d9f59799SBjoern A. Zeeb 		goto out;
2292018d93ecSBjoern A. Zeeb 	}
2293d9f59799SBjoern A. Zeeb 
229467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2295d9f59799SBjoern A. Zeeb 
2296d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2297d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2298d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2299d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2300e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2301018d93ecSBjoern A. Zeeb 	if (error != 0) {
2302018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2303018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2304d9f59799SBjoern A. Zeeb 		goto out;
2305018d93ecSBjoern A. Zeeb 	}
2306d9f59799SBjoern A. Zeeb 
230767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2308d9f59799SBjoern A. Zeeb 
2309d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2310d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2311d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2312d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2313e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2314018d93ecSBjoern A. Zeeb 	if (error != 0) {
2315018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2316018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2317d9f59799SBjoern A. Zeeb 		goto out;
2318018d93ecSBjoern A. Zeeb 	}
2319d9f59799SBjoern A. Zeeb 
23205a4d2461SBjoern A. Zeeb 	bss_changed = 0;
232116e688b2SBjoern A. Zeeb 	/*
23225a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
23235a4d2461SBjoern A. Zeeb 	 *
232416e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
23255a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
23265a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
23275a4d2461SBjoern A. Zeeb 	 *
23285a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
23295a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
23305a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
23315a4d2461SBjoern A. Zeeb 	 * a fw assert.
23325a4d2461SBjoern A. Zeeb 	 *
23335a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
23345a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
23355a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
23365a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
23375a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
23385a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
23395a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
23405a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
23415a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
23425a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
234316e688b2SBjoern A. Zeeb 	 */
23445a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
23455a4d2461SBjoern A. Zeeb 
23465a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
234716e688b2SBjoern A. Zeeb 
2348d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2349d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2350d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2351d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2352e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2353d9f59799SBjoern A. Zeeb 	if (error != 0) {
2354d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2355018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2356018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2357d9f59799SBjoern A. Zeeb 		goto out;
2358d9f59799SBjoern A. Zeeb 	}
2359d9f59799SBjoern A. Zeeb 
23605a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23615a4d2461SBjoern A. Zeeb 
236216e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2363d9f59799SBjoern A. Zeeb 
2364d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2365d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2366d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2367616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2368616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2369d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
23705a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
23715a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
23725a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2373d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2374d9f59799SBjoern A. Zeeb 
23752ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23762ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23772ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23782ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23792ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23800936c648SBjoern A. Zeeb 	/*
23810936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23820936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23830936c648SBjoern A. Zeeb 	 * and potentially freed.
23840936c648SBjoern A. Zeeb 	 */
23850936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2386d9f59799SBjoern A. Zeeb 
2387d9f59799SBjoern A. Zeeb 	/* conf_tx */
2388d9f59799SBjoern A. Zeeb 
2389d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2390d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2391c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2392d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2393d9f59799SBjoern A. Zeeb 
2394d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2395d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
239668541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2397d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2398d9f59799SBjoern A. Zeeb 
23995a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24005a4d2461SBjoern A. Zeeb 
2401d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2402d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2403d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2404c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2405d9f59799SBjoern A. Zeeb 	}
2406d9f59799SBjoern A. Zeeb 
2407d9f59799SBjoern A. Zeeb 	error = EALREADY;
2408d9f59799SBjoern A. Zeeb out:
24098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2410d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2411d9f59799SBjoern A. Zeeb outni:
2412d9f59799SBjoern A. Zeeb 	return (error);
2413d9f59799SBjoern A. Zeeb }
2414d9f59799SBjoern A. Zeeb 
2415d9f59799SBjoern A. Zeeb static int
2416d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2417d9f59799SBjoern A. Zeeb {
2418d9f59799SBjoern A. Zeeb 
2419d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2420d9f59799SBjoern A. Zeeb }
2421d9f59799SBjoern A. Zeeb 
2422d9f59799SBjoern A. Zeeb static int
24236b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24246b4cac81SBjoern A. Zeeb {
24256b4cac81SBjoern A. Zeeb 	int error;
24266b4cac81SBjoern A. Zeeb 
2427d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2428d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2429d9f59799SBjoern A. Zeeb 		return (error);
2430d9f59799SBjoern A. Zeeb 
2431d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2432d9f59799SBjoern A. Zeeb 
2433d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
24346b4cac81SBjoern A. Zeeb 	return (error);
24356b4cac81SBjoern A. Zeeb }
24366b4cac81SBjoern A. Zeeb 
2437d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
24386b4cac81SBjoern A. Zeeb 
24396b4cac81SBjoern A. Zeeb /*
24406b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
24416b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
24426b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
24436b4cac81SBjoern A. Zeeb  */
24446b4cac81SBjoern A. Zeeb struct fsm_state {
24456b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
24466b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
24476b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
24486b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
24496b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
24506b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
24516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
24526b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2453d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2454d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
24556b4cac81SBjoern A. Zeeb 
24566b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
24576b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
24586b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
24596b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2460d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
24616b4cac81SBjoern A. Zeeb 
2462d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2463d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2464d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2465d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2466d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
24676b4cac81SBjoern A. Zeeb 
2468d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2469d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2470d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
24716b4cac81SBjoern A. Zeeb 
24726b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
24736b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
24746b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
24756b4cac81SBjoern A. Zeeb 
24766b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
24776b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
24786b4cac81SBjoern A. Zeeb };
24796b4cac81SBjoern A. Zeeb 
24806b4cac81SBjoern A. Zeeb static int
24816b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24826b4cac81SBjoern A. Zeeb {
24836b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
24846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24856b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24866b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24876b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
24886b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
24896b4cac81SBjoern A. Zeeb 	int error;
24906b4cac81SBjoern A. Zeeb 
24916b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
24926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
24936b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
24946b4cac81SBjoern A. Zeeb 
24959d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24969d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
24976b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
24986b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
24999d9ba2b7SBjoern A. Zeeb #endif
25006b4cac81SBjoern A. Zeeb 
25016b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
25026b4cac81SBjoern A. Zeeb 
25036b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
25046b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25056b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
25066b4cac81SBjoern A. Zeeb 
25076b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
25086b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
25096b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
25106b4cac81SBjoern A. Zeeb 
25116b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
25126b4cac81SBjoern A. Zeeb 
25136b4cac81SBjoern A. Zeeb 	} else {
25146b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
25156b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
25166b4cac81SBjoern A. Zeeb 		return (ENOSYS);
25176b4cac81SBjoern A. Zeeb 	}
25186b4cac81SBjoern A. Zeeb 
25196b4cac81SBjoern A. Zeeb 	error = 0;
25206b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
25216b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
25229d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25239d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2524d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2525d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2526d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2527d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
25289d9ba2b7SBjoern A. Zeeb #endif
25296b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
25306b4cac81SBjoern A. Zeeb 			break;
25316b4cac81SBjoern A. Zeeb 		}
25326b4cac81SBjoern A. Zeeb 	}
25336b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
25346b4cac81SBjoern A. Zeeb 
25356b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2536d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
25376b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
25386b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
25396b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
25406b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
25416b4cac81SBjoern A. Zeeb 		return (ENOSYS);
25426b4cac81SBjoern A. Zeeb 	}
25436b4cac81SBjoern A. Zeeb 
25446b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
25459d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25469d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2547d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2548d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2549d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2550d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
25519d9ba2b7SBjoern A. Zeeb #endif
25526b4cac81SBjoern A. Zeeb 		return (0);
25536b4cac81SBjoern A. Zeeb 	}
25546b4cac81SBjoern A. Zeeb 
25556b4cac81SBjoern A. Zeeb 	if (error != 0) {
25566b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
25576b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
25586b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
25596b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
256045c27ad5SBjoern A. Zeeb 		return (error);
25616b4cac81SBjoern A. Zeeb 	}
25626b4cac81SBjoern A. Zeeb 
25639d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25649d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2565d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2566d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
25676b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
25689d9ba2b7SBjoern A. Zeeb #endif
25696b4cac81SBjoern A. Zeeb 
25706b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
25716b4cac81SBjoern A. Zeeb }
25726b4cac81SBjoern A. Zeeb 
25736b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
25746b4cac81SBjoern A. Zeeb 
2575d9f59799SBjoern A. Zeeb /*
2576d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2577d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2578d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2579d9f59799SBjoern A. Zeeb  */
2580d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2581d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2582d9f59799SBjoern A. Zeeb {
2583d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25842ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2585d9f59799SBjoern A. Zeeb 
25862ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2587d9f59799SBjoern A. Zeeb 
2588ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25892ac8a218SBjoern A. Zeeb 
25902ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25912ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
25922ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25932ac8a218SBjoern A. Zeeb 
25942ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
25952ac8a218SBjoern A. Zeeb 	return (rni);
2596d9f59799SBjoern A. Zeeb }
2597d9f59799SBjoern A. Zeeb 
25984a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25996b4cac81SBjoern A. Zeeb static int
26004a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
26016b4cac81SBjoern A. Zeeb {
26024a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
26036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26066b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
26076b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
26086b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
26096b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
26106b4cac81SBjoern A. Zeeb 	int error;
26116b4cac81SBjoern A. Zeeb 	uint16_t ac;
26126b4cac81SBjoern A. Zeeb 
26136b4cac81SBjoern A. Zeeb 	IMPROVE();
26146b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
26156b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
26166b4cac81SBjoern A. Zeeb 
26176b4cac81SBjoern A. Zeeb 	if (vap == NULL)
26186b4cac81SBjoern A. Zeeb 		return (0);
26196b4cac81SBjoern A. Zeeb 
26204a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
26214a67f1dfSBjoern A. Zeeb 		return (0);
26224a67f1dfSBjoern A. Zeeb 
26236b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
26246b4cac81SBjoern A. Zeeb 		return (0);
26256b4cac81SBjoern A. Zeeb 
26264a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
26274a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
26284a67f1dfSBjoern A. Zeeb 		return (0);
26294a67f1dfSBjoern A. Zeeb 	}
26304a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
26316b4cac81SBjoern A. Zeeb 
26324a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
26336b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
26346b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
26356b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
26366b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
26376b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
26386b4cac81SBjoern A. Zeeb 
26394a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26404a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26414a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26424a67f1dfSBjoern A. Zeeb 
26436b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
26446b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
26456b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
26466b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
26476b4cac81SBjoern A. Zeeb 
26486b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
26496b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
26506b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
26516b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
26526b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
26536b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
265468541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
26556b4cac81SBjoern A. Zeeb 		if (error != 0)
26563f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
26576b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
26586b4cac81SBjoern A. Zeeb 	}
26596b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
26606b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
26614a67f1dfSBjoern A. Zeeb 	if (!planned)
26626b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
26634a67f1dfSBjoern A. Zeeb 
26644a67f1dfSBjoern A. Zeeb 	return (changed);
26654a67f1dfSBjoern A. Zeeb }
26666b4cac81SBjoern A. Zeeb #endif
26676b4cac81SBjoern A. Zeeb 
26684a67f1dfSBjoern A. Zeeb static int
26694a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
26704a67f1dfSBjoern A. Zeeb {
26714a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26724a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
26734a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
26744a67f1dfSBjoern A. Zeeb 
26754a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
26764a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
26774a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
26786b4cac81SBjoern A. Zeeb 		return (0);
26794a67f1dfSBjoern A. Zeeb 
26804a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
26814a67f1dfSBjoern A. Zeeb 
26824a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
26834a67f1dfSBjoern A. Zeeb #endif
26844a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
26856b4cac81SBjoern A. Zeeb }
26866b4cac81SBjoern A. Zeeb 
26876b4cac81SBjoern A. Zeeb static struct ieee80211vap *
26886b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
26896b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
26906b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
26916b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
26926b4cac81SBjoern A. Zeeb {
26936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26966b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
26976b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2698a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
26996b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
27006b4cac81SBjoern A. Zeeb 	size_t len;
2701e3a0b120SBjoern A. Zeeb 	int error, i;
2702a6042e17SBjoern A. Zeeb 	uint16_t ac;
27036b4cac81SBjoern A. Zeeb 
27046b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
27056b4cac81SBjoern A. Zeeb 		return (NULL);
27066b4cac81SBjoern A. Zeeb 
27076b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
27086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27096b4cac81SBjoern A. Zeeb 
27106b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
27116b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
27126b4cac81SBjoern A. Zeeb 
27136b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
27146b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
27156b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
27162ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
27172ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
27186b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
27196b4cac81SBjoern A. Zeeb 
27206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27216b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
27226b4cac81SBjoern A. Zeeb 	vif->p2p = false;
27236b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
27246b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
27256b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
27266b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
27276b4cac81SBjoern A. Zeeb 	IMPROVE();
27286b4cac81SBjoern A. Zeeb 
27296b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
27306b4cac81SBjoern A. Zeeb #if 1
27316b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
2732adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
27336ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
27346ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
27356ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
27366b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
27376b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
27386b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
27396b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
27406b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
27416b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2742616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
2743616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
2744616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
2745616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
27466ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2747caaa79c3SBjoern A. Zeeb 	/*
2748caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2749caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2750caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2751caaa79c3SBjoern A. Zeeb 	 */
27526ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
27536b4cac81SBjoern A. Zeeb #endif
27546b4cac81SBjoern A. Zeeb #if 0
27556b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
27566b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
27576b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
27586b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
27596b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
27606b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
27616b4cac81SBjoern A. Zeeb #endif
2762e3a0b120SBjoern A. Zeeb 
27636ffb7bd4SBjoern A. Zeeb 	/* Link Config */
27646ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
27656ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
27666ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
27676ffb7bd4SBjoern A. Zeeb 	}
27686ffb7bd4SBjoern A. Zeeb 
2769e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2770e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2771e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2772e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2773e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2774e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2775e3a0b120SBjoern A. Zeeb 		else
2776e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
27770cbcfa19SBjoern A. Zeeb 
27780cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
27790cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
2780e3a0b120SBjoern A. Zeeb 	}
2781e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2782e3a0b120SBjoern A. Zeeb 
27836b4cac81SBjoern A. Zeeb 	IMPROVE();
27846b4cac81SBjoern A. Zeeb 
27856b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
27866b4cac81SBjoern A. Zeeb 	if (error != 0) {
27873f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
27886b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
27896b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
27906b4cac81SBjoern A. Zeeb 		return (NULL);
27916b4cac81SBjoern A. Zeeb 	}
27926b4cac81SBjoern A. Zeeb 
27936b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
27946b4cac81SBjoern A. Zeeb 	if (error != 0) {
27956b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
27963f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
27976b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
27986b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
27996b4cac81SBjoern A. Zeeb 		return (NULL);
28006b4cac81SBjoern A. Zeeb 	}
28016b4cac81SBjoern A. Zeeb 
28028891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
28036b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
28048891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
28056b4cac81SBjoern A. Zeeb 
28066b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
28076b4cac81SBjoern A. Zeeb 	changed = 0;
28086b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
28096b4cac81SBjoern A. Zeeb 
2810a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2811a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2812a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2813a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2814a6042e17SBjoern A. Zeeb 
2815a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
2816a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
2817a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
2818a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
2819a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
2820a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2821a6042e17SBjoern A. Zeeb 		if (error != 0)
2822a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2823a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
2824a6042e17SBjoern A. Zeeb 	}
2825a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2826a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
2827a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
28286b4cac81SBjoern A. Zeeb 
28296b4cac81SBjoern A. Zeeb 	/* Force MC init. */
28306b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
28316b4cac81SBjoern A. Zeeb 
28326b4cac81SBjoern A. Zeeb 	IMPROVE();
28336b4cac81SBjoern A. Zeeb 
28346b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
28356b4cac81SBjoern A. Zeeb 
28366b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
28376b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
28386b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2839d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2840d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
28416b4cac81SBjoern A. Zeeb 
28426b4cac81SBjoern A. Zeeb 	/* Key management. */
28436b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2844b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
28456b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
28466b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
28476b4cac81SBjoern A. Zeeb #endif
28486b4cac81SBjoern A. Zeeb 	}
28496b4cac81SBjoern A. Zeeb 
28509fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
28519fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
28529fb91463SBjoern A. Zeeb #endif
28539fb91463SBjoern A. Zeeb 
28546b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
28556b4cac81SBjoern A. Zeeb 
28566b4cac81SBjoern A. Zeeb 	/* Complete setup. */
28576b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
28586b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
28596b4cac81SBjoern A. Zeeb 
28606b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
28616b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
28626b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
28636b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
28646b4cac81SBjoern A. Zeeb 
28656b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
28666b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
28676b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
28686b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
28696b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
28706b4cac81SBjoern A. Zeeb 	/* any others? */
28716b4cac81SBjoern A. Zeeb 	IMPROVE();
28726b4cac81SBjoern A. Zeeb 
28736b4cac81SBjoern A. Zeeb 	return (vap);
28746b4cac81SBjoern A. Zeeb }
28756b4cac81SBjoern A. Zeeb 
28764b0af114SBjoern A. Zeeb void
28774b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
28784b0af114SBjoern A. Zeeb {
28794b0af114SBjoern A. Zeeb 
28804b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
28814b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
28824b0af114SBjoern A. Zeeb 
28834b0af114SBjoern A. Zeeb 	IMPROVE();
28844b0af114SBjoern A. Zeeb }
28854b0af114SBjoern A. Zeeb 
28864b0af114SBjoern A. Zeeb void
28874b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
28884b0af114SBjoern A. Zeeb {
28894b0af114SBjoern A. Zeeb 
28904b0af114SBjoern A. Zeeb 	TODO();
28914b0af114SBjoern A. Zeeb }
28924b0af114SBjoern A. Zeeb 
28936b4cac81SBjoern A. Zeeb static void
28946b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
28956b4cac81SBjoern A. Zeeb {
28966b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28986b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29006b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29016b4cac81SBjoern A. Zeeb 
29026b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
29036b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29046b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
29056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
29066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29076b4cac81SBjoern A. Zeeb 
29088891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
29096b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
29108891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
29116b4cac81SBjoern A. Zeeb 
29126b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
29136b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
2914dbf76919SBjoern A. Zeeb 
2915dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
2916dbf76919SBjoern A. Zeeb 
2917dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
2918dbf76919SBjoern A. Zeeb 
29196c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
29206c38c6b1SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw);
29216c38c6b1SBjoern A. Zeeb 
29226b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
29236b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
29246b4cac81SBjoern A. Zeeb }
29256b4cac81SBjoern A. Zeeb 
29266b4cac81SBjoern A. Zeeb static void
29276b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
29286b4cac81SBjoern A. Zeeb {
29296b4cac81SBjoern A. Zeeb 
29306b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
29316b4cac81SBjoern A. Zeeb 	TRACEOK();
29326b4cac81SBjoern A. Zeeb }
29336b4cac81SBjoern A. Zeeb 
29346b4cac81SBjoern A. Zeeb static void
29356b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
29366b4cac81SBjoern A. Zeeb {
29376b4cac81SBjoern A. Zeeb 
29386b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
29396b4cac81SBjoern A. Zeeb }
29406b4cac81SBjoern A. Zeeb 
29416b4cac81SBjoern A. Zeeb static void
29426b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
29436b4cac81SBjoern A. Zeeb {
29446b4cac81SBjoern A. Zeeb 
29456b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
29466b4cac81SBjoern A. Zeeb }
29476b4cac81SBjoern A. Zeeb 
29486b4cac81SBjoern A. Zeeb /* Start / stop device. */
29496b4cac81SBjoern A. Zeeb static void
29506b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
29516b4cac81SBjoern A. Zeeb {
29526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29538d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29546b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29556b4cac81SBjoern A. Zeeb 	int error;
29568d58a057SBjoern A. Zeeb #endif
29576b4cac81SBjoern A. Zeeb 	bool start_all;
29586b4cac81SBjoern A. Zeeb 
29596b4cac81SBjoern A. Zeeb 	IMPROVE();
29606b4cac81SBjoern A. Zeeb 
29616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
29628d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29648d58a057SBjoern A. Zeeb #endif
29656b4cac81SBjoern A. Zeeb 	start_all = false;
29666b4cac81SBjoern A. Zeeb 
29678ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
29688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
29696b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
29708d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29716b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
29726b4cac81SBjoern A. Zeeb 		if (error == 0)
29738d58a057SBjoern A. Zeeb #endif
29746b4cac81SBjoern A. Zeeb 			start_all = true;
29756b4cac81SBjoern A. Zeeb 	} else {
29768d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29776b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
29788d58a057SBjoern A. Zeeb #endif
29796b4cac81SBjoern A. Zeeb 	}
29808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
29818ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
29826b4cac81SBjoern A. Zeeb 
29836b4cac81SBjoern A. Zeeb 	if (start_all)
29846b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
29856b4cac81SBjoern A. Zeeb }
29866b4cac81SBjoern A. Zeeb 
29876b4cac81SBjoern A. Zeeb bool
29886b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
29896b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
29906b4cac81SBjoern A. Zeeb {
29916b4cac81SBjoern A. Zeeb 	int i;
29926b4cac81SBjoern A. Zeeb 
29936b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
29946b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
29956b4cac81SBjoern A. Zeeb 			return (true);
29966b4cac81SBjoern A. Zeeb 	}
29976b4cac81SBjoern A. Zeeb 
29986b4cac81SBjoern A. Zeeb 	return (false);
29996b4cac81SBjoern A. Zeeb }
30006b4cac81SBjoern A. Zeeb 
30016b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
30026b4cac81SBjoern A. Zeeb bool
30036b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
30046b4cac81SBjoern A. Zeeb {
30056b4cac81SBjoern A. Zeeb 	size_t x;
30066b4cac81SBjoern A. Zeeb 	uint8_t l;
30076b4cac81SBjoern A. Zeeb 
30086b4cac81SBjoern A. Zeeb 	x = *xp;
30096b4cac81SBjoern A. Zeeb 
30106b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
30116b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
30126b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
30136b4cac81SBjoern A. Zeeb 	x += 2 + l;
30146b4cac81SBjoern A. Zeeb 
30156b4cac81SBjoern A. Zeeb 	if (x > ies_len)
30166b4cac81SBjoern A. Zeeb 		return (false);
30176b4cac81SBjoern A. Zeeb 
30186b4cac81SBjoern A. Zeeb 	*xp = x;
30196b4cac81SBjoern A. Zeeb 	return (true);
30206b4cac81SBjoern A. Zeeb }
30216b4cac81SBjoern A. Zeeb 
3022d9945d78SBjoern A. Zeeb static uint8_t *
3023d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3024d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
30256b4cac81SBjoern A. Zeeb {
3026d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3027d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
30283dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3029d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3030d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3031d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3032d9945d78SBjoern A. Zeeb 	int band, i;
30336b4cac81SBjoern A. Zeeb 
30343dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3035d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3036d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3037d9945d78SBjoern A. Zeeb 			continue;
3038d9945d78SBjoern A. Zeeb 
3039d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3040d9945d78SBjoern A. Zeeb 		/*
3041d9945d78SBjoern A. Zeeb 		 * This should not happen;
3042d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3043d9945d78SBjoern A. Zeeb 		 */
3044d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3045d9945d78SBjoern A. Zeeb 			continue;
3046d9945d78SBjoern A. Zeeb 
3047d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3048d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3049d9945d78SBjoern A. Zeeb 		chan = NULL;
3050d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3051d9945d78SBjoern A. Zeeb 
3052d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3053d9945d78SBjoern A. Zeeb 				continue;
3054d9945d78SBjoern A. Zeeb 
30553dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3056d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3057d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3058d9945d78SBjoern A. Zeeb 				break;
3059d9945d78SBjoern A. Zeeb 		}
3060d9945d78SBjoern A. Zeeb 
3061d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3062d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3063d9945d78SBjoern A. Zeeb 			continue;
3064d9945d78SBjoern A. Zeeb 
3065d9945d78SBjoern A. Zeeb 		pb = p;
30663dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3067d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3068d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3069d9945d78SBjoern A. Zeeb 
30703dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
30713dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
30723dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
30733dd98026SBjoern A. Zeeb 
30743dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
30753dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
30763dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
30773dd98026SBjoern A. Zeeb 		}
30783dd98026SBjoern A. Zeeb #endif
30793dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
30803dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
30813dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
30823dd98026SBjoern A. Zeeb 
30833dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
30843dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
30853dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
30863dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
30873dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
30883dd98026SBjoern A. Zeeb 		}
30893dd98026SBjoern A. Zeeb #endif
30903dd98026SBjoern A. Zeeb 
3091d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3092d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3093d9945d78SBjoern A. Zeeb 	}
3094d9945d78SBjoern A. Zeeb 
3095d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3096d9945d78SBjoern A. Zeeb 	pb = p;
3097d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3098d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3099d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3100d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3101d9945d78SBjoern A. Zeeb 	}
3102d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3103d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3104d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3105d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3106d9945d78SBjoern A. Zeeb 	}
3107d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3108d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3109d9945d78SBjoern A. Zeeb 
3110d9945d78SBjoern A. Zeeb 	return (p);
31116b4cac81SBjoern A. Zeeb }
31126b4cac81SBjoern A. Zeeb 
31136b4cac81SBjoern A. Zeeb static void
31146b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
31156b4cac81SBjoern A. Zeeb {
31166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
31176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
31186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31196b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31206b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
31216b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
31226b4cac81SBjoern A. Zeeb 	int error;
31238ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
31246b4cac81SBjoern A. Zeeb 
31256b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
31268ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3127a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
31286b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
31298ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
31306b4cac81SBjoern A. Zeeb 		return;
31316b4cac81SBjoern A. Zeeb 	}
31328ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
31338ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
31346b4cac81SBjoern A. Zeeb 
31356b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
31366b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
31376b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3138d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
31396b4cac81SBjoern A. Zeeb 		return;
31406b4cac81SBjoern A. Zeeb 	}
31416b4cac81SBjoern A. Zeeb 
31426b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
31438ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3144a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3145a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3146d3ef7fb4SBjoern A. Zeeb sw_scan:
31476b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
31486b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3149086be6a8SBjoern A. Zeeb 
3150086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3151086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3152086be6a8SBjoern A. Zeeb 
31536b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
31546b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
31556b4cac81SBjoern A. Zeeb 		IMPROVE();
31566b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
31576b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
31586b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
31596b4cac81SBjoern A. Zeeb 
31606b4cac81SBjoern A. Zeeb 	} else {
31616b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
31626b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
31636b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
31646b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
31656b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
31666b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3167d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3168d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3169d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
31703206587aSBjoern A. Zeeb 		bool running;
3171d9945d78SBjoern A. Zeeb 
3172d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3173d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
31746b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
31756b4cac81SBjoern A. Zeeb 
3176d9945d78SBjoern A. Zeeb 		band_mask = 0;
31776b4cac81SBjoern A. Zeeb 		nchan = 0;
3178d9945d78SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++) {
31796b4cac81SBjoern A. Zeeb 			nchan++;
3180d9945d78SBjoern A. Zeeb 			band = lkpi_net80211_chan_to_nl80211_band(
3181d9945d78SBjoern A. Zeeb 			    ss->ss_chans[ss->ss_next + i]);
3182d9945d78SBjoern A. Zeeb 			band_mask |= (1 << band);
3183d9945d78SBjoern A. Zeeb 		}
31843206587aSBjoern A. Zeeb 
31853206587aSBjoern A. Zeeb 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
31863206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
31873206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
31883206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
31893206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
31903206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
31913206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
31923206587aSBjoern A. Zeeb 			 */
31933206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
31943206587aSBjoern A. Zeeb 		}
31953206587aSBjoern A. Zeeb 
31966b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
31976b4cac81SBjoern A. Zeeb 
3198d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3199d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3200d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3201d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3202d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3203d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3204d9945d78SBjoern A. Zeeb 
3205d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3206d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3207d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3208d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3209d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3210d9945d78SBjoern A. Zeeb 		}
3211d9945d78SBjoern A. Zeeb 
32123206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3213d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3214d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
32156b4cac81SBjoern A. Zeeb 
32166b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
32176b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
32186b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
32196b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
32206b4cac81SBjoern A. Zeeb #if 0
32216b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
32226b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
32236b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
32246b4cac81SBjoern A. Zeeb #endif
32256b4cac81SBjoern A. Zeeb #ifdef __notyet__
32266b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
32276b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
32286b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
32296b4cac81SBjoern A. Zeeb #endif
3230e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
32316b4cac81SBjoern A. Zeeb 
32326b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
32336b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
32346b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
32356b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
32366b4cac81SBjoern A. Zeeb 			*(cpp + i) =
32376b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
32386b4cac81SBjoern A. Zeeb 		}
32396b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
32406b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
32416b4cac81SBjoern A. Zeeb 
32426b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
32433206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
32446b4cac81SBjoern A. Zeeb 			/* lc->flags */
32456b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
32466b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
32476b4cac81SBjoern A. Zeeb 			/* lc-> ... */
32486b4cac81SBjoern A. Zeeb 			lc++;
32496b4cac81SBjoern A. Zeeb 		}
32506b4cac81SBjoern A. Zeeb 
3251d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
32526b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
32536b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
32546b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3255d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
32566b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
32576b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
32586b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
32596b4cac81SBjoern A. Zeeb 				ssids++;
32606b4cac81SBjoern A. Zeeb 			}
32616b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
32626b4cac81SBjoern A. Zeeb 		} else {
32636b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
32646b4cac81SBjoern A. Zeeb 		}
32656b4cac81SBjoern A. Zeeb 
32666b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
32676b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
32686b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
32696b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
32706b4cac81SBjoern A. Zeeb 		/* s6gp->... */
32716b4cac81SBjoern A. Zeeb 
3272d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3273d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3274d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3275d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3276d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3277d9945d78SBjoern A. Zeeb 
32786b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32796b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
32803206587aSBjoern A. Zeeb 
32813206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
32823206587aSBjoern A. Zeeb 		/* Re-check under lock. */
32833206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
32843206587aSBjoern A. Zeeb 		if (!running) {
32853206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
32863206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
32873206587aSBjoern A. Zeeb 
32883206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
32893206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
32903206587aSBjoern A. Zeeb 		}
32913206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32923206587aSBjoern A. Zeeb 		if (running) {
32933206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
32943206587aSBjoern A. Zeeb 			return;
32953206587aSBjoern A. Zeeb 		}
32963206587aSBjoern A. Zeeb 
32976b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
32986b4cac81SBjoern A. Zeeb 		if (error != 0) {
3299d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3300d9945d78SBjoern A. Zeeb 
33013206587aSBjoern A. Zeeb 			/*
33023206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
33033206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
33043206587aSBjoern A. Zeeb 			 * and only there.
33053206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
33063206587aSBjoern A. Zeeb 			 * behave differently:
33073206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
33083206587aSBjoern A. Zeeb 			 *        and can then still return an error.
33093206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
33103206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
33113206587aSBjoern A. Zeeb 			 * ...
33123206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
33133206587aSBjoern A. Zeeb 			 * and balance between both code paths.
33143206587aSBjoern A. Zeeb 			 */
33153206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
33163206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
33173206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
33186b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
33193206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
33203206587aSBjoern A. Zeeb 			}
33213206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3322d3ef7fb4SBjoern A. Zeeb 
3323d3ef7fb4SBjoern A. Zeeb 			/*
3324d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3325d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3326d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3327d3ef7fb4SBjoern A. Zeeb 			 */
3328196cfd0bSBjoern A. Zeeb 			if (error == 1) {
33298ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3330a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
33318ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33323206587aSBjoern A. Zeeb 				/*
33333206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
33343206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
33353206587aSBjoern A. Zeeb 				 * currently not re-enable it?
33363206587aSBjoern A. Zeeb 				 */
33373206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3338196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3339196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3340196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3341196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3342196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3343196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3344196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3345196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3346d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3347196cfd0bSBjoern A. Zeeb 			}
3348d3ef7fb4SBjoern A. Zeeb 
3349d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3350d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
33516b4cac81SBjoern A. Zeeb 		}
33526b4cac81SBjoern A. Zeeb 	}
33536b4cac81SBjoern A. Zeeb }
33546b4cac81SBjoern A. Zeeb 
33556b4cac81SBjoern A. Zeeb static void
33566b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
33576b4cac81SBjoern A. Zeeb {
33586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33598ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
33606b4cac81SBjoern A. Zeeb 
33616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33628ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3363a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
33648ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33656b4cac81SBjoern A. Zeeb 		return;
33666b4cac81SBjoern A. Zeeb 	}
33678ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
33688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33696b4cac81SBjoern A. Zeeb 
33708ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3371a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3372a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
33736b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
33746b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
33756b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
33766b4cac81SBjoern A. Zeeb 
3377a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3378a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
33796b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
33806b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33816b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3382a486fbbdSBjoern A. Zeeb 
33836b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
33846b4cac81SBjoern A. Zeeb 
33856b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3386086be6a8SBjoern A. Zeeb 
3387086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3388086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
33896b4cac81SBjoern A. Zeeb 	}
33906b4cac81SBjoern A. Zeeb }
33916b4cac81SBjoern A. Zeeb 
33926b4cac81SBjoern A. Zeeb static void
3393a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3394a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3395a486fbbdSBjoern A. Zeeb {
3396a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33978ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3398a486fbbdSBjoern A. Zeeb 
3399a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
34008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
34018ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
34028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34038ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3404a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3405a486fbbdSBjoern A. Zeeb }
3406a486fbbdSBjoern A. Zeeb 
3407a486fbbdSBjoern A. Zeeb static void
3408a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3409a486fbbdSBjoern A. Zeeb {
3410a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
34118ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3412a486fbbdSBjoern A. Zeeb 
3413a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
34148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
34158ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
34168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34178ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3418a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3419a486fbbdSBjoern A. Zeeb }
3420a486fbbdSBjoern A. Zeeb 
3421a486fbbdSBjoern A. Zeeb static void
34226b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
34236b4cac81SBjoern A. Zeeb {
34246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34256b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3426b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3427b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
34286b4cac81SBjoern A. Zeeb 	int error;
34298ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
34306b4cac81SBjoern A. Zeeb 
34316b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3432b2cf3c21SBjoern A. Zeeb 
3433b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3434b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
34356b4cac81SBjoern A. Zeeb 		return;
34366b4cac81SBjoern A. Zeeb 
3437a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
34388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
34398ac540d3SBjoern A. Zeeb 	hw_scan_running =
34408ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
34418ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
34428ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34438ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3444a486fbbdSBjoern A. Zeeb 		return;
3445a486fbbdSBjoern A. Zeeb 
3446b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3447b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
34486b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
34496b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
34506b4cac81SBjoern A. Zeeb 		return;
34516b4cac81SBjoern A. Zeeb 	}
34526b4cac81SBjoern A. Zeeb 
34536b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
34546b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
34556b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
34566b4cac81SBjoern A. Zeeb 		    c, chan);
34576b4cac81SBjoern A. Zeeb 		return;
34586b4cac81SBjoern A. Zeeb 	}
34596b4cac81SBjoern A. Zeeb 
34606b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
34616b4cac81SBjoern A. Zeeb 	IMPROVE();
34626b4cac81SBjoern A. Zeeb 
34636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34644a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
34659fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
34669fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
34679fb91463SBjoern A. Zeeb #endif
34684a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
34696b4cac81SBjoern A. Zeeb 
34706b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
34716b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
34726b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
34736b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
34746b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
34756b4cac81SBjoern A. Zeeb 		IMPROVE();
34766b4cac81SBjoern A. Zeeb 	} else {
34776b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
34786b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
34796b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
34806b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
34816b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
34826b4cac81SBjoern A. Zeeb 	}
34836b4cac81SBjoern A. Zeeb 
34846b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
34856b4cac81SBjoern A. Zeeb 	IMPROVE();
34866b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
34876b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
34886b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
34896b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
34906b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
34916b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
34926b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
34936b4cac81SBjoern A. Zeeb 			    error);
34946b4cac81SBjoern A. Zeeb 	}
34956b4cac81SBjoern A. Zeeb }
34966b4cac81SBjoern A. Zeeb 
34976b4cac81SBjoern A. Zeeb static struct ieee80211_node *
34986b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
34996b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
35006b4cac81SBjoern A. Zeeb {
35016b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35034f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
35046b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35066b4cac81SBjoern A. Zeeb 
35076b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
35086b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35096b4cac81SBjoern A. Zeeb 
35106b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
35114f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
35124f61ef8bSBjoern A. Zeeb 		return (NULL);
35134f61ef8bSBjoern A. Zeeb 
35146b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
35156b4cac81SBjoern A. Zeeb 	if (ni == NULL)
35166b4cac81SBjoern A. Zeeb 		return (NULL);
35176b4cac81SBjoern A. Zeeb 
35186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35194f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
35206b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
35216b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
35226b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
35236b4cac81SBjoern A. Zeeb 		return (NULL);
35246b4cac81SBjoern A. Zeeb 	}
35256b4cac81SBjoern A. Zeeb 
35266b4cac81SBjoern A. Zeeb 	return (ni);
35276b4cac81SBjoern A. Zeeb }
35286b4cac81SBjoern A. Zeeb 
35296b4cac81SBjoern A. Zeeb static int
35306b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
35316b4cac81SBjoern A. Zeeb {
35326b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35346b4cac81SBjoern A. Zeeb 	int error;
35356b4cac81SBjoern A. Zeeb 
35366b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35376b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35386b4cac81SBjoern A. Zeeb 
35396b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
35406b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
35416b4cac81SBjoern A. Zeeb 		if (error != 0)
35426b4cac81SBjoern A. Zeeb 			return (error);
35436b4cac81SBjoern A. Zeeb 	}
35446b4cac81SBjoern A. Zeeb 
35456b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
35466b4cac81SBjoern A. Zeeb 	IMPROVE();
35476b4cac81SBjoern A. Zeeb 
35486b4cac81SBjoern A. Zeeb 	return (0);
35496b4cac81SBjoern A. Zeeb }
35506b4cac81SBjoern A. Zeeb 
35516b4cac81SBjoern A. Zeeb static void
35526b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
35536b4cac81SBjoern A. Zeeb {
35546b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35566b4cac81SBjoern A. Zeeb 
35576b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35586b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35596b4cac81SBjoern A. Zeeb 
35606b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
35616b4cac81SBjoern A. Zeeb 	IMPROVE();
35626b4cac81SBjoern A. Zeeb 
35636b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
35646b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
35656b4cac81SBjoern A. Zeeb }
35666b4cac81SBjoern A. Zeeb 
35676b4cac81SBjoern A. Zeeb static void
35686b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
35696b4cac81SBjoern A. Zeeb {
35706b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35716b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35726b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35736b4cac81SBjoern A. Zeeb 
35746b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35756b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35766b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
35776b4cac81SBjoern A. Zeeb 
35780936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
35796b4cac81SBjoern A. Zeeb 
35800936c648SBjoern A. Zeeb 	/*
35810936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
35820936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
35830936c648SBjoern A. Zeeb 	 */
35840936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
35856b4cac81SBjoern A. Zeeb 
35866b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
35876b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
35886b4cac81SBjoern A. Zeeb }
35896b4cac81SBjoern A. Zeeb 
35906b4cac81SBjoern A. Zeeb static int
35916b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
35926b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
35936b4cac81SBjoern A. Zeeb {
35946b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35956b4cac81SBjoern A. Zeeb 
35966b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3597fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
35980936c648SBjoern A. Zeeb 	if (!lsta->txq_ready) {
3599fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3600fa4e4257SBjoern A. Zeeb 		/*
3601fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3602fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
3603fa4e4257SBjoern A. Zeeb 		 */
36040936c648SBjoern A. Zeeb 		m_free(m);
36050936c648SBjoern A. Zeeb 		return (ENETDOWN);
36060936c648SBjoern A. Zeeb 	}
36076b4cac81SBjoern A. Zeeb 
36086b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
36096b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
3610fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3611fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
36126b4cac81SBjoern A. Zeeb 
36139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
36149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
36156b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
36166b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
36176b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
36189d9ba2b7SBjoern A. Zeeb #endif
36196b4cac81SBjoern A. Zeeb 
36206b4cac81SBjoern A. Zeeb 	return (0);
36216b4cac81SBjoern A. Zeeb }
36226b4cac81SBjoern A. Zeeb 
36236b4cac81SBjoern A. Zeeb static void
36246b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
36256b4cac81SBjoern A. Zeeb {
36266b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3627b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
36286b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3629b35f6cd0SBjoern A. Zeeb #endif
36306b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
36316b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
36326b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36346b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36356b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36366b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36376b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
36386b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
36396b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
36406b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3641e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
3642ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
36436b4cac81SBjoern A. Zeeb 	void *buf;
3644e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
36456b4cac81SBjoern A. Zeeb 
36466b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
36476b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
36489d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
36496b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
36506b4cac81SBjoern A. Zeeb #endif
36516b4cac81SBjoern A. Zeeb 
36526b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3653b35f6cd0SBjoern A. Zeeb 	k = NULL;
3654b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
36556b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
36566b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
36576b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
36586b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
36596b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
36606b4cac81SBjoern A. Zeeb 		if (k == NULL) {
36616b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
36626b4cac81SBjoern A. Zeeb 			m_freem(m);
36636b4cac81SBjoern A. Zeeb 			return;
36646b4cac81SBjoern A. Zeeb 		}
36656b4cac81SBjoern A. Zeeb 	}
36666b4cac81SBjoern A. Zeeb #endif
36676b4cac81SBjoern A. Zeeb 
36686b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36696b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36706b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36716b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
36726b4cac81SBjoern A. Zeeb 
36736b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
36746b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
36756b4cac81SBjoern A. Zeeb 
36766b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
36776b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
36786b4cac81SBjoern A. Zeeb 		if (k != NULL)
36796b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
36806b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
36816b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
36826b4cac81SBjoern A. Zeeb 		IMPROVE();
36836b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
36846b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
36856b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
36866b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
36876b4cac81SBjoern A. Zeeb 		}
36886b4cac81SBjoern A. Zeeb 
36896b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
36906b4cac81SBjoern A. Zeeb 	}
36916b4cac81SBjoern A. Zeeb 
36926b4cac81SBjoern A. Zeeb 	/*
36936b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
36946b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
36953d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
36963d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
36976b4cac81SBjoern A. Zeeb 	 */
36986b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
36996b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
37003f0083c4SBjoern A. Zeeb 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
37016b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
37026b4cac81SBjoern A. Zeeb 		m_freem(m);
37036b4cac81SBjoern A. Zeeb 		return;
37046b4cac81SBjoern A. Zeeb 	}
37056b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
37066b4cac81SBjoern A. Zeeb 
37076b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
37086b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
37096b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
37106b4cac81SBjoern A. Zeeb 	skb->m = m;
37116b4cac81SBjoern A. Zeeb #if 0
37126b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
37136b4cac81SBjoern A. Zeeb #else
37146b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
37156b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
37166b4cac81SBjoern A. Zeeb #endif
37176b4cac81SBjoern A. Zeeb 	/* Save the ni. */
37186b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
37196b4cac81SBjoern A. Zeeb 
37206b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
37216b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
37226b4cac81SBjoern A. Zeeb 
3723e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3724e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3725e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
37261665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
37271665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
37281665ef97SBjoern A. Zeeb 			skb->priority = 7;
37291665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
37301665ef97SBjoern A. Zeeb 		} else {
37311665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
37321665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
3733e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
3734e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
37351665ef97SBjoern A. Zeeb 		}
3736e3a0b120SBjoern A. Zeeb 	} else {
3737e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3738fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
3739e3a0b120SBjoern A. Zeeb 	}
37406b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
37416b4cac81SBjoern A. Zeeb 
37426b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
37436b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
37446b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
37456b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
37466b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
37476b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3748e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
37496b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
37506b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
37516b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
37526b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
37539fb91463SBjoern A. Zeeb #ifdef __notyet__
37549fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
37559fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
37569fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
37579fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
37589fb91463SBjoern A. Zeeb #endif
37599fb91463SBjoern A. Zeeb #endif
37606b4cac81SBjoern A. Zeeb 
37616b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3762b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
37636b4cac81SBjoern A. Zeeb 	info->control.hw_key = lsta->kc;
37646b4cac81SBjoern A. Zeeb #endif
37656b4cac81SBjoern A. Zeeb 
37666b4cac81SBjoern A. Zeeb 	IMPROVE();
37676b4cac81SBjoern A. Zeeb 
3768e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
3769e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
3770e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
3771e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
3772e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3773d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3774e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
3775e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
3776e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3777e3a0b120SBjoern A. Zeeb 	}
3778e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
3779d0d29110SBjoern A. Zeeb 		goto ops_tx;
3780e3a0b120SBjoern A. Zeeb 
3781d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3782d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3783d0d29110SBjoern A. Zeeb 
3784eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
37856b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
37869d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37879d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3788d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3789d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3790d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3791fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
3792d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
3793d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3794d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3795d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
37969d9ba2b7SBjoern A. Zeeb #endif
3797eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
379845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3799d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
380045bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
38016b4cac81SBjoern A. Zeeb 	return;
38026b4cac81SBjoern A. Zeeb 
38036b4cac81SBjoern A. Zeeb ops_tx:
38049d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
38059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3806d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3807d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
38086b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
38096b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
38109d9ba2b7SBjoern A. Zeeb #endif
38116b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
38126b4cac81SBjoern A. Zeeb 	control.sta = sta;
381345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
38146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
381545bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
38166b4cac81SBjoern A. Zeeb }
38176b4cac81SBjoern A. Zeeb 
38186b4cac81SBjoern A. Zeeb static void
38196b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
38206b4cac81SBjoern A. Zeeb {
38216b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
38226b4cac81SBjoern A. Zeeb 	struct mbufq mq;
38236b4cac81SBjoern A. Zeeb 	struct mbuf *m;
38246b4cac81SBjoern A. Zeeb 
38256b4cac81SBjoern A. Zeeb 	lsta = ctx;
38266b4cac81SBjoern A. Zeeb 
38279d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
38289d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
38296b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
38309d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
38316b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
38329d9ba2b7SBjoern A. Zeeb #endif
38336b4cac81SBjoern A. Zeeb 
38346b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
38356b4cac81SBjoern A. Zeeb 
3836fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3837fa4e4257SBjoern A. Zeeb 	/*
3838fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
3839fa4e4257SBjoern A. Zeeb 	 * disassoc frame still.
3840fa4e4257SBjoern A. Zeeb 	 */
38416b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
3842fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
38436b4cac81SBjoern A. Zeeb 
38446b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
38456b4cac81SBjoern A. Zeeb 	while (m != NULL) {
38466b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
38476b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
38486b4cac81SBjoern A. Zeeb 	}
38496b4cac81SBjoern A. Zeeb }
38506b4cac81SBjoern A. Zeeb 
38516b4cac81SBjoern A. Zeeb static int
38526b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
38536b4cac81SBjoern A. Zeeb {
38546b4cac81SBjoern A. Zeeb 
38556b4cac81SBjoern A. Zeeb 	/* XXX TODO */
38566b4cac81SBjoern A. Zeeb 	IMPROVE();
38576b4cac81SBjoern A. Zeeb 
38586b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
38596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
38606b4cac81SBjoern A. Zeeb 
38616b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
38626b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
38636b4cac81SBjoern A. Zeeb }
38646b4cac81SBjoern A. Zeeb 
38659fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38669fb91463SBjoern A. Zeeb static int
38679fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
38689fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
38699fb91463SBjoern A. Zeeb {
38709fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38719fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38729fb91463SBjoern A. Zeeb 
38739fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38749fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38759fb91463SBjoern A. Zeeb 
38769fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38779fb91463SBjoern A. Zeeb 
38789fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
38799fb91463SBjoern A. Zeeb }
38809fb91463SBjoern A. Zeeb 
38819fb91463SBjoern A. Zeeb static int
38829fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
38839fb91463SBjoern A. Zeeb {
38849fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38859fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38869fb91463SBjoern A. Zeeb 
38879fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38889fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38899fb91463SBjoern A. Zeeb 
38909fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38919fb91463SBjoern A. Zeeb 
38929fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
38939fb91463SBjoern A. Zeeb }
38949fb91463SBjoern A. Zeeb 
38959fb91463SBjoern A. Zeeb 
38969fb91463SBjoern A. Zeeb static int
38979fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38989fb91463SBjoern A. Zeeb {
38999fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39009fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39019fb91463SBjoern A. Zeeb 
39029fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39039fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39049fb91463SBjoern A. Zeeb 
39059fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39069fb91463SBjoern A. Zeeb 
39079fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
39089fb91463SBjoern A. Zeeb }
39099fb91463SBjoern A. Zeeb 
39109fb91463SBjoern A. Zeeb static int
39119fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
39129fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
39139fb91463SBjoern A. Zeeb {
39149fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39159fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39169fb91463SBjoern A. Zeeb 
39179fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39189fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39199fb91463SBjoern A. Zeeb 
39209fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39219fb91463SBjoern A. Zeeb 
39229fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
39239fb91463SBjoern A. Zeeb }
39249fb91463SBjoern A. Zeeb 
39259fb91463SBjoern A. Zeeb static int
39269fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
39279fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
39289fb91463SBjoern A. Zeeb {
39299fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39309fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39319fb91463SBjoern A. Zeeb 
39329fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39339fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39349fb91463SBjoern A. Zeeb 
39359fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39369fb91463SBjoern A. Zeeb 
39379fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
39389fb91463SBjoern A. Zeeb }
39399fb91463SBjoern A. Zeeb 
39409fb91463SBjoern A. Zeeb static void
39419fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
39429fb91463SBjoern A. Zeeb {
39439fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39449fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39459fb91463SBjoern A. Zeeb 
39469fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39479fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39489fb91463SBjoern A. Zeeb 
39499fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39509fb91463SBjoern A. Zeeb 
39519fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
39529fb91463SBjoern A. Zeeb }
39539fb91463SBjoern A. Zeeb 
39549fb91463SBjoern A. Zeeb static void
39559fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
39569fb91463SBjoern A. Zeeb {
39579fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39589fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39599fb91463SBjoern A. Zeeb 
39609fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39619fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39629fb91463SBjoern A. Zeeb 
39639fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39649fb91463SBjoern A. Zeeb 
39659fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
39669fb91463SBjoern A. Zeeb }
39679fb91463SBjoern A. Zeeb 
39689fb91463SBjoern A. Zeeb static void
39699fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
39709fb91463SBjoern A. Zeeb     int status)
39719fb91463SBjoern A. Zeeb {
39729fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39739fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39749fb91463SBjoern A. Zeeb 
39759fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39769fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39779fb91463SBjoern A. Zeeb 
39789fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39799fb91463SBjoern A. Zeeb 
39809fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
39819fb91463SBjoern A. Zeeb }
39829fb91463SBjoern A. Zeeb 
39839fb91463SBjoern A. Zeeb static int
39849fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
39859fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
39869fb91463SBjoern A. Zeeb {
39879fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39889fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39899fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39909fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
39919fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39929fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39939fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39949fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
39959fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
39969fb91463SBjoern A. Zeeb 	int error;
39979fb91463SBjoern A. Zeeb 
39989fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39999fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40009fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40019fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
40029fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
40039fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
40049fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
40059fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
40069fb91463SBjoern A. Zeeb 
40079fb91463SBjoern A. Zeeb 	params.sta = sta;
40089fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
40099fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
40109fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
40119fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
40129fb91463SBjoern A. Zeeb 	else
40139fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
40149fb91463SBjoern A. Zeeb 	if (params.buf_size > hw->max_rx_aggregation_subframes)
40159fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
40169fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
40179fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
40189fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
40199fb91463SBjoern A. Zeeb 	params.amsdu = false;
40209fb91463SBjoern A. Zeeb 
40219fb91463SBjoern A. Zeeb 	IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
40229fb91463SBjoern A. Zeeb 
40239fb91463SBjoern A. Zeeb 	/* This may call kalloc.  Make sure we can sleep. */
40249fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
40259fb91463SBjoern A. Zeeb 	if (error != 0) {
40269fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
40279fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
40289fb91463SBjoern A. Zeeb 		return (error);
40299fb91463SBjoern A. Zeeb 	}
40309fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
40319fb91463SBjoern A. Zeeb 
40329fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
40339fb91463SBjoern A. Zeeb 	return (error);
40349fb91463SBjoern A. Zeeb }
40359fb91463SBjoern A. Zeeb 
40369fb91463SBjoern A. Zeeb static void
40379fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
40389fb91463SBjoern A. Zeeb {
40399fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40409fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40419fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40429fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
40439fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
40449fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
40459fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
40469fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
40479fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
40489fb91463SBjoern A. Zeeb 	int error;
40499fb91463SBjoern A. Zeeb 	uint8_t tid;
40509fb91463SBjoern A. Zeeb 
40519fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40529fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40539fb91463SBjoern A. Zeeb 
40549fb91463SBjoern A. Zeeb 	/*
40559fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
40569fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
40579fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
40589fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
40599fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
40609fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
40619fb91463SBjoern A. Zeeb 	 * track some state itself.
40629fb91463SBjoern A. Zeeb 	 */
40639fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
40649fb91463SBjoern A. Zeeb 		goto net80211_only;
40659fb91463SBjoern A. Zeeb 
40669fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40679fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
40689fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
40699fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
40709fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
40719fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
40729fb91463SBjoern A. Zeeb 
40739fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
40749fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
40759fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
40769fb91463SBjoern A. Zeeb 			break;
40779fb91463SBjoern A. Zeeb 	}
40789fb91463SBjoern A. Zeeb 
40799fb91463SBjoern A. Zeeb 	params.sta = sta;
40809fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
40819fb91463SBjoern A. Zeeb 	params.buf_size = 0;
40829fb91463SBjoern A. Zeeb 	params.timeout = 0;
40839fb91463SBjoern A. Zeeb 	params.ssn = 0;
40849fb91463SBjoern A. Zeeb 	params.tid = tid;
40859fb91463SBjoern A. Zeeb 	params.amsdu = false;
40869fb91463SBjoern A. Zeeb 
40879fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
40889fb91463SBjoern A. Zeeb 	if (error != 0)
40899fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
40909fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
40919fb91463SBjoern A. Zeeb 
40929fb91463SBjoern A. Zeeb net80211_only:
40939fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
40949fb91463SBjoern A. Zeeb }
40959fb91463SBjoern A. Zeeb #endif
40969fb91463SBjoern A. Zeeb 
40979fb91463SBjoern A. Zeeb static void
40989fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
40999fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
41009fb91463SBjoern A. Zeeb {
41019fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
41029fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
41039fb91463SBjoern A. Zeeb 
41049fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
41059fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
41069fb91463SBjoern A. Zeeb 		return;
41079fb91463SBjoern A. Zeeb 
41089fb91463SBjoern A. Zeeb 	switch (band) {
41099fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
41109fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
41119fb91463SBjoern A. Zeeb 		break;
41129fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
41139fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
41149fb91463SBjoern A. Zeeb 		break;
41159fb91463SBjoern A. Zeeb 	default:
41169fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
41179fb91463SBjoern A. Zeeb 		return;
41189fb91463SBjoern A. Zeeb 	}
41199fb91463SBjoern A. Zeeb 
41209fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
41219fb91463SBjoern A. Zeeb 
41229fb91463SBjoern A. Zeeb 	/*
41239fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
41249fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
41259fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
41269fb91463SBjoern A. Zeeb 	 */
41279fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
41289fb91463SBjoern A. Zeeb 
41299fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
41309fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
41319fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
41329fb91463SBjoern A. Zeeb #ifdef __notyet__
41339fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
41349fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
41359fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
41369fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
41379fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
41389fb91463SBjoern A. Zeeb #endif
41399fb91463SBjoern A. Zeeb 
41409fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
41419fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
41429fb91463SBjoern A. Zeeb 
41439fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
41449fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
41459fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
41469fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
41479fb91463SBjoern A. Zeeb #endif
41489fb91463SBjoern A. Zeeb }
41499fb91463SBjoern A. Zeeb 
41506b4cac81SBjoern A. Zeeb static void
41516b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
41526b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
41536b4cac81SBjoern A. Zeeb {
41546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41556b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41566b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
41576b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
41586b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
41596b4cac81SBjoern A. Zeeb 
41606b4cac81SBjoern A. Zeeb 	/* Channels */
41616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41626b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41636b4cac81SBjoern A. Zeeb 
41646b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
41656b4cac81SBjoern A. Zeeb 	nchans = 0;
41666b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
41676b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
41686b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
41696b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
41706b4cac81SBjoern A. Zeeb 		chan_flags = 0;
41716b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
41726b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
41739fb91463SBjoern A. Zeeb 
41749fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
41756b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
41769fb91463SBjoern A. Zeeb 
41779fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
41789fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
41796b4cac81SBjoern A. Zeeb 
41806b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4181cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
41826b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
41836b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
41846b4cac81SBjoern A. Zeeb 
41856b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
41863f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
41873f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
41886b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
41896b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
41906b4cac81SBjoern A. Zeeb 				continue;
41916b4cac81SBjoern A. Zeeb 			}
41926b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
41936b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
41946b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
41956b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
41966b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
41976b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
41986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
41996b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
42006b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
42016b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
42026b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
42036b4cac81SBjoern A. Zeeb 
42046b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
42056b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
42066b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
42075856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4208cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4209cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
42103f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
42113f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
42126b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
42136b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
42146b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4215cee56e77SBjoern A. Zeeb 			if (error != 0)
42166b4cac81SBjoern A. Zeeb 				break;
42176b4cac81SBjoern A. Zeeb 		}
42186b4cac81SBjoern A. Zeeb 	}
42196b4cac81SBjoern A. Zeeb 
42206b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
42216b4cac81SBjoern A. Zeeb 	nchans = 0;
42226b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
42236b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
42246b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
42256b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
42266b4cac81SBjoern A. Zeeb 		chan_flags = 0;
42276b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
42289fb91463SBjoern A. Zeeb 
42299fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
42309fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
42319fb91463SBjoern A. Zeeb 
42329fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
42336b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
42346b4cac81SBjoern A. Zeeb 
42356b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4236562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
42376b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
42386b4cac81SBjoern A. Zeeb 
42396b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
42406b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
42416b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4242562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
42436b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
42446b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4245562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
42466b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
42476b4cac81SBjoern A. Zeeb 		}
42486b4cac81SBjoern A. Zeeb #endif
42496b4cac81SBjoern A. Zeeb 
42506b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4251cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
42526b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
42536b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
42546b4cac81SBjoern A. Zeeb 
42556b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
42563f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
42573f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
42586b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
42596b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
42606b4cac81SBjoern A. Zeeb 				continue;
42616b4cac81SBjoern A. Zeeb 			}
42626b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
42636b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
42646b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
42656b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
42666b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
42676b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
42686b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
42696b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
42706b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
42716b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
42726b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
42736b4cac81SBjoern A. Zeeb 
42746b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
42756b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
42766b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
42775856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4278cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4279cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
42803f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
42813f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
42826b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
42836b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
42846b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4285cee56e77SBjoern A. Zeeb 			if (error != 0)
42866b4cac81SBjoern A. Zeeb 				break;
42876b4cac81SBjoern A. Zeeb 		}
42886b4cac81SBjoern A. Zeeb 	}
42896b4cac81SBjoern A. Zeeb }
42906b4cac81SBjoern A. Zeeb 
42916b4cac81SBjoern A. Zeeb static void *
42926b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
42936b4cac81SBjoern A. Zeeb {
42946b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42956b4cac81SBjoern A. Zeeb 
42966b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
42976b4cac81SBjoern A. Zeeb 	if (ic == NULL)
42986b4cac81SBjoern A. Zeeb 		return (NULL);
42996b4cac81SBjoern A. Zeeb 
43006b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
43016b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
43026b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
43036b4cac81SBjoern A. Zeeb 
43046b4cac81SBjoern A. Zeeb 	return (ic);
43056b4cac81SBjoern A. Zeeb }
43066b4cac81SBjoern A. Zeeb 
43076b4cac81SBjoern A. Zeeb struct ieee80211_hw *
43086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
43096b4cac81SBjoern A. Zeeb {
43106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43126b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4313a5ae63edSBjoern A. Zeeb 	int ac;
43146b4cac81SBjoern A. Zeeb 
43156b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
43166b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
43176b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
43186b4cac81SBjoern A. Zeeb 		return (NULL);
43196b4cac81SBjoern A. Zeeb 
43206b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
43216b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4322652e22d3SBjoern A. Zeeb 
43238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
43248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4325eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
43268891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
43276b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4328a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4329a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4330a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4331a5ae63edSBjoern A. Zeeb 	}
43326b4cac81SBjoern A. Zeeb 
4333759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
4334759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4335759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4336759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4337759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
4338759a996dSBjoern A. Zeeb 
43396b4cac81SBjoern A. Zeeb 	/*
43406b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
43416b4cac81SBjoern A. Zeeb 	 * not initialized.
43426b4cac81SBjoern A. Zeeb 	 */
43436b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
43446b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4345086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
43466b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
43476b4cac81SBjoern A. Zeeb 
43486b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
43496b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
43506b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
43516b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
43526b4cac81SBjoern A. Zeeb 		return (NULL);
43536b4cac81SBjoern A. Zeeb 	}
43546b4cac81SBjoern A. Zeeb 
43556b4cac81SBjoern A. Zeeb 	IMPROVE();
43566b4cac81SBjoern A. Zeeb 
43576b4cac81SBjoern A. Zeeb 	return (hw);
43586b4cac81SBjoern A. Zeeb }
43596b4cac81SBjoern A. Zeeb 
43606b4cac81SBjoern A. Zeeb void
43616b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
43626b4cac81SBjoern A. Zeeb {
43636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4364759a996dSBjoern A. Zeeb 	struct mbuf *m;
43656b4cac81SBjoern A. Zeeb 
43666b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
43676b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
43686b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
43696b4cac81SBjoern A. Zeeb 
4370759a996dSBjoern A. Zeeb 	/*
4371759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
4372759a996dSBjoern A. Zeeb 	 */
4373759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4374759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
4375759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4376759a996dSBjoern A. Zeeb 
4377759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4378759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4379759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4380759a996dSBjoern A. Zeeb 
4381759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4382759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
4383759a996dSBjoern A. Zeeb 	while (m != NULL) {
4384759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
4385759a996dSBjoern A. Zeeb 
4386759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4387759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
4388759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
4389759a996dSBjoern A. Zeeb 
4390759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4391759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
4392759a996dSBjoern A. Zeeb 		}
4393759a996dSBjoern A. Zeeb 		m_freem(m);
4394759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
4395759a996dSBjoern A. Zeeb 	}
4396759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4397759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4398759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4399759a996dSBjoern A. Zeeb 
44006b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4401eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
44028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4403eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4404eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
44056b4cac81SBjoern A. Zeeb 	IMPROVE();
44066b4cac81SBjoern A. Zeeb }
44076b4cac81SBjoern A. Zeeb 
44086b4cac81SBjoern A. Zeeb void
44096b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
44106b4cac81SBjoern A. Zeeb {
44116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44126b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44136b4cac81SBjoern A. Zeeb 
44146b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
44156b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
44166b4cac81SBjoern A. Zeeb 
44176b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
44186b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
44196b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
44206b4cac81SBjoern A. Zeeb 
44216b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
44226b4cac81SBjoern A. Zeeb }
44236b4cac81SBjoern A. Zeeb 
44246b4cac81SBjoern A. Zeeb struct ieee80211_hw *
44256b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
44266b4cac81SBjoern A. Zeeb {
44276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44286b4cac81SBjoern A. Zeeb 
44296b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
44306b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
44316b4cac81SBjoern A. Zeeb }
44326b4cac81SBjoern A. Zeeb 
44336b4cac81SBjoern A. Zeeb static void
44346b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
44356b4cac81SBjoern A. Zeeb {
44366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44376b4cac81SBjoern A. Zeeb 
44386b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
44396b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
44406b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
44416b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
44426b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
44436b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
44446b4cac81SBjoern A. Zeeb }
44456b4cac81SBjoern A. Zeeb 
44462e183d99SBjoern A. Zeeb int
44476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
44486b4cac81SBjoern A. Zeeb {
44496b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4451c5b96b3eSBjoern A. Zeeb 	int band, i;
44526b4cac81SBjoern A. Zeeb 
44536b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
44546b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
44556b4cac81SBjoern A. Zeeb 
4456652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
4457652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4458652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
4459652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
4460652e22d3SBjoern A. Zeeb 
44616b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
44626b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
44636b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
44646b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
44656b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
44666b4cac81SBjoern A. Zeeb 		/* We take the first one. */
44676b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
44686b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
44696b4cac81SBjoern A. Zeeb 	} else {
44706b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
44716b4cac81SBjoern A. Zeeb 	}
44726b4cac81SBjoern A. Zeeb 
44733d09d310SBjoern A. Zeeb #ifdef __not_yet__
44743d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
44756b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
44763d09d310SBjoern A. Zeeb #endif
44776b4cac81SBjoern A. Zeeb 
44786b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
44796b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
44806b4cac81SBjoern A. Zeeb 
44816b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
44826b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
44836b4cac81SBjoern A. Zeeb 	ic->ic_caps =
44846b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
44856b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
44866b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
44874a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
44886b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
44894a67f1dfSBjoern A. Zeeb #endif
44906b4cac81SBjoern A. Zeeb #if 0
44916b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
44926b4cac81SBjoern A. Zeeb #endif
44936b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
44946b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
44956b4cac81SBjoern A. Zeeb 	    ;
44966b4cac81SBjoern A. Zeeb #if 0
44976b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
44986b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
44996b4cac81SBjoern A. Zeeb #endif
4500cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
4501cc4e78d5SBjoern A. Zeeb 		/*
4502cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
4503cc4e78d5SBjoern A. Zeeb 		 *
4504cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4505cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
4506cc4e78d5SBjoern A. Zeeb 		 * the flag.
4507cc4e78d5SBjoern A. Zeeb 		 */
45086b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4509a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
45106b4cac81SBjoern A. Zeeb 	}
45116b4cac81SBjoern A. Zeeb 
4512527687a9SBjoern A. Zeeb 	/*
4513527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
4514527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
4515527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
4516527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4517527687a9SBjoern A. Zeeb 	 */
4518527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
4519527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
4520527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
4521527687a9SBjoern A. Zeeb 
4522527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4523527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
4524527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
4525527687a9SBjoern A. Zeeb 		}
4526527687a9SBjoern A. Zeeb 	}
4527527687a9SBjoern A. Zeeb 
45286b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
4529b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
45306b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
45316b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
45326b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
45336b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
45346b4cac81SBjoern A. Zeeb 	}
45356b4cac81SBjoern A. Zeeb #endif
45366b4cac81SBjoern A. Zeeb 
45376b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
45386b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
45396b4cac81SBjoern A. Zeeb 
45406b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
45416b4cac81SBjoern A. Zeeb 
45426b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
45436b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
45446b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
45456b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
45466b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
45476b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
45486b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
45496b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
45506b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
45516b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
45526b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
45536b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
45546b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
45556b4cac81SBjoern A. Zeeb 
4556a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4557a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4558a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4559a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4560a486fbbdSBjoern A. Zeeb 
45616b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
45626b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
45636b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
45646b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
45656b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
45666b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
45676b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
45686b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
45696b4cac81SBjoern A. Zeeb 
45709fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45719fb91463SBjoern A. Zeeb 	lhw->ic_recv_action = ic->ic_recv_action;
45729fb91463SBjoern A. Zeeb 	ic->ic_recv_action = lkpi_ic_recv_action;
45739fb91463SBjoern A. Zeeb 	lhw->ic_send_action = ic->ic_send_action;
45749fb91463SBjoern A. Zeeb 	ic->ic_send_action = lkpi_ic_send_action;
45759fb91463SBjoern A. Zeeb 
45769fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
45779fb91463SBjoern A. Zeeb 	ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
45789fb91463SBjoern A. Zeeb 
45799fb91463SBjoern A. Zeeb 	lhw->ic_addba_request = ic->ic_addba_request;
45809fb91463SBjoern A. Zeeb 	ic->ic_addba_request = lkpi_ic_addba_request;
45819fb91463SBjoern A. Zeeb 	lhw->ic_addba_response = ic->ic_addba_response;
45829fb91463SBjoern A. Zeeb 	ic->ic_addba_response = lkpi_ic_addba_response;
45839fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop = ic->ic_addba_stop;
45849fb91463SBjoern A. Zeeb 	ic->ic_addba_stop = lkpi_ic_addba_stop;
45859fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
45869fb91463SBjoern A. Zeeb 	ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
45879fb91463SBjoern A. Zeeb 
45889fb91463SBjoern A. Zeeb 	lhw->ic_bar_response = ic->ic_bar_response;
45899fb91463SBjoern A. Zeeb 	ic->ic_bar_response = lkpi_ic_bar_response;
45909fb91463SBjoern A. Zeeb 
45919fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
45929fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
45939fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
45949fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
45959fb91463SBjoern A. Zeeb #endif
45969fb91463SBjoern A. Zeeb 
45976b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
45986b4cac81SBjoern A. Zeeb 
4599c5b96b3eSBjoern A. Zeeb 	/*
4600c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
4601c5b96b3eSBjoern A. Zeeb 	 * expect one.
4602d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
4603d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4604c5b96b3eSBjoern A. Zeeb 	 */
4605d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
4606f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4607c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
4608c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
4609c5b96b3eSBjoern A. Zeeb 
4610c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4611c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4612c5b96b3eSBjoern A. Zeeb 			continue;
4613c5b96b3eSBjoern A. Zeeb 
4614d9945d78SBjoern A. Zeeb 		lhw->supbands++;
4615d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4616d9945d78SBjoern A. Zeeb 
4617f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
4618f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
4619f454a4a1SBjoern A. Zeeb 			continue;
4620f454a4a1SBjoern A. Zeeb 
4621c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
4622c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4623c5b96b3eSBjoern A. Zeeb 
4624c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4625c5b96b3eSBjoern A. Zeeb 				continue;
4626c5b96b3eSBjoern A. Zeeb 
46274a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
46289fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46299fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
46309fb91463SBjoern A. Zeeb #endif
4631c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
4632c5b96b3eSBjoern A. Zeeb 			break;
4633c5b96b3eSBjoern A. Zeeb 		}
4634c5b96b3eSBjoern A. Zeeb 	}
4635c5b96b3eSBjoern A. Zeeb 
4636d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4637d9945d78SBjoern A. Zeeb 
4638d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
4639d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4640d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4641d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4642d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4643d9945d78SBjoern A. Zeeb 	}
4644d9945d78SBjoern A. Zeeb 
4645d9945d78SBjoern A. Zeeb 	/*
4646d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
4647d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
4648d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
4649d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
4650d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
4651d9945d78SBjoern A. Zeeb 	 */
4652d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4653d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4654d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
465578ca45dfSBjoern A. Zeeb 
465678ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4657d9945d78SBjoern A. Zeeb 		/*
465878ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
465978ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
466078ca45dfSBjoern A. Zeeb 		 * space in.
4661d9945d78SBjoern A. Zeeb 		 */
4662d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
466378ca45dfSBjoern A. Zeeb 	}
4664d9945d78SBjoern A. Zeeb 
46659fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
46669fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
46679fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
46689fb91463SBjoern A. Zeeb #endif
46699fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
46709fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
46719fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
46729fb91463SBjoern A. Zeeb #endif
46739fb91463SBjoern A. Zeeb 
4674d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
467578ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
467678ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
467778ca45dfSBjoern A. Zeeb 			goto err;
4678d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
467978ca45dfSBjoern A. Zeeb 	}
4680d9945d78SBjoern A. Zeeb 
46816b4cac81SBjoern A. Zeeb 	if (bootverbose)
46826b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
46832e183d99SBjoern A. Zeeb 
46842e183d99SBjoern A. Zeeb 	return (0);
468578ca45dfSBjoern A. Zeeb err:
468678ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
468778ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
46886b4cac81SBjoern A. Zeeb }
46896b4cac81SBjoern A. Zeeb 
46906b4cac81SBjoern A. Zeeb void
46916b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
46926b4cac81SBjoern A. Zeeb {
46936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46946b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46956b4cac81SBjoern A. Zeeb 
46966b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
46976b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
46986b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
46996b4cac81SBjoern A. Zeeb }
47006b4cac81SBjoern A. Zeeb 
47016b4cac81SBjoern A. Zeeb void
47026b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
47036b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
47046b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
47056b4cac81SBjoern A. Zeeb     void *arg)
47066b4cac81SBjoern A. Zeeb {
47076b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47086b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
47096b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
471061a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
47116b4cac81SBjoern A. Zeeb 
47126b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
47136b4cac81SBjoern A. Zeeb 
47146b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
47156b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
471661a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4717adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
47186b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
47196b4cac81SBjoern A. Zeeb 		    __func__, flags);
47206b4cac81SBjoern A. Zeeb 	}
47216b4cac81SBjoern A. Zeeb 
4722adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
47236b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
472461a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
47256b4cac81SBjoern A. Zeeb 
47266b4cac81SBjoern A. Zeeb 	if (atomic)
47278891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
47286b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
47296b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
47306b4cac81SBjoern A. Zeeb 
47316b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
47326b4cac81SBjoern A. Zeeb 
47336b4cac81SBjoern A. Zeeb 		/*
47346b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
47356b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
47366b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
47376b4cac81SBjoern A. Zeeb 		 * driver does not know about.
47386b4cac81SBjoern A. Zeeb 		 */
47396b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
47406b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
47416b4cac81SBjoern A. Zeeb 			continue;
47426b4cac81SBjoern A. Zeeb 
47436b4cac81SBjoern A. Zeeb 		/*
474461a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
474561a68e50SBjoern A. Zeeb 		 * if we haven't yet.
474661a68e50SBjoern A. Zeeb 		 */
474761a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
474861a68e50SBjoern A. Zeeb 			continue;
474961a68e50SBjoern A. Zeeb 
475061a68e50SBjoern A. Zeeb 		/*
47516b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
47526b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
47536b4cac81SBjoern A. Zeeb 		 */
47546b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
47556b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
47566b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
47576b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
47586b4cac81SBjoern A. Zeeb 	}
47596b4cac81SBjoern A. Zeeb 	if (atomic)
47608891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
47616b4cac81SBjoern A. Zeeb }
47626b4cac81SBjoern A. Zeeb 
47636b4cac81SBjoern A. Zeeb void
47646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
47656b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
47666b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
47676b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
47686b4cac81SBjoern A. Zeeb     void *arg)
47696b4cac81SBjoern A. Zeeb {
47706b4cac81SBjoern A. Zeeb 
47716b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
47726b4cac81SBjoern A. Zeeb }
47736b4cac81SBjoern A. Zeeb 
47746b4cac81SBjoern A. Zeeb void
47756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
47766b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
47776b4cac81SBjoern A. Zeeb 	void *),
47786b4cac81SBjoern A. Zeeb     void *arg)
47796b4cac81SBjoern A. Zeeb {
4780c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4781c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4782c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4783c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
47846b4cac81SBjoern A. Zeeb 
4785c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
4786c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4787c5e25798SBjoern A. Zeeb 
4788c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
4789c5e25798SBjoern A. Zeeb 
4790c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
4791c5e25798SBjoern A. Zeeb 
4792c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4793c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4794c5e25798SBjoern A. Zeeb 
4795c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4796c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
4797c5e25798SBjoern A. Zeeb 			continue;
4798c5e25798SBjoern A. Zeeb 
4799c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4800c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
4801c5e25798SBjoern A. Zeeb 			continue;
4802c5e25798SBjoern A. Zeeb 
4803d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
4804c5e25798SBjoern A. Zeeb 	}
4805c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
48066b4cac81SBjoern A. Zeeb }
48076b4cac81SBjoern A. Zeeb 
48086b4cac81SBjoern A. Zeeb void
48096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
48106b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
48116b4cac81SBjoern A. Zeeb {
48126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
48146b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
48156b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
48166b4cac81SBjoern A. Zeeb 
48176b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
48186b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
48196b4cac81SBjoern A. Zeeb 
48206b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
48216b4cac81SBjoern A. Zeeb 
48228891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
48236b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
48246b4cac81SBjoern A. Zeeb 
48256b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
48266b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
48276b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
48286b4cac81SBjoern A. Zeeb 				continue;
48296b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
48306b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
48316b4cac81SBjoern A. Zeeb 		}
48326b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
48336b4cac81SBjoern A. Zeeb 	}
48348891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
48356b4cac81SBjoern A. Zeeb }
48366b4cac81SBjoern A. Zeeb 
4837673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
4838673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4839673d62fcSBjoern A. Zeeb {
4840673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
4841673d62fcSBjoern A. Zeeb 
4842673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4843673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
4844673d62fcSBjoern A. Zeeb 	return (regd);
4845673d62fcSBjoern A. Zeeb }
4846673d62fcSBjoern A. Zeeb 
48476b4cac81SBjoern A. Zeeb int
48486b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
48496b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
48506b4cac81SBjoern A. Zeeb {
48516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48526b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48536b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
48546b4cac81SBjoern A. Zeeb 
48556b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
48566b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48576b4cac81SBjoern A. Zeeb 
48586b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
48596b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
48606b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
48616b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
48626b4cac81SBjoern A. Zeeb 	}
48636b4cac81SBjoern A. Zeeb 
48646b4cac81SBjoern A. Zeeb 	TODO();
48656b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
48666b4cac81SBjoern A. Zeeb 
48676b4cac81SBjoern A. Zeeb 	return (0);
48686b4cac81SBjoern A. Zeeb }
48696b4cac81SBjoern A. Zeeb 
48706b4cac81SBjoern A. Zeeb void
48716b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
48726b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
48736b4cac81SBjoern A. Zeeb {
48746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48756b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48766b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
48776b4cac81SBjoern A. Zeeb 
48786b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
48796b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48806b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
48816b4cac81SBjoern A. Zeeb 
48826b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
48836b4cac81SBjoern A. Zeeb 
48848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
48856b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
48866b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
4887a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
48886b4cac81SBjoern A. Zeeb 	wakeup(lhw);
48898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
48906b4cac81SBjoern A. Zeeb 
48916b4cac81SBjoern A. Zeeb 	return;
48926b4cac81SBjoern A. Zeeb }
48936b4cac81SBjoern A. Zeeb 
4894759a996dSBjoern A. Zeeb static void
4895759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
4896759a996dSBjoern A. Zeeb {
4897759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
4898759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
4899759a996dSBjoern A. Zeeb 	int ok;
4900759a996dSBjoern A. Zeeb 
4901759a996dSBjoern A. Zeeb 	ni = NULL;
4902759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4903759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
4904759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
4905759a996dSBjoern A. Zeeb 
4906759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4907759a996dSBjoern A. Zeeb 		ni = rxni->ni;
4908759a996dSBjoern A. Zeeb 	}
4909759a996dSBjoern A. Zeeb 
4910759a996dSBjoern A. Zeeb 	if (ni != NULL) {
4911759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
4912759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
4913759a996dSBjoern A. Zeeb 		if (ok < 0)
4914759a996dSBjoern A. Zeeb 			m_freem(m);
4915759a996dSBjoern A. Zeeb 	} else {
4916759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
4917759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
4918759a996dSBjoern A. Zeeb 	}
4919759a996dSBjoern A. Zeeb 
4920759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4921759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4922759a996dSBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4923759a996dSBjoern A. Zeeb #endif
4924759a996dSBjoern A. Zeeb }
4925759a996dSBjoern A. Zeeb 
4926759a996dSBjoern A. Zeeb static void
4927759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
4928759a996dSBjoern A. Zeeb {
4929759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
4930759a996dSBjoern A. Zeeb 	struct mbufq mq;
4931759a996dSBjoern A. Zeeb 	struct mbuf *m;
4932759a996dSBjoern A. Zeeb 
4933759a996dSBjoern A. Zeeb 	lhw = ctx;
4934759a996dSBjoern A. Zeeb 
4935759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4936759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4937759a996dSBjoern A. Zeeb 		printf("%s:%d lhw %p pending %d mbuf_qlen %d\n",
4938759a996dSBjoern A. Zeeb 		    __func__, __LINE__, lhw, pending, mbufq_len(&lhw->rxq));
4939759a996dSBjoern A. Zeeb #endif
4940759a996dSBjoern A. Zeeb 
4941759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
4942759a996dSBjoern A. Zeeb 
4943759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4944759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
4945759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4946759a996dSBjoern A. Zeeb 
4947759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
4948759a996dSBjoern A. Zeeb 	while (m != NULL) {
4949759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
4950759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
4951759a996dSBjoern A. Zeeb 	}
4952759a996dSBjoern A. Zeeb }
4953759a996dSBjoern A. Zeeb 
4954e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
49556b4cac81SBjoern A. Zeeb void
49566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4957e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4958e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
49596b4cac81SBjoern A. Zeeb {
49606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49616b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49626b4cac81SBjoern A. Zeeb 	struct mbuf *m;
49636b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
49646b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
49656b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
49666b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
49676b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
49686b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
49696b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49709d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
4971170acccfSBjoern A. Zeeb 	int8_t rssi;
4972c0cadd99SBjoern A. Zeeb 	bool is_beacon;
49736b4cac81SBjoern A. Zeeb 
49746b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
49756b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
49766b4cac81SBjoern A. Zeeb 		IMPROVE();
49776b4cac81SBjoern A. Zeeb 		goto err;
49786b4cac81SBjoern A. Zeeb 	}
49796b4cac81SBjoern A. Zeeb 
49806b4cac81SBjoern A. Zeeb 	/*
49816b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
49826b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
49836b4cac81SBjoern A. Zeeb 	 */
49846b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
49856b4cac81SBjoern A. Zeeb 	if (m == NULL)
49866b4cac81SBjoern A. Zeeb 		goto err;
49876b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
49886b4cac81SBjoern A. Zeeb 
49896b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
49906b4cac81SBjoern A. Zeeb 	offset = m->m_len;
49916b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
49926b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
49936b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
49946b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
49956b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
49966b4cac81SBjoern A. Zeeb 	}
49976b4cac81SBjoern A. Zeeb 
49986b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
49996b4cac81SBjoern A. Zeeb 
50006b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
5001c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
5002c0cadd99SBjoern A. Zeeb 
5003c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50049d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
50056b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
50066b4cac81SBjoern A. Zeeb 
50079d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
50086b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5009c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
50106b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
50116b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
50126b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
5013c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
50146b4cac81SBjoern A. Zeeb 
50159d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
50166b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
50176b4cac81SBjoern A. Zeeb 
50186b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
50199d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5020c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
502160970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
50226b4cac81SBjoern A. Zeeb 			__func__,
5023c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
5024c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
50256b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
50266b4cac81SBjoern A. Zeeb 			rx_status->flag,
50276b4cac81SBjoern A. Zeeb 			rx_status->freq,
50286b4cac81SBjoern A. Zeeb 			rx_status->bw,
50296b4cac81SBjoern A. Zeeb 			rx_status->encoding,
50306b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
50316b4cac81SBjoern A. Zeeb 			rx_status->band,
50326b4cac81SBjoern A. Zeeb 			rx_status->chains,
50336b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
50346b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
50356b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
503660970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
50376b4cac81SBjoern A. Zeeb 			rx_status->signal,
50386b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
50396b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
50406b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
50416b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
50426b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
50436b4cac81SBjoern A. Zeeb 			rx_status->nss,
50446b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
50456b4cac81SBjoern A. Zeeb no_trace_beacons:
50466b4cac81SBjoern A. Zeeb #endif
50476b4cac81SBjoern A. Zeeb 
50486b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
50496b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
505060970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
505160970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
50526b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
50536b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5054170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
50556b4cac81SBjoern A. Zeeb 	else
5056170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
5057170acccfSBjoern A. Zeeb 	/*
5058170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
5059170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
5060170acccfSBjoern A. Zeeb 	 */
5061170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
5062170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
50636b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
50646b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
50656b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
50666b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
50676b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
50686b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
50696b4cac81SBjoern A. Zeeb 
50706b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
50716b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
50726b4cac81SBjoern A. Zeeb 
50736b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
50746b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
50756b4cac81SBjoern A. Zeeb 
50766b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
50776b4cac81SBjoern A. Zeeb 	if (ok == 0) {
5078dbc06dd9SBjoern A. Zeeb 		m_freem(m);
50796b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
50806b4cac81SBjoern A. Zeeb 		goto err;
50816b4cac81SBjoern A. Zeeb 	}
50826b4cac81SBjoern A. Zeeb 
508358246901SBjoern A. Zeeb 	lsta = NULL;
50846b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
50856b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
50866b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
50876b4cac81SBjoern A. Zeeb 	} else {
5088c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
5089c8dafefaSBjoern A. Zeeb 
50906b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
50916b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
50926b4cac81SBjoern A. Zeeb 		if (ni != NULL)
50936b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
50946b4cac81SBjoern A. Zeeb 	}
50956b4cac81SBjoern A. Zeeb 
50966b4cac81SBjoern A. Zeeb 	if (ni != NULL)
50976b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
50986b4cac81SBjoern A. Zeeb 	else
50996b4cac81SBjoern A. Zeeb 		/*
51006b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
51016b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
51026b4cac81SBjoern A. Zeeb 		 */
51036b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
51046b4cac81SBjoern A. Zeeb 
51059d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51069d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5107c0cadd99SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5108c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5109c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
51109d9ba2b7SBjoern A. Zeeb #endif
51116b4cac81SBjoern A. Zeeb 
5112c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
5113c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
5114c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5115c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
5116c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
5117c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
5118c8dafefaSBjoern A. Zeeb 
5119c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
5120c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5121c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
5122c8dafefaSBjoern A. Zeeb 
5123c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
5124c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5125c8dafefaSBjoern A. Zeeb 
5126c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
5127c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
5128c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5129c8dafefaSBjoern A. Zeeb 		/*
5130c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
5131c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5132c8dafefaSBjoern A. Zeeb 		 */
5133c8dafefaSBjoern A. Zeeb skip_device_ts:
51349fb91463SBjoern A. Zeeb 		;
51359fb91463SBjoern A. Zeeb 	}
5136c8dafefaSBjoern A. Zeeb 
51376b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
51386b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
51396b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
51406b4cac81SBjoern A. Zeeb 
51416b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
51426b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
51436b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
51446b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
51456b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
51466b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
51476b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
51486b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
51496b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
51506b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
51516b4cac81SBjoern A. Zeeb #endif
51526b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
51536b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
51546b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
51556b4cac81SBjoern A. Zeeb 		IMPROVE();
51566b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
51576b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
51586b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
51596b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5160170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
51616b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
51626b4cac81SBjoern A. Zeeb 	}
51636b4cac81SBjoern A. Zeeb 
51646b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
51656b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
51666b4cac81SBjoern A. Zeeb 
5167e30e05d3SBjoern A. Zeeb #if 0
5168e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
5169e30e05d3SBjoern A. Zeeb 		/*
5170e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
5171e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5172e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
5173e30e05d3SBjoern A. Zeeb 		*/
5174e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5175e30e05d3SBjoern A. Zeeb 	}
5176e30e05d3SBjoern A. Zeeb #endif
5177e30e05d3SBjoern A. Zeeb 
5178759a996dSBjoern A. Zeeb 	/*
5179759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
5180759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
5181759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
5182759a996dSBjoern A. Zeeb 	 */
51836b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
5184759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5185759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5186759a996dSBjoern A. Zeeb 
5187759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5188759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5189759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5190759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5191759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
5192759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
5193759a996dSBjoern A. Zeeb 		}
51946b4cac81SBjoern A. Zeeb 	}
51956b4cac81SBjoern A. Zeeb 
5196759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5197759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
5198759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5199759a996dSBjoern A. Zeeb 		m_freem(m);
5200759a996dSBjoern A. Zeeb 		goto err;
5201759a996dSBjoern A. Zeeb 	}
5202759a996dSBjoern A. Zeeb 
5203759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
5204759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5205759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
52066b4cac81SBjoern A. Zeeb 
52076b4cac81SBjoern A. Zeeb 	IMPROVE();
52086b4cac81SBjoern A. Zeeb 
52096b4cac81SBjoern A. Zeeb err:
52106b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
52116b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
52126b4cac81SBjoern A. Zeeb }
52136b4cac81SBjoern A. Zeeb 
52146b4cac81SBjoern A. Zeeb uint8_t
5215ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
52166b4cac81SBjoern A. Zeeb {
52176b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
5218ec190d91SBjoern A. Zeeb 	uint8_t tid;
5219ec190d91SBjoern A. Zeeb 
5220ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
5221ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5222ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5223ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
52246b4cac81SBjoern A. Zeeb 
52256b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
5226ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
5227ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5228ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5229ec190d91SBjoern A. Zeeb 
5230ec190d91SBjoern A. Zeeb 	return (tid);
52316b4cac81SBjoern A. Zeeb }
52326b4cac81SBjoern A. Zeeb 
52336b4cac81SBjoern A. Zeeb struct wiphy *
52346b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
52356b4cac81SBjoern A. Zeeb {
52366b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
52376b4cac81SBjoern A. Zeeb 
52386b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
52396b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
52406b4cac81SBjoern A. Zeeb 		return (NULL);
52416b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
52426b4cac81SBjoern A. Zeeb 
52436b4cac81SBjoern A. Zeeb 	/* XXX TODO */
52446b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
52456b4cac81SBjoern A. Zeeb }
52466b4cac81SBjoern A. Zeeb 
52476b4cac81SBjoern A. Zeeb void
52486b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
52496b4cac81SBjoern A. Zeeb {
52506b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
52516b4cac81SBjoern A. Zeeb 
52526b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
52536b4cac81SBjoern A. Zeeb 		return;
52546b4cac81SBjoern A. Zeeb 
52556b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
52566b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
52576b4cac81SBjoern A. Zeeb }
52586b4cac81SBjoern A. Zeeb 
52596b4cac81SBjoern A. Zeeb uint32_t
52606b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
52616b4cac81SBjoern A. Zeeb     enum nl80211_band band)
52626b4cac81SBjoern A. Zeeb {
52636b4cac81SBjoern A. Zeeb 
52646b4cac81SBjoern A. Zeeb 	switch (band) {
52656b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
52666b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
52676b4cac81SBjoern A. Zeeb 		break;
52686b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
52696b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
52706b4cac81SBjoern A. Zeeb 		break;
52716b4cac81SBjoern A. Zeeb 	default:
52726b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
52736b4cac81SBjoern A. Zeeb 		break;
52746b4cac81SBjoern A. Zeeb 	}
52756b4cac81SBjoern A. Zeeb 
52766b4cac81SBjoern A. Zeeb 	return (0);
52776b4cac81SBjoern A. Zeeb }
52786b4cac81SBjoern A. Zeeb 
52796b4cac81SBjoern A. Zeeb uint32_t
52806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
52816b4cac81SBjoern A. Zeeb {
52826b4cac81SBjoern A. Zeeb 
52836b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
52846b4cac81SBjoern A. Zeeb }
52856b4cac81SBjoern A. Zeeb 
5286ac867c20SBjoern A. Zeeb #if 0
52876b4cac81SBjoern A. Zeeb static struct lkpi_sta *
52886b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
52896b4cac81SBjoern A. Zeeb {
52906b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
52916b4cac81SBjoern A. Zeeb 
52926b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
52936b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
52946b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
52956b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
52966b4cac81SBjoern A. Zeeb 			return (lsta);
52976b4cac81SBjoern A. Zeeb 		}
52986b4cac81SBjoern A. Zeeb 	}
52996b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
53006b4cac81SBjoern A. Zeeb 
53016b4cac81SBjoern A. Zeeb 	return (NULL);
53026b4cac81SBjoern A. Zeeb }
5303ac867c20SBjoern A. Zeeb #endif
53046b4cac81SBjoern A. Zeeb 
53056b4cac81SBjoern A. Zeeb struct ieee80211_sta *
53066b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
53076b4cac81SBjoern A. Zeeb {
53086b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53096b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
53106b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
53116b4cac81SBjoern A. Zeeb 
53126b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
53136b4cac81SBjoern A. Zeeb 
53146b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
53156b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
53166b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
53176b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
53186b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
53196b4cac81SBjoern A. Zeeb 			return (sta);
53206b4cac81SBjoern A. Zeeb 		}
53216b4cac81SBjoern A. Zeeb 	}
53226b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
53236b4cac81SBjoern A. Zeeb 	return (NULL);
53246b4cac81SBjoern A. Zeeb }
53256b4cac81SBjoern A. Zeeb 
53266b4cac81SBjoern A. Zeeb struct ieee80211_sta *
53272e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
53282e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
53296b4cac81SBjoern A. Zeeb {
53306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53316b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53326b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
53336b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
53346b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
53356b4cac81SBjoern A. Zeeb 
53366b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
53376b4cac81SBjoern A. Zeeb 	sta = NULL;
53386b4cac81SBjoern A. Zeeb 
53398891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
53406b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
53416b4cac81SBjoern A. Zeeb 
53426b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
53436b4cac81SBjoern A. Zeeb 
53446b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
53456b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
53466b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
53476b4cac81SBjoern A. Zeeb 			continue;
53486b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
53496b4cac81SBjoern A. Zeeb 		if (sta != NULL)
53506b4cac81SBjoern A. Zeeb 			break;
53516b4cac81SBjoern A. Zeeb 	}
53528891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
53536b4cac81SBjoern A. Zeeb 
53546b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
53556b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
53566b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
53576b4cac81SBjoern A. Zeeb 			return (NULL);
53586b4cac81SBjoern A. Zeeb 	}
53596b4cac81SBjoern A. Zeeb 
53606b4cac81SBjoern A. Zeeb 	return (sta);
53616b4cac81SBjoern A. Zeeb }
53626b4cac81SBjoern A. Zeeb 
53636b4cac81SBjoern A. Zeeb struct sk_buff *
53646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
53656b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
53666b4cac81SBjoern A. Zeeb {
53676b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
53680cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53696b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
53706b4cac81SBjoern A. Zeeb 
53710cbcfa19SBjoern A. Zeeb 	skb = NULL;
53726b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
53736b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
53746b4cac81SBjoern A. Zeeb 
53750cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
53760cbcfa19SBjoern A. Zeeb 		goto stopped;
53770cbcfa19SBjoern A. Zeeb 
53780cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
53790cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
53800cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
53810cbcfa19SBjoern A. Zeeb 		goto stopped;
53820cbcfa19SBjoern A. Zeeb 	}
53830cbcfa19SBjoern A. Zeeb 
5384eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
5385eac3646fSBjoern A. Zeeb 
5386eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
53876b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
5388eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
53896b4cac81SBjoern A. Zeeb 
53900cbcfa19SBjoern A. Zeeb stopped:
53916b4cac81SBjoern A. Zeeb 	return (skb);
53926b4cac81SBjoern A. Zeeb }
53936b4cac81SBjoern A. Zeeb 
53946b4cac81SBjoern A. Zeeb void
53956b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
539686220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
53976b4cac81SBjoern A. Zeeb {
53986b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
53996b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
540086220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
54016b4cac81SBjoern A. Zeeb 
54026b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
54036b4cac81SBjoern A. Zeeb 
54046b4cac81SBjoern A. Zeeb 	fc = bc = 0;
5405eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
54066b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
54076b4cac81SBjoern A. Zeeb 		fc++;
54086b4cac81SBjoern A. Zeeb 		bc += skb->len;
54096b4cac81SBjoern A. Zeeb 	}
5410eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
54116b4cac81SBjoern A. Zeeb 	if (frame_cnt)
54126b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
54136b4cac81SBjoern A. Zeeb 	if (byte_cnt)
54146b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
54156b4cac81SBjoern A. Zeeb 
54166b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
54176b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
54186b4cac81SBjoern A. Zeeb 	IMPROVE();
54196b4cac81SBjoern A. Zeeb }
54206b4cac81SBjoern A. Zeeb 
54216b4cac81SBjoern A. Zeeb /*
54226b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
54236b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
54246b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
54256b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
54266b4cac81SBjoern A. Zeeb  */
5427a8397571SBjoern A. Zeeb static void
5428a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
54296b4cac81SBjoern A. Zeeb     int status)
54306b4cac81SBjoern A. Zeeb {
54316b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
54326b4cac81SBjoern A. Zeeb 	struct mbuf *m;
54336b4cac81SBjoern A. Zeeb 
54346b4cac81SBjoern A. Zeeb 	m = skb->m;
54356b4cac81SBjoern A. Zeeb 	skb->m = NULL;
54366b4cac81SBjoern A. Zeeb 
54376b4cac81SBjoern A. Zeeb 	if (m != NULL) {
54386b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
54396b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
54406b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
54416b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
54426b4cac81SBjoern A. Zeeb 	}
5443a8397571SBjoern A. Zeeb }
54446b4cac81SBjoern A. Zeeb 
5445a8397571SBjoern A. Zeeb void
5446a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5447a8397571SBjoern A. Zeeb     int status)
5448a8397571SBjoern A. Zeeb {
5449a8397571SBjoern A. Zeeb 
5450a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
54516b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
54526b4cac81SBjoern A. Zeeb }
54536b4cac81SBjoern A. Zeeb 
5454383b3e8fSBjoern A. Zeeb void
5455a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5456a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
5457383b3e8fSBjoern A. Zeeb {
5458a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
5459383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
5460383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
5461383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
5462383b3e8fSBjoern A. Zeeb 	int status;
5463383b3e8fSBjoern A. Zeeb 
5464a8397571SBjoern A. Zeeb 	skb = txstat->skb;
5465383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
5466383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
5467383b3e8fSBjoern A. Zeeb 
5468383b3e8fSBjoern A. Zeeb 		m = skb->m;
5469383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
5470383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
5471383b3e8fSBjoern A. Zeeb 	} else {
5472383b3e8fSBjoern A. Zeeb 		ni = NULL;
5473383b3e8fSBjoern A. Zeeb 	}
5474383b3e8fSBjoern A. Zeeb 
5475a8397571SBjoern A. Zeeb 	info = txstat->info;
5476383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5477383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
5478383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5479383b3e8fSBjoern A. Zeeb 	} else {
5480383b3e8fSBjoern A. Zeeb 		status = 1;
5481383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5482383b3e8fSBjoern A. Zeeb 	}
5483383b3e8fSBjoern A. Zeeb 
5484383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
5485e2c5ab09SJohn Baldwin 		int ridx __unused;
5486383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5487383b3e8fSBjoern A. Zeeb 		int old_rate;
5488383b3e8fSBjoern A. Zeeb 
5489383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5490383b3e8fSBjoern A. Zeeb #endif
5491383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
5492383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5493383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
5494383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5495383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5496383b3e8fSBjoern A. Zeeb 		}
5497383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
5498a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5499383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
5500383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5501383b3e8fSBjoern A. Zeeb #endif
5502adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5503383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5504383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5505383b3e8fSBjoern A. Zeeb 		}
5506383b3e8fSBjoern A. Zeeb 
5507383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5508383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
5509383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5510383b3e8fSBjoern A. Zeeb 
5511383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5512383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5513383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5514383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
5515383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5516383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
5517383b3e8fSBjoern A. Zeeb 		}
5518383b3e8fSBjoern A. Zeeb #endif
5519383b3e8fSBjoern A. Zeeb 	}
5520383b3e8fSBjoern A. Zeeb 
5521383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5522383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5523383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5524383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
5525383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5526383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5527adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
5528383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
5529383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
5530383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
5531383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
5532383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
5533383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
5534383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
5535383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
5536383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
5537383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
5538383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
5539383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
5540383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
5541adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
5542383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
5543383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
5544383b3e8fSBjoern A. Zeeb #endif
5545383b3e8fSBjoern A. Zeeb 
5546a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
5547a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5548a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
5549a8397571SBjoern A. Zeeb 	} else {
5550383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5551383b3e8fSBjoern A. Zeeb 	}
5552a8397571SBjoern A. Zeeb }
5553a8397571SBjoern A. Zeeb 
5554a8397571SBjoern A. Zeeb void
5555a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5556a8397571SBjoern A. Zeeb {
5557a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
5558a8397571SBjoern A. Zeeb 
5559a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
5560a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
5561a8397571SBjoern A. Zeeb 	status.skb = skb;
5562a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
5563a8397571SBjoern A. Zeeb 
5564a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
5565a8397571SBjoern A. Zeeb }
5566383b3e8fSBjoern A. Zeeb 
55676b4cac81SBjoern A. Zeeb /*
55686b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
55696b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
55706b4cac81SBjoern A. Zeeb  * mbufs this should go away.
55716b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
55726b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
55736b4cac81SBjoern A. Zeeb  */
55746b4cac81SBjoern A. Zeeb static void
55756b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
55766b4cac81SBjoern A. Zeeb {
55776b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
55786b4cac81SBjoern A. Zeeb 	struct mbuf *m;
55796b4cac81SBjoern A. Zeeb 
55806b4cac81SBjoern A. Zeeb 	if (p == NULL)
55816b4cac81SBjoern A. Zeeb 		return;
55826b4cac81SBjoern A. Zeeb 
55836b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
55846b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
55856b4cac81SBjoern A. Zeeb 
55866b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
55876b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
55886b4cac81SBjoern A. Zeeb 	if (ni != NULL)
55896b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
55906b4cac81SBjoern A. Zeeb 	m_freem(m);
55916b4cac81SBjoern A. Zeeb }
55926b4cac81SBjoern A. Zeeb 
55936b4cac81SBjoern A. Zeeb void
55946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
55956b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
55966b4cac81SBjoern A. Zeeb {
55976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55986b4cac81SBjoern A. Zeeb 
55996b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
56006b4cac81SBjoern A. Zeeb 	IMPROVE();
56016b4cac81SBjoern A. Zeeb 
56026b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56036b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
56046b4cac81SBjoern A. Zeeb }
56056b4cac81SBjoern A. Zeeb 
56066b4cac81SBjoern A. Zeeb void
56076b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
56086b4cac81SBjoern A. Zeeb     struct work_struct *w)
56096b4cac81SBjoern A. Zeeb {
56106b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56116b4cac81SBjoern A. Zeeb 
56126b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
56136b4cac81SBjoern A. Zeeb 	IMPROVE();
56146b4cac81SBjoern A. Zeeb 
56156b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56166b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
56176b4cac81SBjoern A. Zeeb }
56186b4cac81SBjoern A. Zeeb 
56196b4cac81SBjoern A. Zeeb struct sk_buff *
5620ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5621ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5622ade774b1SBjoern A. Zeeb {
5623ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
5624ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
5625ade774b1SBjoern A. Zeeb 	uint8_t *p;
5626ade774b1SBjoern A. Zeeb 	size_t len;
5627ade774b1SBjoern A. Zeeb 
5628ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
5629ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
5630ade774b1SBjoern A. Zeeb 
5631ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5632ade774b1SBjoern A. Zeeb 	if (skb == NULL)
5633ade774b1SBjoern A. Zeeb 		return (NULL);
5634ade774b1SBjoern A. Zeeb 
5635ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
5636ade774b1SBjoern A. Zeeb 
5637ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
5638ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5639ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5640ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5641ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5642ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5643ade774b1SBjoern A. Zeeb 
5644ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
5645ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
5646ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
5647ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
5648ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
5649ade774b1SBjoern A. Zeeb 
5650ade774b1SBjoern A. Zeeb 	return (skb);
5651ade774b1SBjoern A. Zeeb }
5652ade774b1SBjoern A. Zeeb 
5653ade774b1SBjoern A. Zeeb struct sk_buff *
56546b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
56556b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
56566b4cac81SBjoern A. Zeeb {
56576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56586b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
56596b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
56606b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
56616b4cac81SBjoern A. Zeeb 	uint16_t v;
56626b4cac81SBjoern A. Zeeb 
56636b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
56646b4cac81SBjoern A. Zeeb 	if (skb == NULL)
56656b4cac81SBjoern A. Zeeb 		return (NULL);
56666b4cac81SBjoern A. Zeeb 
56676b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
56686b4cac81SBjoern A. Zeeb 
56696b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
56706b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
56716b4cac81SBjoern A. Zeeb 
56726b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
56736b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
56746b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5675616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
56766b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
56776b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
56786b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
56796b4cac81SBjoern A. Zeeb 
56806b4cac81SBjoern A. Zeeb 	return (skb);
56816b4cac81SBjoern A. Zeeb }
56826b4cac81SBjoern A. Zeeb 
56836b4cac81SBjoern A. Zeeb struct sk_buff *
56846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5685adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
56866b4cac81SBjoern A. Zeeb {
56876b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56886b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
56896b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
56906b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
56916b4cac81SBjoern A. Zeeb 
5692adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
5693adff403fSBjoern A. Zeeb 
56946b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
56956b4cac81SBjoern A. Zeeb 	if (skb == NULL)
56966b4cac81SBjoern A. Zeeb 		return (NULL);
56976b4cac81SBjoern A. Zeeb 
56986b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
56996b4cac81SBjoern A. Zeeb 
57006b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
57016b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
57026b4cac81SBjoern A. Zeeb 
57036b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
57046b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
57056b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
57066b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
57076b4cac81SBjoern A. Zeeb 
57086b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
57096b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
57106b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
57116b4cac81SBjoern A. Zeeb 
57126b4cac81SBjoern A. Zeeb 	return (skb);
57136b4cac81SBjoern A. Zeeb }
57146b4cac81SBjoern A. Zeeb 
57156b4cac81SBjoern A. Zeeb struct wireless_dev *
57166b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
57176b4cac81SBjoern A. Zeeb {
57186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57196b4cac81SBjoern A. Zeeb 
57206b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
57216b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
57226b4cac81SBjoern A. Zeeb }
57236b4cac81SBjoern A. Zeeb 
57246b4cac81SBjoern A. Zeeb void
57256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
57266b4cac81SBjoern A. Zeeb {
57276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57286b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
57296b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
57306b4cac81SBjoern A. Zeeb 	int arg;
57316b4cac81SBjoern A. Zeeb 
57326b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
57336b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
57346b4cac81SBjoern A. Zeeb 
57356b4cac81SBjoern A. Zeeb 	/*
5736f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
57376b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
57386b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
57396b4cac81SBjoern A. Zeeb 	 */
5740f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
5741bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
57426b4cac81SBjoern A. Zeeb 
5743018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5744018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
57456b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
57466b4cac81SBjoern A. Zeeb }
57476b4cac81SBjoern A. Zeeb 
5748bb81db90SBjoern A. Zeeb void
5749bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
5750bb81db90SBjoern A. Zeeb {
5751bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5752bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
5753bb81db90SBjoern A. Zeeb 
5754bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
5755bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
5756bb81db90SBjoern A. Zeeb 
5757bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5758bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
57593540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
5760bb81db90SBjoern A. Zeeb }
5761bb81db90SBjoern A. Zeeb 
57625edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
57635edde07cSBjoern A. Zeeb 
57645a9a0d78SBjoern A. Zeeb void
57655a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
57665a9a0d78SBjoern A. Zeeb {
57675a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57685a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57695a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
57705a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
57715a9a0d78SBjoern A. Zeeb 
57725a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
57735a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
57745a9a0d78SBjoern A. Zeeb 
57755a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
57765a9a0d78SBjoern A. Zeeb 
57775a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
57785a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
57795a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
57805a9a0d78SBjoern A. Zeeb 	else
57815a9a0d78SBjoern A. Zeeb 		ac_count = 1;
57825a9a0d78SBjoern A. Zeeb 
57835a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
57845a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
57855a9a0d78SBjoern A. Zeeb 
57865a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
57875a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
57885a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
57895a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
57900d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
57915a9a0d78SBjoern A. Zeeb 				/*
57925a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
57935a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
57945a9a0d78SBjoern A. Zeeb 				 */
57950d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
57960d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
57975a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
57985a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
57995a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
58005a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
58010d2cb6a6SBjoern A. Zeeb #endif
58025a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
58035a9a0d78SBjoern A. Zeeb 			}
58045a9a0d78SBjoern A. Zeeb 		}
58055a9a0d78SBjoern A. Zeeb 	}
58065a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58075a9a0d78SBjoern A. Zeeb }
58085a9a0d78SBjoern A. Zeeb 
58095a9a0d78SBjoern A. Zeeb void
58105a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
58115a9a0d78SBjoern A. Zeeb {
58125a9a0d78SBjoern A. Zeeb 	int i;
58135a9a0d78SBjoern A. Zeeb 
58145a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
58155a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
58165a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
58175a9a0d78SBjoern A. Zeeb }
58185a9a0d78SBjoern A. Zeeb 
58195a9a0d78SBjoern A. Zeeb 
58205a9a0d78SBjoern A. Zeeb static void
58215a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
58225a9a0d78SBjoern A. Zeeb {
58235a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58245a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58255a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
58265a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
58275a9a0d78SBjoern A. Zeeb 
58285a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
58295a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
58305a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
58315a9a0d78SBjoern A. Zeeb 	else
58325a9a0d78SBjoern A. Zeeb 		ac_count = 1;
58335a9a0d78SBjoern A. Zeeb 
58345a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
58355a9a0d78SBjoern A. Zeeb 
58365a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
58375a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
58385a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58395a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
58405a9a0d78SBjoern A. Zeeb 
58415a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
58425a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
58435a9a0d78SBjoern A. Zeeb 
58445a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
58455a9a0d78SBjoern A. Zeeb 
58465a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
58475a9a0d78SBjoern A. Zeeb 
58480d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
58495a9a0d78SBjoern A. Zeeb 				/*
58505a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
58515a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
58525a9a0d78SBjoern A. Zeeb 				 */
58530d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
58540d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
58555a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
58565a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
58575a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
58585a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
58590d2cb6a6SBjoern A. Zeeb #endif
58605a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
58615a9a0d78SBjoern A. Zeeb 
58625a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
58635a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
58645a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
58655a9a0d78SBjoern A. Zeeb 
58665a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
58675a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
58685a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
58695a9a0d78SBjoern A. Zeeb 
58705a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
58715a9a0d78SBjoern A. Zeeb 							continue;
58725a9a0d78SBjoern A. Zeeb 
58735a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
58745a9a0d78SBjoern A. Zeeb 							continue;
58755a9a0d78SBjoern A. Zeeb 
58765a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
58775a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
58785a9a0d78SBjoern A. Zeeb 							continue;
58795a9a0d78SBjoern A. Zeeb 
58805a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
58815a9a0d78SBjoern A. Zeeb 
58825a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
58835a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
58845a9a0d78SBjoern A. Zeeb 					}
58855a9a0d78SBjoern A. Zeeb 				}
58865a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
58875a9a0d78SBjoern A. Zeeb 			}
58885a9a0d78SBjoern A. Zeeb 		}
58895a9a0d78SBjoern A. Zeeb 	}
58905a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58915a9a0d78SBjoern A. Zeeb }
58925a9a0d78SBjoern A. Zeeb 
58935a9a0d78SBjoern A. Zeeb void
58945a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
58955a9a0d78SBjoern A. Zeeb {
58965a9a0d78SBjoern A. Zeeb 	int i;
58975a9a0d78SBjoern A. Zeeb 
58985a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
58995a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
59005a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
59015a9a0d78SBjoern A. Zeeb }
59025a9a0d78SBjoern A. Zeeb 
59035a9a0d78SBjoern A. Zeeb void
59045a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
59055a9a0d78SBjoern A. Zeeb {
59065a9a0d78SBjoern A. Zeeb 
59075a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
59085a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
59095a9a0d78SBjoern A. Zeeb 
59105a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
59115a9a0d78SBjoern A. Zeeb }
59125a9a0d78SBjoern A. Zeeb 
59135a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
59145a9a0d78SBjoern A. Zeeb void
59155a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
59165a9a0d78SBjoern A. Zeeb {
59175a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59185a9a0d78SBjoern A. Zeeb 
59195a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59205a9a0d78SBjoern A. Zeeb 
59215a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
59225a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
59235a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
59245a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
59255a9a0d78SBjoern A. Zeeb }
59265a9a0d78SBjoern A. Zeeb 
59275a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
59285a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
59295a9a0d78SBjoern A. Zeeb {
59305a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59315a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
59325a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
59335a9a0d78SBjoern A. Zeeb 
59345a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59355a9a0d78SBjoern A. Zeeb 	txq = NULL;
59365a9a0d78SBjoern A. Zeeb 
59375a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
59385a9a0d78SBjoern A. Zeeb 
59395a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
59405a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
59415a9a0d78SBjoern A. Zeeb 		goto out;
59425a9a0d78SBjoern A. Zeeb 
59435a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
59445a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
59455a9a0d78SBjoern A. Zeeb 		goto out;
59465a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
59475a9a0d78SBjoern A. Zeeb 		goto out;
59485a9a0d78SBjoern A. Zeeb 
59495a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
59505a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
59515a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
59525a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
59535a9a0d78SBjoern A. Zeeb 
59545a9a0d78SBjoern A. Zeeb out:
59555a9a0d78SBjoern A. Zeeb 	return (txq);
59565a9a0d78SBjoern A. Zeeb }
59575a9a0d78SBjoern A. Zeeb 
59585a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
59595a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
59605a9a0d78SBjoern A. Zeeb {
59615a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59625a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
5963eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
59645a9a0d78SBjoern A. Zeeb 
59655a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
59665a9a0d78SBjoern A. Zeeb 
59675a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
59685a9a0d78SBjoern A. Zeeb 
59695a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
5970eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
5971eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
5972eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5973eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
59745a9a0d78SBjoern A. Zeeb 		goto out;
59755a9a0d78SBjoern A. Zeeb 
59765a9a0d78SBjoern A. Zeeb 	/* Make sure we do not double-schedule. */
59775a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_entry.tqe_next != NULL)
59785a9a0d78SBjoern A. Zeeb 		goto out;
59795a9a0d78SBjoern A. Zeeb 
59805a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59815a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
59825a9a0d78SBjoern A. Zeeb out:
59835a9a0d78SBjoern A. Zeeb 	return;
59845a9a0d78SBjoern A. Zeeb }
59855a9a0d78SBjoern A. Zeeb 
5986eac3646fSBjoern A. Zeeb void
5987eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
5988eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
5989eac3646fSBjoern A. Zeeb {
5990eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5991eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
5992eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
5993eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
5994eac3646fSBjoern A. Zeeb 
5995eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5996eac3646fSBjoern A. Zeeb 
5997eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
5998eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
5999eac3646fSBjoern A. Zeeb 	do {
6000eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
6001eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
6002eac3646fSBjoern A. Zeeb 			break;
6003eac3646fSBjoern A. Zeeb 
6004eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
6005eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
6006eac3646fSBjoern A. Zeeb 		do {
6007eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6008eac3646fSBjoern A. Zeeb 			if (skb == NULL)
6009eac3646fSBjoern A. Zeeb 				break;
6010eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
6011eac3646fSBjoern A. Zeeb 		} while(1);
6012eac3646fSBjoern A. Zeeb 
6013eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
6014eac3646fSBjoern A. Zeeb 	} while (1);
6015eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
6016eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6017eac3646fSBjoern A. Zeeb }
6018eac3646fSBjoern A. Zeeb 
60195a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
60205a9a0d78SBjoern A. Zeeb 
60215edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
60225edde07cSBjoern A. Zeeb 	u_int refcnt;
60235edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
60245edde07cSBjoern A. Zeeb };
60255edde07cSBjoern A. Zeeb 
60265edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
60275edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
60285edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
60295edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
60305edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
60315edde07cSBjoern A. Zeeb 	size_t ssid_len;
60325edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
60335edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
60345edde07cSBjoern A. Zeeb 
60355edde07cSBjoern A. Zeeb 	/*
60365edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
60375edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
60385edde07cSBjoern A. Zeeb 	 */
60395edde07cSBjoern A. Zeeb 	bool match;
60405edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
60415edde07cSBjoern A. Zeeb };
60425edde07cSBjoern A. Zeeb 
60435edde07cSBjoern A. Zeeb static void
60445edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
60455edde07cSBjoern A. Zeeb {
60465edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
60475edde07cSBjoern A. Zeeb 	size_t ielen;
60485edde07cSBjoern A. Zeeb 
60495edde07cSBjoern A. Zeeb 	lookup = arg;
60505edde07cSBjoern A. Zeeb 
60515edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
60525edde07cSBjoern A. Zeeb 	if (lookup->match)
60535edde07cSBjoern A. Zeeb 		return;
60545edde07cSBjoern A. Zeeb 
60555edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
60565edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
60575edde07cSBjoern A. Zeeb 		return;
60585edde07cSBjoern A. Zeeb 
60595edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
60605edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
60615edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
60625edde07cSBjoern A. Zeeb 		return;
60635edde07cSBjoern A. Zeeb 	}
60645edde07cSBjoern A. Zeeb 
60655edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
60665edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
60675edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
60685edde07cSBjoern A. Zeeb 		return;
60695edde07cSBjoern A. Zeeb 	}
60705edde07cSBjoern A. Zeeb 
60715edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
60725edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
60735edde07cSBjoern A. Zeeb 
60745edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
60755edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
60765edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
60775edde07cSBjoern A. Zeeb 			return;
60785edde07cSBjoern A. Zeeb 	}
60795edde07cSBjoern A. Zeeb 
60805edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
60815edde07cSBjoern A. Zeeb 		return;
60825edde07cSBjoern A. Zeeb 
60835edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
60845edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
60855edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
60865edde07cSBjoern A. Zeeb 			return;
60875edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
60885edde07cSBjoern A. Zeeb 			return;
60895edde07cSBjoern A. Zeeb 	}
60905edde07cSBjoern A. Zeeb 
60915edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
60925edde07cSBjoern A. Zeeb 
60935edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
60945edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
60955edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
60965edde07cSBjoern A. Zeeb 		return;
60975edde07cSBjoern A. Zeeb 
60985edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
60995edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
61005edde07cSBjoern A. Zeeb 	if (ielen)
61015edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
61025edde07cSBjoern A. Zeeb 
61035edde07cSBjoern A. Zeeb 	lookup->match = true;
61045edde07cSBjoern A. Zeeb }
61055edde07cSBjoern A. Zeeb 
61065edde07cSBjoern A. Zeeb struct cfg80211_bss *
61075edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
61085edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
61095edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
61105edde07cSBjoern A. Zeeb {
61115edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
61125edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
61135edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
61145edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
61155edde07cSBjoern A. Zeeb 
61165edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
61175edde07cSBjoern A. Zeeb 
61185edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
61195edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
61205edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
61215edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
61225edde07cSBjoern A. Zeeb 		return (NULL);
61235edde07cSBjoern A. Zeeb 	}
61245edde07cSBjoern A. Zeeb 
61255edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
61265edde07cSBjoern A. Zeeb 	lookup.chan = chan;
61275edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
61285edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
61295edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
61305edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
61315edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
61325edde07cSBjoern A. Zeeb 	lookup.match = false;
61335edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
61345edde07cSBjoern A. Zeeb 
61355edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
61365edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
61375edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
61385edde07cSBjoern A. Zeeb 	if (!lookup.match) {
61395edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
61405edde07cSBjoern A. Zeeb 		return (NULL);
61415edde07cSBjoern A. Zeeb 	}
61425edde07cSBjoern A. Zeeb 
61435edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
61445edde07cSBjoern A. Zeeb 	return (&lbss->bss);
61455edde07cSBjoern A. Zeeb }
61465edde07cSBjoern A. Zeeb 
61475edde07cSBjoern A. Zeeb void
61485edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
61495edde07cSBjoern A. Zeeb {
61505edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
61515edde07cSBjoern A. Zeeb 
61525edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
61535edde07cSBjoern A. Zeeb 
61545edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
61555edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
61565edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
61575edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
61585edde07cSBjoern A. Zeeb 	}
61595edde07cSBjoern A. Zeeb }
61605edde07cSBjoern A. Zeeb 
61615edde07cSBjoern A. Zeeb void
61625edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
61635edde07cSBjoern A. Zeeb {
61645edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
61655edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
61665edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
61675edde07cSBjoern A. Zeeb 
61685edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
61695edde07cSBjoern A. Zeeb 	ic = lhw->ic;
61705edde07cSBjoern A. Zeeb 
61715edde07cSBjoern A. Zeeb 	/*
61725edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
61735edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
61745edde07cSBjoern A. Zeeb 	 */
61755edde07cSBjoern A. Zeeb 	if (ic == NULL ||
61765edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
61775edde07cSBjoern A. Zeeb 		return;
61785edde07cSBjoern A. Zeeb 
61795edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
61805edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
61815edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
61825edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
61835edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
61845edde07cSBjoern A. Zeeb }
61855edde07cSBjoern A. Zeeb 
61865edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
61875edde07cSBjoern A. Zeeb 
61886b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
61896b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
61906b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6191