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