xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision d3ef7fb459ff924911e5276db0c04b13cd8074d9)
16b4cac81SBjoern A. Zeeb /*-
22e183d99SBjoern A. Zeeb  * Copyright (c) 2020-2022 The FreeBSD Foundation
3086be6a8SBjoern A. Zeeb  * Copyright (c) 2020-2022 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
426b4cac81SBjoern A. Zeeb #include <sys/cdefs.h>
436b4cac81SBjoern A. Zeeb __FBSDID("$FreeBSD$");
446b4cac81SBjoern A. Zeeb 
456b4cac81SBjoern A. Zeeb #include <sys/param.h>
466b4cac81SBjoern A. Zeeb #include <sys/types.h>
476b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
486b4cac81SBjoern A. Zeeb #include <sys/errno.h>
496b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
506b4cac81SBjoern A. Zeeb #include <sys/module.h>
516b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
526b4cac81SBjoern A. Zeeb #include <sys/socket.h>
536b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
546b4cac81SBjoern A. Zeeb #include <sys/queue.h>
556b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
566b4cac81SBjoern A. Zeeb 
576b4cac81SBjoern A. Zeeb #include <net/if.h>
586b4cac81SBjoern A. Zeeb #include <net/if_var.h>
596b4cac81SBjoern A. Zeeb #include <net/if_media.h>
606b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
646b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
656b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
686b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
696b4cac81SBjoern A. Zeeb 
706b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
716b4cac81SBjoern A. Zeeb #include "linux_80211.h"
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "Linux KPI 80211 compat");
746b4cac81SBjoern A. Zeeb 
756b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
766b4cac81SBjoern A. Zeeb 
776b4cac81SBjoern A. Zeeb int debug_80211;
786b4cac81SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_80211, CTLFLAG_RWTUN,
796b4cac81SBjoern A. Zeeb     &debug_80211, 0, "80211 debug Level");
806b4cac81SBjoern A. Zeeb 
816b4cac81SBjoern A. Zeeb #define LINUXKPI_DEBUG_80211
826b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
836b4cac81SBjoern A. Zeeb #ifndef	D80211_TODO
846b4cac81SBjoern A. Zeeb #define	D80211_TODO		0x1
856b4cac81SBjoern A. Zeeb #endif
866b4cac81SBjoern A. Zeeb #ifndef D80211_IMPROVE
876b4cac81SBjoern A. Zeeb #define	D80211_IMPROVE		0x2
886b4cac81SBjoern A. Zeeb #endif
896b4cac81SBjoern A. Zeeb #define	D80211_TRACE		0x10
906b4cac81SBjoern A. Zeeb #define	D80211_TRACEOK		0x20
916b4cac81SBjoern A. Zeeb #define	D80211_TRACE_TX		0x100
926b4cac81SBjoern A. Zeeb #define	D80211_TRACE_TX_DUMP	0x200
936b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX		0x1000
946b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX_DUMP	0x2000
956b4cac81SBjoern A. Zeeb #define	D80211_TRACE_RX_BEACONS	0x4000
966b4cac81SBjoern A. Zeeb #define	D80211_TRACEX		(D80211_TRACE_TX|D80211_TRACE_RX)
976b4cac81SBjoern A. Zeeb #define	D80211_TRACEX_DUMP	(D80211_TRACE_TX_DUMP|D80211_TRACE_RX_DUMP)
986b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		if (debug_80211 & D80211_TODO)		\
996b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1006b4cac81SBjoern A. Zeeb #define	TRACEOK()		if (debug_80211 & D80211_TRACEOK)	\
1016b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1026b4cac81SBjoern A. Zeeb #else
1036b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1046b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1056b4cac81SBjoern A. Zeeb #endif
1066b4cac81SBjoern A. Zeeb 
1076b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1086b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1096b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1106b4cac81SBjoern A. Zeeb #endif
1116b4cac81SBjoern A. Zeeb 
1126b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1136b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1146b4cac81SBjoern A. Zeeb 
1156b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1166b4cac81SBjoern A. Zeeb 	/*
1176b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1186b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1196b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1206b4cac81SBjoern A. Zeeb 	 */
1216b4cac81SBjoern A. Zeeb };
1226b4cac81SBjoern A. Zeeb 
1236b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1246b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
1256b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
1266b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb static enum nl80211_band
1296b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
1306b4cac81SBjoern A. Zeeb {
1316b4cac81SBjoern A. Zeeb 
1326b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
1336b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
1346b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
1356b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
1366b4cac81SBjoern A. Zeeb #ifdef __notyet__
1376b4cac81SBjoern A. Zeeb 	else if ()
1386b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
1396b4cac81SBjoern A. Zeeb 	else if ()
1406b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
1416b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
1426b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
1436b4cac81SBjoern A. Zeeb #endif
1446b4cac81SBjoern A. Zeeb 	else
1456b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
1466b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
1476b4cac81SBjoern A. Zeeb }
1486b4cac81SBjoern A. Zeeb 
1496b4cac81SBjoern A. Zeeb static uint32_t
1506b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
1516b4cac81SBjoern A. Zeeb {
1526b4cac81SBjoern A. Zeeb 
1536b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
1546b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
1556b4cac81SBjoern A. Zeeb 	switch (band) {
1566b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
1576b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
1586b4cac81SBjoern A. Zeeb 		break;
1596b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
1606b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
1616b4cac81SBjoern A. Zeeb 		break;
1626b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
1636b4cac81SBjoern A. Zeeb 		break;
1646b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
1656b4cac81SBjoern A. Zeeb 		break;
1666b4cac81SBjoern A. Zeeb 	default:
1676b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
1686b4cac81SBjoern A. Zeeb 		break;
1696b4cac81SBjoern A. Zeeb 	}
1706b4cac81SBjoern A. Zeeb 
1716b4cac81SBjoern A. Zeeb 	IMPROVE();
1726b4cac81SBjoern A. Zeeb 	return (0x00);
1736b4cac81SBjoern A. Zeeb }
1746b4cac81SBjoern A. Zeeb 
1756b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
1766b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
1776b4cac81SBjoern A. Zeeb {
1786b4cac81SBjoern A. Zeeb 
1796b4cac81SBjoern A. Zeeb 	switch (ac) {
1806b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
1816b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
1826b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
1836b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
1846b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
1856b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
1866b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
1876b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
1886b4cac81SBjoern A. Zeeb 	default:
1896b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
1906b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
1916b4cac81SBjoern A. Zeeb 	}
1926b4cac81SBjoern A. Zeeb }
1936b4cac81SBjoern A. Zeeb 
1946b4cac81SBjoern A. Zeeb static enum nl80211_iftype
1956b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
1966b4cac81SBjoern A. Zeeb {
1976b4cac81SBjoern A. Zeeb 
1986b4cac81SBjoern A. Zeeb 	switch (opmode) {
1996b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
2006b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
2016b4cac81SBjoern A. Zeeb 		break;
2026b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
2036b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
2046b4cac81SBjoern A. Zeeb 		break;
2056b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
2066b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
2076b4cac81SBjoern A. Zeeb 		break;
2086b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
2096b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
2106b4cac81SBjoern A. Zeeb 		break;
2116b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
2126b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
2136b4cac81SBjoern A. Zeeb 		break;
2146b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
2156b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
2166b4cac81SBjoern A. Zeeb 		break;
2176b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
2186b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
2196b4cac81SBjoern A. Zeeb 	default:
2206b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
2216b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
2226b4cac81SBjoern A. Zeeb 	}
2236b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
2246b4cac81SBjoern A. Zeeb }
2256b4cac81SBjoern A. Zeeb 
2266b4cac81SBjoern A. Zeeb #ifdef __notyet__
2276b4cac81SBjoern A. Zeeb static uint32_t
2286b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
2296b4cac81SBjoern A. Zeeb {
2306b4cac81SBjoern A. Zeeb 
2316b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
2326b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
2336b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
2346b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
2356b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
2366b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
2376b4cac81SBjoern A. Zeeb 		return (IEEE80211_CIPHER_AES_CCM);
2386b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
2396b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
2406b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
2416b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
2426b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
2436b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
2446b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2456b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2476b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
2486b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
2496b4cac81SBjoern A. Zeeb 		break;
2506b4cac81SBjoern A. Zeeb 	default:
2516b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
2526b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
2536b4cac81SBjoern A. Zeeb 	}
2546b4cac81SBjoern A. Zeeb 
2556b4cac81SBjoern A. Zeeb 	return (0);
2566b4cac81SBjoern A. Zeeb }
2576b4cac81SBjoern A. Zeeb #endif
2586b4cac81SBjoern A. Zeeb 
2596b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
2606b4cac81SBjoern A. Zeeb static uint32_t
2616b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
2626b4cac81SBjoern A. Zeeb {
2636b4cac81SBjoern A. Zeeb 
2646b4cac81SBjoern A. Zeeb 	switch (cipher) {
2656b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
2666b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
2676b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
2686b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
2696b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
2706b4cac81SBjoern A. Zeeb 		if (keylen < 8)
2716b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
2726b4cac81SBjoern A. Zeeb 		else
2736b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
2746b4cac81SBjoern A. Zeeb 		break;
2756b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
2766b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
2776b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
2786b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
2796b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
2806b4cac81SBjoern A. Zeeb 		break;
2816b4cac81SBjoern A. Zeeb 	default:
2826b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
2836b4cac81SBjoern A. Zeeb 	};
2846b4cac81SBjoern A. Zeeb 	return (0);
2856b4cac81SBjoern A. Zeeb }
2866b4cac81SBjoern A. Zeeb #endif
2876b4cac81SBjoern A. Zeeb 
2886b4cac81SBjoern A. Zeeb #ifdef __notyet__
2896b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
2906b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
2916b4cac81SBjoern A. Zeeb {
2926b4cac81SBjoern A. Zeeb 
2936b4cac81SBjoern A. Zeeb 	/*
2946b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
2956b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
2966b4cac81SBjoern A. Zeeb 	 */
2976b4cac81SBjoern A. Zeeb 	switch (state) {
2986b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
2996b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
3006b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
3016b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
3026b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
3036b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
3046b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
3056b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
3066b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
3076b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
3086b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
3096b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
3106b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
3116b4cac81SBjoern A. Zeeb 	default:
3126b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
3136b4cac81SBjoern A. Zeeb 	};
3146b4cac81SBjoern A. Zeeb 
3156b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
3166b4cac81SBjoern A. Zeeb }
3176b4cac81SBjoern A. Zeeb #endif
3186b4cac81SBjoern A. Zeeb 
3196b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
3206b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
3216b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
3226b4cac81SBjoern A. Zeeb {
3236b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3246b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
3256b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
3266b4cac81SBjoern A. Zeeb 	int i, nchans;
3276b4cac81SBjoern A. Zeeb 
3286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3296b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
3306b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
3316b4cac81SBjoern A. Zeeb 		return (NULL);
3326b4cac81SBjoern A. Zeeb 
3336b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
3346b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
3356b4cac81SBjoern A. Zeeb 		return (NULL);
3366b4cac81SBjoern A. Zeeb 
3376b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
3386b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
3396b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
3406b4cac81SBjoern A. Zeeb 			return (&channels[i]);
3416b4cac81SBjoern A. Zeeb 	}
3426b4cac81SBjoern A. Zeeb 
3436b4cac81SBjoern A. Zeeb 	return (NULL);
3446b4cac81SBjoern A. Zeeb }
3456b4cac81SBjoern A. Zeeb 
3466b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
3476b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
3486b4cac81SBjoern A. Zeeb {
3496b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
3506b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
3516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3526b4cac81SBjoern A. Zeeb 
3536b4cac81SBjoern A. Zeeb 	chan = NULL;
3546b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
3556b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
3566b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
3576b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
3586b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
3596b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
3606b4cac81SBjoern A. Zeeb 	else
3616b4cac81SBjoern A. Zeeb 		c = NULL;
3626b4cac81SBjoern A. Zeeb 
3636b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3646b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
3656b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
3666b4cac81SBjoern A. Zeeb 	}
3676b4cac81SBjoern A. Zeeb 
3686b4cac81SBjoern A. Zeeb 	return (chan);
3696b4cac81SBjoern A. Zeeb }
3706b4cac81SBjoern A. Zeeb 
3712e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
3722e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
3732e183d99SBjoern A. Zeeb {
3742e183d99SBjoern A. Zeeb 	enum nl80211_band band;
3752e183d99SBjoern A. Zeeb 
3762e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3772e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
3782e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
3792e183d99SBjoern A. Zeeb 		int i;
3802e183d99SBjoern A. Zeeb 
3812e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
3822e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3832e183d99SBjoern A. Zeeb 			continue;
3842e183d99SBjoern A. Zeeb 
3852e183d99SBjoern A. Zeeb 		channels = supband->channels;
3862e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3872e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
3882e183d99SBjoern A. Zeeb 				return (&channels[i]);
3892e183d99SBjoern A. Zeeb 		}
3902e183d99SBjoern A. Zeeb 	}
3912e183d99SBjoern A. Zeeb 
3922e183d99SBjoern A. Zeeb 	return (NULL);
3932e183d99SBjoern A. Zeeb }
3942e183d99SBjoern A. Zeeb 
3956b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
3966b4cac81SBjoern A. Zeeb static int
3976b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
3986b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
3996b4cac81SBjoern A. Zeeb {
4006b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
4016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4026b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4046b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4056b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
4076b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
4086b4cac81SBjoern A. Zeeb 	int error;
4096b4cac81SBjoern A. Zeeb 
4106b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
4116b4cac81SBjoern A. Zeeb 
4126b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
4136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4156b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4166b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4176b4cac81SBjoern A. Zeeb 
4186b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
4196b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
4206b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
4216b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
4226b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
4236b4cac81SBjoern A. Zeeb #if 0
4246b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
4256b4cac81SBjoern A. Zeeb #endif
4266b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
4276b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
4286b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
4296b4cac81SBjoern A. Zeeb 
4306b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
4316b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
4326b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
4336b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
4346b4cac81SBjoern A. Zeeb 		break;
4356b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
4366b4cac81SBjoern A. Zeeb 	default:
4376b4cac81SBjoern A. Zeeb 		IMPROVE();
4386b4cac81SBjoern A. Zeeb 		return (0);
4396b4cac81SBjoern A. Zeeb 	};
4406b4cac81SBjoern A. Zeeb 
4416b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
4426b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
4436b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
4446b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
4456b4cac81SBjoern A. Zeeb 
4466b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
4476b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
4486b4cac81SBjoern A. Zeeb 	}
4496b4cac81SBjoern A. Zeeb 
4506b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
4516b4cac81SBjoern A. Zeeb 	if (error != 0) {
4526b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
4536b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
4546b4cac81SBjoern A. Zeeb 		return (0);
4556b4cac81SBjoern A. Zeeb 	} else {
4566b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
4576b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
4586b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
4596b4cac81SBjoern A. Zeeb 		return (1);
4606b4cac81SBjoern A. Zeeb 	}
4616b4cac81SBjoern A. Zeeb }
4626b4cac81SBjoern A. Zeeb 
4636b4cac81SBjoern A. Zeeb static int
4646b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4656b4cac81SBjoern A. Zeeb {
4666b4cac81SBjoern A. Zeeb 
4676b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
4686b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
4696b4cac81SBjoern A. Zeeb }
4706b4cac81SBjoern A. Zeeb static  int
4716b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
4726b4cac81SBjoern A. Zeeb {
4736b4cac81SBjoern A. Zeeb 
4746b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
4756b4cac81SBjoern A. Zeeb }
4766b4cac81SBjoern A. Zeeb #endif
4776b4cac81SBjoern A. Zeeb 
4786b4cac81SBjoern A. Zeeb static u_int
4796b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
4806b4cac81SBjoern A. Zeeb {
4816b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
4826b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
4836b4cac81SBjoern A. Zeeb 
4846b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
4856b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
4866b4cac81SBjoern A. Zeeb 
4876b4cac81SBjoern A. Zeeb 	mc_list = arg;
4886b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
4896b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
4906b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
4916b4cac81SBjoern A. Zeeb 			return (0);
4926b4cac81SBjoern A. Zeeb 	}
4936b4cac81SBjoern A. Zeeb 
4946b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
4956b4cac81SBjoern A. Zeeb 	if (addr == NULL)
4966b4cac81SBjoern A. Zeeb 		return (0);
4976b4cac81SBjoern A. Zeeb 
4986b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
4996b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
5006b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
5016b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
5026b4cac81SBjoern A. Zeeb 	mc_list->count++;
5036b4cac81SBjoern A. Zeeb 
5046b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
5056b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
5066b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
5076b4cac81SBjoern A. Zeeb 
5086b4cac81SBjoern A. Zeeb 	return (1);
5096b4cac81SBjoern A. Zeeb }
5106b4cac81SBjoern A. Zeeb 
5116b4cac81SBjoern A. Zeeb static void
5126b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
5136b4cac81SBjoern A. Zeeb {
5146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5166b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
5176b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
5186b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
5196b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
5206b4cac81SBjoern A. Zeeb 	u64 mc;
5216b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
5226b4cac81SBjoern A. Zeeb 
5236b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
5246b4cac81SBjoern A. Zeeb 
5256b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
5266b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
5276b4cac81SBjoern A. Zeeb 		return;
5286b4cac81SBjoern A. Zeeb 
5296b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
5306b4cac81SBjoern A. Zeeb 		return;
5316b4cac81SBjoern A. Zeeb 
5326b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
5336b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
5346b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
5356b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
5366b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
5376b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
5386b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
5396b4cac81SBjoern A. Zeeb 	} else {
5406b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
5416b4cac81SBjoern A. Zeeb 	}
5426b4cac81SBjoern A. Zeeb 
5436b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5446b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
5456b4cac81SBjoern A. Zeeb 	/*
5466b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
5476b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
5486b4cac81SBjoern A. Zeeb 	 */
5496b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
5506b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
5516b4cac81SBjoern A. Zeeb 
5526b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
5536b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
5546b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
5556b4cac81SBjoern A. Zeeb 
5566b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
5576b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
5586b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
5596b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
5606b4cac81SBjoern A. Zeeb 			mc_list.count--;
5616b4cac81SBjoern A. Zeeb 		}
5626b4cac81SBjoern A. Zeeb 	}
5636b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
5646b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
5656b4cac81SBjoern A. Zeeb }
5666b4cac81SBjoern A. Zeeb 
5676b4cac81SBjoern A. Zeeb const uint8_t tid_to_mac80211_ac[] = {
5686b4cac81SBjoern A. Zeeb 	IEEE80211_AC_BE,
5696b4cac81SBjoern A. Zeeb 	IEEE80211_AC_BK,
5706b4cac81SBjoern A. Zeeb 	IEEE80211_AC_BK,
5716b4cac81SBjoern A. Zeeb 	IEEE80211_AC_BE,
5726b4cac81SBjoern A. Zeeb 	IEEE80211_AC_VI,
5736b4cac81SBjoern A. Zeeb 	IEEE80211_AC_VI,
5746b4cac81SBjoern A. Zeeb 	IEEE80211_AC_VO,
5756b4cac81SBjoern A. Zeeb 	IEEE80211_AC_VO,
5766b4cac81SBjoern A. Zeeb #if 0
5776b4cac81SBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
5786b4cac81SBjoern A. Zeeb #endif
5796b4cac81SBjoern A. Zeeb };
5806b4cac81SBjoern A. Zeeb 
5816b4cac81SBjoern A. Zeeb static void
5826b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
5836b4cac81SBjoern A. Zeeb {
5846b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5856b4cac81SBjoern A. Zeeb 	int error;
5866b4cac81SBjoern A. Zeeb 
5876b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0)
5886b4cac81SBjoern A. Zeeb 		return;
5896b4cac81SBjoern A. Zeeb 
5906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5916b4cac81SBjoern A. Zeeb 
592bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
593bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
5946b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
5956b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
5966b4cac81SBjoern A. Zeeb 
5976b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
5986b4cac81SBjoern A. Zeeb 	error = msleep(lhw, &lhw->mtx, 0, "lhwscanstop", hz/2);
599bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
600bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
6016b4cac81SBjoern A. Zeeb 
6026b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0)
6036b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
6046b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
6056b4cac81SBjoern A. Zeeb }
6066b4cac81SBjoern A. Zeeb 
6076b4cac81SBjoern A. Zeeb static void
608086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
609086be6a8SBjoern A. Zeeb {
610086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
611086be6a8SBjoern A. Zeeb 	int error;
612086be6a8SBjoern A. Zeeb 	bool old;
613086be6a8SBjoern A. Zeeb 
614086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
615086be6a8SBjoern A. Zeeb 	if (old == new)
616086be6a8SBjoern A. Zeeb 		return;
617086be6a8SBjoern A. Zeeb 
618086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
619086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
620086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
621086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
622086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
623086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
624086be6a8SBjoern A. Zeeb 	}
625086be6a8SBjoern A. Zeeb }
626086be6a8SBjoern A. Zeeb 
627086be6a8SBjoern A. Zeeb static void
6286b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
6296b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
6306b4cac81SBjoern A. Zeeb {
6316b4cac81SBjoern A. Zeeb 	sta->aid = 0;
6326b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.assoc) {
6336b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
6346b4cac81SBjoern A. Zeeb 		enum ieee80211_bss_changed changed;
6356b4cac81SBjoern A. Zeeb 
6366b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
6376b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
6386b4cac81SBjoern A. Zeeb 
6396b4cac81SBjoern A. Zeeb 		changed = 0;
6406b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = false;
6416b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = 0;
6426b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
6436b4cac81SBjoern A. Zeeb 		IMPROVE();
6446b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
6456b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
6466b4cac81SBjoern A. Zeeb 		    changed);
647086be6a8SBjoern A. Zeeb 
648086be6a8SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
6496b4cac81SBjoern A. Zeeb 	}
6506b4cac81SBjoern A. Zeeb }
6516b4cac81SBjoern A. Zeeb 
6526b4cac81SBjoern A. Zeeb static void
6536b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
6546b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
6556b4cac81SBjoern A. Zeeb {
6566b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
6576b4cac81SBjoern A. Zeeb 	int tid;
6586b4cac81SBjoern A. Zeeb 
6596b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
6606b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
6616b4cac81SBjoern A. Zeeb 
6626b4cac81SBjoern A. Zeeb 			if (tid == IEEE80211_NUM_TIDS) {
6636b4cac81SBjoern A. Zeeb 				IMPROVE("station specific?");
6646b4cac81SBjoern A. Zeeb 				if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
6656b4cac81SBjoern A. Zeeb 					continue;
6666b4cac81SBjoern A. Zeeb 			} else if (tid >= hw->queues)
6676b4cac81SBjoern A. Zeeb 				continue;
6686b4cac81SBjoern A. Zeeb 
6696b4cac81SBjoern A. Zeeb 			if (sta->txq[tid] == NULL)
6706b4cac81SBjoern A. Zeeb 				continue;
6716b4cac81SBjoern A. Zeeb 
6726b4cac81SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
6736b4cac81SBjoern A. Zeeb 			if (dequeue_seen && !ltxq->seen_dequeue)
6746b4cac81SBjoern A. Zeeb 				continue;
6756b4cac81SBjoern A. Zeeb 
6766b4cac81SBjoern A. Zeeb 			if (no_emptyq && skb_queue_empty(&ltxq->skbq))
6776b4cac81SBjoern A. Zeeb 				continue;
6786b4cac81SBjoern A. Zeeb 
6796b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
6806b4cac81SBjoern A. Zeeb 	}
6816b4cac81SBjoern A. Zeeb }
6826b4cac81SBjoern A. Zeeb 
6836b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6846b4cac81SBjoern A. Zeeb 
6856b4cac81SBjoern A. Zeeb static int
6866b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
6876b4cac81SBjoern A. Zeeb {
6886b4cac81SBjoern A. Zeeb 
6896b4cac81SBjoern A. Zeeb 	return (0);
6906b4cac81SBjoern A. Zeeb }
6916b4cac81SBjoern A. Zeeb 
6926b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
6936b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
6946b4cac81SBjoern A. Zeeb 
6956b4cac81SBjoern A. Zeeb static int
6966b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
6976b4cac81SBjoern A. Zeeb {
6986b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6996b4cac81SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *conf;
7006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7026b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7036b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
7046b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
7056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
7066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
7076b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
7086b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
7096b4cac81SBjoern A. Zeeb 	uint32_t changed;
7106b4cac81SBjoern A. Zeeb 	int error;
7116b4cac81SBjoern A. Zeeb 
7126b4cac81SBjoern A. Zeeb 	chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
7136b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
7146b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
7156b4cac81SBjoern A. Zeeb 		return (ESRCH);
7166b4cac81SBjoern A. Zeeb 	}
7176b4cac81SBjoern A. Zeeb 
7186b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
7196b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7206b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7216b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7226b4cac81SBjoern A. Zeeb 
7236b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
7246b4cac81SBjoern A. Zeeb 
7256b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
7266b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
7276b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
7286b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
7296b4cac81SBjoern A. Zeeb 	} else {
7306b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
7316b4cac81SBjoern A. Zeeb 		conf = malloc(sizeof(*conf) + hw->chanctx_data_size,
7326b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
7336b4cac81SBjoern A. Zeeb 	}
7346b4cac81SBjoern A. Zeeb 
7356b4cac81SBjoern A. Zeeb 	conf->rx_chains_dynamic = 1;
7366b4cac81SBjoern A. Zeeb 	conf->rx_chains_static = 1;
7376b4cac81SBjoern A. Zeeb 	conf->radar_enabled =
7386b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
7396b4cac81SBjoern A. Zeeb 	conf->def.chan = chan;
7406b4cac81SBjoern A. Zeeb 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
7416b4cac81SBjoern A. Zeeb 	conf->def.center_freq1 = chan->center_freq;
7426b4cac81SBjoern A. Zeeb 	conf->def.center_freq2 = 0;
7436b4cac81SBjoern A. Zeeb 	/* Responder ... */
7446b4cac81SBjoern A. Zeeb 	conf->min_def.chan = chan;
7456b4cac81SBjoern A. Zeeb 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
7466b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq1 = chan->center_freq;
7476b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq2 = 0;
7486b4cac81SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT only");
7496b4cac81SBjoern A. Zeeb 
7506b4cac81SBjoern A. Zeeb 	ni = NULL;
7516b4cac81SBjoern A. Zeeb 	error = 0;
7526b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
7536b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
7546b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
7556b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
7566b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
7576b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
7586b4cac81SBjoern A. Zeeb 	} else {
7596b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, conf);
7606b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
7616b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.chan = conf->def.chan;
7626b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.width = conf->def.width;
7636b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
7646b4cac81SBjoern A. Zeeb 			    conf->def.center_freq1;
7656b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
7666b4cac81SBjoern A. Zeeb 			    conf->def.center_freq2;
7676b4cac81SBjoern A. Zeeb 		} else {
7686b4cac81SBjoern A. Zeeb 			goto out;
7696b4cac81SBjoern A. Zeeb 		}
7706b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
7716b4cac81SBjoern A. Zeeb 		if (error == 0)
7726b4cac81SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, conf);
7736b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
7746b4cac81SBjoern A. Zeeb 			error = 0;
7756b4cac81SBjoern A. Zeeb 		if (error != 0) {
7766b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, conf);
7776b4cac81SBjoern A. Zeeb 			free(conf, M_LKPI80211);
7786b4cac81SBjoern A. Zeeb 			goto out;
7796b4cac81SBjoern A. Zeeb 		}
7806b4cac81SBjoern A. Zeeb 	}
7816b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
7826b4cac81SBjoern A. Zeeb 
7836b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
7846b4cac81SBjoern A. Zeeb 
7856b4cac81SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
7866b4cac81SBjoern A. Zeeb 	bss_changed = 0;
7876b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, ni->ni_bssid);
7886b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
7896b4cac81SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
7906b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
7916b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = false;
7926b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
7936b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ni->ni_intval;
7946b4cac81SBjoern A. Zeeb 	/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
7956b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
7966b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
7976b4cac81SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BEACON_INT;
798c8dafefaSBjoern A. Zeeb 	vif->bss_conf.dtim_period = vap->iv_dtim_period;
799c8dafefaSBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BEACON_INFO;
800c8dafefaSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
801c8dafefaSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
802c8dafefaSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
803c8dafefaSBjoern A. Zeeb 
8046b4cac81SBjoern A. Zeeb 	/* Should almost assert it is this. */
8056b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
8066b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
8076b4cac81SBjoern A. Zeeb 	/* RATES */
8086b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
8096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
8106b4cac81SBjoern A. Zeeb 
8116b4cac81SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
8126b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
8136b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
8146b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
8156b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
8166b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
8176b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
8186b4cac81SBjoern A. Zeeb 	if (error != 0) {
8196b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
8206b4cac81SBjoern A. Zeeb 		goto out;
8216b4cac81SBjoern A. Zeeb 	}
8226b4cac81SBjoern A. Zeeb #if 0
8236b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
8246b4cac81SBjoern A. Zeeb #endif
8256b4cac81SBjoern A. Zeeb 
8266b4cac81SBjoern A. Zeeb 	/*
8276b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
8286b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
8296b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
8306b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
8316b4cac81SBjoern A. Zeeb 	 */
8326b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, false);
8336b4cac81SBjoern A. Zeeb 
8346b4cac81SBjoern A. Zeeb 	{
8356b4cac81SBjoern A. Zeeb 		int i, count;
8366b4cac81SBjoern A. Zeeb 
837500be2e8SBjoern A. Zeeb 		for (i = 3 * (hw->queues + 1); i > 0; i--) {
8386b4cac81SBjoern A. Zeeb 			struct lkpi_txq *ltxq;
8396b4cac81SBjoern A. Zeeb 			int tid;
8406b4cac81SBjoern A. Zeeb 
8416b4cac81SBjoern A. Zeeb 			count = 0;
8426b4cac81SBjoern A. Zeeb 			/* Wake up all queues to know they are allocated in the driver. */
8436b4cac81SBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
8446b4cac81SBjoern A. Zeeb 
8456b4cac81SBjoern A. Zeeb 				if (tid == IEEE80211_NUM_TIDS) {
8466b4cac81SBjoern A. Zeeb 					IMPROVE("station specific?");
8476b4cac81SBjoern A. Zeeb 					if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
8486b4cac81SBjoern A. Zeeb 						continue;
8496b4cac81SBjoern A. Zeeb 				} else if (tid >= hw->queues)
8506b4cac81SBjoern A. Zeeb 					continue;
8516b4cac81SBjoern A. Zeeb 
8526b4cac81SBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
8536b4cac81SBjoern A. Zeeb 					continue;
8546b4cac81SBjoern A. Zeeb 
8556b4cac81SBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8566b4cac81SBjoern A. Zeeb 				if (!ltxq->seen_dequeue)
8576b4cac81SBjoern A. Zeeb 					count++;
8586b4cac81SBjoern A. Zeeb 			}
8596b4cac81SBjoern A. Zeeb 			if (count == 0)
8606b4cac81SBjoern A. Zeeb 				break;
8616b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8626b4cac81SBjoern A. Zeeb 			if (count > 0)
863312ba38aSRuslan Makhmatkhanov 				ic_printf(vap->iv_ic, "%s: waiting for %d queues "
8646b4cac81SBjoern A. Zeeb 				    "to be allocated by driver\n", __func__, count);
8656b4cac81SBjoern A. Zeeb #endif
866500be2e8SBjoern A. Zeeb 			pause("lkpi80211txq", hz/10);
8676b4cac81SBjoern A. Zeeb 		}
8686b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8696b4cac81SBjoern A. Zeeb 		if (count > 0)
870312ba38aSRuslan Makhmatkhanov 			ic_printf(vap->iv_ic, "%s: %d queues still not "
8716b4cac81SBjoern A. Zeeb 			    "allocated by driver\n", __func__, count);
8726b4cac81SBjoern A. Zeeb #endif
8736b4cac81SBjoern A. Zeeb 	}
8746b4cac81SBjoern A. Zeeb 
8756b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
8766b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
8776b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
8786b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
8796b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
8806b4cac81SBjoern A. Zeeb 
8816b4cac81SBjoern A. Zeeb 	/*
8826b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
8836b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
8846b4cac81SBjoern A. Zeeb 	 * - event_callback
8856b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
8866b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
8876b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
8886b4cac81SBjoern A. Zeeb 	 */
8896b4cac81SBjoern A. Zeeb 
8906b4cac81SBjoern A. Zeeb out:
8916b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
8926b4cac81SBjoern A. Zeeb 	if (ni != NULL)
8936b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
8946b4cac81SBjoern A. Zeeb 	return (error);
8956b4cac81SBjoern A. Zeeb }
8966b4cac81SBjoern A. Zeeb 
8976b4cac81SBjoern A. Zeeb static int
8986b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
8996b4cac81SBjoern A. Zeeb {
9006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9026b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
9036b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
9046b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
9056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
9066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
9076b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
9086b4cac81SBjoern A. Zeeb 	int error;
9096b4cac81SBjoern A. Zeeb 
9106b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
9116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9126b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
9136b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
9146b4cac81SBjoern A. Zeeb 
9156b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
9166b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
9176b4cac81SBjoern A. Zeeb 
9186b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
9196b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
9206b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
9216b4cac81SBjoern A. Zeeb 
9226b4cac81SBjoern A. Zeeb 	/* flush, drop. */
9236b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
9246b4cac81SBjoern A. Zeeb 
9256b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
9266b4cac81SBjoern A. Zeeb 
9276b4cac81SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential deauth packet out. */
9286b4cac81SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
9296b4cac81SBjoern A. Zeeb 	if (error != 0)
9306b4cac81SBjoern A. Zeeb 		goto outni;
9316b4cac81SBjoern A. Zeeb 
9326b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
9336b4cac81SBjoern A. Zeeb 
9346b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
9356b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
9366b4cac81SBjoern A. Zeeb 
9376b4cac81SBjoern A. Zeeb 	/* flush, no drop */
9386b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
9396b4cac81SBjoern A. Zeeb 
9406b4cac81SBjoern A. Zeeb 	/* Take the station and chan ctx down again. */
9416b4cac81SBjoern A. Zeeb 
9426b4cac81SBjoern A. Zeeb 	IMPROVE("event callback with failure?");
9436b4cac81SBjoern A. Zeeb 
9446b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
9456b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
9466b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
9476b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
9486b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
9496b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
9506b4cac81SBjoern A. Zeeb 	}
9516b4cac81SBjoern A. Zeeb 
9526b4cac81SBjoern A. Zeeb #ifdef __not_yet__
9536b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
9546b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
9556b4cac81SBjoern A. Zeeb 
9566b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
9576b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
9586b4cac81SBjoern A. Zeeb #endif
9596b4cac81SBjoern A. Zeeb 
9606b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
9616b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
9626b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
9636b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
9646b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
9656b4cac81SBjoern A. Zeeb 	if (error != 0) {
9666b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
9676b4cac81SBjoern A. Zeeb 		goto out;
9686b4cac81SBjoern A. Zeeb 	}
9696b4cac81SBjoern A. Zeeb #if 0
9706b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
9716b4cac81SBjoern A. Zeeb #endif
9726b4cac81SBjoern A. Zeeb 
9736b4cac81SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
9746b4cac81SBjoern A. Zeeb 
9756b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
9766b4cac81SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
9776b4cac81SBjoern A. Zeeb 
9786b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
9796b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
9806b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
9816b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
9826b4cac81SBjoern A. Zeeb 
9836b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
9846b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
9856b4cac81SBjoern A. Zeeb 		free(conf, M_LKPI80211);
9866b4cac81SBjoern A. Zeeb 	}
9876b4cac81SBjoern A. Zeeb 
9886b4cac81SBjoern A. Zeeb 	/* No need to start a scan; ic_scan_start should do. */
9896b4cac81SBjoern A. Zeeb 
9906b4cac81SBjoern A. Zeeb 	error = EALREADY;
9916b4cac81SBjoern A. Zeeb out:
9926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
9936b4cac81SBjoern A. Zeeb outni:
9946b4cac81SBjoern A. Zeeb 	if (ni != NULL)
9956b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
9966b4cac81SBjoern A. Zeeb 	return (error);
9976b4cac81SBjoern A. Zeeb }
9986b4cac81SBjoern A. Zeeb 
9996b4cac81SBjoern A. Zeeb static int
10006b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10016b4cac81SBjoern A. Zeeb {
10026b4cac81SBjoern A. Zeeb 	int error;
10036b4cac81SBjoern A. Zeeb 
10046b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
10056b4cac81SBjoern A. Zeeb 	if (error == 0)
10066b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
10076b4cac81SBjoern A. Zeeb 	return (error);
10086b4cac81SBjoern A. Zeeb }
10096b4cac81SBjoern A. Zeeb 
10106b4cac81SBjoern A. Zeeb static int
10116b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10126b4cac81SBjoern A. Zeeb {
10136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10146b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10166b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10176b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10196b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
10206b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10216b4cac81SBjoern A. Zeeb 	int error;
10226b4cac81SBjoern A. Zeeb 
10236b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
10246b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10256b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10266b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10276b4cac81SBjoern A. Zeeb 
10286b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
10296b4cac81SBjoern A. Zeeb 	ni = NULL;
10306b4cac81SBjoern A. Zeeb 
10316b4cac81SBjoern A. Zeeb 	/* Finish auth. */
10326b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
10336b4cac81SBjoern A. Zeeb 
10346b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
10356b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
10366b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
10376b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
10386b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
10396b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
10406b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
10416b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
10426b4cac81SBjoern A. Zeeb 	if (error != 0)
10436b4cac81SBjoern A. Zeeb 		goto out;
10446b4cac81SBjoern A. Zeeb 
10456b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
10466b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
10476b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
10486b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
10496b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
10506b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
10516b4cac81SBjoern A. Zeeb 	}
10526b4cac81SBjoern A. Zeeb 
10536b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
10546b4cac81SBjoern A. Zeeb 
10556b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
10566b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
10576b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
10586b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
10596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
10606b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
10616b4cac81SBjoern A. Zeeb 	}
10626b4cac81SBjoern A. Zeeb 
10636b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
10646b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
10656b4cac81SBjoern A. Zeeb 
10666b4cac81SBjoern A. Zeeb 	/*
10676b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
10686b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
10696b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
10706b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
10716b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
10726b4cac81SBjoern A. Zeeb 	 * - event_callback
10736b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
10746b4cac81SBjoern A. Zeeb 	 */
10756b4cac81SBjoern A. Zeeb 
10766b4cac81SBjoern A. Zeeb out:
10776b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
10786b4cac81SBjoern A. Zeeb 	if (ni != NULL)
10796b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
10806b4cac81SBjoern A. Zeeb 	return (error);
10816b4cac81SBjoern A. Zeeb }
10826b4cac81SBjoern A. Zeeb 
10836b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
10846b4cac81SBjoern A. Zeeb static int
10856b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10866b4cac81SBjoern A. Zeeb {
10876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10906b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10916b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10936b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10946b4cac81SBjoern A. Zeeb 
10956b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
10966b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10976b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10986b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10996b4cac81SBjoern A. Zeeb 
11006b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11016b4cac81SBjoern A. Zeeb 
11026b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11036b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
11046b4cac81SBjoern A. Zeeb 
11056b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
11066b4cac81SBjoern A. Zeeb 
11076b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
11086b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
11096b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
11106b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
11116b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
11126b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
11136b4cac81SBjoern A. Zeeb 	}
11146b4cac81SBjoern A. Zeeb 
11156b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
11166b4cac81SBjoern A. Zeeb 
11176b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
11186b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
11196b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
11206b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
11216b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
11226b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
11236b4cac81SBjoern A. Zeeb 	}
11246b4cac81SBjoern A. Zeeb 
11256b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
11266b4cac81SBjoern A. Zeeb 	if (ni != NULL)
11276b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
11286b4cac81SBjoern A. Zeeb 
11296b4cac81SBjoern A. Zeeb 	return (0);
11306b4cac81SBjoern A. Zeeb }
11316b4cac81SBjoern A. Zeeb 
11326b4cac81SBjoern A. Zeeb static int
11336b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11346b4cac81SBjoern A. Zeeb {
11356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11366b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11376b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
11386b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11396b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11406b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11416b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
11426b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
11436b4cac81SBjoern A. Zeeb 	int error;
11446b4cac81SBjoern A. Zeeb 
11456b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11466b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11476b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11486b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11496b4cac81SBjoern A. Zeeb 
11506b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
11516b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11526b4cac81SBjoern A. Zeeb 
11536b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11546b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
11556b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
11566b4cac81SBjoern A. Zeeb 
11576b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
11586b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
11596b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
11606b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
11616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
11626b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
11636b4cac81SBjoern A. Zeeb 	}
11646b4cac81SBjoern A. Zeeb 
11656b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
11666b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
11676b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
11686b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
11696b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
11706b4cac81SBjoern A. Zeeb 	if (error != 0)
11716b4cac81SBjoern A. Zeeb 		goto out;
11726b4cac81SBjoern A. Zeeb 
11736b4cac81SBjoern A. Zeeb 	IMPROVE("anything else?");
11746b4cac81SBjoern A. Zeeb 
11756b4cac81SBjoern A. Zeeb out:
11766b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
11776b4cac81SBjoern A. Zeeb 	if (ni != NULL)
11786b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
11796b4cac81SBjoern A. Zeeb 	return (error);
11806b4cac81SBjoern A. Zeeb }
11816b4cac81SBjoern A. Zeeb 
11826b4cac81SBjoern A. Zeeb static int
11836b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11846b4cac81SBjoern A. Zeeb {
11856b4cac81SBjoern A. Zeeb 	int error;
11866b4cac81SBjoern A. Zeeb 
11876b4cac81SBjoern A. Zeeb 	error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
11886b4cac81SBjoern A. Zeeb 	if (error == 0)
11896b4cac81SBjoern A. Zeeb 		error = lkpi_sta_auth_to_scan(vap, nstate, arg);
11906b4cac81SBjoern A. Zeeb 	return (error);
11916b4cac81SBjoern A. Zeeb }
11926b4cac81SBjoern A. Zeeb 
11936b4cac81SBjoern A. Zeeb static int
11946b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11956b4cac81SBjoern A. Zeeb {
11966b4cac81SBjoern A. Zeeb 	int error;
11976b4cac81SBjoern A. Zeeb 
11986b4cac81SBjoern A. Zeeb 	error = lkpi_sta_assoc_to_scan(vap, nstate, arg);
11996b4cac81SBjoern A. Zeeb 	if (error == 0)
12006b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
12016b4cac81SBjoern A. Zeeb 	return (error);
12026b4cac81SBjoern A. Zeeb }
12036b4cac81SBjoern A. Zeeb 
12046b4cac81SBjoern A. Zeeb static int
12056b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
12066b4cac81SBjoern A. Zeeb {
12076b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12086b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12096b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
12106b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12116b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12126b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12136b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12146b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
12156b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
12166b4cac81SBjoern A. Zeeb 	int error;
12176b4cac81SBjoern A. Zeeb 
12186b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
12196b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
12206b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
12216b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
12226b4cac81SBjoern A. Zeeb 
12236b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
12246b4cac81SBjoern A. Zeeb 	ni = NULL;
12256b4cac81SBjoern A. Zeeb 
12266b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
12276b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
12286b4cac81SBjoern A. Zeeb 
12296b4cac81SBjoern A. Zeeb 	/* Finish assoc. */
12306b4cac81SBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
12316b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
12326b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
12336b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12346b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
12356b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
12366b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
12376b4cac81SBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
12386b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
12396b4cac81SBjoern A. Zeeb 	if (error != 0)
12406b4cac81SBjoern A. Zeeb 		goto out;
12416b4cac81SBjoern A. Zeeb 
12426b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
12436b4cac81SBjoern A. Zeeb 
12446b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
12456b4cac81SBjoern A. Zeeb 	bss_changed = 0;
12466b4cac81SBjoern A. Zeeb 	if (!vif->bss_conf.assoc || vif->bss_conf.aid != IEEE80211_NODE_AID(ni)) {
12476b4cac81SBjoern A. Zeeb 		vif->bss_conf.assoc = true;
12486b4cac81SBjoern A. Zeeb 		vif->bss_conf.aid = IEEE80211_NODE_AID(ni);
12496b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
12506b4cac81SBjoern A. Zeeb 	}
12516b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
12526b4cac81SBjoern A. Zeeb 	vif->bss_conf.ssid_len = ni->ni_esslen;
12536b4cac81SBjoern A. Zeeb 	memcpy(vif->bss_conf.ssid, ni->ni_essid, ni->ni_esslen);
12546b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
12556b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
12566b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
12576b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
12586b4cac81SBjoern A. Zeeb 	}
12596b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
12606b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
12616b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
12626b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
12636b4cac81SBjoern A. Zeeb 	}
12646b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
12656b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
12666b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
12676b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
12686b4cac81SBjoern A. Zeeb 	}
12696b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
12706b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
12716b4cac81SBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
12726b4cac81SBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
12736b4cac81SBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
12746b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
12756b4cac81SBjoern A. Zeeb 	}
12766b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
12776b4cac81SBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
12786b4cac81SBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
12796b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
12806b4cac81SBjoern A. Zeeb 	}
12816b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
12826b4cac81SBjoern A. Zeeb 
12836b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
12846b4cac81SBjoern A. Zeeb 	 * - event_callback
12856b4cac81SBjoern A. Zeeb 	 */
12866b4cac81SBjoern A. Zeeb 
12876b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
12886b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
12896b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12906b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
12916b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
12926b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
12936b4cac81SBjoern A. Zeeb 	}
12946b4cac81SBjoern A. Zeeb 
1295086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1296086be6a8SBjoern A. Zeeb 
12976b4cac81SBjoern A. Zeeb 	/*
12986b4cac81SBjoern A. Zeeb 	 * And then:
12996b4cac81SBjoern A. Zeeb 	 * - (more packets)?
13006b4cac81SBjoern A. Zeeb 	 * - set_key
13016b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
13026b4cac81SBjoern A. Zeeb 	 * - set_key (?)
13036b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
13046b4cac81SBjoern A. Zeeb 	 */
13056b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
13066b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
13076b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
13086b4cac81SBjoern A. Zeeb 
13096b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
13106b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
13116b4cac81SBjoern A. Zeeb 	}
13126b4cac81SBjoern A. Zeeb 
13136b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
13146b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
13156b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
13166b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13176b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
13186b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
13196b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13206b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTHORIZED);
13216b4cac81SBjoern A. Zeeb 	if (error != 0) {
13226b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
13236b4cac81SBjoern A. Zeeb 		goto out;
13246b4cac81SBjoern A. Zeeb 	}
13256b4cac81SBjoern A. Zeeb 
13266b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
13276b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
13286b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
13296b4cac81SBjoern A. Zeeb 	 *
13306b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
13316b4cac81SBjoern A. Zeeb 	 */
13326b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
13336b4cac81SBjoern A. Zeeb 
13346b4cac81SBjoern A. Zeeb out:
13356b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
13366b4cac81SBjoern A. Zeeb 	if (ni != NULL)
13376b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
13386b4cac81SBjoern A. Zeeb 	return (error);
13396b4cac81SBjoern A. Zeeb }
13406b4cac81SBjoern A. Zeeb 
13416b4cac81SBjoern A. Zeeb static int
13426b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13436b4cac81SBjoern A. Zeeb {
13446b4cac81SBjoern A. Zeeb 	int error;
13456b4cac81SBjoern A. Zeeb 
13466b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
13476b4cac81SBjoern A. Zeeb 	if (error == 0)
13486b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
13496b4cac81SBjoern A. Zeeb 	return (error);
13506b4cac81SBjoern A. Zeeb }
13516b4cac81SBjoern A. Zeeb 
13526b4cac81SBjoern A. Zeeb static int
13536b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13546b4cac81SBjoern A. Zeeb {
13556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13586b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13616b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13626b4cac81SBjoern A. Zeeb 	int error;
13636b4cac81SBjoern A. Zeeb 
13646b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13656b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13666b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13676b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13686b4cac81SBjoern A. Zeeb 
13696b4cac81SBjoern A. Zeeb 	/* Keep ni around. */
13706b4cac81SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
13716b4cac81SBjoern A. Zeeb 
13726b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
13736b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
13746b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13756b4cac81SBjoern A. Zeeb 
13766b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
13776b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13786b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
13796b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
13806b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
13816b4cac81SBjoern A. Zeeb 	if (error != 0)
13826b4cac81SBjoern A. Zeeb 		goto out;
13836b4cac81SBjoern A. Zeeb 
13846b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
13856b4cac81SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
13866b4cac81SBjoern A. Zeeb 
13876b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
13886b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13896b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
13906b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
13916b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13926b4cac81SBjoern A. Zeeb 	sta->aid = 0;
13936b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
13946b4cac81SBjoern A. Zeeb 	if (error != 0)
13956b4cac81SBjoern A. Zeeb 		goto out;
13966b4cac81SBjoern A. Zeeb 
13976b4cac81SBjoern A. Zeeb 	IMPROVE("if ASSOC is final state, prep_tx_info?");
13986b4cac81SBjoern A. Zeeb 
13996b4cac81SBjoern A. Zeeb out:
14006b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
14016b4cac81SBjoern A. Zeeb 	if (ni != NULL)
14026b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
14036b4cac81SBjoern A. Zeeb 	return (error);
14046b4cac81SBjoern A. Zeeb }
14056b4cac81SBjoern A. Zeeb 
14066b4cac81SBjoern A. Zeeb static int
14076b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14086b4cac81SBjoern A. Zeeb {
14096b4cac81SBjoern A. Zeeb 	int error;
14106b4cac81SBjoern A. Zeeb 
14116b4cac81SBjoern A. Zeeb 	error = lkpi_sta_run_to_assoc(vap, nstate, arg);
14126b4cac81SBjoern A. Zeeb 	if (error == 0)
14136b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
14146b4cac81SBjoern A. Zeeb 	return (error);
14156b4cac81SBjoern A. Zeeb }
14166b4cac81SBjoern A. Zeeb 
14176b4cac81SBjoern A. Zeeb static int
14186b4cac81SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14196b4cac81SBjoern A. Zeeb {
14206b4cac81SBjoern A. Zeeb 	int error;
14216b4cac81SBjoern A. Zeeb 
14226b4cac81SBjoern A. Zeeb 	error = lkpi_sta_run_to_auth(vap, nstate, arg);
14236b4cac81SBjoern A. Zeeb 	if (error == 0)
14246b4cac81SBjoern A. Zeeb 		error = lkpi_sta_auth_to_scan(vap, nstate, arg);
14256b4cac81SBjoern A. Zeeb 	return (error);
14266b4cac81SBjoern A. Zeeb }
14276b4cac81SBjoern A. Zeeb 
14286b4cac81SBjoern A. Zeeb static int
14296b4cac81SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14306b4cac81SBjoern A. Zeeb {
14316b4cac81SBjoern A. Zeeb 	int error;
14326b4cac81SBjoern A. Zeeb 
14336b4cac81SBjoern A. Zeeb 	error = lkpi_sta_run_to_scan(vap, nstate, arg);
14346b4cac81SBjoern A. Zeeb 	if (error == 0)
14356b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
14366b4cac81SBjoern A. Zeeb 	return (error);
14376b4cac81SBjoern A. Zeeb }
14386b4cac81SBjoern A. Zeeb 
14396b4cac81SBjoern A. Zeeb /*
14406b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
14416b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
14426b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
14436b4cac81SBjoern A. Zeeb  */
14446b4cac81SBjoern A. Zeeb struct fsm_state {
14456b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
14466b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
14476b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
14486b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
14496b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
14506b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
14516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
14526b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
14536b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },
14546b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },
14556b4cac81SBjoern A. Zeeb 
14566b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
14576b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
14586b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
14596b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
14606b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },
14616b4cac81SBjoern A. Zeeb 
14626b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },
14636b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },
14646b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },
14656b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },
14666b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },
14676b4cac81SBjoern A. Zeeb 
14686b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },
14696b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },
14706b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },
14716b4cac81SBjoern A. Zeeb 
14726b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
14736b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
14746b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
14756b4cac81SBjoern A. Zeeb 
14766b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
14776b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
14786b4cac81SBjoern A. Zeeb };
14796b4cac81SBjoern A. Zeeb 
14806b4cac81SBjoern A. Zeeb static int
14816b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14826b4cac81SBjoern A. Zeeb {
14836b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
14846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14856b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14866b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14876b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
14886b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
14896b4cac81SBjoern A. Zeeb 	int error;
14906b4cac81SBjoern A. Zeeb 
14916b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
14926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
14936b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
14946b4cac81SBjoern A. Zeeb 
14956b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
14966b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
14976b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
14986b4cac81SBjoern A. Zeeb 
14996b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
15006b4cac81SBjoern A. Zeeb 
15016b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
15026b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
15036b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
15046b4cac81SBjoern A. Zeeb 
15056b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
15066b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
15076b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
15086b4cac81SBjoern A. Zeeb 
15096b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
15106b4cac81SBjoern A. Zeeb 
15116b4cac81SBjoern A. Zeeb 	} else {
15126b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
15136b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
15146b4cac81SBjoern A. Zeeb 		return (ENOSYS);
15156b4cac81SBjoern A. Zeeb 	}
15166b4cac81SBjoern A. Zeeb 
15176b4cac81SBjoern A. Zeeb 	error = 0;
15186b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
15196b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
15206b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
15216b4cac81SBjoern A. Zeeb 			break;
15226b4cac81SBjoern A. Zeeb 		}
15236b4cac81SBjoern A. Zeeb 	}
15246b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
15256b4cac81SBjoern A. Zeeb 
15266b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
15276b4cac81SBjoern A. Zeeb 		IMPROVE("thurn this into a KASSERT\n");
15286b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
15296b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
15306b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
15316b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
15326b4cac81SBjoern A. Zeeb 		return (ENOSYS);
15336b4cac81SBjoern A. Zeeb 	}
15346b4cac81SBjoern A. Zeeb 
15356b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
15366b4cac81SBjoern A. Zeeb 		IMPROVE("make this a debug log later");
15376b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
15386b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s): iv_newstate already handled.\n",
15396b4cac81SBjoern A. Zeeb 		    __func__, error,
15406b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
15416b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
15426b4cac81SBjoern A. Zeeb 		return (0);
15436b4cac81SBjoern A. Zeeb 	}
15446b4cac81SBjoern A. Zeeb 
15456b4cac81SBjoern A. Zeeb 	if (error != 0) {
15466b4cac81SBjoern A. Zeeb 		/* XXX-BZ currently expected so ignore. */
15476b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
15486b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
15496b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
15506b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
15516b4cac81SBjoern A. Zeeb 		/* return (error); */
15526b4cac81SBjoern A. Zeeb 	}
15536b4cac81SBjoern A. Zeeb 
15546b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
15556b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x calling net80211 parent\n",
15566b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
15576b4cac81SBjoern A. Zeeb 
15586b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
15596b4cac81SBjoern A. Zeeb }
15606b4cac81SBjoern A. Zeeb 
15616b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
15626b4cac81SBjoern A. Zeeb 
15636b4cac81SBjoern A. Zeeb static int
15646b4cac81SBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
15656b4cac81SBjoern A. Zeeb {
15666b4cac81SBjoern A. Zeeb 	/* This needs queuing and go at the right moment. */
15676b4cac81SBjoern A. Zeeb #ifdef WITH_WME_UPDATE
15686b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
15696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15736b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
15746b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
15756b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
15766b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
15776b4cac81SBjoern A. Zeeb 	int error;
15786b4cac81SBjoern A. Zeeb 	uint16_t ac;
15796b4cac81SBjoern A. Zeeb #endif
15806b4cac81SBjoern A. Zeeb 
15816b4cac81SBjoern A. Zeeb 	IMPROVE();
15826b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
15836b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
15846b4cac81SBjoern A. Zeeb 
15856b4cac81SBjoern A. Zeeb #ifdef WITH_WME_UPDATE
15866b4cac81SBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
15876b4cac81SBjoern A. Zeeb 	if (vap == NULL)
15886b4cac81SBjoern A. Zeeb 		return (0);
15896b4cac81SBjoern A. Zeeb 
15906b4cac81SBjoern A. Zeeb 	/* We should factor this out into per-vap (*wme_update). */
15916b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
15926b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
15936b4cac81SBjoern A. Zeeb 		return (0);
15946b4cac81SBjoern A. Zeeb 
15956b4cac81SBjoern A. Zeeb 	/* XXX-BZ check amount of hw queues */
15966b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15976b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15986b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15996b4cac81SBjoern A. Zeeb 
16006b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
16016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
16026b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
16036b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
16046b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
16056b4cac81SBjoern A. Zeeb 
16066b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
16076b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16086b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16096b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 		/* XXX-BZ should keep this in lvif? */
16126b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
16136b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
16146b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
16156b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
16166b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
16176b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
16186b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
16196b4cac81SBjoern A. Zeeb 		if (error != 0)
16206b4cac81SBjoern A. Zeeb 			printf("%s: conf_tx ac %u failed %d\n",
16216b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
16226b4cac81SBjoern A. Zeeb 	}
16236b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16246b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
16256b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
16266b4cac81SBjoern A. Zeeb #endif
16276b4cac81SBjoern A. Zeeb 
16286b4cac81SBjoern A. Zeeb 	return (0);
16296b4cac81SBjoern A. Zeeb }
16306b4cac81SBjoern A. Zeeb 
16316b4cac81SBjoern A. Zeeb static struct ieee80211vap *
16326b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
16336b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
16346b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
16356b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
16366b4cac81SBjoern A. Zeeb {
16376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16406b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
16416b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16426b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
16436b4cac81SBjoern A. Zeeb 	size_t len;
16446b4cac81SBjoern A. Zeeb 	int error;
16456b4cac81SBjoern A. Zeeb 
16466b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
16476b4cac81SBjoern A. Zeeb 		return (NULL);
16486b4cac81SBjoern A. Zeeb 
16496b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
16506b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16516b4cac81SBjoern A. Zeeb 
16526b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
16536b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
16546b4cac81SBjoern A. Zeeb 
16556b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
16566b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
16576b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
16586b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
16596b4cac81SBjoern A. Zeeb 
16606b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16616b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
16626b4cac81SBjoern A. Zeeb 	vif->p2p = false;
16636b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
16646b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
16656b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
16666b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
16676b4cac81SBjoern A. Zeeb 	IMPROVE();
16686b4cac81SBjoern A. Zeeb 
16696b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
16706b4cac81SBjoern A. Zeeb #if 1
16716b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
16726b4cac81SBjoern A. Zeeb 	vif->bss_conf.idle = true;
16736b4cac81SBjoern A. Zeeb 	vif->bss_conf.ps = false;
16746b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
16756b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
16766b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
16776b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
16786b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
16796b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
16806b4cac81SBjoern A. Zeeb 	vif->bss_conf.assoc = false;
16816b4cac81SBjoern A. Zeeb 	vif->bss_conf.aid = 0;
16826b4cac81SBjoern A. Zeeb #endif
16836b4cac81SBjoern A. Zeeb #if 0
16846b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
16856b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
16866b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
16876b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
16886b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
16896b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
16906b4cac81SBjoern A. Zeeb #endif
16916b4cac81SBjoern A. Zeeb 	IMPROVE();
16926b4cac81SBjoern A. Zeeb 
16936b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
16946b4cac81SBjoern A. Zeeb 	if (error != 0) {
16956b4cac81SBjoern A. Zeeb 		printf("%s: failed to start hw: %d\n", __func__, error);
16966b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
16976b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
16986b4cac81SBjoern A. Zeeb 		return (NULL);
16996b4cac81SBjoern A. Zeeb 	}
17006b4cac81SBjoern A. Zeeb 
17016b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
17026b4cac81SBjoern A. Zeeb 	if (error != 0) {
17036b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
17046b4cac81SBjoern A. Zeeb 		printf("%s: failed to add interface: %d\n", __func__, error);
17056b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
17066b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
17076b4cac81SBjoern A. Zeeb 		return (NULL);
17086b4cac81SBjoern A. Zeeb 	}
17096b4cac81SBjoern A. Zeeb 
17106b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17116b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
17126b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17136b4cac81SBjoern A. Zeeb 
17146b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
17156b4cac81SBjoern A. Zeeb 	changed = 0;
17166b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
17176b4cac81SBjoern A. Zeeb 
17186b4cac81SBjoern A. Zeeb 	/* conf_tx setup; default WME? */
17196b4cac81SBjoern A. Zeeb 
17206b4cac81SBjoern A. Zeeb 	/* Force MC init. */
17216b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
17226b4cac81SBjoern A. Zeeb 
17236b4cac81SBjoern A. Zeeb 	IMPROVE();
17246b4cac81SBjoern A. Zeeb 
17256b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
17266b4cac81SBjoern A. Zeeb 
17276b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
17286b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
17296b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
17306b4cac81SBjoern A. Zeeb 
17316b4cac81SBjoern A. Zeeb 	/* Key management. */
17326b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
17336b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
17346b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
17356b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
17366b4cac81SBjoern A. Zeeb #endif
17376b4cac81SBjoern A. Zeeb 	}
17386b4cac81SBjoern A. Zeeb 
17396b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
17406b4cac81SBjoern A. Zeeb 
17416b4cac81SBjoern A. Zeeb 	/* Complete setup. */
17426b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
17436b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
17446b4cac81SBjoern A. Zeeb 
17456b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
17466b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
17476b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
17486b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
17496b4cac81SBjoern A. Zeeb 
17506b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
17516b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
17526b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
17536b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
17546b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
17556b4cac81SBjoern A. Zeeb 	/* any others? */
17566b4cac81SBjoern A. Zeeb 	IMPROVE();
17576b4cac81SBjoern A. Zeeb 
17586b4cac81SBjoern A. Zeeb 	return (vap);
17596b4cac81SBjoern A. Zeeb }
17606b4cac81SBjoern A. Zeeb 
17616b4cac81SBjoern A. Zeeb static void
17626b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
17636b4cac81SBjoern A. Zeeb {
17646b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
17656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17666b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17696b4cac81SBjoern A. Zeeb 
17706b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17716b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17726b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
17736b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
17746b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17756b4cac81SBjoern A. Zeeb 
17766b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17776b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
17786b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17796b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
17806b4cac81SBjoern A. Zeeb 
17816b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
17826b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
17836b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
17846b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
17856b4cac81SBjoern A. Zeeb }
17866b4cac81SBjoern A. Zeeb 
17876b4cac81SBjoern A. Zeeb static void
17886b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
17896b4cac81SBjoern A. Zeeb {
17906b4cac81SBjoern A. Zeeb 
17916b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
17926b4cac81SBjoern A. Zeeb 	TRACEOK();
17936b4cac81SBjoern A. Zeeb }
17946b4cac81SBjoern A. Zeeb 
17956b4cac81SBjoern A. Zeeb static void
17966b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
17976b4cac81SBjoern A. Zeeb {
17986b4cac81SBjoern A. Zeeb 
17996b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
18006b4cac81SBjoern A. Zeeb }
18016b4cac81SBjoern A. Zeeb 
18026b4cac81SBjoern A. Zeeb static void
18036b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
18046b4cac81SBjoern A. Zeeb {
18056b4cac81SBjoern A. Zeeb 
18066b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
18076b4cac81SBjoern A. Zeeb }
18086b4cac81SBjoern A. Zeeb 
18096b4cac81SBjoern A. Zeeb /* Start / stop device. */
18106b4cac81SBjoern A. Zeeb static void
18116b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
18126b4cac81SBjoern A. Zeeb {
18136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18146b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18156b4cac81SBjoern A. Zeeb 	int error;
18166b4cac81SBjoern A. Zeeb 	bool start_all;
18176b4cac81SBjoern A. Zeeb 
18186b4cac81SBjoern A. Zeeb 	IMPROVE();
18196b4cac81SBjoern A. Zeeb 
18206b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
18216b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18226b4cac81SBjoern A. Zeeb 	start_all = false;
18236b4cac81SBjoern A. Zeeb 
18246b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
18256b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
18266b4cac81SBjoern A. Zeeb 		if (error == 0)
18276b4cac81SBjoern A. Zeeb 			start_all = true;
18286b4cac81SBjoern A. Zeeb 	} else {
18296b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
18306b4cac81SBjoern A. Zeeb 	}
18316b4cac81SBjoern A. Zeeb 
18326b4cac81SBjoern A. Zeeb 	if (start_all)
18336b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
18346b4cac81SBjoern A. Zeeb }
18356b4cac81SBjoern A. Zeeb 
18366b4cac81SBjoern A. Zeeb bool
18376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
18386b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
18396b4cac81SBjoern A. Zeeb {
18406b4cac81SBjoern A. Zeeb 	int i;
18416b4cac81SBjoern A. Zeeb 
18426b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
18436b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
18446b4cac81SBjoern A. Zeeb 			return (true);
18456b4cac81SBjoern A. Zeeb 	}
18466b4cac81SBjoern A. Zeeb 
18476b4cac81SBjoern A. Zeeb 	return (false);
18486b4cac81SBjoern A. Zeeb }
18496b4cac81SBjoern A. Zeeb 
18506b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
18516b4cac81SBjoern A. Zeeb bool
18526b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
18536b4cac81SBjoern A. Zeeb {
18546b4cac81SBjoern A. Zeeb 	size_t x;
18556b4cac81SBjoern A. Zeeb 	uint8_t l;
18566b4cac81SBjoern A. Zeeb 
18576b4cac81SBjoern A. Zeeb 	x = *xp;
18586b4cac81SBjoern A. Zeeb 
18596b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
18606b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
18616b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
18626b4cac81SBjoern A. Zeeb 	x += 2 + l;
18636b4cac81SBjoern A. Zeeb 
18646b4cac81SBjoern A. Zeeb 	if (x > ies_len)
18656b4cac81SBjoern A. Zeeb 		return (false);
18666b4cac81SBjoern A. Zeeb 
18676b4cac81SBjoern A. Zeeb 	*xp = x;
18686b4cac81SBjoern A. Zeeb 	return (true);
18696b4cac81SBjoern A. Zeeb }
18706b4cac81SBjoern A. Zeeb 
18716b4cac81SBjoern A. Zeeb static int
18726b4cac81SBjoern A. Zeeb lkpi_ieee80211_probereq_ie_alloc(struct ieee80211vap *vap,
18736b4cac81SBjoern A. Zeeb     struct ieee80211com *ic, struct ieee80211_scan_ies *scan_ies,
18746b4cac81SBjoern A. Zeeb     const uint8_t *ssid, size_t ssidlen)
18756b4cac81SBjoern A. Zeeb {
18766b4cac81SBjoern A. Zeeb 
18776b4cac81SBjoern A. Zeeb 	return (ieee80211_probereq_ie(vap, ic,
18786b4cac81SBjoern A. Zeeb 	    &scan_ies->common_ies, &scan_ies->common_ie_len,
18796b4cac81SBjoern A. Zeeb 	    ssid, ssidlen, true));
18806b4cac81SBjoern A. Zeeb }
18816b4cac81SBjoern A. Zeeb 
18826b4cac81SBjoern A. Zeeb static void
18836b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
18846b4cac81SBjoern A. Zeeb {
18856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18866b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18876b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18886b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18896b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
18906b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
18916b4cac81SBjoern A. Zeeb 	int error;
18926b4cac81SBjoern A. Zeeb 
18936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
18946b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0) {
18956b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
18966b4cac81SBjoern A. Zeeb 		return;
18976b4cac81SBjoern A. Zeeb 	}
18986b4cac81SBjoern A. Zeeb 
18996b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
19006b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
19016b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
1902*d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
19036b4cac81SBjoern A. Zeeb 		return;
19046b4cac81SBjoern A. Zeeb 	}
19056b4cac81SBjoern A. Zeeb 
19066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19076b4cac81SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) {
1908*d3ef7fb4SBjoern A. Zeeb sw_scan:
19096b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
19106b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
1911086be6a8SBjoern A. Zeeb 
1912086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
1913086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
1914086be6a8SBjoern A. Zeeb 
19156b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
19166b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
19176b4cac81SBjoern A. Zeeb 		IMPROVE();
19186b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
19196b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
19206b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
19216b4cac81SBjoern A. Zeeb 
19226b4cac81SBjoern A. Zeeb 	} else {
19236b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
19246b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
19256b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
19266b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
19276b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
19286b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
19296b4cac81SBjoern A. Zeeb 		int i;
19306b4cac81SBjoern A. Zeeb 
19316b4cac81SBjoern A. Zeeb 		ssids_len = ss->ss_nssid * sizeof(*ssids);;
19326b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
19336b4cac81SBjoern A. Zeeb 
19346b4cac81SBjoern A. Zeeb 		nchan = 0;
19356b4cac81SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++)
19366b4cac81SBjoern A. Zeeb 			nchan++;
19376b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
19386b4cac81SBjoern A. Zeeb 
19396b4cac81SBjoern A. Zeeb 		KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
19406b4cac81SBjoern A. Zeeb 		    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
19416b4cac81SBjoern A. Zeeb 		lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
19426b4cac81SBjoern A. Zeeb 		    s6ghzlen + chan_len, M_LKPI80211, M_WAITOK | M_ZERO);
19436b4cac81SBjoern A. Zeeb 
19446b4cac81SBjoern A. Zeeb 		error = lkpi_ieee80211_probereq_ie_alloc(vap, ic,
19456b4cac81SBjoern A. Zeeb 		    &hw_req->ies, NULL, -1);
19466b4cac81SBjoern A. Zeeb 		if (error != 0)
19476b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: probereq_ie returned %d\n",
19486b4cac81SBjoern A. Zeeb 			    __func__, error);
19496b4cac81SBjoern A. Zeeb 
19506b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
19516b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
19526b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
19536b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
19546b4cac81SBjoern A. Zeeb #if 0
19556b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
19566b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
19576b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
19586b4cac81SBjoern A. Zeeb #endif
19596b4cac81SBjoern A. Zeeb #ifdef __notyet__
19606b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
19616b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
19626b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
19636b4cac81SBjoern A. Zeeb #endif
19646b4cac81SBjoern A. Zeeb #if 0
19656b4cac81SBjoern A. Zeeb 		hw_req->req.ie_len = ;
19666b4cac81SBjoern A. Zeeb 		hw_req->req.ie = ;
19676b4cac81SBjoern A. Zeeb #endif
1968*d3ef7fb4SBjoern A. Zeeb #if 0
1969*d3ef7fb4SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len
1970*d3ef7fb4SBjoern A. Zeeb 		hw->wiphy->max_scan_ssids
1971*d3ef7fb4SBjoern A. Zeeb #endif
19726b4cac81SBjoern A. Zeeb 
19736b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
19746b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
19756b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
19766b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
19776b4cac81SBjoern A. Zeeb 			*(cpp + i) =
19786b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
19796b4cac81SBjoern A. Zeeb 		}
19806b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
19816b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
19826b4cac81SBjoern A. Zeeb 
19836b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
19846b4cac81SBjoern A. Zeeb 			lc->center_freq = c->ic_freq;
19856b4cac81SBjoern A. Zeeb 			/* lc->flags */
19866b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
19876b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
19886b4cac81SBjoern A. Zeeb 			/* lc-> ... */
19896b4cac81SBjoern A. Zeeb 			lc++;
19906b4cac81SBjoern A. Zeeb 		}
19916b4cac81SBjoern A. Zeeb 
19926b4cac81SBjoern A. Zeeb 		hw_req->req.n_ssids = ss->ss_nssid;
19936b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
19946b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
19956b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
19966b4cac81SBjoern A. Zeeb 			for (i = 0; i < ss->ss_nssid; i++) {
19976b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
19986b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
19996b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
20006b4cac81SBjoern A. Zeeb 				ssids++;
20016b4cac81SBjoern A. Zeeb 			}
20026b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
20036b4cac81SBjoern A. Zeeb 		} else {
20046b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
20056b4cac81SBjoern A. Zeeb 		}
20066b4cac81SBjoern A. Zeeb 
20076b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
20086b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
20096b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
20106b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
20116b4cac81SBjoern A. Zeeb 		/* s6gp->... */
20126b4cac81SBjoern A. Zeeb 
20136b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
20146b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
20156b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
20166b4cac81SBjoern A. Zeeb 		if (error != 0) {
20176b4cac81SBjoern A. Zeeb 			free(hw_req->ies.common_ies, M_80211_VAP);
20186b4cac81SBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
20196b4cac81SBjoern A. Zeeb 			lhw->hw_req = NULL;
2020*d3ef7fb4SBjoern A. Zeeb 
2021*d3ef7fb4SBjoern A. Zeeb 			/*
2022*d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
2023*d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
2024*d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
2025*d3ef7fb4SBjoern A. Zeeb 			 */
2026*d3ef7fb4SBjoern A. Zeeb 			if (error == 1)
2027*d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
2028*d3ef7fb4SBjoern A. Zeeb 
2029*d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2030*d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
2031*d3ef7fb4SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
20326b4cac81SBjoern A. Zeeb 		}
20336b4cac81SBjoern A. Zeeb 	}
20346b4cac81SBjoern A. Zeeb }
20356b4cac81SBjoern A. Zeeb 
20366b4cac81SBjoern A. Zeeb static void
20376b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
20386b4cac81SBjoern A. Zeeb {
20396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20406b4cac81SBjoern A. Zeeb 
20416b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
20426b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
20436b4cac81SBjoern A. Zeeb 		return;
20446b4cac81SBjoern A. Zeeb 	}
20456b4cac81SBjoern A. Zeeb 
20466b4cac81SBjoern A. Zeeb 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
20476b4cac81SBjoern A. Zeeb 		/* Nothing to do. */
20486b4cac81SBjoern A. Zeeb 	} else {
20496b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
20506b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
20516b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
20526b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
20536b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
20546b4cac81SBjoern A. Zeeb 
20556b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
20566b4cac81SBjoern A. Zeeb 		ss = ic->ic_scan;
20576b4cac81SBjoern A. Zeeb 		vap = ss->ss_vap;
20586b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
20596b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
20606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
20616b4cac81SBjoern A. Zeeb 
20626b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
2063086be6a8SBjoern A. Zeeb 
2064086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
2065086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
20666b4cac81SBjoern A. Zeeb 	}
20676b4cac81SBjoern A. Zeeb }
20686b4cac81SBjoern A. Zeeb 
20696b4cac81SBjoern A. Zeeb static void
20706b4cac81SBjoern A. Zeeb lkpi_ic_scan_curchan_nada(struct ieee80211_scan_state *ss __unused,
20716b4cac81SBjoern A. Zeeb     unsigned long maxdwell __unused)
20726b4cac81SBjoern A. Zeeb {
20736b4cac81SBjoern A. Zeeb }
20746b4cac81SBjoern A. Zeeb 
20756b4cac81SBjoern A. Zeeb static void
20766b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
20776b4cac81SBjoern A. Zeeb {
20786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20796b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20806b4cac81SBjoern A. Zeeb 	int error;
20816b4cac81SBjoern A. Zeeb 
20826b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
20836b4cac81SBjoern A. Zeeb #ifdef __no_longer__
20846b4cac81SBjoern A. Zeeb 	/* For now only be concerned if scanning. */
20856b4cac81SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
20866b4cac81SBjoern A. Zeeb 		IMPROVE();
20876b4cac81SBjoern A. Zeeb 		return;
20886b4cac81SBjoern A. Zeeb 	}
20896b4cac81SBjoern A. Zeeb #endif
20906b4cac81SBjoern A. Zeeb 
20916b4cac81SBjoern A. Zeeb 	if (ic->ic_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
20926b4cac81SBjoern A. Zeeb 		/*
20936b4cac81SBjoern A. Zeeb 		 * AP scanning is taken care of by firmware, so only switch
20946b4cac81SBjoern A. Zeeb 		 * channels in monitor mode (maybe, maybe not; to be
20956b4cac81SBjoern A. Zeeb 		 * investigated at the right time).
20966b4cac81SBjoern A. Zeeb 		 */
20976b4cac81SBjoern A. Zeeb 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
20986b4cac81SBjoern A. Zeeb 			UNIMPLEMENTED;
20996b4cac81SBjoern A. Zeeb 		}
21006b4cac81SBjoern A. Zeeb 	} else {
21016b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c = ic->ic_curchan;
21026b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
21036b4cac81SBjoern A. Zeeb 		struct cfg80211_chan_def chandef;
21046b4cac81SBjoern A. Zeeb 
21056b4cac81SBjoern A. Zeeb 		if (c == NULL || c == IEEE80211_CHAN_ANYC ||
21066b4cac81SBjoern A. Zeeb 		    lhw->ops->config == NULL) {
21076b4cac81SBjoern A. Zeeb 			ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
21086b4cac81SBjoern A. Zeeb 			    c, lhw->ops->config);
21096b4cac81SBjoern A. Zeeb 			return;
21106b4cac81SBjoern A. Zeeb 		}
21116b4cac81SBjoern A. Zeeb 
21126b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
21136b4cac81SBjoern A. Zeeb 		if (chan == NULL) {
21146b4cac81SBjoern A. Zeeb 			ic_printf(ic, "%s: c %p chan %p\n", __func__,
21156b4cac81SBjoern A. Zeeb 			    c, chan);
21166b4cac81SBjoern A. Zeeb 			return;
21176b4cac81SBjoern A. Zeeb 		}
21186b4cac81SBjoern A. Zeeb 
21196b4cac81SBjoern A. Zeeb 		memset(&chandef, 0, sizeof(chandef));
21206b4cac81SBjoern A. Zeeb 		chandef.chan = chan;
21216b4cac81SBjoern A. Zeeb 		chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
21226b4cac81SBjoern A. Zeeb 		chandef.center_freq1 = chandef.chan->center_freq;
21236b4cac81SBjoern A. Zeeb 
21246b4cac81SBjoern A. Zeeb 		/* XXX max power for scanning? */
21256b4cac81SBjoern A. Zeeb 		IMPROVE();
21266b4cac81SBjoern A. Zeeb 
21276b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
21286b4cac81SBjoern A. Zeeb 		hw->conf.chandef = chandef;
21296b4cac81SBjoern A. Zeeb 
21306b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
21316b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP) {
21326b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
21336b4cac81SBjoern A. Zeeb 			    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
21346b4cac81SBjoern A. Zeeb 			/* XXX should we unroll to the previous chandef? */
21356b4cac81SBjoern A. Zeeb 			IMPROVE();
21366b4cac81SBjoern A. Zeeb 		} else {
21376b4cac81SBjoern A. Zeeb 			/* Update radiotap channels as well. */
21386b4cac81SBjoern A. Zeeb 			lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
21396b4cac81SBjoern A. Zeeb 			lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
21406b4cac81SBjoern A. Zeeb 			lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
21416b4cac81SBjoern A. Zeeb 			lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
21426b4cac81SBjoern A. Zeeb 		}
21436b4cac81SBjoern A. Zeeb 
21446b4cac81SBjoern A. Zeeb 		/* Currently PS is hard coded off! Not sure it belongs here. */
21456b4cac81SBjoern A. Zeeb 		IMPROVE();
21466b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
21476b4cac81SBjoern A. Zeeb 		    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
21486b4cac81SBjoern A. Zeeb 			hw->conf.flags &= ~IEEE80211_CONF_PS;
21496b4cac81SBjoern A. Zeeb 			error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
21506b4cac81SBjoern A. Zeeb 			if (error != 0 && error != EOPNOTSUPP)
21516b4cac81SBjoern A. Zeeb 				ic_printf(ic, "ERROR: %s: config %#0x returned "
21526b4cac81SBjoern A. Zeeb 				    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
21536b4cac81SBjoern A. Zeeb 				    error);
21546b4cac81SBjoern A. Zeeb 		}
21556b4cac81SBjoern A. Zeeb 	}
21566b4cac81SBjoern A. Zeeb }
21576b4cac81SBjoern A. Zeeb 
21586b4cac81SBjoern A. Zeeb static struct ieee80211_node *
21596b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
21606b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
21616b4cac81SBjoern A. Zeeb {
21626b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
21636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21656b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21666b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
21676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21696b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
21706b4cac81SBjoern A. Zeeb 	int tid;
21716b4cac81SBjoern A. Zeeb 
21726b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
21736b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
21746b4cac81SBjoern A. Zeeb 
21756b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
21766b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_alloc != NULL) {
21776b4cac81SBjoern A. Zeeb 		ni = lhw->ic_node_alloc(vap, mac);
21786b4cac81SBjoern A. Zeeb 		if (ni == NULL)
21796b4cac81SBjoern A. Zeeb 			return (NULL);
21806b4cac81SBjoern A. Zeeb 	}
21816b4cac81SBjoern A. Zeeb 
21826b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21836b4cac81SBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
21846b4cac81SBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
21856b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
21866b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
21876b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
21886b4cac81SBjoern A. Zeeb 		return (NULL);
21896b4cac81SBjoern A. Zeeb 	}
21906b4cac81SBjoern A. Zeeb 
21916b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;
21926b4cac81SBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
21936b4cac81SBjoern A. Zeeb #if 0
21946b4cac81SBjoern A. Zeeb 	/*
21956b4cac81SBjoern A. Zeeb 	 * This needs to be done in node_init() as ieee80211_alloc_node()
21966b4cac81SBjoern A. Zeeb 	 * will initialise the refcount after us.
21976b4cac81SBjoern A. Zeeb 	 */
21986b4cac81SBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
21996b4cac81SBjoern A. Zeeb #endif
22006b4cac81SBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
22016b4cac81SBjoern A. Zeeb 	ni->ni_drv_data = lsta;
22026b4cac81SBjoern A. Zeeb 
22036b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22046b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22056b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22066b4cac81SBjoern A. Zeeb 
22076b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
22086b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
22096b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
22106b4cac81SBjoern A. Zeeb 
22116b4cac81SBjoern A. Zeeb 		/*
22126b4cac81SBjoern A. Zeeb 		 * We are neither limiting ourselves to hw.queues here,
22136b4cac81SBjoern A. Zeeb 		 * nor do we check if driver wants IEEE80211_NUM_TIDS queue.
22146b4cac81SBjoern A. Zeeb 		 */
22156b4cac81SBjoern A. Zeeb 
22166b4cac81SBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
22176b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
22186b4cac81SBjoern A. Zeeb 		if (ltxq == NULL)
22196b4cac81SBjoern A. Zeeb 			goto cleanup;
22206b4cac81SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
22216b4cac81SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
22226b4cac81SBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
22236b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
22246b4cac81SBjoern A. Zeeb 			IMPROVE();
22256b4cac81SBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
22266b4cac81SBjoern A. Zeeb 		} else {
22276b4cac81SBjoern A. Zeeb 			ltxq->txq.ac = tid_to_mac80211_ac[tid & 7];
22286b4cac81SBjoern A. Zeeb 		}
22296b4cac81SBjoern A. Zeeb 		ltxq->txq.tid = tid;
22306b4cac81SBjoern A. Zeeb 		ltxq->txq.sta = sta;
22316b4cac81SBjoern A. Zeeb 		ltxq->txq.vif = vif;
22326b4cac81SBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
22336b4cac81SBjoern A. Zeeb 	}
22346b4cac81SBjoern A. Zeeb 
22356b4cac81SBjoern A. Zeeb 	/* Deferred TX path. */
22366b4cac81SBjoern A. Zeeb 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
22376b4cac81SBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
22386b4cac81SBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
22396b4cac81SBjoern A. Zeeb 
22406b4cac81SBjoern A. Zeeb 	return (ni);
22416b4cac81SBjoern A. Zeeb 
22426b4cac81SBjoern A. Zeeb cleanup:
22436b4cac81SBjoern A. Zeeb 	for (; tid >= 0; tid--)
22446b4cac81SBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
22456b4cac81SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
22466b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
22476b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
22486b4cac81SBjoern A. Zeeb 	return (NULL);
22496b4cac81SBjoern A. Zeeb }
22506b4cac81SBjoern A. Zeeb 
22516b4cac81SBjoern A. Zeeb static int
22526b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
22536b4cac81SBjoern A. Zeeb {
22546b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
22556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22566b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22586b4cac81SBjoern A. Zeeb 	int error;
22596b4cac81SBjoern A. Zeeb 
22606b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
22616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
22626b4cac81SBjoern A. Zeeb 
22636b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
22646b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
22656b4cac81SBjoern A. Zeeb 		if (error != 0)
22666b4cac81SBjoern A. Zeeb 			return (error);
22676b4cac81SBjoern A. Zeeb 	}
22686b4cac81SBjoern A. Zeeb 
22696b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
22706b4cac81SBjoern A. Zeeb 
22716b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
22726b4cac81SBjoern A. Zeeb 
22736b4cac81SBjoern A. Zeeb 	/* Now take the reference before linking it to the table. */
22746b4cac81SBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
22756b4cac81SBjoern A. Zeeb 
22766b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22776b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
22786b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22796b4cac81SBjoern A. Zeeb 
22806b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
22816b4cac81SBjoern A. Zeeb 	IMPROVE();
22826b4cac81SBjoern A. Zeeb 
22836b4cac81SBjoern A. Zeeb 	return (0);
22846b4cac81SBjoern A. Zeeb }
22856b4cac81SBjoern A. Zeeb 
22866b4cac81SBjoern A. Zeeb static void
22876b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
22886b4cac81SBjoern A. Zeeb {
22896b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
22906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22916b4cac81SBjoern A. Zeeb 
22926b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
22936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
22946b4cac81SBjoern A. Zeeb 
22956b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
22966b4cac81SBjoern A. Zeeb 	IMPROVE();
22976b4cac81SBjoern A. Zeeb 
22986b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
22996b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
23006b4cac81SBjoern A. Zeeb }
23016b4cac81SBjoern A. Zeeb 
23026b4cac81SBjoern A. Zeeb static void
23036b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
23046b4cac81SBjoern A. Zeeb {
23056b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
23066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23086b4cac81SBjoern A. Zeeb 
23096b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
23106b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
23116b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
23126b4cac81SBjoern A. Zeeb 
23136b4cac81SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
23146b4cac81SBjoern A. Zeeb 	IMPROVE();
23156b4cac81SBjoern A. Zeeb 
23166b4cac81SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
23176b4cac81SBjoern A. Zeeb #ifdef __notyet__
23186b4cac81SBjoern A. Zeeb 	KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
23196b4cac81SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
23206b4cac81SBjoern A. Zeeb #endif
23216b4cac81SBjoern A. Zeeb 	/* Drain taskq. */
23226b4cac81SBjoern A. Zeeb 
23236b4cac81SBjoern A. Zeeb 	/* Drain sta->txq[] */
23246b4cac81SBjoern A. Zeeb 	mtx_destroy(&lsta->txq_mtx);
23256b4cac81SBjoern A. Zeeb 
23266b4cac81SBjoern A. Zeeb 	/* Remove lsta if added_to_drv. */
23276b4cac81SBjoern A. Zeeb 	/* Remove lsta from vif */
23286b4cac81SBjoern A. Zeeb 
23296b4cac81SBjoern A. Zeeb 	/* remove ref from lsta node... */
23306b4cac81SBjoern A. Zeeb 
23316b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
23326b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
23336b4cac81SBjoern A. Zeeb 
23346b4cac81SBjoern A. Zeeb 	/* Free lsta. */
23356b4cac81SBjoern A. Zeeb }
23366b4cac81SBjoern A. Zeeb 
23376b4cac81SBjoern A. Zeeb static int
23386b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
23396b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
23406b4cac81SBjoern A. Zeeb {
23416b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23426b4cac81SBjoern A. Zeeb 
23436b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
23446b4cac81SBjoern A. Zeeb 
23456b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
23466b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
23476b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
23486b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
23496b4cac81SBjoern A. Zeeb 
23506b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
23516b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
23526b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
23536b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
23546b4cac81SBjoern A. Zeeb 
23556b4cac81SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
23566b4cac81SBjoern A. Zeeb 	return (0);
23576b4cac81SBjoern A. Zeeb }
23586b4cac81SBjoern A. Zeeb 
23596b4cac81SBjoern A. Zeeb static void
23606b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
23616b4cac81SBjoern A. Zeeb {
23626b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23636b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
23646b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
23656b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
23666b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
23676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23686b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23696b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23706b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23716b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
23726b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
23736b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
23746b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
23756b4cac81SBjoern A. Zeeb 	void *buf;
23766b4cac81SBjoern A. Zeeb 	int ac;
23776b4cac81SBjoern A. Zeeb 
23786b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
23796b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23806b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX_DUMP)
23816b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
23826b4cac81SBjoern A. Zeeb #endif
23836b4cac81SBjoern A. Zeeb 
23846b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
23856b4cac81SBjoern A. Zeeb #ifndef TRY_HW_CRYPTO
23866b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
23876b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
23886b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
23896b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
23906b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
23916b4cac81SBjoern A. Zeeb 		if (k == NULL) {
23926b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
23936b4cac81SBjoern A. Zeeb 			m_freem(m);
23946b4cac81SBjoern A. Zeeb 			return;
23956b4cac81SBjoern A. Zeeb 		}
23966b4cac81SBjoern A. Zeeb 	}
23976b4cac81SBjoern A. Zeeb #endif
23986b4cac81SBjoern A. Zeeb 
23996b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
24006b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
24016b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24026b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
24036b4cac81SBjoern A. Zeeb 
24046b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
24056b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
24066b4cac81SBjoern A. Zeeb 
24076b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
24086b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
24096b4cac81SBjoern A. Zeeb 		if (k != NULL)
24106b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
24116b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
24126b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
24136b4cac81SBjoern A. Zeeb 		IMPROVE();
24146b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
24156b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
24166b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
24176b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
24186b4cac81SBjoern A. Zeeb 		}
24196b4cac81SBjoern A. Zeeb 
24206b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
24216b4cac81SBjoern A. Zeeb 	}
24226b4cac81SBjoern A. Zeeb 
24236b4cac81SBjoern A. Zeeb 	/*
24246b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
24256b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
24266b4cac81SBjoern A. Zeeb 	 */
24276b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
24286b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
24296b4cac81SBjoern A. Zeeb 		printf("XXX ERROR %s: skb alloc failed\n", __func__);
24306b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
24316b4cac81SBjoern A. Zeeb 		m_freem(m);
24326b4cac81SBjoern A. Zeeb 		return;
24336b4cac81SBjoern A. Zeeb 	}
24346b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
24356b4cac81SBjoern A. Zeeb 
24366b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
24376b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
24386b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
24396b4cac81SBjoern A. Zeeb 	skb->m = m;
24406b4cac81SBjoern A. Zeeb #if 0
24416b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
24426b4cac81SBjoern A. Zeeb #else
24436b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
24446b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
24456b4cac81SBjoern A. Zeeb #endif
24466b4cac81SBjoern A. Zeeb 	/* Save the ni. */
24476b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
24486b4cac81SBjoern A. Zeeb 
24496b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
24506b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24516b4cac81SBjoern A. Zeeb 
24526b4cac81SBjoern A. Zeeb 	/* XXX-BZ review this at some point [4] vs. [8] vs. [16](TID). */
24536b4cac81SBjoern A. Zeeb 	ac = M_WME_GETAC(m);
24546b4cac81SBjoern A. Zeeb 	skb->priority = WME_AC_TO_TID(ac);
24556b4cac81SBjoern A. Zeeb 	ac = lkpi_ac_net_to_l80211(ac);
24566b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
24576b4cac81SBjoern A. Zeeb 
24586b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
24596b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
24606b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
24616b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
24626b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
24636b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
24646b4cac81SBjoern A. Zeeb 	info->hw_queue = ac;		/* XXX-BZ is this correct? */
24656b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
24666b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
24676b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
24686b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
24696b4cac81SBjoern A. Zeeb 
24706b4cac81SBjoern A. Zeeb 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
24716b4cac81SBjoern A. Zeeb 	if (lsta != NULL) {
24726b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
24736b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
24746b4cac81SBjoern A. Zeeb 		info->control.hw_key = lsta->kc;
24756b4cac81SBjoern A. Zeeb #endif
24766b4cac81SBjoern A. Zeeb 	} else {
24776b4cac81SBjoern A. Zeeb 		sta = NULL;
24786b4cac81SBjoern A. Zeeb 	}
24796b4cac81SBjoern A. Zeeb 
24806b4cac81SBjoern A. Zeeb 	IMPROVE();
24816b4cac81SBjoern A. Zeeb 
24826b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
24836b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
24846b4cac81SBjoern A. Zeeb 
24856b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[ac]);	/* XXX-BZ re-check */
24866b4cac81SBjoern A. Zeeb 		/*
24876b4cac81SBjoern A. Zeeb 		 * We currently do not use queues but do direct TX.
24886b4cac81SBjoern A. Zeeb 		 * The exception to the rule is initial packets, as we cannot
24896b4cac81SBjoern A. Zeeb 		 * TX until queues are allocated (at least for iwlwifi).
24906b4cac81SBjoern A. Zeeb 		 * So we wake_tx_queue in newstate and register any dequeue
24916b4cac81SBjoern A. Zeeb 		 * calls.  In the time until then we queue packets and
24926b4cac81SBjoern A. Zeeb 		 * let the driver deal with them.
24936b4cac81SBjoern A. Zeeb 		 */
24946b4cac81SBjoern A. Zeeb 		if (!ltxq->seen_dequeue) {
24956b4cac81SBjoern A. Zeeb 
24966b4cac81SBjoern A. Zeeb 			/* Prevent an ordering problem, likely other issues. */
24976b4cac81SBjoern A. Zeeb 			while (!skb_queue_empty(&ltxq->skbq)) {
24986b4cac81SBjoern A. Zeeb 				struct sk_buff *skb2;
24996b4cac81SBjoern A. Zeeb 
25006b4cac81SBjoern A. Zeeb 				skb2 = skb_dequeue(&ltxq->skbq);
25016b4cac81SBjoern A. Zeeb 				if (skb2 != NULL) {
25026b4cac81SBjoern A. Zeeb 					memset(&control, 0, sizeof(control));
25036b4cac81SBjoern A. Zeeb 					control.sta = sta;
25046b4cac81SBjoern A. Zeeb 					lkpi_80211_mo_tx(hw, &control, skb2);
25056b4cac81SBjoern A. Zeeb 				}
25066b4cac81SBjoern A. Zeeb 			}
25076b4cac81SBjoern A. Zeeb 			goto ops_tx;
25086b4cac81SBjoern A. Zeeb 		}
25096b4cac81SBjoern A. Zeeb 		if (0 && ltxq->seen_dequeue && skb_queue_empty(&ltxq->skbq))
25106b4cac81SBjoern A. Zeeb 			goto ops_tx;
25116b4cac81SBjoern A. Zeeb 
25126b4cac81SBjoern A. Zeeb 		skb_queue_tail(&ltxq->skbq, skb);
25136b4cac81SBjoern A. Zeeb 		if (debug_80211 & D80211_TRACE_TX)
25146b4cac81SBjoern A. Zeeb 			printf("%s:%d lsta %p sta %p ni %p %6D skb %p lxtq %p "
25156b4cac81SBjoern A. Zeeb 			    "qlen %u WAKE_TX_Q ac %d prio %u qmap %u\n",
25166b4cac81SBjoern A. Zeeb 			    __func__, __LINE__, lsta, sta, ni,
25176b4cac81SBjoern A. Zeeb 			    ni->ni_macaddr, ":", skb, ltxq,
25186b4cac81SBjoern A. Zeeb 			    skb_queue_len(&ltxq->skbq), ac,
25196b4cac81SBjoern A. Zeeb 			    skb->priority, skb->qmap);
25206b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[ac]);	/* XXX-BZ */
25216b4cac81SBjoern A. Zeeb 		return;
25226b4cac81SBjoern A. Zeeb 	}
25236b4cac81SBjoern A. Zeeb 
25246b4cac81SBjoern A. Zeeb ops_tx:
25256b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
25266b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p sta %p ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
25276b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
25286b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
25296b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
25306b4cac81SBjoern A. Zeeb 	control.sta = sta;
25316b4cac81SBjoern A. Zeeb 
25326b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
25336b4cac81SBjoern A. Zeeb 	return;
25346b4cac81SBjoern A. Zeeb }
25356b4cac81SBjoern A. Zeeb 
25366b4cac81SBjoern A. Zeeb static void
25376b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
25386b4cac81SBjoern A. Zeeb {
25396b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25406b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25416b4cac81SBjoern A. Zeeb 	struct mbufq mq;
25426b4cac81SBjoern A. Zeeb 	struct mbuf *m;
25436b4cac81SBjoern A. Zeeb 
25446b4cac81SBjoern A. Zeeb 	lsta = ctx;
25456b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
25466b4cac81SBjoern A. Zeeb 
25476b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_TX)
25486b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
25496b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
25506b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
25516b4cac81SBjoern A. Zeeb 
25526b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
25536b4cac81SBjoern A. Zeeb 
25546b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
25556b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
25566b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
25576b4cac81SBjoern A. Zeeb 
25586b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
25596b4cac81SBjoern A. Zeeb 	while (m != NULL) {
25606b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
25616b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
25626b4cac81SBjoern A. Zeeb 	}
25636b4cac81SBjoern A. Zeeb }
25646b4cac81SBjoern A. Zeeb 
25656b4cac81SBjoern A. Zeeb static int
25666b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
25676b4cac81SBjoern A. Zeeb {
25686b4cac81SBjoern A. Zeeb 
25696b4cac81SBjoern A. Zeeb 	/* XXX TODO */
25706b4cac81SBjoern A. Zeeb 	IMPROVE();
25716b4cac81SBjoern A. Zeeb 
25726b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
25736b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25746b4cac81SBjoern A. Zeeb 
25756b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
25766b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
25776b4cac81SBjoern A. Zeeb }
25786b4cac81SBjoern A. Zeeb 
25796b4cac81SBjoern A. Zeeb static void
25806b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
25816b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
25826b4cac81SBjoern A. Zeeb {
25836b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25846b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25856b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
25866b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
25876b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
25886b4cac81SBjoern A. Zeeb 
25896b4cac81SBjoern A. Zeeb 	/* Channels */
25906b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
25916b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25926b4cac81SBjoern A. Zeeb 
25936b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
25946b4cac81SBjoern A. Zeeb 	nchans = 0;
25956b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
25966b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
25976b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
25986b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
25996b4cac81SBjoern A. Zeeb 		chan_flags = 0;
26006b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
26016b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
26026b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
26036b4cac81SBjoern A. Zeeb #ifdef __notyet__
26046b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
26056b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NG);
26066b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
26076b4cac81SBjoern A. Zeeb 		}
26086b4cac81SBjoern A. Zeeb #endif
26096b4cac81SBjoern A. Zeeb 
26106b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
2611cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
26126b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
26136b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
26146b4cac81SBjoern A. Zeeb 
26156b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
26166b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
26176b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
26186b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
26196b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
26206b4cac81SBjoern A. Zeeb 				continue;
26216b4cac81SBjoern A. Zeeb 			}
26226b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
26236b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
26246b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
26256b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
26266b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
26276b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
26286b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
26296b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
26306b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
26316b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
26326b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
26336b4cac81SBjoern A. Zeeb 
26346b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
26356b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
26366b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
26376b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
2638cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
2639cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
26406b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
26416b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
26426b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
26436b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
26446b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
2645cee56e77SBjoern A. Zeeb 			if (error != 0)
26466b4cac81SBjoern A. Zeeb 				break;
26476b4cac81SBjoern A. Zeeb 		}
26486b4cac81SBjoern A. Zeeb 	}
26496b4cac81SBjoern A. Zeeb 
26506b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
26516b4cac81SBjoern A. Zeeb 	nchans = 0;
26526b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
26536b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
26546b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
26556b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
26566b4cac81SBjoern A. Zeeb 		chan_flags = 0;
26576b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
26586b4cac81SBjoern A. Zeeb #ifdef __not_yet__
26596b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
26606b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_11NA);
26616b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_HT40;
26626b4cac81SBjoern A. Zeeb 		}
26636b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
26646b4cac81SBjoern A. Zeeb 
26656b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
26666b4cac81SBjoern A. Zeeb 			ic->ic_vhtcaps =
26676b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
26686b4cac81SBjoern A. Zeeb 
26696b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
26706b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
26716b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
26726b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
26736b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
26746b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
26756b4cac81SBjoern A. Zeeb 			    ic->ic_vhtcaps))
26766b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
26776b4cac81SBjoern A. Zeeb 		}
26786b4cac81SBjoern A. Zeeb #endif
26796b4cac81SBjoern A. Zeeb 
26806b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
2681cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
26826b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
26836b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
26846b4cac81SBjoern A. Zeeb 
26856b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
26866b4cac81SBjoern A. Zeeb 				printf("%s: %s: Skipping disabled chan "
26876b4cac81SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", ic->ic_name, __func__,
26886b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
26896b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
26906b4cac81SBjoern A. Zeeb 				continue;
26916b4cac81SBjoern A. Zeeb 			}
26926b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
26936b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
26946b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
26956b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
26966b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
26976b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
26986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
26996b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
27006b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
27016b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
27026b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
27036b4cac81SBjoern A. Zeeb 
27046b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
27056b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
27066b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
27076b4cac81SBjoern A. Zeeb 			    nflags, bands, chan_flags);
2708cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
2709cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
27106b4cac81SBjoern A. Zeeb 				printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
27116b4cac81SBjoern A. Zeeb 				    "returned error %d\n", ic->ic_name,
27126b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
27136b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
27146b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
2715cee56e77SBjoern A. Zeeb 			if (error != 0)
27166b4cac81SBjoern A. Zeeb 				break;
27176b4cac81SBjoern A. Zeeb 		}
27186b4cac81SBjoern A. Zeeb 	}
27196b4cac81SBjoern A. Zeeb }
27206b4cac81SBjoern A. Zeeb 
27216b4cac81SBjoern A. Zeeb static void *
27226b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
27236b4cac81SBjoern A. Zeeb {
27246b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27256b4cac81SBjoern A. Zeeb 
27266b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
27276b4cac81SBjoern A. Zeeb 	if (ic == NULL)
27286b4cac81SBjoern A. Zeeb 		return (NULL);
27296b4cac81SBjoern A. Zeeb 
27306b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
27316b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
27326b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
27336b4cac81SBjoern A. Zeeb 
27346b4cac81SBjoern A. Zeeb 	return (ic);
27356b4cac81SBjoern A. Zeeb }
27366b4cac81SBjoern A. Zeeb 
27376b4cac81SBjoern A. Zeeb struct ieee80211_hw *
27386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
27396b4cac81SBjoern A. Zeeb {
27406b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27426b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
27436b4cac81SBjoern A. Zeeb 
27446b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
27456b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
27466b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
27476b4cac81SBjoern A. Zeeb 		return (NULL);
27486b4cac81SBjoern A. Zeeb 
27496b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
27506b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
27516b4cac81SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(wiphy), 0);
27526b4cac81SBjoern A. Zeeb 	if (lhw->workq == NULL) {
27536b4cac81SBjoern A. Zeeb 		wiphy_free(wiphy);
27546b4cac81SBjoern A. Zeeb 		return (NULL);
27556b4cac81SBjoern A. Zeeb 	}
27566b4cac81SBjoern A. Zeeb 	mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
27576b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
27586b4cac81SBjoern A. Zeeb 
27596b4cac81SBjoern A. Zeeb 	/*
27606b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
27616b4cac81SBjoern A. Zeeb 	 * not initialized.
27626b4cac81SBjoern A. Zeeb 	 */
27636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27646b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
2765086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
27666b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
27676b4cac81SBjoern A. Zeeb 
27686b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
27696b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
27706b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
27716b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
27726b4cac81SBjoern A. Zeeb 		return (NULL);
27736b4cac81SBjoern A. Zeeb 	}
27746b4cac81SBjoern A. Zeeb 
27756b4cac81SBjoern A. Zeeb 	IMPROVE();
27766b4cac81SBjoern A. Zeeb 
27776b4cac81SBjoern A. Zeeb 	return (hw);
27786b4cac81SBjoern A. Zeeb }
27796b4cac81SBjoern A. Zeeb 
27806b4cac81SBjoern A. Zeeb void
27816b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
27826b4cac81SBjoern A. Zeeb {
27836b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27846b4cac81SBjoern A. Zeeb 
27856b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
27866b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
27876b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
27886b4cac81SBjoern A. Zeeb 
27896b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
27906b4cac81SBjoern A. Zeeb 	mtx_destroy(&lhw->mtx);
27916b4cac81SBjoern A. Zeeb 	IMPROVE();
27926b4cac81SBjoern A. Zeeb }
27936b4cac81SBjoern A. Zeeb 
27946b4cac81SBjoern A. Zeeb void
27956b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
27966b4cac81SBjoern A. Zeeb {
27976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27986b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27996b4cac81SBjoern A. Zeeb 
28006b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
28016b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
28026b4cac81SBjoern A. Zeeb 
28036b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
28046b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
28056b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
28066b4cac81SBjoern A. Zeeb 
28076b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
28086b4cac81SBjoern A. Zeeb }
28096b4cac81SBjoern A. Zeeb 
28106b4cac81SBjoern A. Zeeb struct ieee80211_hw *
28116b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
28126b4cac81SBjoern A. Zeeb {
28136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28146b4cac81SBjoern A. Zeeb 
28156b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
28166b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
28176b4cac81SBjoern A. Zeeb }
28186b4cac81SBjoern A. Zeeb 
28196b4cac81SBjoern A. Zeeb static void
28206b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
28216b4cac81SBjoern A. Zeeb {
28226b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28236b4cac81SBjoern A. Zeeb 
28246b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
28256b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
28266b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
28276b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
28286b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
28296b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
28306b4cac81SBjoern A. Zeeb }
28316b4cac81SBjoern A. Zeeb 
28322e183d99SBjoern A. Zeeb int
28336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
28346b4cac81SBjoern A. Zeeb {
28356b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28376b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
28386b4cac81SBjoern A. Zeeb 	int i;
28396b4cac81SBjoern A. Zeeb #endif
28406b4cac81SBjoern A. Zeeb 
28416b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
28426b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
28436b4cac81SBjoern A. Zeeb 
28446b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
28456b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
28466b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
28476b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
28486b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
28496b4cac81SBjoern A. Zeeb 		/* We take the first one. */
28506b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
28516b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
28526b4cac81SBjoern A. Zeeb 	} else {
28536b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
28546b4cac81SBjoern A. Zeeb 	}
28556b4cac81SBjoern A. Zeeb 
28566b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
28576b4cac81SBjoern A. Zeeb 
28586b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
28596b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
28606b4cac81SBjoern A. Zeeb 
28616b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
28626b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
28636b4cac81SBjoern A. Zeeb 	ic->ic_caps =
28646b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
28656b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
28666b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
28676b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
28686b4cac81SBjoern A. Zeeb #if 0
28696b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
28706b4cac81SBjoern A. Zeeb #endif
28716b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
28726b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
28736b4cac81SBjoern A. Zeeb 	    ;
28746b4cac81SBjoern A. Zeeb #if 0
28756b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
28766b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
28776b4cac81SBjoern A. Zeeb #endif
28786b4cac81SBjoern A. Zeeb 	if (lhw->ops->hw_scan &&
28796b4cac81SBjoern A. Zeeb 	    ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
28806b4cac81SBjoern A. Zeeb 		/* Advertise full-offload scanning */
28816b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
28826b4cac81SBjoern A. Zeeb 	}
28836b4cac81SBjoern A. Zeeb 
28846b4cac81SBjoern A. Zeeb #ifdef __notyet__
28856b4cac81SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
28866b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
28876b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
28886b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_MAXAMSDU_3839
28896b4cac81SBjoern A. Zeeb 						/* max A-MSDU length */
28906b4cac81SBjoern A. Zeeb 		    | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
28916b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
28926b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
28936b4cac81SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
28946b4cac81SBjoern A. Zeeb #endif
28956b4cac81SBjoern A. Zeeb 
28966b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
28976b4cac81SBjoern A. Zeeb #ifdef TRY_HW_CRYPTO
28986b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
28996b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
29006b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
29016b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
29026b4cac81SBjoern A. Zeeb 	}
29036b4cac81SBjoern A. Zeeb #endif
29046b4cac81SBjoern A. Zeeb 
29056b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
29066b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
29076b4cac81SBjoern A. Zeeb 
29086b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
29096b4cac81SBjoern A. Zeeb 
29106b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
29116b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
29126b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
29136b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
29146b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
29156b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
29166b4cac81SBjoern A. Zeeb 	if (lhw->ops->hw_scan &&
29176b4cac81SBjoern A. Zeeb 	    ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS))
29186b4cac81SBjoern A. Zeeb 		ic->ic_scan_curchan = lkpi_ic_scan_curchan_nada;
29196b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
29206b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
29216b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
29226b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
29236b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
29246b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
29256b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
29266b4cac81SBjoern A. Zeeb 
29276b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
29286b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
29296b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
29306b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
29316b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
29326b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
29336b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
29346b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
29356b4cac81SBjoern A. Zeeb 
29366b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
29376b4cac81SBjoern A. Zeeb 
29386b4cac81SBjoern A. Zeeb 	if (bootverbose)
29396b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
29402e183d99SBjoern A. Zeeb 
29412e183d99SBjoern A. Zeeb 	return (0);
29426b4cac81SBjoern A. Zeeb }
29436b4cac81SBjoern A. Zeeb 
29446b4cac81SBjoern A. Zeeb void
29456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
29466b4cac81SBjoern A. Zeeb {
29476b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29486b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
29496b4cac81SBjoern A. Zeeb 
29506b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
29516b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
29526b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
29536b4cac81SBjoern A. Zeeb }
29546b4cac81SBjoern A. Zeeb 
29556b4cac81SBjoern A. Zeeb void
29566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
29576b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
29586b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
29596b4cac81SBjoern A. Zeeb     void *arg)
29606b4cac81SBjoern A. Zeeb {
29616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29626b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29646b4cac81SBjoern A. Zeeb 	bool active, atomic;
29656b4cac81SBjoern A. Zeeb 
29666b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
29676b4cac81SBjoern A. Zeeb 
29686b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
29696b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
29706b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
29716b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
29726b4cac81SBjoern A. Zeeb 		    __func__, flags);
29736b4cac81SBjoern A. Zeeb 	}
29746b4cac81SBjoern A. Zeeb 
29756b4cac81SBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
29766b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
29776b4cac81SBjoern A. Zeeb 
29786b4cac81SBjoern A. Zeeb 	if (atomic)
29796b4cac81SBjoern A. Zeeb 		LKPI_80211_LHW_LOCK(lhw);
29806b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
29816b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
29826b4cac81SBjoern A. Zeeb 
29836b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
29846b4cac81SBjoern A. Zeeb 
29856b4cac81SBjoern A. Zeeb 		/*
29866b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
29876b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
29886b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
29896b4cac81SBjoern A. Zeeb 		 * driver does not know about.
29906b4cac81SBjoern A. Zeeb 		 */
29916b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
29926b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
29936b4cac81SBjoern A. Zeeb 			continue;
29946b4cac81SBjoern A. Zeeb 
29956b4cac81SBjoern A. Zeeb 		/*
29966b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
29976b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
29986b4cac81SBjoern A. Zeeb 		 */
29996b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
30006b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
30016b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
30026b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
30036b4cac81SBjoern A. Zeeb 	}
30046b4cac81SBjoern A. Zeeb 	if (atomic)
30056b4cac81SBjoern A. Zeeb 		LKPI_80211_LHW_UNLOCK(lhw);
30066b4cac81SBjoern A. Zeeb }
30076b4cac81SBjoern A. Zeeb 
30086b4cac81SBjoern A. Zeeb void
30096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
30106b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
30116b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
30126b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
30136b4cac81SBjoern A. Zeeb     void *arg)
30146b4cac81SBjoern A. Zeeb {
30156b4cac81SBjoern A. Zeeb 
30166b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30176b4cac81SBjoern A. Zeeb }
30186b4cac81SBjoern A. Zeeb 
30196b4cac81SBjoern A. Zeeb void
30206b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
30216b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
30226b4cac81SBjoern A. Zeeb 	void *),
30236b4cac81SBjoern A. Zeeb     void *arg)
30246b4cac81SBjoern A. Zeeb {
30256b4cac81SBjoern A. Zeeb 
30266b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30276b4cac81SBjoern A. Zeeb }
30286b4cac81SBjoern A. Zeeb 
30296b4cac81SBjoern A. Zeeb void
30306b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
30316b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
30326b4cac81SBjoern A. Zeeb {
30336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30346b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30356b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
30366b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
30376b4cac81SBjoern A. Zeeb 
30386b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
30396b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
30406b4cac81SBjoern A. Zeeb 
30416b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
30426b4cac81SBjoern A. Zeeb 
30436b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
30446b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
30456b4cac81SBjoern A. Zeeb 
30466b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
30476b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
30486b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
30496b4cac81SBjoern A. Zeeb 				continue;
30506b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
30516b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
30526b4cac81SBjoern A. Zeeb 		}
30536b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
30546b4cac81SBjoern A. Zeeb 	}
30556b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
30566b4cac81SBjoern A. Zeeb }
30576b4cac81SBjoern A. Zeeb 
30586b4cac81SBjoern A. Zeeb int
30596b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
30606b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
30616b4cac81SBjoern A. Zeeb {
30626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30636b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30646b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
30656b4cac81SBjoern A. Zeeb 
30666b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
30676b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
30686b4cac81SBjoern A. Zeeb 
30696b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
30706b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
30716b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
30726b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
30736b4cac81SBjoern A. Zeeb 	}
30746b4cac81SBjoern A. Zeeb 
30756b4cac81SBjoern A. Zeeb 	TODO();
30766b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
30776b4cac81SBjoern A. Zeeb 
30786b4cac81SBjoern A. Zeeb 	return (0);
30796b4cac81SBjoern A. Zeeb }
30806b4cac81SBjoern A. Zeeb 
30816b4cac81SBjoern A. Zeeb void
30826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
30836b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
30846b4cac81SBjoern A. Zeeb {
30856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30866b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30876b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
30886b4cac81SBjoern A. Zeeb 
30896b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
30906b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
30916b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
30926b4cac81SBjoern A. Zeeb 
30936b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
30946b4cac81SBjoern A. Zeeb 
30956b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
30966b4cac81SBjoern A. Zeeb 	free(lhw->hw_req->ies.common_ies, M_80211_VAP);
30976b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
30986b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
30996b4cac81SBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_SCAN_RUNNING;
31006b4cac81SBjoern A. Zeeb 	wakeup(lhw);
31016b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
31026b4cac81SBjoern A. Zeeb 
31036b4cac81SBjoern A. Zeeb 	return;
31046b4cac81SBjoern A. Zeeb }
31056b4cac81SBjoern A. Zeeb 
31066b4cac81SBjoern A. Zeeb void
31076b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
31086b4cac81SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused)
31096b4cac81SBjoern A. Zeeb {
31106b4cac81SBjoern A. Zeeb 	struct epoch_tracker et;
31116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
31126b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
31136b4cac81SBjoern A. Zeeb 	struct mbuf *m;
31146b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
31156b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
31166b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
31176b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
31186b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
31196b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
31206b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
31216b4cac81SBjoern A. Zeeb 	int i, offset, ok, type;
31226b4cac81SBjoern A. Zeeb 
31236b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
31246b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
31256b4cac81SBjoern A. Zeeb 		IMPROVE();
31266b4cac81SBjoern A. Zeeb 		goto err;
31276b4cac81SBjoern A. Zeeb 	}
31286b4cac81SBjoern A. Zeeb 
31296b4cac81SBjoern A. Zeeb 	/*
31306b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
31316b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
31326b4cac81SBjoern A. Zeeb 	 */
31336b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
31346b4cac81SBjoern A. Zeeb 	if (m == NULL)
31356b4cac81SBjoern A. Zeeb 		goto err;
31366b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
31376b4cac81SBjoern A. Zeeb 
31386b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
31396b4cac81SBjoern A. Zeeb 	offset = m->m_len;
31406b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
31416b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
31426b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
31436b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
31446b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
31456b4cac81SBjoern A. Zeeb 	}
31466b4cac81SBjoern A. Zeeb 
31476b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
31486b4cac81SBjoern A. Zeeb 
31496b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31506b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
31516b4cac81SBjoern A. Zeeb 	if ((debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
31526b4cac81SBjoern A. Zeeb 	    ieee80211_is_beacon(hdr->frame_control))
31536b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
31546b4cac81SBjoern A. Zeeb 
31556b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
31566b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
31576b4cac81SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u\n",
31586b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
31596b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
31606b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
31616b4cac81SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len);
31626b4cac81SBjoern A. Zeeb 
31636b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX_DUMP)
31646b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
31656b4cac81SBjoern A. Zeeb 
31666b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
31676b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
3168c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
31696b4cac81SBjoern A. Zeeb 		    "%u band %u, %u %u %u %u, %u, %#x %#x %#x %#x %u %u %u\n",
31706b4cac81SBjoern A. Zeeb 			__func__,
3171c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
3172c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
31736b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
31746b4cac81SBjoern A. Zeeb 			rx_status->flag,
31756b4cac81SBjoern A. Zeeb 			rx_status->freq,
31766b4cac81SBjoern A. Zeeb 			rx_status->bw,
31776b4cac81SBjoern A. Zeeb 			rx_status->encoding,
31786b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
31796b4cac81SBjoern A. Zeeb 			rx_status->band,
31806b4cac81SBjoern A. Zeeb 			rx_status->chains,
31816b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
31826b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
31836b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
31846b4cac81SBjoern A. Zeeb 			rx_status->signal,
31856b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
31866b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
31876b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
31886b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
31896b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
31906b4cac81SBjoern A. Zeeb 			rx_status->nss,
31916b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
31926b4cac81SBjoern A. Zeeb no_trace_beacons:
31936b4cac81SBjoern A. Zeeb #endif
31946b4cac81SBjoern A. Zeeb 
31956b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
31966b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
31976b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
31986b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
31996b4cac81SBjoern A. Zeeb 		rx_stats.c_rssi = rx_status->signal;
32006b4cac81SBjoern A. Zeeb 	else
32016b4cac81SBjoern A. Zeeb 		rx_stats.c_rssi = 0;			/* XXX */
32026b4cac81SBjoern A. Zeeb 	rx_stats.c_nf = -96;				/* XXX */
32036b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
32046b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
32056b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
32066b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
32076b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
32086b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
32096b4cac81SBjoern A. Zeeb 
32106b4cac81SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor. */
32116b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
32126b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
32136b4cac81SBjoern A. Zeeb 
32146b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
32156b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
32166b4cac81SBjoern A. Zeeb 
32176b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
32186b4cac81SBjoern A. Zeeb 	if (ok == 0) {
32196b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
32206b4cac81SBjoern A. Zeeb 		goto err;
32216b4cac81SBjoern A. Zeeb 	}
32226b4cac81SBjoern A. Zeeb 
32236b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
32246b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
32256b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
32266b4cac81SBjoern A. Zeeb 	} else {
3227c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
3228c8dafefaSBjoern A. Zeeb 
32296b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
32306b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
32316b4cac81SBjoern A. Zeeb 		if (ni != NULL)
32326b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
32336b4cac81SBjoern A. Zeeb 	}
32346b4cac81SBjoern A. Zeeb 
32356b4cac81SBjoern A. Zeeb 	if (ni != NULL)
32366b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
32376b4cac81SBjoern A. Zeeb 	else
32386b4cac81SBjoern A. Zeeb 		/*
32396b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
32406b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
32416b4cac81SBjoern A. Zeeb 		 */
32426b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
32436b4cac81SBjoern A. Zeeb 
32446b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
32456b4cac81SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p ni %p vap %p\n", __func__, sta, lsta, ni, vap);
32466b4cac81SBjoern A. Zeeb 
3247c8dafefaSBjoern A. Zeeb 	if (ni != NULL && vap != NULL &&
3248c8dafefaSBjoern A. Zeeb 	    ieee80211_is_beacon(hdr->frame_control) &&
3249c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
3250c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
3251c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
3252c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
3253c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
3254c8dafefaSBjoern A. Zeeb 
3255c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
3256c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
3257c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
3258c8dafefaSBjoern A. Zeeb 
3259c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
3260c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3261c8dafefaSBjoern A. Zeeb 
3262c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
3263c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
3264c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
3265c8dafefaSBjoern A. Zeeb 		/*
3266c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
3267c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
3268c8dafefaSBjoern A. Zeeb 		 */
3269c8dafefaSBjoern A. Zeeb 	}
3270c8dafefaSBjoern A. Zeeb skip_device_ts:
3271c8dafefaSBjoern A. Zeeb 
32726b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
32736b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
32746b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
32756b4cac81SBjoern A. Zeeb 
32766b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
32776b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
32786b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
32796b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
32806b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
32816b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
32826b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
32836b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
32846b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
32856b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
32866b4cac81SBjoern A. Zeeb #endif
32876b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
32886b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
32896b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
32906b4cac81SBjoern A. Zeeb 		IMPROVE();
32916b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
32926b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
32936b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
32946b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
32956b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rx_stats.c_rssi;
32966b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
32976b4cac81SBjoern A. Zeeb 	}
32986b4cac81SBjoern A. Zeeb 
32996b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
33006b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
33016b4cac81SBjoern A. Zeeb 
33026b4cac81SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
33036b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
33046b4cac81SBjoern A. Zeeb 		type = ieee80211_input_mimo(ni, m);
33056b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
33066b4cac81SBjoern A. Zeeb 	} else {
33076b4cac81SBjoern A. Zeeb 		type = ieee80211_input_mimo_all(ic, m);
33086b4cac81SBjoern A. Zeeb 	}
33096b4cac81SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
33106b4cac81SBjoern A. Zeeb 
33116b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE_RX)
33126b4cac81SBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, type);
33136b4cac81SBjoern A. Zeeb 
33146b4cac81SBjoern A. Zeeb 	IMPROVE();
33156b4cac81SBjoern A. Zeeb 
33166b4cac81SBjoern A. Zeeb err:
33176b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
33186b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
33196b4cac81SBjoern A. Zeeb }
33206b4cac81SBjoern A. Zeeb 
33216b4cac81SBjoern A. Zeeb uint8_t
33226b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr)
33236b4cac81SBjoern A. Zeeb {
33246b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
33256b4cac81SBjoern A. Zeeb 
33266b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
33276b4cac81SBjoern A. Zeeb 	return (ieee80211_gettid(wh));
33286b4cac81SBjoern A. Zeeb }
33296b4cac81SBjoern A. Zeeb 
33306b4cac81SBjoern A. Zeeb struct wiphy *
33316b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
33326b4cac81SBjoern A. Zeeb {
33336b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
33346b4cac81SBjoern A. Zeeb 
33356b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
33366b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
33376b4cac81SBjoern A. Zeeb 		return (NULL);
33386b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
33396b4cac81SBjoern A. Zeeb 
33406b4cac81SBjoern A. Zeeb 	/* XXX TODO */
33416b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
33426b4cac81SBjoern A. Zeeb }
33436b4cac81SBjoern A. Zeeb 
33446b4cac81SBjoern A. Zeeb void
33456b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
33466b4cac81SBjoern A. Zeeb {
33476b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
33486b4cac81SBjoern A. Zeeb 
33496b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
33506b4cac81SBjoern A. Zeeb 		return;
33516b4cac81SBjoern A. Zeeb 
33526b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
33536b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
33546b4cac81SBjoern A. Zeeb }
33556b4cac81SBjoern A. Zeeb 
33566b4cac81SBjoern A. Zeeb uint32_t
33576b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
33586b4cac81SBjoern A. Zeeb     enum nl80211_band band)
33596b4cac81SBjoern A. Zeeb {
33606b4cac81SBjoern A. Zeeb 
33616b4cac81SBjoern A. Zeeb 	switch (band) {
33626b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
33636b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
33646b4cac81SBjoern A. Zeeb 		break;
33656b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
33666b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
33676b4cac81SBjoern A. Zeeb 		break;
33686b4cac81SBjoern A. Zeeb 	default:
33696b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
33706b4cac81SBjoern A. Zeeb 		break;
33716b4cac81SBjoern A. Zeeb 	}
33726b4cac81SBjoern A. Zeeb 
33736b4cac81SBjoern A. Zeeb 	return (0);
33746b4cac81SBjoern A. Zeeb }
33756b4cac81SBjoern A. Zeeb 
33766b4cac81SBjoern A. Zeeb uint32_t
33776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
33786b4cac81SBjoern A. Zeeb {
33796b4cac81SBjoern A. Zeeb 
33806b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
33816b4cac81SBjoern A. Zeeb }
33826b4cac81SBjoern A. Zeeb 
33836b4cac81SBjoern A. Zeeb static struct lkpi_sta *
33846b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
33856b4cac81SBjoern A. Zeeb {
33866b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
33876b4cac81SBjoern A. Zeeb 
33886b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
33896b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
33906b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
33916b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
33926b4cac81SBjoern A. Zeeb 			return (lsta);
33936b4cac81SBjoern A. Zeeb 		}
33946b4cac81SBjoern A. Zeeb 	}
33956b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
33966b4cac81SBjoern A. Zeeb 
33976b4cac81SBjoern A. Zeeb 	return (NULL);
33986b4cac81SBjoern A. Zeeb }
33996b4cac81SBjoern A. Zeeb 
34006b4cac81SBjoern A. Zeeb struct ieee80211_sta *
34016b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
34026b4cac81SBjoern A. Zeeb {
34036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34046b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
34056b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
34066b4cac81SBjoern A. Zeeb 
34076b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
34086b4cac81SBjoern A. Zeeb 
34096b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
34106b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
34116b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
34126b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
34136b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
34146b4cac81SBjoern A. Zeeb 			return (sta);
34156b4cac81SBjoern A. Zeeb 		}
34166b4cac81SBjoern A. Zeeb 	}
34176b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
34186b4cac81SBjoern A. Zeeb 	return (NULL);
34196b4cac81SBjoern A. Zeeb }
34206b4cac81SBjoern A. Zeeb 
34216b4cac81SBjoern A. Zeeb struct ieee80211_sta *
34222e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
34232e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
34246b4cac81SBjoern A. Zeeb {
34256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34276b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
34286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34296b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
34306b4cac81SBjoern A. Zeeb 
34316b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
34326b4cac81SBjoern A. Zeeb 	sta = NULL;
34336b4cac81SBjoern A. Zeeb 
34346b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
34356b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
34366b4cac81SBjoern A. Zeeb 
34376b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
34386b4cac81SBjoern A. Zeeb 
34396b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
34406b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
34416b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
34426b4cac81SBjoern A. Zeeb 			continue;
34436b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
34446b4cac81SBjoern A. Zeeb 		if (sta != NULL)
34456b4cac81SBjoern A. Zeeb 			break;
34466b4cac81SBjoern A. Zeeb 	}
34476b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
34486b4cac81SBjoern A. Zeeb 
34496b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
34506b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
34516b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
34526b4cac81SBjoern A. Zeeb 			return (NULL);
34536b4cac81SBjoern A. Zeeb 	}
34546b4cac81SBjoern A. Zeeb 
34556b4cac81SBjoern A. Zeeb 	return (sta);
34566b4cac81SBjoern A. Zeeb }
34576b4cac81SBjoern A. Zeeb 
34586b4cac81SBjoern A. Zeeb struct sk_buff *
34596b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
34606b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
34616b4cac81SBjoern A. Zeeb {
34626b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
34636b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
34646b4cac81SBjoern A. Zeeb 
34656b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
34666b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
34676b4cac81SBjoern A. Zeeb 
34686b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
34696b4cac81SBjoern A. Zeeb 
34706b4cac81SBjoern A. Zeeb 	return (skb);
34716b4cac81SBjoern A. Zeeb }
34726b4cac81SBjoern A. Zeeb 
34736b4cac81SBjoern A. Zeeb void
34746b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
34756b4cac81SBjoern A. Zeeb     uint64_t *frame_cnt, uint64_t *byte_cnt)
34766b4cac81SBjoern A. Zeeb {
34776b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
34786b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
34796b4cac81SBjoern A. Zeeb 	uint64_t fc, bc;
34806b4cac81SBjoern A. Zeeb 
34816b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
34826b4cac81SBjoern A. Zeeb 
34836b4cac81SBjoern A. Zeeb 	fc = bc = 0;
34846b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
34856b4cac81SBjoern A. Zeeb 		fc++;
34866b4cac81SBjoern A. Zeeb 		bc += skb->len;
34876b4cac81SBjoern A. Zeeb 	}
34886b4cac81SBjoern A. Zeeb 	if (frame_cnt)
34896b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
34906b4cac81SBjoern A. Zeeb 	if (byte_cnt)
34916b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
34926b4cac81SBjoern A. Zeeb 
34936b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
34946b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
34956b4cac81SBjoern A. Zeeb 	IMPROVE();
34966b4cac81SBjoern A. Zeeb }
34976b4cac81SBjoern A. Zeeb 
34986b4cac81SBjoern A. Zeeb /*
34996b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
35006b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
35016b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
35026b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
35036b4cac81SBjoern A. Zeeb  */
35046b4cac81SBjoern A. Zeeb void
35056b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
35066b4cac81SBjoern A. Zeeb     int status)
35076b4cac81SBjoern A. Zeeb {
35086b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
35096b4cac81SBjoern A. Zeeb 	struct mbuf *m;
35106b4cac81SBjoern A. Zeeb 
35116b4cac81SBjoern A. Zeeb 	m = skb->m;
35126b4cac81SBjoern A. Zeeb 	skb->m = NULL;
35136b4cac81SBjoern A. Zeeb 
35146b4cac81SBjoern A. Zeeb 	if (m != NULL) {
35156b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
35166b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
35176b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
35186b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
35196b4cac81SBjoern A. Zeeb 	}
35206b4cac81SBjoern A. Zeeb 
35216b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
35226b4cac81SBjoern A. Zeeb }
35236b4cac81SBjoern A. Zeeb 
35246b4cac81SBjoern A. Zeeb /*
35256b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
35266b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
35276b4cac81SBjoern A. Zeeb  * mbufs this should go away.
35286b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
35296b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
35306b4cac81SBjoern A. Zeeb  */
35316b4cac81SBjoern A. Zeeb static void
35326b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
35336b4cac81SBjoern A. Zeeb {
35346b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
35356b4cac81SBjoern A. Zeeb 	struct mbuf *m;
35366b4cac81SBjoern A. Zeeb 
35376b4cac81SBjoern A. Zeeb 	if (p == NULL)
35386b4cac81SBjoern A. Zeeb 		return;
35396b4cac81SBjoern A. Zeeb 
35406b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
35416b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
35426b4cac81SBjoern A. Zeeb 
35436b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
35446b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
35456b4cac81SBjoern A. Zeeb 	if (ni != NULL)
35466b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
35476b4cac81SBjoern A. Zeeb 	m_freem(m);
35486b4cac81SBjoern A. Zeeb }
35496b4cac81SBjoern A. Zeeb 
35506b4cac81SBjoern A. Zeeb void
35516b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
35526b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
35536b4cac81SBjoern A. Zeeb {
35546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35556b4cac81SBjoern A. Zeeb 
35566b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
35576b4cac81SBjoern A. Zeeb 	IMPROVE();
35586b4cac81SBjoern A. Zeeb 
35596b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
35606b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
35616b4cac81SBjoern A. Zeeb }
35626b4cac81SBjoern A. Zeeb 
35636b4cac81SBjoern A. Zeeb void
35646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
35656b4cac81SBjoern A. Zeeb     struct work_struct *w)
35666b4cac81SBjoern A. Zeeb {
35676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35686b4cac81SBjoern A. Zeeb 
35696b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
35706b4cac81SBjoern A. Zeeb 	IMPROVE();
35716b4cac81SBjoern A. Zeeb 
35726b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
35736b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
35746b4cac81SBjoern A. Zeeb }
35756b4cac81SBjoern A. Zeeb 
35766b4cac81SBjoern A. Zeeb struct sk_buff *
35776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
35786b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
35796b4cac81SBjoern A. Zeeb {
35806b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35816b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
35826b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
35836b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
35846b4cac81SBjoern A. Zeeb 	uint16_t v;
35856b4cac81SBjoern A. Zeeb 
35866b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
35876b4cac81SBjoern A. Zeeb 	if (skb == NULL)
35886b4cac81SBjoern A. Zeeb 		return (NULL);
35896b4cac81SBjoern A. Zeeb 
35906b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
35916b4cac81SBjoern A. Zeeb 
35926b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
35936b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
35946b4cac81SBjoern A. Zeeb 
35956b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
35966b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
35976b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
35986b4cac81SBjoern A. Zeeb 	v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
35996b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
36006b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
36016b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
36026b4cac81SBjoern A. Zeeb 
36036b4cac81SBjoern A. Zeeb 	return (skb);
36046b4cac81SBjoern A. Zeeb }
36056b4cac81SBjoern A. Zeeb 
36066b4cac81SBjoern A. Zeeb struct sk_buff *
36076b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
36086b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif, bool qos)
36096b4cac81SBjoern A. Zeeb {
36106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36116b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36126b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
36136b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
36146b4cac81SBjoern A. Zeeb 
36156b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
36166b4cac81SBjoern A. Zeeb 	if (skb == NULL)
36176b4cac81SBjoern A. Zeeb 		return (NULL);
36186b4cac81SBjoern A. Zeeb 
36196b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
36206b4cac81SBjoern A. Zeeb 
36216b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
36226b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
36236b4cac81SBjoern A. Zeeb 
36246b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
36256b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
36266b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
36276b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
36286b4cac81SBjoern A. Zeeb 
36296b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
36306b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
36316b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
36326b4cac81SBjoern A. Zeeb 
36336b4cac81SBjoern A. Zeeb 	return (skb);
36346b4cac81SBjoern A. Zeeb }
36356b4cac81SBjoern A. Zeeb 
36366b4cac81SBjoern A. Zeeb struct wireless_dev *
36376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
36386b4cac81SBjoern A. Zeeb {
36396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36406b4cac81SBjoern A. Zeeb 
36416b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
36426b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
36436b4cac81SBjoern A. Zeeb }
36446b4cac81SBjoern A. Zeeb 
36456b4cac81SBjoern A. Zeeb void
36466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
36476b4cac81SBjoern A. Zeeb {
36486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36496b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36506b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
36516b4cac81SBjoern A. Zeeb 	int arg;
36526b4cac81SBjoern A. Zeeb 
36536b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
36546b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
36556b4cac81SBjoern A. Zeeb 
36566b4cac81SBjoern A. Zeeb 	/*
3657f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
36586b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
36596b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
36606b4cac81SBjoern A. Zeeb 	 */
3661f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
36626b4cac81SBjoern A. Zeeb 	arg = 0;
36636b4cac81SBjoern A. Zeeb 
36646b4cac81SBjoern A. Zeeb 	if (debug_80211 & D80211_TRACE)
36656b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
36666b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
36676b4cac81SBjoern A. Zeeb }
36686b4cac81SBjoern A. Zeeb 
36696b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
36706b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
36716b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
3672