18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2010 Broadcom Corporation
38c2ecf20Sopenharmony_ci * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
68c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
78c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
128c2ecf20Sopenharmony_ci * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
148c2ecf20Sopenharmony_ci * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
158c2ecf20Sopenharmony_ci * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
218c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
228c2ecf20Sopenharmony_ci#include <net/cfg80211.h>
238c2ecf20Sopenharmony_ci#include <net/mac80211.h>
248c2ecf20Sopenharmony_ci#include <brcm_hw_ids.h>
258c2ecf20Sopenharmony_ci#include <aiutils.h>
268c2ecf20Sopenharmony_ci#include <chipcommon.h>
278c2ecf20Sopenharmony_ci#include "rate.h"
288c2ecf20Sopenharmony_ci#include "scb.h"
298c2ecf20Sopenharmony_ci#include "phy/phy_hal.h"
308c2ecf20Sopenharmony_ci#include "channel.h"
318c2ecf20Sopenharmony_ci#include "antsel.h"
328c2ecf20Sopenharmony_ci#include "stf.h"
338c2ecf20Sopenharmony_ci#include "ampdu.h"
348c2ecf20Sopenharmony_ci#include "mac80211_if.h"
358c2ecf20Sopenharmony_ci#include "ucode_loader.h"
368c2ecf20Sopenharmony_ci#include "main.h"
378c2ecf20Sopenharmony_ci#include "soc.h"
388c2ecf20Sopenharmony_ci#include "dma.h"
398c2ecf20Sopenharmony_ci#include "debug.h"
408c2ecf20Sopenharmony_ci#include "brcms_trace_events.h"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* watchdog timer, in unit of ms */
438c2ecf20Sopenharmony_ci#define TIMER_INTERVAL_WATCHDOG		1000
448c2ecf20Sopenharmony_ci/* radio monitor timer, in unit of ms */
458c2ecf20Sopenharmony_ci#define TIMER_INTERVAL_RADIOCHK		800
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* beacon interval, in unit of 1024TU */
488c2ecf20Sopenharmony_ci#define BEACON_INTERVAL_DEFAULT		100
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* n-mode support capability */
518c2ecf20Sopenharmony_ci/* 2x2 includes both 1x1 & 2x2 devices
528c2ecf20Sopenharmony_ci * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
538c2ecf20Sopenharmony_ci * control it independently
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define WL_11N_2x2			1
568c2ecf20Sopenharmony_ci#define WL_11N_3x3			3
578c2ecf20Sopenharmony_ci#define WL_11N_4x4			4
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define EDCF_ACI_MASK			0x60
608c2ecf20Sopenharmony_ci#define EDCF_ACI_SHIFT			5
618c2ecf20Sopenharmony_ci#define EDCF_ECWMIN_MASK		0x0f
628c2ecf20Sopenharmony_ci#define EDCF_ECWMAX_SHIFT		4
638c2ecf20Sopenharmony_ci#define EDCF_AIFSN_MASK			0x0f
648c2ecf20Sopenharmony_ci#define EDCF_AIFSN_MAX			15
658c2ecf20Sopenharmony_ci#define EDCF_ECWMAX_MASK		0xf0
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define EDCF_AC_BE_TXOP_STA		0x0000
688c2ecf20Sopenharmony_ci#define EDCF_AC_BK_TXOP_STA		0x0000
698c2ecf20Sopenharmony_ci#define EDCF_AC_VO_ACI_STA		0x62
708c2ecf20Sopenharmony_ci#define EDCF_AC_VO_ECW_STA		0x32
718c2ecf20Sopenharmony_ci#define EDCF_AC_VI_ACI_STA		0x42
728c2ecf20Sopenharmony_ci#define EDCF_AC_VI_ECW_STA		0x43
738c2ecf20Sopenharmony_ci#define EDCF_AC_BK_ECW_STA		0xA4
748c2ecf20Sopenharmony_ci#define EDCF_AC_VI_TXOP_STA		0x005e
758c2ecf20Sopenharmony_ci#define EDCF_AC_VO_TXOP_STA		0x002f
768c2ecf20Sopenharmony_ci#define EDCF_AC_BE_ACI_STA		0x03
778c2ecf20Sopenharmony_ci#define EDCF_AC_BE_ECW_STA		0xA4
788c2ecf20Sopenharmony_ci#define EDCF_AC_BK_ACI_STA		0x27
798c2ecf20Sopenharmony_ci#define EDCF_AC_VO_TXOP_AP		0x002f
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define EDCF_TXOP2USEC(txop)		((txop) << 5)
828c2ecf20Sopenharmony_ci#define EDCF_ECW2CW(exp)		((1 << (exp)) - 1)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define APHY_SYMBOL_TIME		4
858c2ecf20Sopenharmony_ci#define APHY_PREAMBLE_TIME		16
868c2ecf20Sopenharmony_ci#define APHY_SIGNAL_TIME		4
878c2ecf20Sopenharmony_ci#define APHY_SIFS_TIME			16
888c2ecf20Sopenharmony_ci#define APHY_SERVICE_NBITS		16
898c2ecf20Sopenharmony_ci#define APHY_TAIL_NBITS			6
908c2ecf20Sopenharmony_ci#define BPHY_SIFS_TIME			10
918c2ecf20Sopenharmony_ci#define BPHY_PLCP_SHORT_TIME		96
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define PREN_PREAMBLE			24
948c2ecf20Sopenharmony_ci#define PREN_MM_EXT			12
958c2ecf20Sopenharmony_ci#define PREN_PREAMBLE_EXT		4
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define DOT11_MAC_HDR_LEN		24
988c2ecf20Sopenharmony_ci#define DOT11_ACK_LEN			10
998c2ecf20Sopenharmony_ci#define DOT11_BA_LEN			4
1008c2ecf20Sopenharmony_ci#define DOT11_OFDM_SIGNAL_EXTENSION	6
1018c2ecf20Sopenharmony_ci#define DOT11_MIN_FRAG_LEN		256
1028c2ecf20Sopenharmony_ci#define DOT11_RTS_LEN			16
1038c2ecf20Sopenharmony_ci#define DOT11_CTS_LEN			10
1048c2ecf20Sopenharmony_ci#define DOT11_BA_BITMAP_LEN		128
1058c2ecf20Sopenharmony_ci#define DOT11_MAXNUMFRAGS		16
1068c2ecf20Sopenharmony_ci#define DOT11_MAX_FRAG_LEN		2346
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define BPHY_PLCP_TIME			192
1098c2ecf20Sopenharmony_ci#define RIFS_11N_TIME			2
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* length of the BCN template area */
1128c2ecf20Sopenharmony_ci#define BCN_TMPL_LEN			512
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* brcms_bss_info flag bit values */
1158c2ecf20Sopenharmony_ci#define BRCMS_BSS_HT			0x0020	/* BSS is HT (MIMO) capable */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* chip rx buffer offset */
1188c2ecf20Sopenharmony_ci#define BRCMS_HWRXOFF			38
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* rfdisable delay timer 500 ms, runs of ALP clock */
1218c2ecf20Sopenharmony_ci#define RFDISABLE_DEFAULT		10000000
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define BRCMS_TEMPSENSE_PERIOD		10	/* 10 second timeout */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* synthpu_dly times in us */
1268c2ecf20Sopenharmony_ci#define SYNTHPU_DLY_APHY_US		3700
1278c2ecf20Sopenharmony_ci#define SYNTHPU_DLY_BPHY_US		1050
1288c2ecf20Sopenharmony_ci#define SYNTHPU_DLY_NPHY_US		2048
1298c2ecf20Sopenharmony_ci#define SYNTHPU_DLY_LPPHY_US		300
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#define ANTCNT				10	/* vanilla M_MAX_ANTCNT val */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* Per-AC retry limit register definitions; uses defs.h bitfield macros */
1348c2ecf20Sopenharmony_ci#define EDCF_SHORT_S			0
1358c2ecf20Sopenharmony_ci#define EDCF_SFB_S			4
1368c2ecf20Sopenharmony_ci#define EDCF_LONG_S			8
1378c2ecf20Sopenharmony_ci#define EDCF_LFB_S			12
1388c2ecf20Sopenharmony_ci#define EDCF_SHORT_M			BITFIELD_MASK(4)
1398c2ecf20Sopenharmony_ci#define EDCF_SFB_M			BITFIELD_MASK(4)
1408c2ecf20Sopenharmony_ci#define EDCF_LONG_M			BITFIELD_MASK(4)
1418c2ecf20Sopenharmony_ci#define EDCF_LFB_M			BITFIELD_MASK(4)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define RETRY_SHORT_DEF			7	/* Default Short retry Limit */
1448c2ecf20Sopenharmony_ci#define RETRY_SHORT_MAX			255	/* Maximum Short retry Limit */
1458c2ecf20Sopenharmony_ci#define RETRY_LONG_DEF			4	/* Default Long retry count */
1468c2ecf20Sopenharmony_ci#define RETRY_SHORT_FB			3	/* Short count for fb rate */
1478c2ecf20Sopenharmony_ci#define RETRY_LONG_FB			2	/* Long count for fb rate */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define APHY_CWMIN			15
1508c2ecf20Sopenharmony_ci#define PHY_CWMAX			1023
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define EDCF_AIFSN_MIN			1
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define FRAGNUM_MASK			0xF
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define APHY_SLOT_TIME			9
1578c2ecf20Sopenharmony_ci#define BPHY_SLOT_TIME			20
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define WL_SPURAVOID_OFF		0
1608c2ecf20Sopenharmony_ci#define WL_SPURAVOID_ON1		1
1618c2ecf20Sopenharmony_ci#define WL_SPURAVOID_ON2		2
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* invalid core flags, use the saved coreflags */
1648c2ecf20Sopenharmony_ci#define BRCMS_USE_COREFLAGS		0xffffffff
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/* values for PLCPHdr_override */
1678c2ecf20Sopenharmony_ci#define BRCMS_PLCP_AUTO			-1
1688c2ecf20Sopenharmony_ci#define BRCMS_PLCP_SHORT		0
1698c2ecf20Sopenharmony_ci#define BRCMS_PLCP_LONG			1
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/* values for g_protection_override and n_protection_override */
1728c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_AUTO		-1
1738c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_OFF		0
1748c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_ON		1
1758c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_MMHDR_ONLY	2
1768c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_CTS_ONLY	3
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/* values for g_protection_control and n_protection_control */
1798c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_CTL_OFF	0
1808c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_CTL_LOCAL	1
1818c2ecf20Sopenharmony_ci#define BRCMS_PROTECTION_CTL_OVERLAP	2
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* values for n_protection */
1848c2ecf20Sopenharmony_ci#define BRCMS_N_PROTECTION_OFF		0
1858c2ecf20Sopenharmony_ci#define BRCMS_N_PROTECTION_OPTIONAL	1
1868c2ecf20Sopenharmony_ci#define BRCMS_N_PROTECTION_20IN40	2
1878c2ecf20Sopenharmony_ci#define BRCMS_N_PROTECTION_MIXEDMODE	3
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/* values for band specific 40MHz capabilities */
1908c2ecf20Sopenharmony_ci#define BRCMS_N_BW_20ALL		0
1918c2ecf20Sopenharmony_ci#define BRCMS_N_BW_40ALL		1
1928c2ecf20Sopenharmony_ci#define BRCMS_N_BW_20IN2G_40IN5G	2
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/* bitflags for SGI support (sgi_rx iovar) */
1958c2ecf20Sopenharmony_ci#define BRCMS_N_SGI_20			0x01
1968c2ecf20Sopenharmony_ci#define BRCMS_N_SGI_40			0x02
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* defines used by the nrate iovar */
1998c2ecf20Sopenharmony_ci/* MSC in use,indicates b0-6 holds an mcs */
2008c2ecf20Sopenharmony_ci#define NRATE_MCS_INUSE			0x00000080
2018c2ecf20Sopenharmony_ci/* rate/mcs value */
2028c2ecf20Sopenharmony_ci#define NRATE_RATE_MASK			0x0000007f
2038c2ecf20Sopenharmony_ci/* stf mode mask: siso, cdd, stbc, sdm */
2048c2ecf20Sopenharmony_ci#define NRATE_STF_MASK			0x0000ff00
2058c2ecf20Sopenharmony_ci/* stf mode shift */
2068c2ecf20Sopenharmony_ci#define NRATE_STF_SHIFT			8
2078c2ecf20Sopenharmony_ci/* bit indicate to override mcs only */
2088c2ecf20Sopenharmony_ci#define NRATE_OVERRIDE_MCS_ONLY		0x40000000
2098c2ecf20Sopenharmony_ci#define NRATE_SGI_MASK			0x00800000	/* sgi mode */
2108c2ecf20Sopenharmony_ci#define NRATE_SGI_SHIFT			23		/* sgi mode */
2118c2ecf20Sopenharmony_ci#define NRATE_LDPC_CODING		0x00400000	/* adv coding in use */
2128c2ecf20Sopenharmony_ci#define NRATE_LDPC_SHIFT		22		/* ldpc shift */
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci#define NRATE_STF_SISO			0		/* stf mode SISO */
2158c2ecf20Sopenharmony_ci#define NRATE_STF_CDD			1		/* stf mode CDD */
2168c2ecf20Sopenharmony_ci#define NRATE_STF_STBC			2		/* stf mode STBC */
2178c2ecf20Sopenharmony_ci#define NRATE_STF_SDM			3		/* stf mode SDM */
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#define MAX_DMA_SEGS			4
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/* # of entries in Tx FIFO */
2228c2ecf20Sopenharmony_ci#define NTXD				64
2238c2ecf20Sopenharmony_ci/* Max # of entries in Rx FIFO based on 4kb page size */
2248c2ecf20Sopenharmony_ci#define NRXD				256
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/* Amount of headroom to leave in Tx FIFO */
2278c2ecf20Sopenharmony_ci#define TX_HEADROOM			4
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci/* try to keep this # rbufs posted to the chip */
2308c2ecf20Sopenharmony_ci#define NRXBUFPOST			32
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/* max # frames to process in brcms_c_recv() */
2338c2ecf20Sopenharmony_ci#define RXBND				8
2348c2ecf20Sopenharmony_ci/* max # tx status to process in wlc_txstatus() */
2358c2ecf20Sopenharmony_ci#define TXSBND				8
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/* brcmu_format_flags() bit description structure */
2388c2ecf20Sopenharmony_cistruct brcms_c_bit_desc {
2398c2ecf20Sopenharmony_ci	u32 bit;
2408c2ecf20Sopenharmony_ci	const char *name;
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/*
2448c2ecf20Sopenharmony_ci * The following table lists the buffer memory allocated to xmt fifos in HW.
2458c2ecf20Sopenharmony_ci * the size is in units of 256bytes(one block), total size is HW dependent
2468c2ecf20Sopenharmony_ci * ucode has default fifo partition, sw can overwrite if necessary
2478c2ecf20Sopenharmony_ci *
2488c2ecf20Sopenharmony_ci * This is documented in twiki under the topic UcodeTxFifo. Please ensure
2498c2ecf20Sopenharmony_ci * the twiki is updated before making changes.
2508c2ecf20Sopenharmony_ci */
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* Starting corerev for the fifo size table */
2538c2ecf20Sopenharmony_ci#define XMTFIFOTBL_STARTREV	17
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistruct d11init {
2568c2ecf20Sopenharmony_ci	__le16 addr;
2578c2ecf20Sopenharmony_ci	__le16 size;
2588c2ecf20Sopenharmony_ci	__le32 value;
2598c2ecf20Sopenharmony_ci};
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistruct edcf_acparam {
2628c2ecf20Sopenharmony_ci	u8 ACI;
2638c2ecf20Sopenharmony_ci	u8 ECW;
2648c2ecf20Sopenharmony_ci	u16 TXOP;
2658c2ecf20Sopenharmony_ci} __packed;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/* debug/trace */
2688c2ecf20Sopenharmony_ciuint brcm_msg_level;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/* TX FIFO number to WME/802.1E Access Category */
2718c2ecf20Sopenharmony_cistatic const u8 wme_fifo2ac[] = {
2728c2ecf20Sopenharmony_ci	IEEE80211_AC_BK,
2738c2ecf20Sopenharmony_ci	IEEE80211_AC_BE,
2748c2ecf20Sopenharmony_ci	IEEE80211_AC_VI,
2758c2ecf20Sopenharmony_ci	IEEE80211_AC_VO,
2768c2ecf20Sopenharmony_ci	IEEE80211_AC_BE,
2778c2ecf20Sopenharmony_ci	IEEE80211_AC_BE
2788c2ecf20Sopenharmony_ci};
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* ieee80211 Access Category to TX FIFO number */
2818c2ecf20Sopenharmony_cistatic const u8 wme_ac2fifo[] = {
2828c2ecf20Sopenharmony_ci	TX_AC_VO_FIFO,
2838c2ecf20Sopenharmony_ci	TX_AC_VI_FIFO,
2848c2ecf20Sopenharmony_ci	TX_AC_BE_FIFO,
2858c2ecf20Sopenharmony_ci	TX_AC_BK_FIFO
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic const u16 xmtfifo_sz[][NFIFO] = {
2898c2ecf20Sopenharmony_ci	/* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
2908c2ecf20Sopenharmony_ci	{20, 192, 192, 21, 17, 5},
2918c2ecf20Sopenharmony_ci	/* corerev 18: */
2928c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 0, 0},
2938c2ecf20Sopenharmony_ci	/* corerev 19: */
2948c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 0, 0},
2958c2ecf20Sopenharmony_ci	/* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
2968c2ecf20Sopenharmony_ci	{20, 192, 192, 21, 17, 5},
2978c2ecf20Sopenharmony_ci	/* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
2988c2ecf20Sopenharmony_ci	{9, 58, 22, 14, 14, 5},
2998c2ecf20Sopenharmony_ci	/* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
3008c2ecf20Sopenharmony_ci	{20, 192, 192, 21, 17, 5},
3018c2ecf20Sopenharmony_ci	/* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
3028c2ecf20Sopenharmony_ci	{20, 192, 192, 21, 17, 5},
3038c2ecf20Sopenharmony_ci	/* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
3048c2ecf20Sopenharmony_ci	{9, 58, 22, 14, 14, 5},
3058c2ecf20Sopenharmony_ci	/* corerev 25: */
3068c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 0, 0},
3078c2ecf20Sopenharmony_ci	/* corerev 26: */
3088c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 0, 0},
3098c2ecf20Sopenharmony_ci	/* corerev 27: */
3108c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 0, 0},
3118c2ecf20Sopenharmony_ci	/* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
3128c2ecf20Sopenharmony_ci	{9, 58, 22, 14, 14, 5},
3138c2ecf20Sopenharmony_ci};
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci#ifdef DEBUG
3168c2ecf20Sopenharmony_cistatic const char * const fifo_names[] = {
3178c2ecf20Sopenharmony_ci	"AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
3188c2ecf20Sopenharmony_ci#else
3198c2ecf20Sopenharmony_cistatic const char fifo_names[6][1];
3208c2ecf20Sopenharmony_ci#endif
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci#ifdef DEBUG
3238c2ecf20Sopenharmony_ci/* pointer to most recently allocated wl/wlc */
3248c2ecf20Sopenharmony_cistatic struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
3258c2ecf20Sopenharmony_ci#endif
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci/* Mapping of ieee80211 AC numbers to tx fifos */
3288c2ecf20Sopenharmony_cistatic const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
3298c2ecf20Sopenharmony_ci	[IEEE80211_AC_VO]	= TX_AC_VO_FIFO,
3308c2ecf20Sopenharmony_ci	[IEEE80211_AC_VI]	= TX_AC_VI_FIFO,
3318c2ecf20Sopenharmony_ci	[IEEE80211_AC_BE]	= TX_AC_BE_FIFO,
3328c2ecf20Sopenharmony_ci	[IEEE80211_AC_BK]	= TX_AC_BK_FIFO,
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci/* Mapping of tx fifos to ieee80211 AC numbers */
3368c2ecf20Sopenharmony_cistatic const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
3378c2ecf20Sopenharmony_ci	[TX_AC_BK_FIFO]	= IEEE80211_AC_BK,
3388c2ecf20Sopenharmony_ci	[TX_AC_BE_FIFO]	= IEEE80211_AC_BE,
3398c2ecf20Sopenharmony_ci	[TX_AC_VI_FIFO]	= IEEE80211_AC_VI,
3408c2ecf20Sopenharmony_ci	[TX_AC_VO_FIFO]	= IEEE80211_AC_VO,
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic u8 brcms_ac_to_fifo(u8 ac)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
3468c2ecf20Sopenharmony_ci		return TX_AC_BE_FIFO;
3478c2ecf20Sopenharmony_ci	return ac_to_fifo_mapping[ac];
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic u8 brcms_fifo_to_ac(u8 fifo)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
3538c2ecf20Sopenharmony_ci		return IEEE80211_AC_BE;
3548c2ecf20Sopenharmony_ci	return fifo_to_ac_mapping[fifo];
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/* Find basic rate for a given rate */
3588c2ecf20Sopenharmony_cistatic u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec))
3618c2ecf20Sopenharmony_ci		return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
3628c2ecf20Sopenharmony_ci		       .leg_ofdm];
3638c2ecf20Sopenharmony_ci	return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic u16 frametype(u32 rspec, u8 mimoframe)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec))
3698c2ecf20Sopenharmony_ci		return mimoframe;
3708c2ecf20Sopenharmony_ci	return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/* currently the best mechanism for determining SIFS is the band in use */
3748c2ecf20Sopenharmony_cistatic u16 get_sifs(struct brcms_band *band)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
3778c2ecf20Sopenharmony_ci				 BPHY_SIFS_TIME;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/*
3818c2ecf20Sopenharmony_ci * Detect Card removed.
3828c2ecf20Sopenharmony_ci * Even checking an sbconfig register read will not false trigger when the core
3838c2ecf20Sopenharmony_ci * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
3848c2ecf20Sopenharmony_ci * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
3858c2ecf20Sopenharmony_ci * reg with fixed 0/1 pattern (some platforms return all 0).
3868c2ecf20Sopenharmony_ci * If clocks are present, call the sb routine which will figure out if the
3878c2ecf20Sopenharmony_ci * device is removed.
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_cistatic bool brcms_deviceremoved(struct brcms_c_info *wlc)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	u32 macctrl;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (!wlc->hw->clk)
3948c2ecf20Sopenharmony_ci		return ai_deviceremoved(wlc->hw->sih);
3958c2ecf20Sopenharmony_ci	macctrl = bcma_read32(wlc->hw->d11core,
3968c2ecf20Sopenharmony_ci			      D11REGOFFS(maccontrol));
3978c2ecf20Sopenharmony_ci	return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/* sum the individual fifo tx pending packet counts */
4018c2ecf20Sopenharmony_cistatic int brcms_txpktpendtot(struct brcms_c_info *wlc)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	int i;
4048c2ecf20Sopenharmony_ci	int pending = 0;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
4078c2ecf20Sopenharmony_ci		if (wlc->hw->di[i])
4088c2ecf20Sopenharmony_ci			pending += dma_txpending(wlc->hw->di[i]);
4098c2ecf20Sopenharmony_ci	return pending;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	return wlc->pub->_nbands > 1 && !wlc->bandlocked;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic int brcms_chspec_bw(u16 chanspec)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	if (CHSPEC_IS40(chanspec))
4208c2ecf20Sopenharmony_ci		return BRCMS_40_MHZ;
4218c2ecf20Sopenharmony_ci	if (CHSPEC_IS20(chanspec))
4228c2ecf20Sopenharmony_ci		return BRCMS_20_MHZ;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return BRCMS_10_MHZ;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	if (cfg == NULL)
4308c2ecf20Sopenharmony_ci		return;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	kfree(cfg->current_bss);
4338c2ecf20Sopenharmony_ci	kfree(cfg);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic void brcms_c_detach_mfree(struct brcms_c_info *wlc)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	if (wlc == NULL)
4398c2ecf20Sopenharmony_ci		return;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	brcms_c_bsscfg_mfree(wlc->bsscfg);
4428c2ecf20Sopenharmony_ci	kfree(wlc->pub);
4438c2ecf20Sopenharmony_ci	kfree(wlc->modulecb);
4448c2ecf20Sopenharmony_ci	kfree(wlc->default_bss);
4458c2ecf20Sopenharmony_ci	kfree(wlc->protection);
4468c2ecf20Sopenharmony_ci	kfree(wlc->stf);
4478c2ecf20Sopenharmony_ci	kfree(wlc->bandstate[0]);
4488c2ecf20Sopenharmony_ci	if (wlc->corestate)
4498c2ecf20Sopenharmony_ci		kfree(wlc->corestate->macstat_snapshot);
4508c2ecf20Sopenharmony_ci	kfree(wlc->corestate);
4518c2ecf20Sopenharmony_ci	if (wlc->hw)
4528c2ecf20Sopenharmony_ci		kfree(wlc->hw->bandstate[0]);
4538c2ecf20Sopenharmony_ci	kfree(wlc->hw);
4548c2ecf20Sopenharmony_ci	if (wlc->beacon)
4558c2ecf20Sopenharmony_ci		dev_kfree_skb_any(wlc->beacon);
4568c2ecf20Sopenharmony_ci	if (wlc->probe_resp)
4578c2ecf20Sopenharmony_ci		dev_kfree_skb_any(wlc->probe_resp);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	kfree(wlc);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct brcms_bss_cfg *cfg;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
4678c2ecf20Sopenharmony_ci	if (cfg == NULL)
4688c2ecf20Sopenharmony_ci		goto fail;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
4718c2ecf20Sopenharmony_ci	if (cfg->current_bss == NULL)
4728c2ecf20Sopenharmony_ci		goto fail;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	return cfg;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci fail:
4778c2ecf20Sopenharmony_ci	brcms_c_bsscfg_mfree(cfg);
4788c2ecf20Sopenharmony_ci	return NULL;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic struct brcms_c_info *
4828c2ecf20Sopenharmony_cibrcms_c_attach_malloc(uint unit, uint *err, uint devid)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
4878c2ecf20Sopenharmony_ci	if (wlc == NULL) {
4888c2ecf20Sopenharmony_ci		*err = 1002;
4898c2ecf20Sopenharmony_ci		goto fail;
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* allocate struct brcms_c_pub state structure */
4938c2ecf20Sopenharmony_ci	wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
4948c2ecf20Sopenharmony_ci	if (wlc->pub == NULL) {
4958c2ecf20Sopenharmony_ci		*err = 1003;
4968c2ecf20Sopenharmony_ci		goto fail;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci	wlc->pub->wlc = wlc;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* allocate struct brcms_hardware state structure */
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
5038c2ecf20Sopenharmony_ci	if (wlc->hw == NULL) {
5048c2ecf20Sopenharmony_ci		*err = 1005;
5058c2ecf20Sopenharmony_ci		goto fail;
5068c2ecf20Sopenharmony_ci	}
5078c2ecf20Sopenharmony_ci	wlc->hw->wlc = wlc;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	wlc->hw->bandstate[0] =
5108c2ecf20Sopenharmony_ci		kcalloc(MAXBANDS, sizeof(struct brcms_hw_band), GFP_ATOMIC);
5118c2ecf20Sopenharmony_ci	if (wlc->hw->bandstate[0] == NULL) {
5128c2ecf20Sopenharmony_ci		*err = 1006;
5138c2ecf20Sopenharmony_ci		goto fail;
5148c2ecf20Sopenharmony_ci	} else {
5158c2ecf20Sopenharmony_ci		int i;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		for (i = 1; i < MAXBANDS; i++)
5188c2ecf20Sopenharmony_ci			wlc->hw->bandstate[i] = (struct brcms_hw_band *)
5198c2ecf20Sopenharmony_ci			    ((unsigned long)wlc->hw->bandstate[0] +
5208c2ecf20Sopenharmony_ci			     (sizeof(struct brcms_hw_band) * i));
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	wlc->modulecb =
5248c2ecf20Sopenharmony_ci		kcalloc(BRCMS_MAXMODULES, sizeof(struct modulecb),
5258c2ecf20Sopenharmony_ci			GFP_ATOMIC);
5268c2ecf20Sopenharmony_ci	if (wlc->modulecb == NULL) {
5278c2ecf20Sopenharmony_ci		*err = 1009;
5288c2ecf20Sopenharmony_ci		goto fail;
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
5328c2ecf20Sopenharmony_ci	if (wlc->default_bss == NULL) {
5338c2ecf20Sopenharmony_ci		*err = 1010;
5348c2ecf20Sopenharmony_ci		goto fail;
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
5388c2ecf20Sopenharmony_ci	if (wlc->bsscfg == NULL) {
5398c2ecf20Sopenharmony_ci		*err = 1011;
5408c2ecf20Sopenharmony_ci		goto fail;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	wlc->protection = kzalloc(sizeof(struct brcms_protection),
5448c2ecf20Sopenharmony_ci				  GFP_ATOMIC);
5458c2ecf20Sopenharmony_ci	if (wlc->protection == NULL) {
5468c2ecf20Sopenharmony_ci		*err = 1016;
5478c2ecf20Sopenharmony_ci		goto fail;
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
5518c2ecf20Sopenharmony_ci	if (wlc->stf == NULL) {
5528c2ecf20Sopenharmony_ci		*err = 1017;
5538c2ecf20Sopenharmony_ci		goto fail;
5548c2ecf20Sopenharmony_ci	}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	wlc->bandstate[0] =
5578c2ecf20Sopenharmony_ci		kcalloc(MAXBANDS, sizeof(struct brcms_band), GFP_ATOMIC);
5588c2ecf20Sopenharmony_ci	if (wlc->bandstate[0] == NULL) {
5598c2ecf20Sopenharmony_ci		*err = 1025;
5608c2ecf20Sopenharmony_ci		goto fail;
5618c2ecf20Sopenharmony_ci	} else {
5628c2ecf20Sopenharmony_ci		int i;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci		for (i = 1; i < MAXBANDS; i++)
5658c2ecf20Sopenharmony_ci			wlc->bandstate[i] = (struct brcms_band *)
5668c2ecf20Sopenharmony_ci				((unsigned long)wlc->bandstate[0]
5678c2ecf20Sopenharmony_ci				+ (sizeof(struct brcms_band)*i));
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
5718c2ecf20Sopenharmony_ci	if (wlc->corestate == NULL) {
5728c2ecf20Sopenharmony_ci		*err = 1026;
5738c2ecf20Sopenharmony_ci		goto fail;
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	wlc->corestate->macstat_snapshot =
5778c2ecf20Sopenharmony_ci		kzalloc(sizeof(struct macstat), GFP_ATOMIC);
5788c2ecf20Sopenharmony_ci	if (wlc->corestate->macstat_snapshot == NULL) {
5798c2ecf20Sopenharmony_ci		*err = 1027;
5808c2ecf20Sopenharmony_ci		goto fail;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	return wlc;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci fail:
5868c2ecf20Sopenharmony_ci	brcms_c_detach_mfree(wlc);
5878c2ecf20Sopenharmony_ci	return NULL;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/*
5918c2ecf20Sopenharmony_ci * Update the slot timing for standard 11b/g (20us slots)
5928c2ecf20Sopenharmony_ci * or shortslot 11g (9us slots)
5938c2ecf20Sopenharmony_ci * The PSM needs to be suspended for this call.
5948c2ecf20Sopenharmony_ci */
5958c2ecf20Sopenharmony_cistatic void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
5968c2ecf20Sopenharmony_ci					bool shortslot)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (shortslot) {
6018c2ecf20Sopenharmony_ci		/* 11g short slot: 11a timing */
6028c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
6038c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
6048c2ecf20Sopenharmony_ci	} else {
6058c2ecf20Sopenharmony_ci		/* 11g long slot: 11b timing */
6068c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
6078c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci/*
6128c2ecf20Sopenharmony_ci * calculate frame duration of a given rate and length, return
6138c2ecf20Sopenharmony_ci * time in usec unit
6148c2ecf20Sopenharmony_ci */
6158c2ecf20Sopenharmony_cistatic uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
6168c2ecf20Sopenharmony_ci				    u8 preamble_type, uint mac_len)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	uint nsyms, dur = 0, Ndps, kNdps;
6198c2ecf20Sopenharmony_ci	uint rate = rspec2rate(ratespec);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (rate == 0) {
6228c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
6238c2ecf20Sopenharmony_ci			  wlc->pub->unit);
6248c2ecf20Sopenharmony_ci		rate = BRCM_RATE_1M;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	if (is_mcs_rate(ratespec)) {
6288c2ecf20Sopenharmony_ci		uint mcs = ratespec & RSPEC_RATE_MASK;
6298c2ecf20Sopenharmony_ci		int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci		dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
6328c2ecf20Sopenharmony_ci		if (preamble_type == BRCMS_MM_PREAMBLE)
6338c2ecf20Sopenharmony_ci			dur += PREN_MM_EXT;
6348c2ecf20Sopenharmony_ci		/* 1000Ndbps = kbps * 4 */
6358c2ecf20Sopenharmony_ci		kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
6368c2ecf20Sopenharmony_ci				   rspec_issgi(ratespec)) * 4;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci		if (rspec_stc(ratespec) == 0)
6398c2ecf20Sopenharmony_ci			nsyms =
6408c2ecf20Sopenharmony_ci			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
6418c2ecf20Sopenharmony_ci				  APHY_TAIL_NBITS) * 1000, kNdps);
6428c2ecf20Sopenharmony_ci		else
6438c2ecf20Sopenharmony_ci			/* STBC needs to have even number of symbols */
6448c2ecf20Sopenharmony_ci			nsyms =
6458c2ecf20Sopenharmony_ci			    2 *
6468c2ecf20Sopenharmony_ci			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
6478c2ecf20Sopenharmony_ci				  APHY_TAIL_NBITS) * 1000, 2 * kNdps);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci		dur += APHY_SYMBOL_TIME * nsyms;
6508c2ecf20Sopenharmony_ci		if (wlc->band->bandtype == BRCM_BAND_2G)
6518c2ecf20Sopenharmony_ci			dur += DOT11_OFDM_SIGNAL_EXTENSION;
6528c2ecf20Sopenharmony_ci	} else if (is_ofdm_rate(rate)) {
6538c2ecf20Sopenharmony_ci		dur = APHY_PREAMBLE_TIME;
6548c2ecf20Sopenharmony_ci		dur += APHY_SIGNAL_TIME;
6558c2ecf20Sopenharmony_ci		/* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
6568c2ecf20Sopenharmony_ci		Ndps = rate * 2;
6578c2ecf20Sopenharmony_ci		/* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
6588c2ecf20Sopenharmony_ci		nsyms =
6598c2ecf20Sopenharmony_ci		    CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
6608c2ecf20Sopenharmony_ci			 Ndps);
6618c2ecf20Sopenharmony_ci		dur += APHY_SYMBOL_TIME * nsyms;
6628c2ecf20Sopenharmony_ci		if (wlc->band->bandtype == BRCM_BAND_2G)
6638c2ecf20Sopenharmony_ci			dur += DOT11_OFDM_SIGNAL_EXTENSION;
6648c2ecf20Sopenharmony_ci	} else {
6658c2ecf20Sopenharmony_ci		/*
6668c2ecf20Sopenharmony_ci		 * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
6678c2ecf20Sopenharmony_ci		 * will divide out
6688c2ecf20Sopenharmony_ci		 */
6698c2ecf20Sopenharmony_ci		mac_len = mac_len * 8 * 2;
6708c2ecf20Sopenharmony_ci		/* calc ceiling of bits/rate = microseconds of air time */
6718c2ecf20Sopenharmony_ci		dur = (mac_len + rate - 1) / rate;
6728c2ecf20Sopenharmony_ci		if (preamble_type & BRCMS_SHORT_PREAMBLE)
6738c2ecf20Sopenharmony_ci			dur += BPHY_PLCP_SHORT_TIME;
6748c2ecf20Sopenharmony_ci		else
6758c2ecf20Sopenharmony_ci			dur += BPHY_PLCP_TIME;
6768c2ecf20Sopenharmony_ci	}
6778c2ecf20Sopenharmony_ci	return dur;
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_cistatic void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
6818c2ecf20Sopenharmony_ci				const struct d11init *inits)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
6848c2ecf20Sopenharmony_ci	int i;
6858c2ecf20Sopenharmony_ci	uint offset;
6868c2ecf20Sopenharmony_ci	u16 size;
6878c2ecf20Sopenharmony_ci	u32 value;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
6928c2ecf20Sopenharmony_ci		size = le16_to_cpu(inits[i].size);
6938c2ecf20Sopenharmony_ci		offset = le16_to_cpu(inits[i].addr);
6948c2ecf20Sopenharmony_ci		value = le32_to_cpu(inits[i].value);
6958c2ecf20Sopenharmony_ci		if (size == 2)
6968c2ecf20Sopenharmony_ci			bcma_write16(core, offset, value);
6978c2ecf20Sopenharmony_ci		else if (size == 4)
6988c2ecf20Sopenharmony_ci			bcma_write32(core, offset, value);
6998c2ecf20Sopenharmony_ci		else
7008c2ecf20Sopenharmony_ci			break;
7018c2ecf20Sopenharmony_ci	}
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	u8 idx;
7078c2ecf20Sopenharmony_ci	u16 addr[] = {
7088c2ecf20Sopenharmony_ci		M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
7098c2ecf20Sopenharmony_ci		M_HOST_FLAGS5
7108c2ecf20Sopenharmony_ci	};
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	for (idx = 0; idx < MHFMAX; idx++)
7138c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
7148c2ecf20Sopenharmony_ci}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_cistatic void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	/* init microcode host flags */
7218c2ecf20Sopenharmony_ci	brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/* do band-specific ucode IHR, SHM, and SCR inits */
7248c2ecf20Sopenharmony_ci	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
7258c2ecf20Sopenharmony_ci		if (BRCMS_ISNPHY(wlc_hw->band))
7268c2ecf20Sopenharmony_ci			brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
7278c2ecf20Sopenharmony_ci		else
7288c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core,
7298c2ecf20Sopenharmony_ci				  "%s: wl%d: unsupported phy in corerev %d\n",
7308c2ecf20Sopenharmony_ci				  __func__, wlc_hw->unit,
7318c2ecf20Sopenharmony_ci				  wlc_hw->corerev);
7328c2ecf20Sopenharmony_ci	} else {
7338c2ecf20Sopenharmony_ci		if (D11REV_IS(wlc_hw->corerev, 24)) {
7348c2ecf20Sopenharmony_ci			if (BRCMS_ISLCNPHY(wlc_hw->band))
7358c2ecf20Sopenharmony_ci				brcms_c_write_inits(wlc_hw,
7368c2ecf20Sopenharmony_ci						    ucode->d11lcn0bsinitvals24);
7378c2ecf20Sopenharmony_ci			else
7388c2ecf20Sopenharmony_ci				brcms_err(wlc_hw->d11core,
7398c2ecf20Sopenharmony_ci					  "%s: wl%d: unsupported phy in core rev %d\n",
7408c2ecf20Sopenharmony_ci					  __func__, wlc_hw->unit,
7418c2ecf20Sopenharmony_ci					  wlc_hw->corerev);
7428c2ecf20Sopenharmony_ci		} else {
7438c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core,
7448c2ecf20Sopenharmony_ci				  "%s: wl%d: unsupported corerev %d\n",
7458c2ecf20Sopenharmony_ci				  __func__, wlc_hw->unit, wlc_hw->corerev);
7468c2ecf20Sopenharmony_ci		}
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
7538c2ecf20Sopenharmony_ci	u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_cistatic void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	wlc_hw->phyclk = clk;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	if (OFF == clk) {	/* clear gmode bit, put phy into reset */
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
7678c2ecf20Sopenharmony_ci				   (SICF_PRST | SICF_FGC));
7688c2ecf20Sopenharmony_ci		udelay(1);
7698c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
7708c2ecf20Sopenharmony_ci		udelay(1);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	} else {		/* take phy out of reset */
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
7758c2ecf20Sopenharmony_ci		udelay(1);
7768c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
7778c2ecf20Sopenharmony_ci		udelay(1);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci/* low-level band switch utility routine */
7838c2ecf20Sopenharmony_cistatic void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
7868c2ecf20Sopenharmony_ci			   bandunit);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	wlc_hw->band = wlc_hw->bandstate[bandunit];
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	/*
7918c2ecf20Sopenharmony_ci	 * BMAC_NOTE:
7928c2ecf20Sopenharmony_ci	 *   until we eliminate need for wlc->band refs in low level code
7938c2ecf20Sopenharmony_ci	 */
7948c2ecf20Sopenharmony_ci	wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* set gmode core flag */
7978c2ecf20Sopenharmony_ci	if (wlc_hw->sbclk && !wlc_hw->noreset) {
7988c2ecf20Sopenharmony_ci		u32 gmode = 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci		if (bandunit == 0)
8018c2ecf20Sopenharmony_ci			gmode = SICF_GMODE;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci/* switch to new band but leave it inactive */
8088c2ecf20Sopenharmony_cistatic u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
8118c2ecf20Sopenharmony_ci	u32 macintmask;
8128c2ecf20Sopenharmony_ci	u32 macctrl;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
8158c2ecf20Sopenharmony_ci	macctrl = bcma_read32(wlc_hw->d11core,
8168c2ecf20Sopenharmony_ci			      D11REGOFFS(maccontrol));
8178c2ecf20Sopenharmony_ci	WARN_ON((macctrl & MCTL_EN_MAC) != 0);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	/* disable interrupts */
8208c2ecf20Sopenharmony_ci	macintmask = brcms_intrsoff(wlc->wl);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	/* radio off */
8238c2ecf20Sopenharmony_ci	wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	brcms_b_core_phy_clk(wlc_hw, OFF);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	brcms_c_setxband(wlc_hw, bandunit);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	return macintmask;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci/* process an individual struct tx_status */
8338c2ecf20Sopenharmony_cistatic bool
8348c2ecf20Sopenharmony_cibrcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	struct sk_buff *p = NULL;
8378c2ecf20Sopenharmony_ci	uint queue = NFIFO;
8388c2ecf20Sopenharmony_ci	struct dma_pub *dma = NULL;
8398c2ecf20Sopenharmony_ci	struct d11txh *txh = NULL;
8408c2ecf20Sopenharmony_ci	struct scb *scb = NULL;
8418c2ecf20Sopenharmony_ci	int tx_frame_count;
8428c2ecf20Sopenharmony_ci	uint supr_status;
8438c2ecf20Sopenharmony_ci	bool lastframe;
8448c2ecf20Sopenharmony_ci	struct ieee80211_hdr *h;
8458c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info;
8468c2ecf20Sopenharmony_ci	struct ieee80211_tx_rate *txrate;
8478c2ecf20Sopenharmony_ci	int i;
8488c2ecf20Sopenharmony_ci	bool fatal = true;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
8518c2ecf20Sopenharmony_ci			     txs->frameid, txs->status, txs->lasttxtime,
8528c2ecf20Sopenharmony_ci			     txs->sequence, txs->phyerr, txs->ackphyrxsh);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	/* discard intermediate indications for ucode with one legitimate case:
8558c2ecf20Sopenharmony_ci	 *   e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
8568c2ecf20Sopenharmony_ci	 *   but the subsequent tx of DATA failed. so it will start rts/cts
8578c2ecf20Sopenharmony_ci	 *   from the beginning (resetting the rts transmission count)
8588c2ecf20Sopenharmony_ci	 */
8598c2ecf20Sopenharmony_ci	if (!(txs->status & TX_STATUS_AMPDU)
8608c2ecf20Sopenharmony_ci	    && (txs->status & TX_STATUS_INTERMEDIATE)) {
8618c2ecf20Sopenharmony_ci		brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
8628c2ecf20Sopenharmony_ci		fatal = false;
8638c2ecf20Sopenharmony_ci		goto out;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	queue = txs->frameid & TXFID_QUEUE_MASK;
8678c2ecf20Sopenharmony_ci	if (queue >= NFIFO) {
8688c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
8698c2ecf20Sopenharmony_ci		goto out;
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	dma = wlc->hw->di[queue];
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
8758c2ecf20Sopenharmony_ci	if (p == NULL) {
8768c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
8778c2ecf20Sopenharmony_ci		goto out;
8788c2ecf20Sopenharmony_ci	}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	txh = (struct d11txh *) (p->data);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (txs->phyerr)
8838c2ecf20Sopenharmony_ci		brcms_dbg_tx(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
8848c2ecf20Sopenharmony_ci			     txs->phyerr, txh->MainRates);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
8878c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
8888c2ecf20Sopenharmony_ci		goto out;
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci	tx_info = IEEE80211_SKB_CB(p);
8918c2ecf20Sopenharmony_ci	h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	if (tx_info->rate_driver_data[0])
8948c2ecf20Sopenharmony_ci		scb = &wlc->pri_scb;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
8978c2ecf20Sopenharmony_ci		brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
8988c2ecf20Sopenharmony_ci		fatal = false;
8998c2ecf20Sopenharmony_ci		goto out;
9008c2ecf20Sopenharmony_ci	}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	/*
9038c2ecf20Sopenharmony_ci	 * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
9048c2ecf20Sopenharmony_ci	 * frames; this traces them for the rest.
9058c2ecf20Sopenharmony_ci	 */
9068c2ecf20Sopenharmony_ci	trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	supr_status = txs->status & TX_STATUS_SUPR_MASK;
9098c2ecf20Sopenharmony_ci	if (supr_status == TX_STATUS_SUPR_BADCH) {
9108c2ecf20Sopenharmony_ci		unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
9118c2ecf20Sopenharmony_ci		brcms_dbg_tx(wlc->hw->d11core,
9128c2ecf20Sopenharmony_ci			     "Pkt tx suppressed, dest chan %u, current %d\n",
9138c2ecf20Sopenharmony_ci			     (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
9148c2ecf20Sopenharmony_ci			     CHSPEC_CHANNEL(wlc->default_bss->chanspec));
9158c2ecf20Sopenharmony_ci	}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	tx_frame_count =
9188c2ecf20Sopenharmony_ci	    (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	lastframe = !ieee80211_has_morefrags(h->frame_control);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	if (!lastframe) {
9238c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "Not last frame!\n");
9248c2ecf20Sopenharmony_ci	} else {
9258c2ecf20Sopenharmony_ci		/*
9268c2ecf20Sopenharmony_ci		 * Set information to be consumed by Minstrel ht.
9278c2ecf20Sopenharmony_ci		 *
9288c2ecf20Sopenharmony_ci		 * The "fallback limit" is the number of tx attempts a given
9298c2ecf20Sopenharmony_ci		 * MPDU is sent at the "primary" rate. Tx attempts beyond that
9308c2ecf20Sopenharmony_ci		 * limit are sent at the "secondary" rate.
9318c2ecf20Sopenharmony_ci		 * A 'short frame' does not exceed RTS treshold.
9328c2ecf20Sopenharmony_ci		 */
9338c2ecf20Sopenharmony_ci		u16 sfbl,	/* Short Frame Rate Fallback Limit */
9348c2ecf20Sopenharmony_ci		    lfbl,	/* Long Frame Rate Fallback Limit */
9358c2ecf20Sopenharmony_ci		    fbl;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci		if (queue < IEEE80211_NUM_ACS) {
9388c2ecf20Sopenharmony_ci			sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
9398c2ecf20Sopenharmony_ci				      EDCF_SFB);
9408c2ecf20Sopenharmony_ci			lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
9418c2ecf20Sopenharmony_ci				      EDCF_LFB);
9428c2ecf20Sopenharmony_ci		} else {
9438c2ecf20Sopenharmony_ci			sfbl = wlc->SFBL;
9448c2ecf20Sopenharmony_ci			lfbl = wlc->LFBL;
9458c2ecf20Sopenharmony_ci		}
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci		txrate = tx_info->status.rates;
9488c2ecf20Sopenharmony_ci		if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
9498c2ecf20Sopenharmony_ci			fbl = lfbl;
9508c2ecf20Sopenharmony_ci		else
9518c2ecf20Sopenharmony_ci			fbl = sfbl;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci		ieee80211_tx_info_clear_status(tx_info);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci		if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
9568c2ecf20Sopenharmony_ci			/*
9578c2ecf20Sopenharmony_ci			 * rate selection requested a fallback rate
9588c2ecf20Sopenharmony_ci			 * and we used it
9598c2ecf20Sopenharmony_ci			 */
9608c2ecf20Sopenharmony_ci			txrate[0].count = fbl;
9618c2ecf20Sopenharmony_ci			txrate[1].count = tx_frame_count - fbl;
9628c2ecf20Sopenharmony_ci		} else {
9638c2ecf20Sopenharmony_ci			/*
9648c2ecf20Sopenharmony_ci			 * rate selection did not request fallback rate, or
9658c2ecf20Sopenharmony_ci			 * we didn't need it
9668c2ecf20Sopenharmony_ci			 */
9678c2ecf20Sopenharmony_ci			txrate[0].count = tx_frame_count;
9688c2ecf20Sopenharmony_ci			/*
9698c2ecf20Sopenharmony_ci			 * rc80211_minstrel.c:minstrel_tx_status() expects
9708c2ecf20Sopenharmony_ci			 * unused rates to be marked with idx = -1
9718c2ecf20Sopenharmony_ci			 */
9728c2ecf20Sopenharmony_ci			txrate[1].idx = -1;
9738c2ecf20Sopenharmony_ci			txrate[1].count = 0;
9748c2ecf20Sopenharmony_ci		}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci		/* clear the rest of the rates */
9778c2ecf20Sopenharmony_ci		for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
9788c2ecf20Sopenharmony_ci			txrate[i].idx = -1;
9798c2ecf20Sopenharmony_ci			txrate[i].count = 0;
9808c2ecf20Sopenharmony_ci		}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci		if (txs->status & TX_STATUS_ACK_RCV)
9838c2ecf20Sopenharmony_ci			tx_info->flags |= IEEE80211_TX_STAT_ACK;
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	if (lastframe) {
9878c2ecf20Sopenharmony_ci		/* remove PLCP & Broadcom tx descriptor header */
9888c2ecf20Sopenharmony_ci		skb_pull(p, D11_PHY_HDR_LEN);
9898c2ecf20Sopenharmony_ci		skb_pull(p, D11_TXH_LEN);
9908c2ecf20Sopenharmony_ci		ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
9918c2ecf20Sopenharmony_ci	} else {
9928c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core,
9938c2ecf20Sopenharmony_ci			  "%s: Not last frame => not calling tx_status\n",
9948c2ecf20Sopenharmony_ci			  __func__);
9958c2ecf20Sopenharmony_ci	}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	fatal = false;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci out:
10008c2ecf20Sopenharmony_ci	if (fatal) {
10018c2ecf20Sopenharmony_ci		if (txh)
10028c2ecf20Sopenharmony_ci			trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
10038c2ecf20Sopenharmony_ci					   sizeof(*txh));
10048c2ecf20Sopenharmony_ci		brcmu_pkt_buf_free_skb(p);
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	if (dma && queue < NFIFO) {
10088c2ecf20Sopenharmony_ci		u16 ac_queue = brcms_fifo_to_ac(queue);
10098c2ecf20Sopenharmony_ci		if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
10108c2ecf20Sopenharmony_ci		    ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
10118c2ecf20Sopenharmony_ci			ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
10128c2ecf20Sopenharmony_ci		dma_kick_tx(dma);
10138c2ecf20Sopenharmony_ci	}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	return fatal;
10168c2ecf20Sopenharmony_ci}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci/* process tx completion events in BMAC
10198c2ecf20Sopenharmony_ci * Return true if more tx status need to be processed. false otherwise.
10208c2ecf20Sopenharmony_ci */
10218c2ecf20Sopenharmony_cistatic bool
10228c2ecf20Sopenharmony_cibrcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct bcma_device *core;
10258c2ecf20Sopenharmony_ci	struct tx_status txstatus, *txs;
10268c2ecf20Sopenharmony_ci	u32 s1, s2;
10278c2ecf20Sopenharmony_ci	uint n = 0;
10288c2ecf20Sopenharmony_ci	/*
10298c2ecf20Sopenharmony_ci	 * Param 'max_tx_num' indicates max. # tx status to process before
10308c2ecf20Sopenharmony_ci	 * break out.
10318c2ecf20Sopenharmony_ci	 */
10328c2ecf20Sopenharmony_ci	uint max_tx_num = bound ? TXSBND : -1;
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	txs = &txstatus;
10358c2ecf20Sopenharmony_ci	core = wlc_hw->d11core;
10368c2ecf20Sopenharmony_ci	*fatal = false;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	while (n < max_tx_num) {
10398c2ecf20Sopenharmony_ci		s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
10408c2ecf20Sopenharmony_ci		if (s1 == 0xffffffff) {
10418c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
10428c2ecf20Sopenharmony_ci				  __func__);
10438c2ecf20Sopenharmony_ci			*fatal = true;
10448c2ecf20Sopenharmony_ci			return false;
10458c2ecf20Sopenharmony_ci		}
10468c2ecf20Sopenharmony_ci		/* only process when valid */
10478c2ecf20Sopenharmony_ci		if (!(s1 & TXS_V))
10488c2ecf20Sopenharmony_ci			break;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
10518c2ecf20Sopenharmony_ci		txs->status = s1 & TXS_STATUS_MASK;
10528c2ecf20Sopenharmony_ci		txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
10538c2ecf20Sopenharmony_ci		txs->sequence = s2 & TXS_SEQ_MASK;
10548c2ecf20Sopenharmony_ci		txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
10558c2ecf20Sopenharmony_ci		txs->lasttxtime = 0;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci		*fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
10588c2ecf20Sopenharmony_ci		if (*fatal)
10598c2ecf20Sopenharmony_ci			return false;
10608c2ecf20Sopenharmony_ci		n++;
10618c2ecf20Sopenharmony_ci	}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	return n >= max_tx_num;
10648c2ecf20Sopenharmony_ci}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_cistatic void brcms_c_tbtt(struct brcms_c_info *wlc)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	if (wlc->bsscfg->type == BRCMS_TYPE_ADHOC)
10698c2ecf20Sopenharmony_ci		/*
10708c2ecf20Sopenharmony_ci		 * DirFrmQ is now valid...defer setting until end
10718c2ecf20Sopenharmony_ci		 * of ATIM window
10728c2ecf20Sopenharmony_ci		 */
10738c2ecf20Sopenharmony_ci		wlc->qvalid |= MCMD_DIRFRMQVAL;
10748c2ecf20Sopenharmony_ci}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci/* set initial host flags value */
10778c2ecf20Sopenharmony_cistatic void
10788c2ecf20Sopenharmony_cibrcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	memset(mhfs, 0, MHFMAX * sizeof(u16));
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	mhfs[MHF2] |= mhf2_init;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	/* prohibit use of slowclock on multifunction boards */
10878c2ecf20Sopenharmony_ci	if (wlc_hw->boardflags & BFL_NOPLLDOWN)
10888c2ecf20Sopenharmony_ci		mhfs[MHF1] |= MHF1_FORCEFASTCLK;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
10918c2ecf20Sopenharmony_ci		mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
10928c2ecf20Sopenharmony_ci		mhfs[MHF1] |= MHF1_IQSWAP_WAR;
10938c2ecf20Sopenharmony_ci	}
10948c2ecf20Sopenharmony_ci}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_cistatic uint
10978c2ecf20Sopenharmony_cidmareg(uint direction, uint fifonum)
10988c2ecf20Sopenharmony_ci{
10998c2ecf20Sopenharmony_ci	if (direction == DMA_TX)
11008c2ecf20Sopenharmony_ci		return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
11018c2ecf20Sopenharmony_ci	return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
11028c2ecf20Sopenharmony_ci}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	uint i;
11078c2ecf20Sopenharmony_ci	char name[8];
11088c2ecf20Sopenharmony_ci	/*
11098c2ecf20Sopenharmony_ci	 * ucode host flag 2 needed for pio mode, independent of band and fifo
11108c2ecf20Sopenharmony_ci	 */
11118c2ecf20Sopenharmony_ci	u16 pio_mhf2 = 0;
11128c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
11138c2ecf20Sopenharmony_ci	uint unit = wlc_hw->unit;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	/* name and offsets for dma_attach */
11168c2ecf20Sopenharmony_ci	snprintf(name, sizeof(name), "wl%d", unit);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	if (wlc_hw->di[0] == NULL) {	/* Init FIFOs */
11198c2ecf20Sopenharmony_ci		int dma_attach_err = 0;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci		/*
11228c2ecf20Sopenharmony_ci		 * FIFO 0
11238c2ecf20Sopenharmony_ci		 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
11248c2ecf20Sopenharmony_ci		 * RX: RX_FIFO (RX data packets)
11258c2ecf20Sopenharmony_ci		 */
11268c2ecf20Sopenharmony_ci		wlc_hw->di[0] = dma_attach(name, wlc,
11278c2ecf20Sopenharmony_ci					   (wme ? dmareg(DMA_TX, 0) : 0),
11288c2ecf20Sopenharmony_ci					   dmareg(DMA_RX, 0),
11298c2ecf20Sopenharmony_ci					   (wme ? NTXD : 0), NRXD,
11308c2ecf20Sopenharmony_ci					   RXBUFSZ, -1, NRXBUFPOST,
11318c2ecf20Sopenharmony_ci					   BRCMS_HWRXOFF);
11328c2ecf20Sopenharmony_ci		dma_attach_err |= (NULL == wlc_hw->di[0]);
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci		/*
11358c2ecf20Sopenharmony_ci		 * FIFO 1
11368c2ecf20Sopenharmony_ci		 * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
11378c2ecf20Sopenharmony_ci		 *   (legacy) TX_DATA_FIFO (TX data packets)
11388c2ecf20Sopenharmony_ci		 * RX: UNUSED
11398c2ecf20Sopenharmony_ci		 */
11408c2ecf20Sopenharmony_ci		wlc_hw->di[1] = dma_attach(name, wlc,
11418c2ecf20Sopenharmony_ci					   dmareg(DMA_TX, 1), 0,
11428c2ecf20Sopenharmony_ci					   NTXD, 0, 0, -1, 0, 0);
11438c2ecf20Sopenharmony_ci		dma_attach_err |= (NULL == wlc_hw->di[1]);
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci		/*
11468c2ecf20Sopenharmony_ci		 * FIFO 2
11478c2ecf20Sopenharmony_ci		 * TX: TX_AC_VI_FIFO (TX AC Video data packets)
11488c2ecf20Sopenharmony_ci		 * RX: UNUSED
11498c2ecf20Sopenharmony_ci		 */
11508c2ecf20Sopenharmony_ci		wlc_hw->di[2] = dma_attach(name, wlc,
11518c2ecf20Sopenharmony_ci					   dmareg(DMA_TX, 2), 0,
11528c2ecf20Sopenharmony_ci					   NTXD, 0, 0, -1, 0, 0);
11538c2ecf20Sopenharmony_ci		dma_attach_err |= (NULL == wlc_hw->di[2]);
11548c2ecf20Sopenharmony_ci		/*
11558c2ecf20Sopenharmony_ci		 * FIFO 3
11568c2ecf20Sopenharmony_ci		 * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
11578c2ecf20Sopenharmony_ci		 *   (legacy) TX_CTL_FIFO (TX control & mgmt packets)
11588c2ecf20Sopenharmony_ci		 */
11598c2ecf20Sopenharmony_ci		wlc_hw->di[3] = dma_attach(name, wlc,
11608c2ecf20Sopenharmony_ci					   dmareg(DMA_TX, 3),
11618c2ecf20Sopenharmony_ci					   0, NTXD, 0, 0, -1,
11628c2ecf20Sopenharmony_ci					   0, 0);
11638c2ecf20Sopenharmony_ci		dma_attach_err |= (NULL == wlc_hw->di[3]);
11648c2ecf20Sopenharmony_ci/* Cleaner to leave this as if with AP defined */
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci		if (dma_attach_err) {
11678c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core,
11688c2ecf20Sopenharmony_ci				  "wl%d: wlc_attach: dma_attach failed\n",
11698c2ecf20Sopenharmony_ci				  unit);
11708c2ecf20Sopenharmony_ci			return false;
11718c2ecf20Sopenharmony_ci		}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci		/* get pointer to dma engine tx flow control variable */
11748c2ecf20Sopenharmony_ci		for (i = 0; i < NFIFO; i++)
11758c2ecf20Sopenharmony_ci			if (wlc_hw->di[i])
11768c2ecf20Sopenharmony_ci				wlc_hw->txavail[i] =
11778c2ecf20Sopenharmony_ci				    (uint *) dma_getvar(wlc_hw->di[i],
11788c2ecf20Sopenharmony_ci							"&txavail");
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	/* initial ucode host flags */
11828c2ecf20Sopenharmony_ci	brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	return true;
11858c2ecf20Sopenharmony_ci}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_cistatic void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
11888c2ecf20Sopenharmony_ci{
11898c2ecf20Sopenharmony_ci	uint j;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	for (j = 0; j < NFIFO; j++) {
11928c2ecf20Sopenharmony_ci		if (wlc_hw->di[j]) {
11938c2ecf20Sopenharmony_ci			dma_detach(wlc_hw->di[j]);
11948c2ecf20Sopenharmony_ci			wlc_hw->di[j] = NULL;
11958c2ecf20Sopenharmony_ci		}
11968c2ecf20Sopenharmony_ci	}
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci/*
12008c2ecf20Sopenharmony_ci * Initialize brcms_c_info default values ...
12018c2ecf20Sopenharmony_ci * may get overrides later in this function
12028c2ecf20Sopenharmony_ci *  BMAC_NOTES, move low out and resolve the dangling ones
12038c2ecf20Sopenharmony_ci */
12048c2ecf20Sopenharmony_cistatic void brcms_b_info_init(struct brcms_hardware *wlc_hw)
12058c2ecf20Sopenharmony_ci{
12068c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = wlc_hw->wlc;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	/* set default sw macintmask value */
12098c2ecf20Sopenharmony_ci	wlc->defmacintmask = DEF_MACINTMASK;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	/* various 802.11g modes */
12128c2ecf20Sopenharmony_ci	wlc_hw->shortslot = false;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	wlc_hw->SFBL = RETRY_SHORT_FB;
12158c2ecf20Sopenharmony_ci	wlc_hw->LFBL = RETRY_LONG_FB;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	/* default mac retry limits */
12188c2ecf20Sopenharmony_ci	wlc_hw->SRL = RETRY_SHORT_DEF;
12198c2ecf20Sopenharmony_ci	wlc_hw->LRL = RETRY_LONG_DEF;
12208c2ecf20Sopenharmony_ci	wlc_hw->chanspec = ch20mhz_chspec(1);
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_cistatic void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
12248c2ecf20Sopenharmony_ci{
12258c2ecf20Sopenharmony_ci	/* delay before first read of ucode state */
12268c2ecf20Sopenharmony_ci	udelay(40);
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	/* wait until ucode is no longer asleep */
12298c2ecf20Sopenharmony_ci	SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
12308c2ecf20Sopenharmony_ci		  DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
12318c2ecf20Sopenharmony_ci}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci/* control chip clock to save power, enable dynamic clock or force fast clock */
12348c2ecf20Sopenharmony_cistatic void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
12358c2ecf20Sopenharmony_ci{
12368c2ecf20Sopenharmony_ci	if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
12378c2ecf20Sopenharmony_ci		/* new chips with PMU, CCS_FORCEHT will distribute the HT clock
12388c2ecf20Sopenharmony_ci		 * on backplane, but mac core will still run on ALP(not HT) when
12398c2ecf20Sopenharmony_ci		 * it enters powersave mode, which means the FCA bit may not be
12408c2ecf20Sopenharmony_ci		 * set. Should wakeup mac if driver wants it to run on HT.
12418c2ecf20Sopenharmony_ci		 */
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci		if (wlc_hw->clk) {
12448c2ecf20Sopenharmony_ci			if (mode == BCMA_CLKMODE_FAST) {
12458c2ecf20Sopenharmony_ci				bcma_set32(wlc_hw->d11core,
12468c2ecf20Sopenharmony_ci					   D11REGOFFS(clk_ctl_st),
12478c2ecf20Sopenharmony_ci					   CCS_FORCEHT);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci				udelay(64);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci				SPINWAIT(
12528c2ecf20Sopenharmony_ci				    ((bcma_read32(wlc_hw->d11core,
12538c2ecf20Sopenharmony_ci				      D11REGOFFS(clk_ctl_st)) &
12548c2ecf20Sopenharmony_ci				      CCS_HTAVAIL) == 0),
12558c2ecf20Sopenharmony_ci				      PMU_MAX_TRANSITION_DLY);
12568c2ecf20Sopenharmony_ci				WARN_ON(!(bcma_read32(wlc_hw->d11core,
12578c2ecf20Sopenharmony_ci					D11REGOFFS(clk_ctl_st)) &
12588c2ecf20Sopenharmony_ci					CCS_HTAVAIL));
12598c2ecf20Sopenharmony_ci			} else {
12608c2ecf20Sopenharmony_ci				if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
12618c2ecf20Sopenharmony_ci				    (bcma_read32(wlc_hw->d11core,
12628c2ecf20Sopenharmony_ci					D11REGOFFS(clk_ctl_st)) &
12638c2ecf20Sopenharmony_ci					(CCS_FORCEHT | CCS_HTAREQ)))
12648c2ecf20Sopenharmony_ci					SPINWAIT(
12658c2ecf20Sopenharmony_ci					    ((bcma_read32(wlc_hw->d11core,
12668c2ecf20Sopenharmony_ci					      offsetof(struct d11regs,
12678c2ecf20Sopenharmony_ci						       clk_ctl_st)) &
12688c2ecf20Sopenharmony_ci					      CCS_HTAVAIL) == 0),
12698c2ecf20Sopenharmony_ci					      PMU_MAX_TRANSITION_DLY);
12708c2ecf20Sopenharmony_ci				bcma_mask32(wlc_hw->d11core,
12718c2ecf20Sopenharmony_ci					D11REGOFFS(clk_ctl_st),
12728c2ecf20Sopenharmony_ci					~CCS_FORCEHT);
12738c2ecf20Sopenharmony_ci			}
12748c2ecf20Sopenharmony_ci		}
12758c2ecf20Sopenharmony_ci		wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
12768c2ecf20Sopenharmony_ci	} else {
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci		/* old chips w/o PMU, force HT through cc,
12798c2ecf20Sopenharmony_ci		 * then use FCA to verify mac is running fast clock
12808c2ecf20Sopenharmony_ci		 */
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci		wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci		/* check fast clock is available (if core is not in reset) */
12858c2ecf20Sopenharmony_ci		if (wlc_hw->forcefastclk && wlc_hw->clk)
12868c2ecf20Sopenharmony_ci			WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
12878c2ecf20Sopenharmony_ci				  SISF_FCLKA));
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci		/*
12908c2ecf20Sopenharmony_ci		 * keep the ucode wake bit on if forcefastclk is on since we
12918c2ecf20Sopenharmony_ci		 * do not want ucode to put us back to slow clock when it dozes
12928c2ecf20Sopenharmony_ci		 * for PM mode. Code below matches the wake override bit with
12938c2ecf20Sopenharmony_ci		 * current forcefastclk state. Only setting bit in wake_override
12948c2ecf20Sopenharmony_ci		 * instead of waking ucode immediately since old code had this
12958c2ecf20Sopenharmony_ci		 * behavior. Older code set wlc->forcefastclk but only had the
12968c2ecf20Sopenharmony_ci		 * wake happen if the wakup_ucode work (protected by an up
12978c2ecf20Sopenharmony_ci		 * check) was executed just below.
12988c2ecf20Sopenharmony_ci		 */
12998c2ecf20Sopenharmony_ci		if (wlc_hw->forcefastclk)
13008c2ecf20Sopenharmony_ci			mboolset(wlc_hw->wake_override,
13018c2ecf20Sopenharmony_ci				 BRCMS_WAKE_OVERRIDE_FORCEFAST);
13028c2ecf20Sopenharmony_ci		else
13038c2ecf20Sopenharmony_ci			mboolclr(wlc_hw->wake_override,
13048c2ecf20Sopenharmony_ci				 BRCMS_WAKE_OVERRIDE_FORCEFAST);
13058c2ecf20Sopenharmony_ci	}
13068c2ecf20Sopenharmony_ci}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci/* set or clear ucode host flag bits
13098c2ecf20Sopenharmony_ci * it has an optimization for no-change write
13108c2ecf20Sopenharmony_ci * it only writes through shared memory when the core has clock;
13118c2ecf20Sopenharmony_ci * pre-CLK changes should use wlc_write_mhf to get around the optimization
13128c2ecf20Sopenharmony_ci *
13138c2ecf20Sopenharmony_ci *
13148c2ecf20Sopenharmony_ci * bands values are: BRCM_BAND_AUTO <--- Current band only
13158c2ecf20Sopenharmony_ci *                   BRCM_BAND_5G   <--- 5G band only
13168c2ecf20Sopenharmony_ci *                   BRCM_BAND_2G   <--- 2G band only
13178c2ecf20Sopenharmony_ci *                   BRCM_BAND_ALL  <--- All bands
13188c2ecf20Sopenharmony_ci */
13198c2ecf20Sopenharmony_civoid
13208c2ecf20Sopenharmony_cibrcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
13218c2ecf20Sopenharmony_ci	     int bands)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	u16 save;
13248c2ecf20Sopenharmony_ci	u16 addr[MHFMAX] = {
13258c2ecf20Sopenharmony_ci		M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
13268c2ecf20Sopenharmony_ci		M_HOST_FLAGS5
13278c2ecf20Sopenharmony_ci	};
13288c2ecf20Sopenharmony_ci	struct brcms_hw_band *band;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	if ((val & ~mask) || idx >= MHFMAX)
13318c2ecf20Sopenharmony_ci		return; /* error condition */
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	switch (bands) {
13348c2ecf20Sopenharmony_ci		/* Current band only or all bands,
13358c2ecf20Sopenharmony_ci		 * then set the band to current band
13368c2ecf20Sopenharmony_ci		 */
13378c2ecf20Sopenharmony_ci	case BRCM_BAND_AUTO:
13388c2ecf20Sopenharmony_ci	case BRCM_BAND_ALL:
13398c2ecf20Sopenharmony_ci		band = wlc_hw->band;
13408c2ecf20Sopenharmony_ci		break;
13418c2ecf20Sopenharmony_ci	case BRCM_BAND_5G:
13428c2ecf20Sopenharmony_ci		band = wlc_hw->bandstate[BAND_5G_INDEX];
13438c2ecf20Sopenharmony_ci		break;
13448c2ecf20Sopenharmony_ci	case BRCM_BAND_2G:
13458c2ecf20Sopenharmony_ci		band = wlc_hw->bandstate[BAND_2G_INDEX];
13468c2ecf20Sopenharmony_ci		break;
13478c2ecf20Sopenharmony_ci	default:
13488c2ecf20Sopenharmony_ci		band = NULL;	/* error condition */
13498c2ecf20Sopenharmony_ci	}
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	if (band) {
13528c2ecf20Sopenharmony_ci		save = band->mhfs[idx];
13538c2ecf20Sopenharmony_ci		band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci		/* optimization: only write through if changed, and
13568c2ecf20Sopenharmony_ci		 * changed band is the current band
13578c2ecf20Sopenharmony_ci		 */
13588c2ecf20Sopenharmony_ci		if (wlc_hw->clk && (band->mhfs[idx] != save)
13598c2ecf20Sopenharmony_ci		    && (band == wlc_hw->band))
13608c2ecf20Sopenharmony_ci			brcms_b_write_shm(wlc_hw, addr[idx],
13618c2ecf20Sopenharmony_ci					   (u16) band->mhfs[idx]);
13628c2ecf20Sopenharmony_ci	}
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	if (bands == BRCM_BAND_ALL) {
13658c2ecf20Sopenharmony_ci		wlc_hw->bandstate[0]->mhfs[idx] =
13668c2ecf20Sopenharmony_ci		    (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
13678c2ecf20Sopenharmony_ci		wlc_hw->bandstate[1]->mhfs[idx] =
13688c2ecf20Sopenharmony_ci		    (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
13698c2ecf20Sopenharmony_ci	}
13708c2ecf20Sopenharmony_ci}
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci/* set the maccontrol register to desired reset state and
13738c2ecf20Sopenharmony_ci * initialize the sw cache of the register
13748c2ecf20Sopenharmony_ci */
13758c2ecf20Sopenharmony_cistatic void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
13768c2ecf20Sopenharmony_ci{
13778c2ecf20Sopenharmony_ci	/* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
13788c2ecf20Sopenharmony_ci	wlc_hw->maccontrol = 0;
13798c2ecf20Sopenharmony_ci	wlc_hw->suspended_fifos = 0;
13808c2ecf20Sopenharmony_ci	wlc_hw->wake_override = 0;
13818c2ecf20Sopenharmony_ci	wlc_hw->mute_override = 0;
13828c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
13838c2ecf20Sopenharmony_ci}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci/*
13868c2ecf20Sopenharmony_ci * write the software state of maccontrol and
13878c2ecf20Sopenharmony_ci * overrides to the maccontrol register
13888c2ecf20Sopenharmony_ci */
13898c2ecf20Sopenharmony_cistatic void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
13908c2ecf20Sopenharmony_ci{
13918c2ecf20Sopenharmony_ci	u32 maccontrol = wlc_hw->maccontrol;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	/* OR in the wake bit if overridden */
13948c2ecf20Sopenharmony_ci	if (wlc_hw->wake_override)
13958c2ecf20Sopenharmony_ci		maccontrol |= MCTL_WAKE;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	/* set AP and INFRA bits for mute if needed */
13988c2ecf20Sopenharmony_ci	if (wlc_hw->mute_override) {
13998c2ecf20Sopenharmony_ci		maccontrol &= ~(MCTL_AP);
14008c2ecf20Sopenharmony_ci		maccontrol |= MCTL_INFRA;
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
14048c2ecf20Sopenharmony_ci		     maccontrol);
14058c2ecf20Sopenharmony_ci}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci/* set or clear maccontrol bits */
14088c2ecf20Sopenharmony_civoid brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
14098c2ecf20Sopenharmony_ci{
14108c2ecf20Sopenharmony_ci	u32 maccontrol;
14118c2ecf20Sopenharmony_ci	u32 new_maccontrol;
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	if (val & ~mask)
14148c2ecf20Sopenharmony_ci		return; /* error condition */
14158c2ecf20Sopenharmony_ci	maccontrol = wlc_hw->maccontrol;
14168c2ecf20Sopenharmony_ci	new_maccontrol = (maccontrol & ~mask) | val;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	/* if the new maccontrol value is the same as the old, nothing to do */
14198c2ecf20Sopenharmony_ci	if (new_maccontrol == maccontrol)
14208c2ecf20Sopenharmony_ci		return;
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	/* something changed, cache the new value */
14238c2ecf20Sopenharmony_ci	wlc_hw->maccontrol = new_maccontrol;
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	/* write the new values with overrides applied */
14268c2ecf20Sopenharmony_ci	brcms_c_mctrl_write(wlc_hw);
14278c2ecf20Sopenharmony_ci}
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_civoid brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
14308c2ecf20Sopenharmony_ci				 u32 override_bit)
14318c2ecf20Sopenharmony_ci{
14328c2ecf20Sopenharmony_ci	if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
14338c2ecf20Sopenharmony_ci		mboolset(wlc_hw->wake_override, override_bit);
14348c2ecf20Sopenharmony_ci		return;
14358c2ecf20Sopenharmony_ci	}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	mboolset(wlc_hw->wake_override, override_bit);
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	brcms_c_mctrl_write(wlc_hw);
14408c2ecf20Sopenharmony_ci	brcms_b_wait_for_wake(wlc_hw);
14418c2ecf20Sopenharmony_ci}
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_civoid brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
14448c2ecf20Sopenharmony_ci				   u32 override_bit)
14458c2ecf20Sopenharmony_ci{
14468c2ecf20Sopenharmony_ci	mboolclr(wlc_hw->wake_override, override_bit);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
14498c2ecf20Sopenharmony_ci		return;
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	brcms_c_mctrl_write(wlc_hw);
14528c2ecf20Sopenharmony_ci}
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci/* When driver needs ucode to stop beaconing, it has to make sure that
14558c2ecf20Sopenharmony_ci * MCTL_AP is clear and MCTL_INFRA is set
14568c2ecf20Sopenharmony_ci * Mode           MCTL_AP        MCTL_INFRA
14578c2ecf20Sopenharmony_ci * AP                1              1
14588c2ecf20Sopenharmony_ci * STA               0              1 <--- This will ensure no beacons
14598c2ecf20Sopenharmony_ci * IBSS              0              0
14608c2ecf20Sopenharmony_ci */
14618c2ecf20Sopenharmony_cistatic void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
14628c2ecf20Sopenharmony_ci{
14638c2ecf20Sopenharmony_ci	wlc_hw->mute_override = 1;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	/* if maccontrol already has AP == 0 and INFRA == 1 without this
14668c2ecf20Sopenharmony_ci	 * override, then there is no change to write
14678c2ecf20Sopenharmony_ci	 */
14688c2ecf20Sopenharmony_ci	if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
14698c2ecf20Sopenharmony_ci		return;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	brcms_c_mctrl_write(wlc_hw);
14728c2ecf20Sopenharmony_ci}
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci/* Clear the override on AP and INFRA bits */
14758c2ecf20Sopenharmony_cistatic void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
14768c2ecf20Sopenharmony_ci{
14778c2ecf20Sopenharmony_ci	if (wlc_hw->mute_override == 0)
14788c2ecf20Sopenharmony_ci		return;
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	wlc_hw->mute_override = 0;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	/* if maccontrol already has AP == 0 and INFRA == 1 without this
14838c2ecf20Sopenharmony_ci	 * override, then there is no change to write
14848c2ecf20Sopenharmony_ci	 */
14858c2ecf20Sopenharmony_ci	if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
14868c2ecf20Sopenharmony_ci		return;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	brcms_c_mctrl_write(wlc_hw);
14898c2ecf20Sopenharmony_ci}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci/*
14928c2ecf20Sopenharmony_ci * Write a MAC address to the given match reg offset in the RXE match engine.
14938c2ecf20Sopenharmony_ci */
14948c2ecf20Sopenharmony_cistatic void
14958c2ecf20Sopenharmony_cibrcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
14968c2ecf20Sopenharmony_ci		       const u8 *addr)
14978c2ecf20Sopenharmony_ci{
14988c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
14998c2ecf20Sopenharmony_ci	u16 mac_l;
15008c2ecf20Sopenharmony_ci	u16 mac_m;
15018c2ecf20Sopenharmony_ci	u16 mac_h;
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	mac_l = addr[0] | (addr[1] << 8);
15068c2ecf20Sopenharmony_ci	mac_m = addr[2] | (addr[3] << 8);
15078c2ecf20Sopenharmony_ci	mac_h = addr[4] | (addr[5] << 8);
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	/* enter the MAC addr into the RXE match registers */
15108c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(rcm_ctl),
15118c2ecf20Sopenharmony_ci		     RCM_INC_DATA | match_reg_offset);
15128c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
15138c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
15148c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
15158c2ecf20Sopenharmony_ci}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_civoid
15188c2ecf20Sopenharmony_cibrcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
15198c2ecf20Sopenharmony_ci			    void *buf)
15208c2ecf20Sopenharmony_ci{
15218c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
15228c2ecf20Sopenharmony_ci	u32 word;
15238c2ecf20Sopenharmony_ci	__le32 word_le;
15248c2ecf20Sopenharmony_ci	__be32 word_be;
15258c2ecf20Sopenharmony_ci	bool be_bit;
15268c2ecf20Sopenharmony_ci	brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	/* if MCTL_BIGEND bit set in mac control register,
15318c2ecf20Sopenharmony_ci	 * the chip swaps data in fifo, as well as data in
15328c2ecf20Sopenharmony_ci	 * template ram
15338c2ecf20Sopenharmony_ci	 */
15348c2ecf20Sopenharmony_ci	be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	while (len > 0) {
15378c2ecf20Sopenharmony_ci		memcpy(&word, buf, sizeof(u32));
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci		if (be_bit) {
15408c2ecf20Sopenharmony_ci			word_be = cpu_to_be32(word);
15418c2ecf20Sopenharmony_ci			word = *(u32 *)&word_be;
15428c2ecf20Sopenharmony_ci		} else {
15438c2ecf20Sopenharmony_ci			word_le = cpu_to_le32(word);
15448c2ecf20Sopenharmony_ci			word = *(u32 *)&word_le;
15458c2ecf20Sopenharmony_ci		}
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(tplatewrdata), word);
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci		buf = (u8 *) buf + sizeof(u32);
15508c2ecf20Sopenharmony_ci		len -= sizeof(u32);
15518c2ecf20Sopenharmony_ci	}
15528c2ecf20Sopenharmony_ci}
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_cistatic void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
15558c2ecf20Sopenharmony_ci{
15568c2ecf20Sopenharmony_ci	wlc_hw->band->CWmin = newmin;
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
15598c2ecf20Sopenharmony_ci		     OBJADDR_SCR_SEL | S_DOT11_CWMIN);
15608c2ecf20Sopenharmony_ci	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
15618c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
15628c2ecf20Sopenharmony_ci}
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_cistatic void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
15658c2ecf20Sopenharmony_ci{
15668c2ecf20Sopenharmony_ci	wlc_hw->band->CWmax = newmax;
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
15698c2ecf20Sopenharmony_ci		     OBJADDR_SCR_SEL | S_DOT11_CWMAX);
15708c2ecf20Sopenharmony_ci	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
15718c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
15728c2ecf20Sopenharmony_ci}
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_civoid brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
15758c2ecf20Sopenharmony_ci{
15768c2ecf20Sopenharmony_ci	bool fastclk;
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	/* request FAST clock if not on */
15798c2ecf20Sopenharmony_ci	fastclk = wlc_hw->forcefastclk;
15808c2ecf20Sopenharmony_ci	if (!fastclk)
15818c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	brcms_b_phy_reset(wlc_hw);
15868c2ecf20Sopenharmony_ci	wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci	/* restore the clk */
15898c2ecf20Sopenharmony_ci	if (!fastclk)
15908c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
15918c2ecf20Sopenharmony_ci}
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_cistatic void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
15948c2ecf20Sopenharmony_ci{
15958c2ecf20Sopenharmony_ci	u16 v;
15968c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = wlc_hw->wlc;
15978c2ecf20Sopenharmony_ci	/* update SYNTHPU_DLY */
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	if (BRCMS_ISLCNPHY(wlc->band))
16008c2ecf20Sopenharmony_ci		v = SYNTHPU_DLY_LPPHY_US;
16018c2ecf20Sopenharmony_ci	else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
16028c2ecf20Sopenharmony_ci		v = SYNTHPU_DLY_NPHY_US;
16038c2ecf20Sopenharmony_ci	else
16048c2ecf20Sopenharmony_ci		v = SYNTHPU_DLY_BPHY_US;
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
16078c2ecf20Sopenharmony_ci}
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_cistatic void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
16108c2ecf20Sopenharmony_ci{
16118c2ecf20Sopenharmony_ci	u16 phyctl;
16128c2ecf20Sopenharmony_ci	u16 phytxant = wlc_hw->bmac_phytxant;
16138c2ecf20Sopenharmony_ci	u16 mask = PHY_TXC_ANT_MASK;
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	/* set the Probe Response frame phy control word */
16168c2ecf20Sopenharmony_ci	phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
16178c2ecf20Sopenharmony_ci	phyctl = (phyctl & ~mask) | phytxant;
16188c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	/* set the Response (ACK/CTS) frame phy control word */
16218c2ecf20Sopenharmony_ci	phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
16228c2ecf20Sopenharmony_ci	phyctl = (phyctl & ~mask) | phytxant;
16238c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
16248c2ecf20Sopenharmony_ci}
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_cistatic u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
16278c2ecf20Sopenharmony_ci					 u8 rate)
16288c2ecf20Sopenharmony_ci{
16298c2ecf20Sopenharmony_ci	uint i;
16308c2ecf20Sopenharmony_ci	u8 plcp_rate = 0;
16318c2ecf20Sopenharmony_ci	struct plcp_signal_rate_lookup {
16328c2ecf20Sopenharmony_ci		u8 rate;
16338c2ecf20Sopenharmony_ci		u8 signal_rate;
16348c2ecf20Sopenharmony_ci	};
16358c2ecf20Sopenharmony_ci	/* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
16368c2ecf20Sopenharmony_ci	const struct plcp_signal_rate_lookup rate_lookup[] = {
16378c2ecf20Sopenharmony_ci		{BRCM_RATE_6M, 0xB},
16388c2ecf20Sopenharmony_ci		{BRCM_RATE_9M, 0xF},
16398c2ecf20Sopenharmony_ci		{BRCM_RATE_12M, 0xA},
16408c2ecf20Sopenharmony_ci		{BRCM_RATE_18M, 0xE},
16418c2ecf20Sopenharmony_ci		{BRCM_RATE_24M, 0x9},
16428c2ecf20Sopenharmony_ci		{BRCM_RATE_36M, 0xD},
16438c2ecf20Sopenharmony_ci		{BRCM_RATE_48M, 0x8},
16448c2ecf20Sopenharmony_ci		{BRCM_RATE_54M, 0xC}
16458c2ecf20Sopenharmony_ci	};
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
16488c2ecf20Sopenharmony_ci		if (rate == rate_lookup[i].rate) {
16498c2ecf20Sopenharmony_ci			plcp_rate = rate_lookup[i].signal_rate;
16508c2ecf20Sopenharmony_ci			break;
16518c2ecf20Sopenharmony_ci		}
16528c2ecf20Sopenharmony_ci	}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	/* Find the SHM pointer to the rate table entry by looking in the
16558c2ecf20Sopenharmony_ci	 * Direct-map Table
16568c2ecf20Sopenharmony_ci	 */
16578c2ecf20Sopenharmony_ci	return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
16588c2ecf20Sopenharmony_ci}
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_cistatic void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
16618c2ecf20Sopenharmony_ci{
16628c2ecf20Sopenharmony_ci	u8 rate;
16638c2ecf20Sopenharmony_ci	u8 rates[8] = {
16648c2ecf20Sopenharmony_ci		BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
16658c2ecf20Sopenharmony_ci		BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
16668c2ecf20Sopenharmony_ci	};
16678c2ecf20Sopenharmony_ci	u16 entry_ptr;
16688c2ecf20Sopenharmony_ci	u16 pctl1;
16698c2ecf20Sopenharmony_ci	uint i;
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
16728c2ecf20Sopenharmony_ci		return;
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	/* walk the phy rate table and update the entries */
16758c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rates); i++) {
16768c2ecf20Sopenharmony_ci		rate = rates[i];
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci		entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci		/* read the SHM Rate Table entry OFDM PCTL1 values */
16818c2ecf20Sopenharmony_ci		pctl1 =
16828c2ecf20Sopenharmony_ci		    brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci		/* modify the value */
16858c2ecf20Sopenharmony_ci		pctl1 &= ~PHY_TXC1_MODE_MASK;
16868c2ecf20Sopenharmony_ci		pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci		/* Update the SHM Rate Table entry OFDM PCTL1 values */
16898c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
16908c2ecf20Sopenharmony_ci				   pctl1);
16918c2ecf20Sopenharmony_ci	}
16928c2ecf20Sopenharmony_ci}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci/* band-specific init */
16958c2ecf20Sopenharmony_cistatic void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
16968c2ecf20Sopenharmony_ci{
16978c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
17008c2ecf20Sopenharmony_ci			   wlc_hw->band->bandunit);
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_ci	brcms_c_ucode_bsinit(wlc_hw);
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	wlc_phy_init(wlc_hw->band->pi, chanspec);
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	brcms_c_ucode_txant_set(wlc_hw);
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	/*
17098c2ecf20Sopenharmony_ci	 * cwmin is band-specific, update hardware
17108c2ecf20Sopenharmony_ci	 * with value for current band
17118c2ecf20Sopenharmony_ci	 */
17128c2ecf20Sopenharmony_ci	brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
17138c2ecf20Sopenharmony_ci	brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	brcms_b_update_slot_timing(wlc_hw,
17168c2ecf20Sopenharmony_ci				   wlc_hw->band->bandtype == BRCM_BAND_5G ?
17178c2ecf20Sopenharmony_ci				   true : wlc_hw->shortslot);
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	/* write phytype and phyvers */
17208c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
17218c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	/*
17248c2ecf20Sopenharmony_ci	 * initialize the txphyctl1 rate table since
17258c2ecf20Sopenharmony_ci	 * shmem is shared between bands
17268c2ecf20Sopenharmony_ci	 */
17278c2ecf20Sopenharmony_ci	brcms_upd_ofdm_pctl1_table(wlc_hw);
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci	brcms_b_upd_synthpu(wlc_hw);
17308c2ecf20Sopenharmony_ci}
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci/* Perform a soft reset of the PHY PLL */
17338c2ecf20Sopenharmony_civoid brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
17348c2ecf20Sopenharmony_ci{
17358c2ecf20Sopenharmony_ci	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
17368c2ecf20Sopenharmony_ci		  ~0, 0);
17378c2ecf20Sopenharmony_ci	udelay(1);
17388c2ecf20Sopenharmony_ci	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
17398c2ecf20Sopenharmony_ci		  0x4, 0);
17408c2ecf20Sopenharmony_ci	udelay(1);
17418c2ecf20Sopenharmony_ci	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
17428c2ecf20Sopenharmony_ci		  0x4, 4);
17438c2ecf20Sopenharmony_ci	udelay(1);
17448c2ecf20Sopenharmony_ci	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
17458c2ecf20Sopenharmony_ci		  0x4, 0);
17468c2ecf20Sopenharmony_ci	udelay(1);
17478c2ecf20Sopenharmony_ci}
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci/* light way to turn on phy clock without reset for NPHY only
17508c2ecf20Sopenharmony_ci *  refer to brcms_b_core_phy_clk for full version
17518c2ecf20Sopenharmony_ci */
17528c2ecf20Sopenharmony_civoid brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
17538c2ecf20Sopenharmony_ci{
17548c2ecf20Sopenharmony_ci	/* support(necessary for NPHY and HYPHY) only */
17558c2ecf20Sopenharmony_ci	if (!BRCMS_ISNPHY(wlc_hw->band))
17568c2ecf20Sopenharmony_ci		return;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	if (ON == clk)
17598c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
17608c2ecf20Sopenharmony_ci	else
17618c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci}
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_civoid brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
17668c2ecf20Sopenharmony_ci{
17678c2ecf20Sopenharmony_ci	if (ON == clk)
17688c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
17698c2ecf20Sopenharmony_ci	else
17708c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
17718c2ecf20Sopenharmony_ci}
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_civoid brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
17748c2ecf20Sopenharmony_ci{
17758c2ecf20Sopenharmony_ci	struct brcms_phy_pub *pih = wlc_hw->band->pi;
17768c2ecf20Sopenharmony_ci	u32 phy_bw_clkbits;
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	if (pih == NULL)
17818c2ecf20Sopenharmony_ci		return;
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci	phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	/* Specific reset sequence required for NPHY rev 3 and 4 */
17868c2ecf20Sopenharmony_ci	if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
17878c2ecf20Sopenharmony_ci	    NREV_LE(wlc_hw->band->phyrev, 4)) {
17888c2ecf20Sopenharmony_ci		/* Set the PHY bandwidth */
17898c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci		udelay(1);
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci		/* Perform a soft reset of the PHY PLL */
17948c2ecf20Sopenharmony_ci		brcms_b_core_phypll_reset(wlc_hw);
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci		/* reset the PHY */
17978c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
17988c2ecf20Sopenharmony_ci				   (SICF_PRST | SICF_PCLKE));
17998c2ecf20Sopenharmony_ci	} else {
18008c2ecf20Sopenharmony_ci		brcms_b_core_ioctl(wlc_hw,
18018c2ecf20Sopenharmony_ci				   (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
18028c2ecf20Sopenharmony_ci				   (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
18038c2ecf20Sopenharmony_ci	}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	udelay(2);
18068c2ecf20Sopenharmony_ci	brcms_b_core_phy_clk(wlc_hw, ON);
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci	wlc_phy_anacore(pih, ON);
18098c2ecf20Sopenharmony_ci}
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ci/* switch to and initialize new band */
18128c2ecf20Sopenharmony_cistatic void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
18138c2ecf20Sopenharmony_ci			    u16 chanspec) {
18148c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = wlc_hw->wlc;
18158c2ecf20Sopenharmony_ci	u32 macintmask;
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci	/* Enable the d11 core before accessing it */
18188c2ecf20Sopenharmony_ci	if (!bcma_core_is_enabled(wlc_hw->d11core)) {
18198c2ecf20Sopenharmony_ci		bcma_core_enable(wlc_hw->d11core, 0);
18208c2ecf20Sopenharmony_ci		brcms_c_mctrl_reset(wlc_hw);
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci	macintmask = brcms_c_setband_inact(wlc, bandunit);
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	if (!wlc_hw->up)
18268c2ecf20Sopenharmony_ci		return;
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci	brcms_b_core_phy_clk(wlc_hw, ON);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	/* band-specific initializations */
18318c2ecf20Sopenharmony_ci	brcms_b_bsinit(wlc, chanspec);
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	/*
18348c2ecf20Sopenharmony_ci	 * If there are any pending software interrupt bits,
18358c2ecf20Sopenharmony_ci	 * then replace these with a harmless nonzero value
18368c2ecf20Sopenharmony_ci	 * so brcms_c_dpc() will re-enable interrupts when done.
18378c2ecf20Sopenharmony_ci	 */
18388c2ecf20Sopenharmony_ci	if (wlc->macintstatus)
18398c2ecf20Sopenharmony_ci		wlc->macintstatus = MI_DMAINT;
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_ci	/* restore macintmask */
18428c2ecf20Sopenharmony_ci	brcms_intrsrestore(wlc->wl, macintmask);
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	/* ucode should still be suspended.. */
18458c2ecf20Sopenharmony_ci	WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
18468c2ecf20Sopenharmony_ci		 MCTL_EN_MAC) != 0);
18478c2ecf20Sopenharmony_ci}
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_cistatic bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
18508c2ecf20Sopenharmony_ci{
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	/* reject unsupported corerev */
18538c2ecf20Sopenharmony_ci	if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
18548c2ecf20Sopenharmony_ci		wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
18558c2ecf20Sopenharmony_ci			  wlc_hw->corerev);
18568c2ecf20Sopenharmony_ci		return false;
18578c2ecf20Sopenharmony_ci	}
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci	return true;
18608c2ecf20Sopenharmony_ci}
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_ci/* Validate some board info parameters */
18638c2ecf20Sopenharmony_cistatic bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
18648c2ecf20Sopenharmony_ci{
18658c2ecf20Sopenharmony_ci	uint boardrev = wlc_hw->boardrev;
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	/* 4 bits each for board type, major, minor, and tiny version */
18688c2ecf20Sopenharmony_ci	uint brt = (boardrev & 0xf000) >> 12;
18698c2ecf20Sopenharmony_ci	uint b0 = (boardrev & 0xf00) >> 8;
18708c2ecf20Sopenharmony_ci	uint b1 = (boardrev & 0xf0) >> 4;
18718c2ecf20Sopenharmony_ci	uint b2 = boardrev & 0xf;
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	/* voards from other vendors are always considered valid */
18748c2ecf20Sopenharmony_ci	if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
18758c2ecf20Sopenharmony_ci		return true;
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci	/* do some boardrev sanity checks when boardvendor is Broadcom */
18788c2ecf20Sopenharmony_ci	if (boardrev == 0)
18798c2ecf20Sopenharmony_ci		return false;
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci	if (boardrev <= 0xff)
18828c2ecf20Sopenharmony_ci		return true;
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci	if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
18858c2ecf20Sopenharmony_ci		|| (b2 > 9))
18868c2ecf20Sopenharmony_ci		return false;
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	return true;
18898c2ecf20Sopenharmony_ci}
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_cistatic void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
18928c2ecf20Sopenharmony_ci{
18938c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
18948c2ecf20Sopenharmony_ci
18958c2ecf20Sopenharmony_ci	/* If macaddr exists, use it (Sromrev4, CIS, ...). */
18968c2ecf20Sopenharmony_ci	if (!is_zero_ether_addr(sprom->il0mac)) {
18978c2ecf20Sopenharmony_ci		memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
18988c2ecf20Sopenharmony_ci		return;
18998c2ecf20Sopenharmony_ci	}
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	if (wlc_hw->_nbands > 1)
19028c2ecf20Sopenharmony_ci		memcpy(etheraddr, sprom->et1mac, ETH_ALEN);
19038c2ecf20Sopenharmony_ci	else
19048c2ecf20Sopenharmony_ci		memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
19058c2ecf20Sopenharmony_ci}
19068c2ecf20Sopenharmony_ci
19078c2ecf20Sopenharmony_ci/* power both the pll and external oscillator on/off */
19088c2ecf20Sopenharmony_cistatic void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
19098c2ecf20Sopenharmony_ci{
19108c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_ci	/*
19138c2ecf20Sopenharmony_ci	 * dont power down if plldown is false or
19148c2ecf20Sopenharmony_ci	 * we must poll hw radio disable
19158c2ecf20Sopenharmony_ci	 */
19168c2ecf20Sopenharmony_ci	if (!want && wlc_hw->pllreq)
19178c2ecf20Sopenharmony_ci		return;
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	wlc_hw->sbclk = want;
19208c2ecf20Sopenharmony_ci	if (!wlc_hw->sbclk) {
19218c2ecf20Sopenharmony_ci		wlc_hw->clk = false;
19228c2ecf20Sopenharmony_ci		if (wlc_hw->band && wlc_hw->band->pi)
19238c2ecf20Sopenharmony_ci			wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
19248c2ecf20Sopenharmony_ci	}
19258c2ecf20Sopenharmony_ci}
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci/*
19288c2ecf20Sopenharmony_ci * Return true if radio is disabled, otherwise false.
19298c2ecf20Sopenharmony_ci * hw radio disable signal is an external pin, users activate it asynchronously
19308c2ecf20Sopenharmony_ci * this function could be called when driver is down and w/o clock
19318c2ecf20Sopenharmony_ci * it operates on different registers depending on corerev and boardflag.
19328c2ecf20Sopenharmony_ci */
19338c2ecf20Sopenharmony_cistatic bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
19348c2ecf20Sopenharmony_ci{
19358c2ecf20Sopenharmony_ci	bool v, clk, xtal;
19368c2ecf20Sopenharmony_ci	u32 flags = 0;
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	xtal = wlc_hw->sbclk;
19398c2ecf20Sopenharmony_ci	if (!xtal)
19408c2ecf20Sopenharmony_ci		brcms_b_xtal(wlc_hw, ON);
19418c2ecf20Sopenharmony_ci
19428c2ecf20Sopenharmony_ci	/* may need to take core out of reset first */
19438c2ecf20Sopenharmony_ci	clk = wlc_hw->clk;
19448c2ecf20Sopenharmony_ci	if (!clk) {
19458c2ecf20Sopenharmony_ci		/*
19468c2ecf20Sopenharmony_ci		 * mac no longer enables phyclk automatically when driver
19478c2ecf20Sopenharmony_ci		 * accesses phyreg throughput mac. This can be skipped since
19488c2ecf20Sopenharmony_ci		 * only mac reg is accessed below
19498c2ecf20Sopenharmony_ci		 */
19508c2ecf20Sopenharmony_ci		if (D11REV_GE(wlc_hw->corerev, 18))
19518c2ecf20Sopenharmony_ci			flags |= SICF_PCLKE;
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci		/*
19548c2ecf20Sopenharmony_ci		 * TODO: test suspend/resume
19558c2ecf20Sopenharmony_ci		 *
19568c2ecf20Sopenharmony_ci		 * AI chip doesn't restore bar0win2 on
19578c2ecf20Sopenharmony_ci		 * hibernation/resume, need sw fixup
19588c2ecf20Sopenharmony_ci		 */
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci		bcma_core_enable(wlc_hw->d11core, flags);
19618c2ecf20Sopenharmony_ci		brcms_c_mctrl_reset(wlc_hw);
19628c2ecf20Sopenharmony_ci	}
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci	v = ((bcma_read32(wlc_hw->d11core,
19658c2ecf20Sopenharmony_ci			  D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_ci	/* put core back into reset */
19688c2ecf20Sopenharmony_ci	if (!clk)
19698c2ecf20Sopenharmony_ci		bcma_core_disable(wlc_hw->d11core, 0);
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	if (!xtal)
19728c2ecf20Sopenharmony_ci		brcms_b_xtal(wlc_hw, OFF);
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	return v;
19758c2ecf20Sopenharmony_ci}
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_cistatic bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
19788c2ecf20Sopenharmony_ci{
19798c2ecf20Sopenharmony_ci	struct dma_pub *di = wlc_hw->di[fifo];
19808c2ecf20Sopenharmony_ci	return dma_rxreset(di);
19818c2ecf20Sopenharmony_ci}
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci/* d11 core reset
19848c2ecf20Sopenharmony_ci *   ensure fask clock during reset
19858c2ecf20Sopenharmony_ci *   reset dma
19868c2ecf20Sopenharmony_ci *   reset d11(out of reset)
19878c2ecf20Sopenharmony_ci *   reset phy(out of reset)
19888c2ecf20Sopenharmony_ci *   clear software macintstatus for fresh new start
19898c2ecf20Sopenharmony_ci * one testing hack wlc_hw->noreset will bypass the d11/phy reset
19908c2ecf20Sopenharmony_ci */
19918c2ecf20Sopenharmony_civoid brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
19928c2ecf20Sopenharmony_ci{
19938c2ecf20Sopenharmony_ci	uint i;
19948c2ecf20Sopenharmony_ci	bool fastclk;
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	if (flags == BRCMS_USE_COREFLAGS)
19978c2ecf20Sopenharmony_ci		flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci	/* request FAST clock if not on  */
20028c2ecf20Sopenharmony_ci	fastclk = wlc_hw->forcefastclk;
20038c2ecf20Sopenharmony_ci	if (!fastclk)
20048c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ci	/* reset the dma engines except first time thru */
20078c2ecf20Sopenharmony_ci	if (bcma_core_is_enabled(wlc_hw->d11core)) {
20088c2ecf20Sopenharmony_ci		for (i = 0; i < NFIFO; i++)
20098c2ecf20Sopenharmony_ci			if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
20108c2ecf20Sopenharmony_ci				brcms_err(wlc_hw->d11core, "wl%d: %s: "
20118c2ecf20Sopenharmony_ci					  "dma_txreset[%d]: cannot stop dma\n",
20128c2ecf20Sopenharmony_ci					   wlc_hw->unit, __func__, i);
20138c2ecf20Sopenharmony_ci
20148c2ecf20Sopenharmony_ci		if ((wlc_hw->di[RX_FIFO])
20158c2ecf20Sopenharmony_ci		    && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
20168c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
20178c2ecf20Sopenharmony_ci				  "[%d]: cannot stop dma\n",
20188c2ecf20Sopenharmony_ci				  wlc_hw->unit, __func__, RX_FIFO);
20198c2ecf20Sopenharmony_ci	}
20208c2ecf20Sopenharmony_ci	/* if noreset, just stop the psm and return */
20218c2ecf20Sopenharmony_ci	if (wlc_hw->noreset) {
20228c2ecf20Sopenharmony_ci		wlc_hw->wlc->macintstatus = 0;	/* skip wl_dpc after down */
20238c2ecf20Sopenharmony_ci		brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
20248c2ecf20Sopenharmony_ci		return;
20258c2ecf20Sopenharmony_ci	}
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	/*
20288c2ecf20Sopenharmony_ci	 * mac no longer enables phyclk automatically when driver accesses
20298c2ecf20Sopenharmony_ci	 * phyreg throughput mac, AND phy_reset is skipped at early stage when
20308c2ecf20Sopenharmony_ci	 * band->pi is invalid. need to enable PHY CLK
20318c2ecf20Sopenharmony_ci	 */
20328c2ecf20Sopenharmony_ci	if (D11REV_GE(wlc_hw->corerev, 18))
20338c2ecf20Sopenharmony_ci		flags |= SICF_PCLKE;
20348c2ecf20Sopenharmony_ci
20358c2ecf20Sopenharmony_ci	/*
20368c2ecf20Sopenharmony_ci	 * reset the core
20378c2ecf20Sopenharmony_ci	 * In chips with PMU, the fastclk request goes through d11 core
20388c2ecf20Sopenharmony_ci	 * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
20398c2ecf20Sopenharmony_ci	 *
20408c2ecf20Sopenharmony_ci	 * This adds some delay and we can optimize it by also requesting
20418c2ecf20Sopenharmony_ci	 * fastclk through chipcommon during this period if necessary. But
20428c2ecf20Sopenharmony_ci	 * that has to work coordinate with other driver like mips/arm since
20438c2ecf20Sopenharmony_ci	 * they may touch chipcommon as well.
20448c2ecf20Sopenharmony_ci	 */
20458c2ecf20Sopenharmony_ci	wlc_hw->clk = false;
20468c2ecf20Sopenharmony_ci	bcma_core_enable(wlc_hw->d11core, flags);
20478c2ecf20Sopenharmony_ci	wlc_hw->clk = true;
20488c2ecf20Sopenharmony_ci	if (wlc_hw->band && wlc_hw->band->pi)
20498c2ecf20Sopenharmony_ci		wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_ci	brcms_c_mctrl_reset(wlc_hw);
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci	if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
20548c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	brcms_b_phy_reset(wlc_hw);
20578c2ecf20Sopenharmony_ci
20588c2ecf20Sopenharmony_ci	/* turn on PHY_PLL */
20598c2ecf20Sopenharmony_ci	brcms_b_core_phypll_ctl(wlc_hw, true);
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci	/* clear sw intstatus */
20628c2ecf20Sopenharmony_ci	wlc_hw->wlc->macintstatus = 0;
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	/* restore the clk setting */
20658c2ecf20Sopenharmony_ci	if (!fastclk)
20668c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
20678c2ecf20Sopenharmony_ci}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci/* txfifo sizes needs to be modified(increased) since the newer cores
20708c2ecf20Sopenharmony_ci * have more memory.
20718c2ecf20Sopenharmony_ci */
20728c2ecf20Sopenharmony_cistatic void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
20738c2ecf20Sopenharmony_ci{
20748c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
20758c2ecf20Sopenharmony_ci	u16 fifo_nu;
20768c2ecf20Sopenharmony_ci	u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
20778c2ecf20Sopenharmony_ci	u16 txfifo_def, txfifo_def1;
20788c2ecf20Sopenharmony_ci	u16 txfifo_cmd;
20798c2ecf20Sopenharmony_ci
20808c2ecf20Sopenharmony_ci	/* tx fifos start at TXFIFO_START_BLK from the Base address */
20818c2ecf20Sopenharmony_ci	txfifo_startblk = TXFIFO_START_BLK;
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci	/* sequence of operations:  reset fifo, set fifo size, reset fifo */
20848c2ecf20Sopenharmony_ci	for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci		txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
20878c2ecf20Sopenharmony_ci		txfifo_def = (txfifo_startblk & 0xff) |
20888c2ecf20Sopenharmony_ci		    (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
20898c2ecf20Sopenharmony_ci		txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
20908c2ecf20Sopenharmony_ci		    ((((txfifo_endblk -
20918c2ecf20Sopenharmony_ci			1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
20928c2ecf20Sopenharmony_ci		txfifo_cmd =
20938c2ecf20Sopenharmony_ci		    TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
20948c2ecf20Sopenharmony_ci
20958c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
20968c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
20978c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci		bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci		txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
21028c2ecf20Sopenharmony_ci	}
21038c2ecf20Sopenharmony_ci	/*
21048c2ecf20Sopenharmony_ci	 * need to propagate to shm location to be in sync since ucode/hw won't
21058c2ecf20Sopenharmony_ci	 * do this
21068c2ecf20Sopenharmony_ci	 */
21078c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
21088c2ecf20Sopenharmony_ci			   wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
21098c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
21108c2ecf20Sopenharmony_ci			   wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
21118c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
21128c2ecf20Sopenharmony_ci			   ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
21138c2ecf20Sopenharmony_ci			    xmtfifo_sz[TX_AC_BK_FIFO]));
21148c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
21158c2ecf20Sopenharmony_ci			   ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
21168c2ecf20Sopenharmony_ci			    xmtfifo_sz[TX_BCMC_FIFO]));
21178c2ecf20Sopenharmony_ci}
21188c2ecf20Sopenharmony_ci
21198c2ecf20Sopenharmony_ci/* This function is used for changing the tsf frac register
21208c2ecf20Sopenharmony_ci * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
21218c2ecf20Sopenharmony_ci * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
21228c2ecf20Sopenharmony_ci * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
21238c2ecf20Sopenharmony_ci * HTPHY Formula is 2^26/freq(MHz) e.g.
21248c2ecf20Sopenharmony_ci * For spuron2 - 126MHz -> 2^26/126 = 532610.0
21258c2ecf20Sopenharmony_ci *  - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
21268c2ecf20Sopenharmony_ci * For spuron: 123MHz -> 2^26/123    = 545600.5
21278c2ecf20Sopenharmony_ci *  - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
21288c2ecf20Sopenharmony_ci * For spur off: 120MHz -> 2^26/120    = 559240.5
21298c2ecf20Sopenharmony_ci *  - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
21308c2ecf20Sopenharmony_ci */
21318c2ecf20Sopenharmony_ci
21328c2ecf20Sopenharmony_civoid brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
21338c2ecf20Sopenharmony_ci{
21348c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci	if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
21378c2ecf20Sopenharmony_ci	    (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
21388c2ecf20Sopenharmony_ci		if (spurmode == WL_SPURAVOID_ON2) {	/* 126Mhz */
21398c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
21408c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
21418c2ecf20Sopenharmony_ci		} else if (spurmode == WL_SPURAVOID_ON1) {	/* 123Mhz */
21428c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
21438c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
21448c2ecf20Sopenharmony_ci		} else {	/* 120Mhz */
21458c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
21468c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
21478c2ecf20Sopenharmony_ci		}
21488c2ecf20Sopenharmony_ci	} else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
21498c2ecf20Sopenharmony_ci		if (spurmode == WL_SPURAVOID_ON1) {	/* 82Mhz */
21508c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
21518c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
21528c2ecf20Sopenharmony_ci		} else {	/* 80Mhz */
21538c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
21548c2ecf20Sopenharmony_ci			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
21558c2ecf20Sopenharmony_ci		}
21568c2ecf20Sopenharmony_ci	}
21578c2ecf20Sopenharmony_ci}
21588c2ecf20Sopenharmony_ci
21598c2ecf20Sopenharmony_civoid brcms_c_start_station(struct brcms_c_info *wlc, u8 *addr)
21608c2ecf20Sopenharmony_ci{
21618c2ecf20Sopenharmony_ci	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
21628c2ecf20Sopenharmony_ci	wlc->bsscfg->type = BRCMS_TYPE_STATION;
21638c2ecf20Sopenharmony_ci}
21648c2ecf20Sopenharmony_ci
21658c2ecf20Sopenharmony_civoid brcms_c_start_ap(struct brcms_c_info *wlc, u8 *addr, const u8 *bssid,
21668c2ecf20Sopenharmony_ci		      u8 *ssid, size_t ssid_len)
21678c2ecf20Sopenharmony_ci{
21688c2ecf20Sopenharmony_ci	brcms_c_set_ssid(wlc, ssid, ssid_len);
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
21718c2ecf20Sopenharmony_ci	memcpy(wlc->bsscfg->BSSID, bssid, sizeof(wlc->bsscfg->BSSID));
21728c2ecf20Sopenharmony_ci	wlc->bsscfg->type = BRCMS_TYPE_AP;
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, MCTL_AP | MCTL_INFRA);
21758c2ecf20Sopenharmony_ci}
21768c2ecf20Sopenharmony_ci
21778c2ecf20Sopenharmony_civoid brcms_c_start_adhoc(struct brcms_c_info *wlc, u8 *addr)
21788c2ecf20Sopenharmony_ci{
21798c2ecf20Sopenharmony_ci	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
21808c2ecf20Sopenharmony_ci	wlc->bsscfg->type = BRCMS_TYPE_ADHOC;
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, 0);
21838c2ecf20Sopenharmony_ci}
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci/* Initialize GPIOs that are controlled by D11 core */
21868c2ecf20Sopenharmony_cistatic void brcms_c_gpio_init(struct brcms_c_info *wlc)
21878c2ecf20Sopenharmony_ci{
21888c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
21898c2ecf20Sopenharmony_ci	u32 gc, gm;
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	/* use GPIO select 0 to get all gpio signals from the gpio out reg */
21928c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci	/*
21958c2ecf20Sopenharmony_ci	 * Common GPIO setup:
21968c2ecf20Sopenharmony_ci	 *      G0 = LED 0 = WLAN Activity
21978c2ecf20Sopenharmony_ci	 *      G1 = LED 1 = WLAN 2.4 GHz Radio State
21988c2ecf20Sopenharmony_ci	 *      G2 = LED 2 = WLAN 5 GHz Radio State
21998c2ecf20Sopenharmony_ci	 *      G4 = radio disable input (HI enabled, LO disabled)
22008c2ecf20Sopenharmony_ci	 */
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci	gc = gm = 0;
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	/* Allocate GPIOs for mimo antenna diversity feature */
22058c2ecf20Sopenharmony_ci	if (wlc_hw->antsel_type == ANTSEL_2x3) {
22068c2ecf20Sopenharmony_ci		/* Enable antenna diversity, use 2x3 mode */
22078c2ecf20Sopenharmony_ci		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
22088c2ecf20Sopenharmony_ci			     MHF3_ANTSEL_EN, BRCM_BAND_ALL);
22098c2ecf20Sopenharmony_ci		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
22108c2ecf20Sopenharmony_ci			     MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci		/* init superswitch control */
22138c2ecf20Sopenharmony_ci		wlc_phy_antsel_init(wlc_hw->band->pi, false);
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_ci	} else if (wlc_hw->antsel_type == ANTSEL_2x4) {
22168c2ecf20Sopenharmony_ci		gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
22178c2ecf20Sopenharmony_ci		/*
22188c2ecf20Sopenharmony_ci		 * The board itself is powered by these GPIOs
22198c2ecf20Sopenharmony_ci		 * (when not sending pattern) so set them high
22208c2ecf20Sopenharmony_ci		 */
22218c2ecf20Sopenharmony_ci		bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
22228c2ecf20Sopenharmony_ci			   (BOARD_GPIO_12 | BOARD_GPIO_13));
22238c2ecf20Sopenharmony_ci		bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
22248c2ecf20Sopenharmony_ci			   (BOARD_GPIO_12 | BOARD_GPIO_13));
22258c2ecf20Sopenharmony_ci
22268c2ecf20Sopenharmony_ci		/* Enable antenna diversity, use 2x4 mode */
22278c2ecf20Sopenharmony_ci		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
22288c2ecf20Sopenharmony_ci			     MHF3_ANTSEL_EN, BRCM_BAND_ALL);
22298c2ecf20Sopenharmony_ci		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
22308c2ecf20Sopenharmony_ci			     BRCM_BAND_ALL);
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci		/* Configure the desired clock to be 4Mhz */
22338c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
22348c2ecf20Sopenharmony_ci				   ANTSEL_CLKDIV_4MHZ);
22358c2ecf20Sopenharmony_ci	}
22368c2ecf20Sopenharmony_ci
22378c2ecf20Sopenharmony_ci	/*
22388c2ecf20Sopenharmony_ci	 * gpio 9 controls the PA. ucode is responsible
22398c2ecf20Sopenharmony_ci	 * for wiggling out and oe
22408c2ecf20Sopenharmony_ci	 */
22418c2ecf20Sopenharmony_ci	if (wlc_hw->boardflags & BFL_PACTRL)
22428c2ecf20Sopenharmony_ci		gm |= gc |= BOARD_GPIO_PACTRL;
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	/* apply to gpiocontrol register */
22458c2ecf20Sopenharmony_ci	bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
22468c2ecf20Sopenharmony_ci}
22478c2ecf20Sopenharmony_ci
22488c2ecf20Sopenharmony_cistatic void brcms_ucode_write(struct brcms_hardware *wlc_hw,
22498c2ecf20Sopenharmony_ci			      const __le32 ucode[], const size_t nbytes)
22508c2ecf20Sopenharmony_ci{
22518c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
22528c2ecf20Sopenharmony_ci	uint i;
22538c2ecf20Sopenharmony_ci	uint count;
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	count = (nbytes / sizeof(u32));
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr),
22608c2ecf20Sopenharmony_ci		     OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
22618c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
22628c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++)
22638c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci}
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_cistatic void brcms_ucode_download(struct brcms_hardware *wlc_hw)
22688c2ecf20Sopenharmony_ci{
22698c2ecf20Sopenharmony_ci	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci	if (wlc_hw->ucode_loaded)
22728c2ecf20Sopenharmony_ci		return;
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
22758c2ecf20Sopenharmony_ci		if (BRCMS_ISNPHY(wlc_hw->band)) {
22768c2ecf20Sopenharmony_ci			brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
22778c2ecf20Sopenharmony_ci					  ucode->bcm43xx_16_mimosz);
22788c2ecf20Sopenharmony_ci			wlc_hw->ucode_loaded = true;
22798c2ecf20Sopenharmony_ci		} else
22808c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core,
22818c2ecf20Sopenharmony_ci				  "%s: wl%d: unsupported phy in corerev %d\n",
22828c2ecf20Sopenharmony_ci				  __func__, wlc_hw->unit, wlc_hw->corerev);
22838c2ecf20Sopenharmony_ci	} else if (D11REV_IS(wlc_hw->corerev, 24)) {
22848c2ecf20Sopenharmony_ci		if (BRCMS_ISLCNPHY(wlc_hw->band)) {
22858c2ecf20Sopenharmony_ci			brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
22868c2ecf20Sopenharmony_ci					  ucode->bcm43xx_24_lcnsz);
22878c2ecf20Sopenharmony_ci			wlc_hw->ucode_loaded = true;
22888c2ecf20Sopenharmony_ci		} else {
22898c2ecf20Sopenharmony_ci			brcms_err(wlc_hw->d11core,
22908c2ecf20Sopenharmony_ci				  "%s: wl%d: unsupported phy in corerev %d\n",
22918c2ecf20Sopenharmony_ci				  __func__, wlc_hw->unit, wlc_hw->corerev);
22928c2ecf20Sopenharmony_ci		}
22938c2ecf20Sopenharmony_ci	}
22948c2ecf20Sopenharmony_ci}
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_civoid brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
22978c2ecf20Sopenharmony_ci{
22988c2ecf20Sopenharmony_ci	/* update sw state */
22998c2ecf20Sopenharmony_ci	wlc_hw->bmac_phytxant = phytxant;
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	/* push to ucode if up */
23028c2ecf20Sopenharmony_ci	if (!wlc_hw->up)
23038c2ecf20Sopenharmony_ci		return;
23048c2ecf20Sopenharmony_ci	brcms_c_ucode_txant_set(wlc_hw);
23058c2ecf20Sopenharmony_ci
23068c2ecf20Sopenharmony_ci}
23078c2ecf20Sopenharmony_ci
23088c2ecf20Sopenharmony_ciu16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
23098c2ecf20Sopenharmony_ci{
23108c2ecf20Sopenharmony_ci	return (u16) wlc_hw->wlc->stf->txant;
23118c2ecf20Sopenharmony_ci}
23128c2ecf20Sopenharmony_ci
23138c2ecf20Sopenharmony_civoid brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
23148c2ecf20Sopenharmony_ci{
23158c2ecf20Sopenharmony_ci	wlc_hw->antsel_type = antsel_type;
23168c2ecf20Sopenharmony_ci
23178c2ecf20Sopenharmony_ci	/* Update the antsel type for phy module to use */
23188c2ecf20Sopenharmony_ci	wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
23198c2ecf20Sopenharmony_ci}
23208c2ecf20Sopenharmony_ci
23218c2ecf20Sopenharmony_cistatic void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
23228c2ecf20Sopenharmony_ci{
23238c2ecf20Sopenharmony_ci	bool fatal = false;
23248c2ecf20Sopenharmony_ci	uint unit;
23258c2ecf20Sopenharmony_ci	uint intstatus, idx;
23268c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci	unit = wlc_hw->unit;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	for (idx = 0; idx < NFIFO; idx++) {
23318c2ecf20Sopenharmony_ci		/* read intstatus register and ignore any non-error bits */
23328c2ecf20Sopenharmony_ci		intstatus =
23338c2ecf20Sopenharmony_ci			bcma_read32(core,
23348c2ecf20Sopenharmony_ci				    D11REGOFFS(intctrlregs[idx].intstatus)) &
23358c2ecf20Sopenharmony_ci			I_ERRORS;
23368c2ecf20Sopenharmony_ci		if (!intstatus)
23378c2ecf20Sopenharmony_ci			continue;
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_ci		brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
23408c2ecf20Sopenharmony_ci			      unit, idx, intstatus);
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci		if (intstatus & I_RO) {
23438c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: receive fifo "
23448c2ecf20Sopenharmony_ci				  "overflow\n", unit, idx);
23458c2ecf20Sopenharmony_ci			fatal = true;
23468c2ecf20Sopenharmony_ci		}
23478c2ecf20Sopenharmony_ci
23488c2ecf20Sopenharmony_ci		if (intstatus & I_PC) {
23498c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: descriptor error\n",
23508c2ecf20Sopenharmony_ci				  unit, idx);
23518c2ecf20Sopenharmony_ci			fatal = true;
23528c2ecf20Sopenharmony_ci		}
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_ci		if (intstatus & I_PD) {
23558c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: data error\n", unit,
23568c2ecf20Sopenharmony_ci				  idx);
23578c2ecf20Sopenharmony_ci			fatal = true;
23588c2ecf20Sopenharmony_ci		}
23598c2ecf20Sopenharmony_ci
23608c2ecf20Sopenharmony_ci		if (intstatus & I_DE) {
23618c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: descriptor protocol "
23628c2ecf20Sopenharmony_ci				  "error\n", unit, idx);
23638c2ecf20Sopenharmony_ci			fatal = true;
23648c2ecf20Sopenharmony_ci		}
23658c2ecf20Sopenharmony_ci
23668c2ecf20Sopenharmony_ci		if (intstatus & I_RU)
23678c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: receive descriptor "
23688c2ecf20Sopenharmony_ci				  "underflow\n", idx, unit);
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci		if (intstatus & I_XU) {
23718c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: fifo %d: transmit fifo "
23728c2ecf20Sopenharmony_ci				  "underflow\n", idx, unit);
23738c2ecf20Sopenharmony_ci			fatal = true;
23748c2ecf20Sopenharmony_ci		}
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci		if (fatal) {
23778c2ecf20Sopenharmony_ci			brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
23788c2ecf20Sopenharmony_ci			break;
23798c2ecf20Sopenharmony_ci		} else
23808c2ecf20Sopenharmony_ci			bcma_write32(core,
23818c2ecf20Sopenharmony_ci				     D11REGOFFS(intctrlregs[idx].intstatus),
23828c2ecf20Sopenharmony_ci				     intstatus);
23838c2ecf20Sopenharmony_ci	}
23848c2ecf20Sopenharmony_ci}
23858c2ecf20Sopenharmony_ci
23868c2ecf20Sopenharmony_civoid brcms_c_intrson(struct brcms_c_info *wlc)
23878c2ecf20Sopenharmony_ci{
23888c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
23898c2ecf20Sopenharmony_ci	wlc->macintmask = wlc->defmacintmask;
23908c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
23918c2ecf20Sopenharmony_ci}
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ciu32 brcms_c_intrsoff(struct brcms_c_info *wlc)
23948c2ecf20Sopenharmony_ci{
23958c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
23968c2ecf20Sopenharmony_ci	u32 macintmask;
23978c2ecf20Sopenharmony_ci
23988c2ecf20Sopenharmony_ci	if (!wlc_hw->clk)
23998c2ecf20Sopenharmony_ci		return 0;
24008c2ecf20Sopenharmony_ci
24018c2ecf20Sopenharmony_ci	macintmask = wlc->macintmask;	/* isr can still happen */
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
24048c2ecf20Sopenharmony_ci	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
24058c2ecf20Sopenharmony_ci	udelay(1);		/* ensure int line is no longer driven */
24068c2ecf20Sopenharmony_ci	wlc->macintmask = 0;
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci	/* return previous macintmask; resolve race between us and our isr */
24098c2ecf20Sopenharmony_ci	return wlc->macintstatus ? 0 : macintmask;
24108c2ecf20Sopenharmony_ci}
24118c2ecf20Sopenharmony_ci
24128c2ecf20Sopenharmony_civoid brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
24138c2ecf20Sopenharmony_ci{
24148c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
24158c2ecf20Sopenharmony_ci	if (!wlc_hw->clk)
24168c2ecf20Sopenharmony_ci		return;
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci	wlc->macintmask = macintmask;
24198c2ecf20Sopenharmony_ci	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
24208c2ecf20Sopenharmony_ci}
24218c2ecf20Sopenharmony_ci
24228c2ecf20Sopenharmony_ci/* assumes that the d11 MAC is enabled */
24238c2ecf20Sopenharmony_cistatic void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
24248c2ecf20Sopenharmony_ci				    uint tx_fifo)
24258c2ecf20Sopenharmony_ci{
24268c2ecf20Sopenharmony_ci	u8 fifo = 1 << tx_fifo;
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci	/* Two clients of this code, 11h Quiet period and scanning. */
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci	/* only suspend if not already suspended */
24318c2ecf20Sopenharmony_ci	if ((wlc_hw->suspended_fifos & fifo) == fifo)
24328c2ecf20Sopenharmony_ci		return;
24338c2ecf20Sopenharmony_ci
24348c2ecf20Sopenharmony_ci	/* force the core awake only if not already */
24358c2ecf20Sopenharmony_ci	if (wlc_hw->suspended_fifos == 0)
24368c2ecf20Sopenharmony_ci		brcms_c_ucode_wake_override_set(wlc_hw,
24378c2ecf20Sopenharmony_ci						BRCMS_WAKE_OVERRIDE_TXFIFO);
24388c2ecf20Sopenharmony_ci
24398c2ecf20Sopenharmony_ci	wlc_hw->suspended_fifos |= fifo;
24408c2ecf20Sopenharmony_ci
24418c2ecf20Sopenharmony_ci	if (wlc_hw->di[tx_fifo]) {
24428c2ecf20Sopenharmony_ci		/*
24438c2ecf20Sopenharmony_ci		 * Suspending AMPDU transmissions in the middle can cause
24448c2ecf20Sopenharmony_ci		 * underflow which may result in mismatch between ucode and
24458c2ecf20Sopenharmony_ci		 * driver so suspend the mac before suspending the FIFO
24468c2ecf20Sopenharmony_ci		 */
24478c2ecf20Sopenharmony_ci		if (BRCMS_PHY_11N_CAP(wlc_hw->band))
24488c2ecf20Sopenharmony_ci			brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
24498c2ecf20Sopenharmony_ci
24508c2ecf20Sopenharmony_ci		dma_txsuspend(wlc_hw->di[tx_fifo]);
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci		if (BRCMS_PHY_11N_CAP(wlc_hw->band))
24538c2ecf20Sopenharmony_ci			brcms_c_enable_mac(wlc_hw->wlc);
24548c2ecf20Sopenharmony_ci	}
24558c2ecf20Sopenharmony_ci}
24568c2ecf20Sopenharmony_ci
24578c2ecf20Sopenharmony_cistatic void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
24588c2ecf20Sopenharmony_ci				   uint tx_fifo)
24598c2ecf20Sopenharmony_ci{
24608c2ecf20Sopenharmony_ci	/* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
24618c2ecf20Sopenharmony_ci	 * but need to be done here for PIO otherwise the watchdog will catch
24628c2ecf20Sopenharmony_ci	 * the inconsistency and fire
24638c2ecf20Sopenharmony_ci	 */
24648c2ecf20Sopenharmony_ci	/* Two clients of this code, 11h Quiet period and scanning. */
24658c2ecf20Sopenharmony_ci	if (wlc_hw->di[tx_fifo])
24668c2ecf20Sopenharmony_ci		dma_txresume(wlc_hw->di[tx_fifo]);
24678c2ecf20Sopenharmony_ci
24688c2ecf20Sopenharmony_ci	/* allow core to sleep again */
24698c2ecf20Sopenharmony_ci	if (wlc_hw->suspended_fifos == 0)
24708c2ecf20Sopenharmony_ci		return;
24718c2ecf20Sopenharmony_ci	else {
24728c2ecf20Sopenharmony_ci		wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
24738c2ecf20Sopenharmony_ci		if (wlc_hw->suspended_fifos == 0)
24748c2ecf20Sopenharmony_ci			brcms_c_ucode_wake_override_clear(wlc_hw,
24758c2ecf20Sopenharmony_ci						BRCMS_WAKE_OVERRIDE_TXFIFO);
24768c2ecf20Sopenharmony_ci	}
24778c2ecf20Sopenharmony_ci}
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci/* precondition: requires the mac core to be enabled */
24808c2ecf20Sopenharmony_cistatic void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
24818c2ecf20Sopenharmony_ci{
24828c2ecf20Sopenharmony_ci	static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
24838c2ecf20Sopenharmony_ci	u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_ci	if (mute_tx) {
24868c2ecf20Sopenharmony_ci		/* suspend tx fifos */
24878c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
24888c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
24898c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
24908c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
24918c2ecf20Sopenharmony_ci
24928c2ecf20Sopenharmony_ci		/* zero the address match register so we do not send ACKs */
24938c2ecf20Sopenharmony_ci		brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
24948c2ecf20Sopenharmony_ci	} else {
24958c2ecf20Sopenharmony_ci		/* resume tx fifos */
24968c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
24978c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
24988c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
24998c2ecf20Sopenharmony_ci		brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_ci		/* Restore address */
25028c2ecf20Sopenharmony_ci		brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
25038c2ecf20Sopenharmony_ci	}
25048c2ecf20Sopenharmony_ci
25058c2ecf20Sopenharmony_ci	wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
25068c2ecf20Sopenharmony_ci
25078c2ecf20Sopenharmony_ci	if (mute_tx)
25088c2ecf20Sopenharmony_ci		brcms_c_ucode_mute_override_set(wlc_hw);
25098c2ecf20Sopenharmony_ci	else
25108c2ecf20Sopenharmony_ci		brcms_c_ucode_mute_override_clear(wlc_hw);
25118c2ecf20Sopenharmony_ci}
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_civoid
25148c2ecf20Sopenharmony_cibrcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
25158c2ecf20Sopenharmony_ci{
25168c2ecf20Sopenharmony_ci	brcms_b_mute(wlc->hw, mute_tx);
25178c2ecf20Sopenharmony_ci}
25188c2ecf20Sopenharmony_ci
25198c2ecf20Sopenharmony_ci/*
25208c2ecf20Sopenharmony_ci * Read and clear macintmask and macintstatus and intstatus registers.
25218c2ecf20Sopenharmony_ci * This routine should be called with interrupts off
25228c2ecf20Sopenharmony_ci * Return:
25238c2ecf20Sopenharmony_ci *   -1 if brcms_deviceremoved(wlc) evaluates to true;
25248c2ecf20Sopenharmony_ci *   0 if the interrupt is not for us, or we are in some special cases;
25258c2ecf20Sopenharmony_ci *   device interrupt status bits otherwise.
25268c2ecf20Sopenharmony_ci */
25278c2ecf20Sopenharmony_cistatic inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
25288c2ecf20Sopenharmony_ci{
25298c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
25308c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
25318c2ecf20Sopenharmony_ci	u32 macintstatus, mask;
25328c2ecf20Sopenharmony_ci
25338c2ecf20Sopenharmony_ci	/* macintstatus includes a DMA interrupt summary bit */
25348c2ecf20Sopenharmony_ci	macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
25358c2ecf20Sopenharmony_ci	mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
25368c2ecf20Sopenharmony_ci
25378c2ecf20Sopenharmony_ci	trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci	/* detect cardbus removed, in power down(suspend) and in reset */
25408c2ecf20Sopenharmony_ci	if (brcms_deviceremoved(wlc))
25418c2ecf20Sopenharmony_ci		return -1;
25428c2ecf20Sopenharmony_ci
25438c2ecf20Sopenharmony_ci	/* brcms_deviceremoved() succeeds even when the core is still resetting,
25448c2ecf20Sopenharmony_ci	 * handle that case here.
25458c2ecf20Sopenharmony_ci	 */
25468c2ecf20Sopenharmony_ci	if (macintstatus == 0xffffffff)
25478c2ecf20Sopenharmony_ci		return 0;
25488c2ecf20Sopenharmony_ci
25498c2ecf20Sopenharmony_ci	/* defer unsolicited interrupts */
25508c2ecf20Sopenharmony_ci	macintstatus &= mask;
25518c2ecf20Sopenharmony_ci
25528c2ecf20Sopenharmony_ci	/* if not for us */
25538c2ecf20Sopenharmony_ci	if (macintstatus == 0)
25548c2ecf20Sopenharmony_ci		return 0;
25558c2ecf20Sopenharmony_ci
25568c2ecf20Sopenharmony_ci	/* turn off the interrupts */
25578c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(macintmask), 0);
25588c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(macintmask));
25598c2ecf20Sopenharmony_ci	wlc->macintmask = 0;
25608c2ecf20Sopenharmony_ci
25618c2ecf20Sopenharmony_ci	/* clear device interrupts */
25628c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
25638c2ecf20Sopenharmony_ci
25648c2ecf20Sopenharmony_ci	/* MI_DMAINT is indication of non-zero intstatus */
25658c2ecf20Sopenharmony_ci	if (macintstatus & MI_DMAINT)
25668c2ecf20Sopenharmony_ci		/*
25678c2ecf20Sopenharmony_ci		 * only fifo interrupt enabled is I_RI in
25688c2ecf20Sopenharmony_ci		 * RX_FIFO. If MI_DMAINT is set, assume it
25698c2ecf20Sopenharmony_ci		 * is set and clear the interrupt.
25708c2ecf20Sopenharmony_ci		 */
25718c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
25728c2ecf20Sopenharmony_ci			     DEF_RXINTMASK);
25738c2ecf20Sopenharmony_ci
25748c2ecf20Sopenharmony_ci	return macintstatus;
25758c2ecf20Sopenharmony_ci}
25768c2ecf20Sopenharmony_ci
25778c2ecf20Sopenharmony_ci/* Update wlc->macintstatus and wlc->intstatus[]. */
25788c2ecf20Sopenharmony_ci/* Return true if they are updated successfully. false otherwise */
25798c2ecf20Sopenharmony_cibool brcms_c_intrsupd(struct brcms_c_info *wlc)
25808c2ecf20Sopenharmony_ci{
25818c2ecf20Sopenharmony_ci	u32 macintstatus;
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_ci	/* read and clear macintstatus and intstatus registers */
25848c2ecf20Sopenharmony_ci	macintstatus = wlc_intstatus(wlc, false);
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci	/* device is removed */
25878c2ecf20Sopenharmony_ci	if (macintstatus == 0xffffffff)
25888c2ecf20Sopenharmony_ci		return false;
25898c2ecf20Sopenharmony_ci
25908c2ecf20Sopenharmony_ci	/* update interrupt status in software */
25918c2ecf20Sopenharmony_ci	wlc->macintstatus |= macintstatus;
25928c2ecf20Sopenharmony_ci
25938c2ecf20Sopenharmony_ci	return true;
25948c2ecf20Sopenharmony_ci}
25958c2ecf20Sopenharmony_ci
25968c2ecf20Sopenharmony_ci/*
25978c2ecf20Sopenharmony_ci * First-level interrupt processing.
25988c2ecf20Sopenharmony_ci * Return true if this was our interrupt
25998c2ecf20Sopenharmony_ci * and if further brcms_c_dpc() processing is required,
26008c2ecf20Sopenharmony_ci * false otherwise.
26018c2ecf20Sopenharmony_ci */
26028c2ecf20Sopenharmony_cibool brcms_c_isr(struct brcms_c_info *wlc)
26038c2ecf20Sopenharmony_ci{
26048c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
26058c2ecf20Sopenharmony_ci	u32 macintstatus;
26068c2ecf20Sopenharmony_ci
26078c2ecf20Sopenharmony_ci	if (!wlc_hw->up || !wlc->macintmask)
26088c2ecf20Sopenharmony_ci		return false;
26098c2ecf20Sopenharmony_ci
26108c2ecf20Sopenharmony_ci	/* read and clear macintstatus and intstatus registers */
26118c2ecf20Sopenharmony_ci	macintstatus = wlc_intstatus(wlc, true);
26128c2ecf20Sopenharmony_ci
26138c2ecf20Sopenharmony_ci	if (macintstatus == 0xffffffff) {
26148c2ecf20Sopenharmony_ci		brcms_err(wlc_hw->d11core,
26158c2ecf20Sopenharmony_ci			  "DEVICEREMOVED detected in the ISR code path\n");
26168c2ecf20Sopenharmony_ci		return false;
26178c2ecf20Sopenharmony_ci	}
26188c2ecf20Sopenharmony_ci
26198c2ecf20Sopenharmony_ci	/* it is not for us */
26208c2ecf20Sopenharmony_ci	if (macintstatus == 0)
26218c2ecf20Sopenharmony_ci		return false;
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci	/* save interrupt status bits */
26248c2ecf20Sopenharmony_ci	wlc->macintstatus = macintstatus;
26258c2ecf20Sopenharmony_ci
26268c2ecf20Sopenharmony_ci	return true;
26278c2ecf20Sopenharmony_ci
26288c2ecf20Sopenharmony_ci}
26298c2ecf20Sopenharmony_ci
26308c2ecf20Sopenharmony_civoid brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
26318c2ecf20Sopenharmony_ci{
26328c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
26338c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
26348c2ecf20Sopenharmony_ci	u32 mc, mi;
26358c2ecf20Sopenharmony_ci
26368c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
26378c2ecf20Sopenharmony_ci			   wlc_hw->band->bandunit);
26388c2ecf20Sopenharmony_ci
26398c2ecf20Sopenharmony_ci	/*
26408c2ecf20Sopenharmony_ci	 * Track overlapping suspend requests
26418c2ecf20Sopenharmony_ci	 */
26428c2ecf20Sopenharmony_ci	wlc_hw->mac_suspend_depth++;
26438c2ecf20Sopenharmony_ci	if (wlc_hw->mac_suspend_depth > 1)
26448c2ecf20Sopenharmony_ci		return;
26458c2ecf20Sopenharmony_ci
26468c2ecf20Sopenharmony_ci	/* force the core awake */
26478c2ecf20Sopenharmony_ci	brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
26488c2ecf20Sopenharmony_ci
26498c2ecf20Sopenharmony_ci	mc = bcma_read32(core, D11REGOFFS(maccontrol));
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_ci	if (mc == 0xffffffff) {
26528c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
26538c2ecf20Sopenharmony_ci			  __func__);
26548c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
26558c2ecf20Sopenharmony_ci		return;
26568c2ecf20Sopenharmony_ci	}
26578c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_PSM_JMP_0);
26588c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_PSM_RUN));
26598c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_EN_MAC));
26608c2ecf20Sopenharmony_ci
26618c2ecf20Sopenharmony_ci	mi = bcma_read32(core, D11REGOFFS(macintstatus));
26628c2ecf20Sopenharmony_ci	if (mi == 0xffffffff) {
26638c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
26648c2ecf20Sopenharmony_ci			  __func__);
26658c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
26668c2ecf20Sopenharmony_ci		return;
26678c2ecf20Sopenharmony_ci	}
26688c2ecf20Sopenharmony_ci	WARN_ON(mi & MI_MACSSPNDD);
26698c2ecf20Sopenharmony_ci
26708c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
26738c2ecf20Sopenharmony_ci		 BRCMS_MAX_MAC_SUSPEND);
26748c2ecf20Sopenharmony_ci
26758c2ecf20Sopenharmony_ci	if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
26768c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
26778c2ecf20Sopenharmony_ci			  " and MI_MACSSPNDD is still not on.\n",
26788c2ecf20Sopenharmony_ci			  wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
26798c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
26808c2ecf20Sopenharmony_ci			  "psm_brc 0x%04x\n", wlc_hw->unit,
26818c2ecf20Sopenharmony_ci			  bcma_read32(core, D11REGOFFS(psmdebug)),
26828c2ecf20Sopenharmony_ci			  bcma_read32(core, D11REGOFFS(phydebug)),
26838c2ecf20Sopenharmony_ci			  bcma_read16(core, D11REGOFFS(psm_brc)));
26848c2ecf20Sopenharmony_ci	}
26858c2ecf20Sopenharmony_ci
26868c2ecf20Sopenharmony_ci	mc = bcma_read32(core, D11REGOFFS(maccontrol));
26878c2ecf20Sopenharmony_ci	if (mc == 0xffffffff) {
26888c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
26898c2ecf20Sopenharmony_ci			  __func__);
26908c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
26918c2ecf20Sopenharmony_ci		return;
26928c2ecf20Sopenharmony_ci	}
26938c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_PSM_JMP_0);
26948c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_PSM_RUN));
26958c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_EN_MAC);
26968c2ecf20Sopenharmony_ci}
26978c2ecf20Sopenharmony_ci
26988c2ecf20Sopenharmony_civoid brcms_c_enable_mac(struct brcms_c_info *wlc)
26998c2ecf20Sopenharmony_ci{
27008c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
27018c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
27028c2ecf20Sopenharmony_ci	u32 mc, mi;
27038c2ecf20Sopenharmony_ci
27048c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
27058c2ecf20Sopenharmony_ci			   wlc->band->bandunit);
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_ci	/*
27088c2ecf20Sopenharmony_ci	 * Track overlapping suspend requests
27098c2ecf20Sopenharmony_ci	 */
27108c2ecf20Sopenharmony_ci	wlc_hw->mac_suspend_depth--;
27118c2ecf20Sopenharmony_ci	if (wlc_hw->mac_suspend_depth > 0)
27128c2ecf20Sopenharmony_ci		return;
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci	mc = bcma_read32(core, D11REGOFFS(maccontrol));
27158c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_PSM_JMP_0);
27168c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_EN_MAC);
27178c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_PSM_RUN));
27188c2ecf20Sopenharmony_ci
27198c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
27208c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
27218c2ecf20Sopenharmony_ci
27228c2ecf20Sopenharmony_ci	mc = bcma_read32(core, D11REGOFFS(maccontrol));
27238c2ecf20Sopenharmony_ci	WARN_ON(mc & MCTL_PSM_JMP_0);
27248c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_EN_MAC));
27258c2ecf20Sopenharmony_ci	WARN_ON(!(mc & MCTL_PSM_RUN));
27268c2ecf20Sopenharmony_ci
27278c2ecf20Sopenharmony_ci	mi = bcma_read32(core, D11REGOFFS(macintstatus));
27288c2ecf20Sopenharmony_ci	WARN_ON(mi & MI_MACSSPNDD);
27298c2ecf20Sopenharmony_ci
27308c2ecf20Sopenharmony_ci	brcms_c_ucode_wake_override_clear(wlc_hw,
27318c2ecf20Sopenharmony_ci					  BRCMS_WAKE_OVERRIDE_MACSUSPEND);
27328c2ecf20Sopenharmony_ci}
27338c2ecf20Sopenharmony_ci
27348c2ecf20Sopenharmony_civoid brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
27358c2ecf20Sopenharmony_ci{
27368c2ecf20Sopenharmony_ci	wlc_hw->hw_stf_ss_opmode = stf_mode;
27378c2ecf20Sopenharmony_ci
27388c2ecf20Sopenharmony_ci	if (wlc_hw->clk)
27398c2ecf20Sopenharmony_ci		brcms_upd_ofdm_pctl1_table(wlc_hw);
27408c2ecf20Sopenharmony_ci}
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_cistatic bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
27438c2ecf20Sopenharmony_ci{
27448c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
27458c2ecf20Sopenharmony_ci	u32 w, val;
27468c2ecf20Sopenharmony_ci	struct wiphy *wiphy = wlc_hw->wlc->wiphy;
27478c2ecf20Sopenharmony_ci
27488c2ecf20Sopenharmony_ci	/* Validate dchip register access */
27498c2ecf20Sopenharmony_ci
27508c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27518c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27528c2ecf20Sopenharmony_ci	w = bcma_read32(core, D11REGOFFS(objdata));
27538c2ecf20Sopenharmony_ci
27548c2ecf20Sopenharmony_ci	/* Can we write and read back a 32bit register? */
27558c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27568c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27578c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
27588c2ecf20Sopenharmony_ci
27598c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27608c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27618c2ecf20Sopenharmony_ci	val = bcma_read32(core, D11REGOFFS(objdata));
27628c2ecf20Sopenharmony_ci	if (val != (u32) 0xaa5555aa) {
27638c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
27648c2ecf20Sopenharmony_ci			  "expected 0xaa5555aa\n", wlc_hw->unit, val);
27658c2ecf20Sopenharmony_ci		return false;
27668c2ecf20Sopenharmony_ci	}
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27698c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27708c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
27718c2ecf20Sopenharmony_ci
27728c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27738c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27748c2ecf20Sopenharmony_ci	val = bcma_read32(core, D11REGOFFS(objdata));
27758c2ecf20Sopenharmony_ci	if (val != (u32) 0x55aaaa55) {
27768c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
27778c2ecf20Sopenharmony_ci			  "expected 0x55aaaa55\n", wlc_hw->unit, val);
27788c2ecf20Sopenharmony_ci		return false;
27798c2ecf20Sopenharmony_ci	}
27808c2ecf20Sopenharmony_ci
27818c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
27828c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
27838c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objdata), w);
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci	/* clear CFPStart */
27868c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci	w = bcma_read32(core, D11REGOFFS(maccontrol));
27898c2ecf20Sopenharmony_ci	if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
27908c2ecf20Sopenharmony_ci	    (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
27918c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
27928c2ecf20Sopenharmony_ci			  "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
27938c2ecf20Sopenharmony_ci			  (MCTL_IHR_EN | MCTL_WAKE),
27948c2ecf20Sopenharmony_ci			  (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
27958c2ecf20Sopenharmony_ci		return false;
27968c2ecf20Sopenharmony_ci	}
27978c2ecf20Sopenharmony_ci
27988c2ecf20Sopenharmony_ci	return true;
27998c2ecf20Sopenharmony_ci}
28008c2ecf20Sopenharmony_ci
28018c2ecf20Sopenharmony_ci#define PHYPLL_WAIT_US	100000
28028c2ecf20Sopenharmony_ci
28038c2ecf20Sopenharmony_civoid brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
28048c2ecf20Sopenharmony_ci{
28058c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
28068c2ecf20Sopenharmony_ci	u32 tmp;
28078c2ecf20Sopenharmony_ci
28088c2ecf20Sopenharmony_ci	brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
28098c2ecf20Sopenharmony_ci
28108c2ecf20Sopenharmony_ci	tmp = 0;
28118c2ecf20Sopenharmony_ci
28128c2ecf20Sopenharmony_ci	if (on) {
28138c2ecf20Sopenharmony_ci		if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
28148c2ecf20Sopenharmony_ci			bcma_set32(core, D11REGOFFS(clk_ctl_st),
28158c2ecf20Sopenharmony_ci				   CCS_ERSRC_REQ_HT |
28168c2ecf20Sopenharmony_ci				   CCS_ERSRC_REQ_D11PLL |
28178c2ecf20Sopenharmony_ci				   CCS_ERSRC_REQ_PHYPLL);
28188c2ecf20Sopenharmony_ci			SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
28198c2ecf20Sopenharmony_ci				  CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
28208c2ecf20Sopenharmony_ci				 PHYPLL_WAIT_US);
28218c2ecf20Sopenharmony_ci
28228c2ecf20Sopenharmony_ci			tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
28238c2ecf20Sopenharmony_ci			if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
28248c2ecf20Sopenharmony_ci				brcms_err(core, "%s: turn on PHY PLL failed\n",
28258c2ecf20Sopenharmony_ci					  __func__);
28268c2ecf20Sopenharmony_ci		} else {
28278c2ecf20Sopenharmony_ci			bcma_set32(core, D11REGOFFS(clk_ctl_st),
28288c2ecf20Sopenharmony_ci				   tmp | CCS_ERSRC_REQ_D11PLL |
28298c2ecf20Sopenharmony_ci				   CCS_ERSRC_REQ_PHYPLL);
28308c2ecf20Sopenharmony_ci			SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
28318c2ecf20Sopenharmony_ci				  (CCS_ERSRC_AVAIL_D11PLL |
28328c2ecf20Sopenharmony_ci				   CCS_ERSRC_AVAIL_PHYPLL)) !=
28338c2ecf20Sopenharmony_ci				 (CCS_ERSRC_AVAIL_D11PLL |
28348c2ecf20Sopenharmony_ci				  CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
28358c2ecf20Sopenharmony_ci
28368c2ecf20Sopenharmony_ci			tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
28378c2ecf20Sopenharmony_ci			if ((tmp &
28388c2ecf20Sopenharmony_ci			     (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
28398c2ecf20Sopenharmony_ci			    !=
28408c2ecf20Sopenharmony_ci			    (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
28418c2ecf20Sopenharmony_ci				brcms_err(core, "%s: turn on PHY PLL failed\n",
28428c2ecf20Sopenharmony_ci					  __func__);
28438c2ecf20Sopenharmony_ci		}
28448c2ecf20Sopenharmony_ci	} else {
28458c2ecf20Sopenharmony_ci		/*
28468c2ecf20Sopenharmony_ci		 * Since the PLL may be shared, other cores can still
28478c2ecf20Sopenharmony_ci		 * be requesting it; so we'll deassert the request but
28488c2ecf20Sopenharmony_ci		 * not wait for status to comply.
28498c2ecf20Sopenharmony_ci		 */
28508c2ecf20Sopenharmony_ci		bcma_mask32(core, D11REGOFFS(clk_ctl_st),
28518c2ecf20Sopenharmony_ci			    ~CCS_ERSRC_REQ_PHYPLL);
28528c2ecf20Sopenharmony_ci		(void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
28538c2ecf20Sopenharmony_ci	}
28548c2ecf20Sopenharmony_ci}
28558c2ecf20Sopenharmony_ci
28568c2ecf20Sopenharmony_cistatic void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
28578c2ecf20Sopenharmony_ci{
28588c2ecf20Sopenharmony_ci	bool dev_gone;
28598c2ecf20Sopenharmony_ci
28608c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
28638c2ecf20Sopenharmony_ci
28648c2ecf20Sopenharmony_ci	if (dev_gone)
28658c2ecf20Sopenharmony_ci		return;
28668c2ecf20Sopenharmony_ci
28678c2ecf20Sopenharmony_ci	if (wlc_hw->noreset)
28688c2ecf20Sopenharmony_ci		return;
28698c2ecf20Sopenharmony_ci
28708c2ecf20Sopenharmony_ci	/* radio off */
28718c2ecf20Sopenharmony_ci	wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
28728c2ecf20Sopenharmony_ci
28738c2ecf20Sopenharmony_ci	/* turn off analog core */
28748c2ecf20Sopenharmony_ci	wlc_phy_anacore(wlc_hw->band->pi, OFF);
28758c2ecf20Sopenharmony_ci
28768c2ecf20Sopenharmony_ci	/* turn off PHYPLL to save power */
28778c2ecf20Sopenharmony_ci	brcms_b_core_phypll_ctl(wlc_hw, false);
28788c2ecf20Sopenharmony_ci
28798c2ecf20Sopenharmony_ci	wlc_hw->clk = false;
28808c2ecf20Sopenharmony_ci	bcma_core_disable(wlc_hw->d11core, 0);
28818c2ecf20Sopenharmony_ci	wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
28828c2ecf20Sopenharmony_ci}
28838c2ecf20Sopenharmony_ci
28848c2ecf20Sopenharmony_cistatic void brcms_c_flushqueues(struct brcms_c_info *wlc)
28858c2ecf20Sopenharmony_ci{
28868c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
28878c2ecf20Sopenharmony_ci	uint i;
28888c2ecf20Sopenharmony_ci
28898c2ecf20Sopenharmony_ci	/* free any posted tx packets */
28908c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++) {
28918c2ecf20Sopenharmony_ci		if (wlc_hw->di[i]) {
28928c2ecf20Sopenharmony_ci			dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
28938c2ecf20Sopenharmony_ci			if (i < TX_BCMC_FIFO)
28948c2ecf20Sopenharmony_ci				ieee80211_wake_queue(wlc->pub->ieee_hw,
28958c2ecf20Sopenharmony_ci						     brcms_fifo_to_ac(i));
28968c2ecf20Sopenharmony_ci		}
28978c2ecf20Sopenharmony_ci	}
28988c2ecf20Sopenharmony_ci
28998c2ecf20Sopenharmony_ci	/* free any posted rx packets */
29008c2ecf20Sopenharmony_ci	dma_rxreclaim(wlc_hw->di[RX_FIFO]);
29018c2ecf20Sopenharmony_ci}
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_cistatic u16
29048c2ecf20Sopenharmony_cibrcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
29058c2ecf20Sopenharmony_ci{
29068c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
29078c2ecf20Sopenharmony_ci	u16 objoff = D11REGOFFS(objdata);
29088c2ecf20Sopenharmony_ci
29098c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
29108c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
29118c2ecf20Sopenharmony_ci	if (offset & 2)
29128c2ecf20Sopenharmony_ci		objoff += 2;
29138c2ecf20Sopenharmony_ci
29148c2ecf20Sopenharmony_ci	return bcma_read16(core, objoff);
29158c2ecf20Sopenharmony_ci}
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_cistatic void
29188c2ecf20Sopenharmony_cibrcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
29198c2ecf20Sopenharmony_ci		     u32 sel)
29208c2ecf20Sopenharmony_ci{
29218c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
29228c2ecf20Sopenharmony_ci	u16 objoff = D11REGOFFS(objdata);
29238c2ecf20Sopenharmony_ci
29248c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
29258c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
29268c2ecf20Sopenharmony_ci	if (offset & 2)
29278c2ecf20Sopenharmony_ci		objoff += 2;
29288c2ecf20Sopenharmony_ci
29298c2ecf20Sopenharmony_ci	bcma_wflush16(core, objoff, v);
29308c2ecf20Sopenharmony_ci}
29318c2ecf20Sopenharmony_ci
29328c2ecf20Sopenharmony_ci/*
29338c2ecf20Sopenharmony_ci * Read a single u16 from shared memory.
29348c2ecf20Sopenharmony_ci * SHM 'offset' needs to be an even address
29358c2ecf20Sopenharmony_ci */
29368c2ecf20Sopenharmony_ciu16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
29378c2ecf20Sopenharmony_ci{
29388c2ecf20Sopenharmony_ci	return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
29398c2ecf20Sopenharmony_ci}
29408c2ecf20Sopenharmony_ci
29418c2ecf20Sopenharmony_ci/*
29428c2ecf20Sopenharmony_ci * Write a single u16 to shared memory.
29438c2ecf20Sopenharmony_ci * SHM 'offset' needs to be an even address
29448c2ecf20Sopenharmony_ci */
29458c2ecf20Sopenharmony_civoid brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
29468c2ecf20Sopenharmony_ci{
29478c2ecf20Sopenharmony_ci	brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
29488c2ecf20Sopenharmony_ci}
29498c2ecf20Sopenharmony_ci
29508c2ecf20Sopenharmony_ci/*
29518c2ecf20Sopenharmony_ci * Copy a buffer to shared memory of specified type .
29528c2ecf20Sopenharmony_ci * SHM 'offset' needs to be an even address and
29538c2ecf20Sopenharmony_ci * Buffer length 'len' must be an even number of bytes
29548c2ecf20Sopenharmony_ci * 'sel' selects the type of memory
29558c2ecf20Sopenharmony_ci */
29568c2ecf20Sopenharmony_civoid
29578c2ecf20Sopenharmony_cibrcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
29588c2ecf20Sopenharmony_ci		      const void *buf, int len, u32 sel)
29598c2ecf20Sopenharmony_ci{
29608c2ecf20Sopenharmony_ci	u16 v;
29618c2ecf20Sopenharmony_ci	const u8 *p = (const u8 *)buf;
29628c2ecf20Sopenharmony_ci	int i;
29638c2ecf20Sopenharmony_ci
29648c2ecf20Sopenharmony_ci	if (len <= 0 || (offset & 1) || (len & 1))
29658c2ecf20Sopenharmony_ci		return;
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci	for (i = 0; i < len; i += 2) {
29688c2ecf20Sopenharmony_ci		v = p[i] | (p[i + 1] << 8);
29698c2ecf20Sopenharmony_ci		brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
29708c2ecf20Sopenharmony_ci	}
29718c2ecf20Sopenharmony_ci}
29728c2ecf20Sopenharmony_ci
29738c2ecf20Sopenharmony_ci/*
29748c2ecf20Sopenharmony_ci * Copy a piece of shared memory of specified type to a buffer .
29758c2ecf20Sopenharmony_ci * SHM 'offset' needs to be an even address and
29768c2ecf20Sopenharmony_ci * Buffer length 'len' must be an even number of bytes
29778c2ecf20Sopenharmony_ci * 'sel' selects the type of memory
29788c2ecf20Sopenharmony_ci */
29798c2ecf20Sopenharmony_civoid
29808c2ecf20Sopenharmony_cibrcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
29818c2ecf20Sopenharmony_ci			 int len, u32 sel)
29828c2ecf20Sopenharmony_ci{
29838c2ecf20Sopenharmony_ci	u16 v;
29848c2ecf20Sopenharmony_ci	u8 *p = (u8 *) buf;
29858c2ecf20Sopenharmony_ci	int i;
29868c2ecf20Sopenharmony_ci
29878c2ecf20Sopenharmony_ci	if (len <= 0 || (offset & 1) || (len & 1))
29888c2ecf20Sopenharmony_ci		return;
29898c2ecf20Sopenharmony_ci
29908c2ecf20Sopenharmony_ci	for (i = 0; i < len; i += 2) {
29918c2ecf20Sopenharmony_ci		v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
29928c2ecf20Sopenharmony_ci		p[i] = v & 0xFF;
29938c2ecf20Sopenharmony_ci		p[i + 1] = (v >> 8) & 0xFF;
29948c2ecf20Sopenharmony_ci	}
29958c2ecf20Sopenharmony_ci}
29968c2ecf20Sopenharmony_ci
29978c2ecf20Sopenharmony_ci/* Copy a buffer to shared memory.
29988c2ecf20Sopenharmony_ci * SHM 'offset' needs to be an even address and
29998c2ecf20Sopenharmony_ci * Buffer length 'len' must be an even number of bytes
30008c2ecf20Sopenharmony_ci */
30018c2ecf20Sopenharmony_cistatic void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
30028c2ecf20Sopenharmony_ci			const void *buf, int len)
30038c2ecf20Sopenharmony_ci{
30048c2ecf20Sopenharmony_ci	brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
30058c2ecf20Sopenharmony_ci}
30068c2ecf20Sopenharmony_ci
30078c2ecf20Sopenharmony_cistatic void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
30088c2ecf20Sopenharmony_ci				   u16 SRL, u16 LRL)
30098c2ecf20Sopenharmony_ci{
30108c2ecf20Sopenharmony_ci	wlc_hw->SRL = SRL;
30118c2ecf20Sopenharmony_ci	wlc_hw->LRL = LRL;
30128c2ecf20Sopenharmony_ci
30138c2ecf20Sopenharmony_ci	/* write retry limit to SCR, shouldn't need to suspend */
30148c2ecf20Sopenharmony_ci	if (wlc_hw->up) {
30158c2ecf20Sopenharmony_ci		bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
30168c2ecf20Sopenharmony_ci			     OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
30178c2ecf20Sopenharmony_ci		(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
30188c2ecf20Sopenharmony_ci		bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
30198c2ecf20Sopenharmony_ci		bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
30208c2ecf20Sopenharmony_ci			     OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
30218c2ecf20Sopenharmony_ci		(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
30228c2ecf20Sopenharmony_ci		bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
30238c2ecf20Sopenharmony_ci	}
30248c2ecf20Sopenharmony_ci}
30258c2ecf20Sopenharmony_ci
30268c2ecf20Sopenharmony_cistatic void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
30278c2ecf20Sopenharmony_ci{
30288c2ecf20Sopenharmony_ci	if (set) {
30298c2ecf20Sopenharmony_ci		if (mboolisset(wlc_hw->pllreq, req_bit))
30308c2ecf20Sopenharmony_ci			return;
30318c2ecf20Sopenharmony_ci
30328c2ecf20Sopenharmony_ci		mboolset(wlc_hw->pllreq, req_bit);
30338c2ecf20Sopenharmony_ci
30348c2ecf20Sopenharmony_ci		if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
30358c2ecf20Sopenharmony_ci			if (!wlc_hw->sbclk)
30368c2ecf20Sopenharmony_ci				brcms_b_xtal(wlc_hw, ON);
30378c2ecf20Sopenharmony_ci		}
30388c2ecf20Sopenharmony_ci	} else {
30398c2ecf20Sopenharmony_ci		if (!mboolisset(wlc_hw->pllreq, req_bit))
30408c2ecf20Sopenharmony_ci			return;
30418c2ecf20Sopenharmony_ci
30428c2ecf20Sopenharmony_ci		mboolclr(wlc_hw->pllreq, req_bit);
30438c2ecf20Sopenharmony_ci
30448c2ecf20Sopenharmony_ci		if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
30458c2ecf20Sopenharmony_ci			if (wlc_hw->sbclk)
30468c2ecf20Sopenharmony_ci				brcms_b_xtal(wlc_hw, OFF);
30478c2ecf20Sopenharmony_ci		}
30488c2ecf20Sopenharmony_ci	}
30498c2ecf20Sopenharmony_ci}
30508c2ecf20Sopenharmony_ci
30518c2ecf20Sopenharmony_cistatic void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
30528c2ecf20Sopenharmony_ci{
30538c2ecf20Sopenharmony_ci	wlc_hw->antsel_avail = antsel_avail;
30548c2ecf20Sopenharmony_ci}
30558c2ecf20Sopenharmony_ci
30568c2ecf20Sopenharmony_ci/*
30578c2ecf20Sopenharmony_ci * conditions under which the PM bit should be set in outgoing frames
30588c2ecf20Sopenharmony_ci * and STAY_AWAKE is meaningful
30598c2ecf20Sopenharmony_ci */
30608c2ecf20Sopenharmony_cistatic bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
30618c2ecf20Sopenharmony_ci{
30628c2ecf20Sopenharmony_ci	/* not supporting PS so always return false for now */
30638c2ecf20Sopenharmony_ci	return false;
30648c2ecf20Sopenharmony_ci}
30658c2ecf20Sopenharmony_ci
30668c2ecf20Sopenharmony_cistatic void brcms_c_statsupd(struct brcms_c_info *wlc)
30678c2ecf20Sopenharmony_ci{
30688c2ecf20Sopenharmony_ci	int i;
30698c2ecf20Sopenharmony_ci	struct macstat *macstats;
30708c2ecf20Sopenharmony_ci#ifdef DEBUG
30718c2ecf20Sopenharmony_ci	u16 delta;
30728c2ecf20Sopenharmony_ci	u16 rxf0ovfl;
30738c2ecf20Sopenharmony_ci	u16 txfunfl[NFIFO];
30748c2ecf20Sopenharmony_ci#endif				/* DEBUG */
30758c2ecf20Sopenharmony_ci
30768c2ecf20Sopenharmony_ci	/* if driver down, make no sense to update stats */
30778c2ecf20Sopenharmony_ci	if (!wlc->pub->up)
30788c2ecf20Sopenharmony_ci		return;
30798c2ecf20Sopenharmony_ci
30808c2ecf20Sopenharmony_ci	macstats = wlc->core->macstat_snapshot;
30818c2ecf20Sopenharmony_ci
30828c2ecf20Sopenharmony_ci#ifdef DEBUG
30838c2ecf20Sopenharmony_ci	/* save last rx fifo 0 overflow count */
30848c2ecf20Sopenharmony_ci	rxf0ovfl = macstats->rxf0ovfl;
30858c2ecf20Sopenharmony_ci
30868c2ecf20Sopenharmony_ci	/* save last tx fifo  underflow count */
30878c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++)
30888c2ecf20Sopenharmony_ci		txfunfl[i] = macstats->txfunfl[i];
30898c2ecf20Sopenharmony_ci#endif				/* DEBUG */
30908c2ecf20Sopenharmony_ci
30918c2ecf20Sopenharmony_ci	/* Read mac stats from contiguous shared memory */
30928c2ecf20Sopenharmony_ci	brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, macstats,
30938c2ecf20Sopenharmony_ci				sizeof(*macstats), OBJADDR_SHM_SEL);
30948c2ecf20Sopenharmony_ci
30958c2ecf20Sopenharmony_ci#ifdef DEBUG
30968c2ecf20Sopenharmony_ci	/* check for rx fifo 0 overflow */
30978c2ecf20Sopenharmony_ci	delta = (u16)(macstats->rxf0ovfl - rxf0ovfl);
30988c2ecf20Sopenharmony_ci	if (delta)
30998c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
31008c2ecf20Sopenharmony_ci			  wlc->pub->unit, delta);
31018c2ecf20Sopenharmony_ci
31028c2ecf20Sopenharmony_ci	/* check for tx fifo underflows */
31038c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++) {
31048c2ecf20Sopenharmony_ci		delta = macstats->txfunfl[i] - txfunfl[i];
31058c2ecf20Sopenharmony_ci		if (delta)
31068c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core,
31078c2ecf20Sopenharmony_ci				  "wl%d: %u tx fifo %d underflows!\n",
31088c2ecf20Sopenharmony_ci				  wlc->pub->unit, delta, i);
31098c2ecf20Sopenharmony_ci	}
31108c2ecf20Sopenharmony_ci#endif				/* DEBUG */
31118c2ecf20Sopenharmony_ci
31128c2ecf20Sopenharmony_ci	/* merge counters from dma module */
31138c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++) {
31148c2ecf20Sopenharmony_ci		if (wlc->hw->di[i])
31158c2ecf20Sopenharmony_ci			dma_counterreset(wlc->hw->di[i]);
31168c2ecf20Sopenharmony_ci	}
31178c2ecf20Sopenharmony_ci}
31188c2ecf20Sopenharmony_ci
31198c2ecf20Sopenharmony_cistatic void brcms_b_reset(struct brcms_hardware *wlc_hw)
31208c2ecf20Sopenharmony_ci{
31218c2ecf20Sopenharmony_ci	/* reset the core */
31228c2ecf20Sopenharmony_ci	if (!brcms_deviceremoved(wlc_hw->wlc))
31238c2ecf20Sopenharmony_ci		brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
31248c2ecf20Sopenharmony_ci
31258c2ecf20Sopenharmony_ci	/* purge the dma rings */
31268c2ecf20Sopenharmony_ci	brcms_c_flushqueues(wlc_hw->wlc);
31278c2ecf20Sopenharmony_ci}
31288c2ecf20Sopenharmony_ci
31298c2ecf20Sopenharmony_civoid brcms_c_reset(struct brcms_c_info *wlc)
31308c2ecf20Sopenharmony_ci{
31318c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
31328c2ecf20Sopenharmony_ci
31338c2ecf20Sopenharmony_ci	/* slurp up hw mac counters before core reset */
31348c2ecf20Sopenharmony_ci	brcms_c_statsupd(wlc);
31358c2ecf20Sopenharmony_ci
31368c2ecf20Sopenharmony_ci	/* reset our snapshot of macstat counters */
31378c2ecf20Sopenharmony_ci	memset(wlc->core->macstat_snapshot, 0, sizeof(struct macstat));
31388c2ecf20Sopenharmony_ci
31398c2ecf20Sopenharmony_ci	brcms_b_reset(wlc->hw);
31408c2ecf20Sopenharmony_ci}
31418c2ecf20Sopenharmony_ci
31428c2ecf20Sopenharmony_civoid brcms_c_init_scb(struct scb *scb)
31438c2ecf20Sopenharmony_ci{
31448c2ecf20Sopenharmony_ci	int i;
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci	memset(scb, 0, sizeof(struct scb));
31478c2ecf20Sopenharmony_ci	scb->flags = SCB_WMECAP | SCB_HTCAP;
31488c2ecf20Sopenharmony_ci	for (i = 0; i < NUMPRIO; i++) {
31498c2ecf20Sopenharmony_ci		scb->seqnum[i] = 0;
31508c2ecf20Sopenharmony_ci		scb->seqctl[i] = 0xFFFF;
31518c2ecf20Sopenharmony_ci	}
31528c2ecf20Sopenharmony_ci
31538c2ecf20Sopenharmony_ci	scb->seqctl_nonqos = 0xFFFF;
31548c2ecf20Sopenharmony_ci	scb->magic = SCB_MAGIC;
31558c2ecf20Sopenharmony_ci}
31568c2ecf20Sopenharmony_ci
31578c2ecf20Sopenharmony_ci/* d11 core init
31588c2ecf20Sopenharmony_ci *   reset PSM
31598c2ecf20Sopenharmony_ci *   download ucode/PCM
31608c2ecf20Sopenharmony_ci *   let ucode run to suspended
31618c2ecf20Sopenharmony_ci *   download ucode inits
31628c2ecf20Sopenharmony_ci *   config other core registers
31638c2ecf20Sopenharmony_ci *   init dma
31648c2ecf20Sopenharmony_ci */
31658c2ecf20Sopenharmony_cistatic void brcms_b_coreinit(struct brcms_c_info *wlc)
31668c2ecf20Sopenharmony_ci{
31678c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
31688c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
31698c2ecf20Sopenharmony_ci	u32 bcnint_us;
31708c2ecf20Sopenharmony_ci	uint i = 0;
31718c2ecf20Sopenharmony_ci	bool fifosz_fixup = false;
31728c2ecf20Sopenharmony_ci	int err = 0;
31738c2ecf20Sopenharmony_ci	u16 buf[NFIFO];
31748c2ecf20Sopenharmony_ci	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
31758c2ecf20Sopenharmony_ci
31768c2ecf20Sopenharmony_ci	brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
31778c2ecf20Sopenharmony_ci
31788c2ecf20Sopenharmony_ci	/* reset PSM */
31798c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
31808c2ecf20Sopenharmony_ci
31818c2ecf20Sopenharmony_ci	brcms_ucode_download(wlc_hw);
31828c2ecf20Sopenharmony_ci	/*
31838c2ecf20Sopenharmony_ci	 * FIFOSZ fixup. driver wants to controls the fifo allocation.
31848c2ecf20Sopenharmony_ci	 */
31858c2ecf20Sopenharmony_ci	fifosz_fixup = true;
31868c2ecf20Sopenharmony_ci
31878c2ecf20Sopenharmony_ci	/* let the PSM run to the suspended state, set mode to BSS STA */
31888c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(macintstatus), -1);
31898c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw, ~0,
31908c2ecf20Sopenharmony_ci		       (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
31918c2ecf20Sopenharmony_ci
31928c2ecf20Sopenharmony_ci	/* wait for ucode to self-suspend after auto-init */
31938c2ecf20Sopenharmony_ci	SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
31948c2ecf20Sopenharmony_ci		   MI_MACSSPNDD) == 0), 1000 * 1000);
31958c2ecf20Sopenharmony_ci	if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
31968c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
31978c2ecf20Sopenharmony_ci			  "suspend!\n", wlc_hw->unit);
31988c2ecf20Sopenharmony_ci
31998c2ecf20Sopenharmony_ci	brcms_c_gpio_init(wlc);
32008c2ecf20Sopenharmony_ci
32018c2ecf20Sopenharmony_ci	bcma_aread32(core, BCMA_IOST);
32028c2ecf20Sopenharmony_ci
32038c2ecf20Sopenharmony_ci	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
32048c2ecf20Sopenharmony_ci		if (BRCMS_ISNPHY(wlc_hw->band))
32058c2ecf20Sopenharmony_ci			brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
32068c2ecf20Sopenharmony_ci		else
32078c2ecf20Sopenharmony_ci			brcms_err(core, "%s: wl%d: unsupported phy in corerev"
32088c2ecf20Sopenharmony_ci				  " %d\n", __func__, wlc_hw->unit,
32098c2ecf20Sopenharmony_ci				  wlc_hw->corerev);
32108c2ecf20Sopenharmony_ci	} else if (D11REV_IS(wlc_hw->corerev, 24)) {
32118c2ecf20Sopenharmony_ci		if (BRCMS_ISLCNPHY(wlc_hw->band))
32128c2ecf20Sopenharmony_ci			brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
32138c2ecf20Sopenharmony_ci		else
32148c2ecf20Sopenharmony_ci			brcms_err(core, "%s: wl%d: unsupported phy in corerev"
32158c2ecf20Sopenharmony_ci				  " %d\n", __func__, wlc_hw->unit,
32168c2ecf20Sopenharmony_ci				  wlc_hw->corerev);
32178c2ecf20Sopenharmony_ci	} else {
32188c2ecf20Sopenharmony_ci		brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
32198c2ecf20Sopenharmony_ci			  __func__, wlc_hw->unit, wlc_hw->corerev);
32208c2ecf20Sopenharmony_ci	}
32218c2ecf20Sopenharmony_ci
32228c2ecf20Sopenharmony_ci	/* For old ucode, txfifo sizes needs to be modified(increased) */
32238c2ecf20Sopenharmony_ci	if (fifosz_fixup)
32248c2ecf20Sopenharmony_ci		brcms_b_corerev_fifofixup(wlc_hw);
32258c2ecf20Sopenharmony_ci
32268c2ecf20Sopenharmony_ci	/* check txfifo allocations match between ucode and driver */
32278c2ecf20Sopenharmony_ci	buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
32288c2ecf20Sopenharmony_ci	if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
32298c2ecf20Sopenharmony_ci		i = TX_AC_BE_FIFO;
32308c2ecf20Sopenharmony_ci		err = -1;
32318c2ecf20Sopenharmony_ci	}
32328c2ecf20Sopenharmony_ci	buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
32338c2ecf20Sopenharmony_ci	if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
32348c2ecf20Sopenharmony_ci		i = TX_AC_VI_FIFO;
32358c2ecf20Sopenharmony_ci		err = -1;
32368c2ecf20Sopenharmony_ci	}
32378c2ecf20Sopenharmony_ci	buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
32388c2ecf20Sopenharmony_ci	buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
32398c2ecf20Sopenharmony_ci	buf[TX_AC_BK_FIFO] &= 0xff;
32408c2ecf20Sopenharmony_ci	if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
32418c2ecf20Sopenharmony_ci		i = TX_AC_BK_FIFO;
32428c2ecf20Sopenharmony_ci		err = -1;
32438c2ecf20Sopenharmony_ci	}
32448c2ecf20Sopenharmony_ci	if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
32458c2ecf20Sopenharmony_ci		i = TX_AC_VO_FIFO;
32468c2ecf20Sopenharmony_ci		err = -1;
32478c2ecf20Sopenharmony_ci	}
32488c2ecf20Sopenharmony_ci	buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
32498c2ecf20Sopenharmony_ci	buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
32508c2ecf20Sopenharmony_ci	buf[TX_BCMC_FIFO] &= 0xff;
32518c2ecf20Sopenharmony_ci	if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
32528c2ecf20Sopenharmony_ci		i = TX_BCMC_FIFO;
32538c2ecf20Sopenharmony_ci		err = -1;
32548c2ecf20Sopenharmony_ci	}
32558c2ecf20Sopenharmony_ci	if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
32568c2ecf20Sopenharmony_ci		i = TX_ATIM_FIFO;
32578c2ecf20Sopenharmony_ci		err = -1;
32588c2ecf20Sopenharmony_ci	}
32598c2ecf20Sopenharmony_ci	if (err != 0)
32608c2ecf20Sopenharmony_ci		brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
32618c2ecf20Sopenharmony_ci			  " driver size %d index %d\n", buf[i],
32628c2ecf20Sopenharmony_ci			  wlc_hw->xmtfifo_sz[i], i);
32638c2ecf20Sopenharmony_ci
32648c2ecf20Sopenharmony_ci	/* make sure we can still talk to the mac */
32658c2ecf20Sopenharmony_ci	WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
32668c2ecf20Sopenharmony_ci
32678c2ecf20Sopenharmony_ci	/* band-specific inits done by wlc_bsinit() */
32688c2ecf20Sopenharmony_ci
32698c2ecf20Sopenharmony_ci	/* Set up frame burst size and antenna swap threshold init values */
32708c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
32718c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
32728c2ecf20Sopenharmony_ci
32738c2ecf20Sopenharmony_ci	/* enable one rx interrupt per received frame */
32748c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
32758c2ecf20Sopenharmony_ci
32768c2ecf20Sopenharmony_ci	/* set the station mode (BSS STA) */
32778c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc_hw,
32788c2ecf20Sopenharmony_ci		       (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
32798c2ecf20Sopenharmony_ci		       (MCTL_INFRA | MCTL_DISCARD_PMQ));
32808c2ecf20Sopenharmony_ci
32818c2ecf20Sopenharmony_ci	/* set up Beacon interval */
32828c2ecf20Sopenharmony_ci	bcnint_us = 0x8000 << 10;
32838c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(tsf_cfprep),
32848c2ecf20Sopenharmony_ci		     (bcnint_us << CFPREP_CBI_SHIFT));
32858c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
32868c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
32878c2ecf20Sopenharmony_ci
32888c2ecf20Sopenharmony_ci	/* write interrupt mask */
32898c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
32908c2ecf20Sopenharmony_ci		     DEF_RXINTMASK);
32918c2ecf20Sopenharmony_ci
32928c2ecf20Sopenharmony_ci	/* allow the MAC to control the PHY clock (dynamic on/off) */
32938c2ecf20Sopenharmony_ci	brcms_b_macphyclk_set(wlc_hw, ON);
32948c2ecf20Sopenharmony_ci
32958c2ecf20Sopenharmony_ci	/* program dynamic clock control fast powerup delay register */
32968c2ecf20Sopenharmony_ci	wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
32978c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
32988c2ecf20Sopenharmony_ci
32998c2ecf20Sopenharmony_ci	/* tell the ucode the corerev */
33008c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
33018c2ecf20Sopenharmony_ci
33028c2ecf20Sopenharmony_ci	/* tell the ucode MAC capabilities */
33038c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
33048c2ecf20Sopenharmony_ci			   (u16) (wlc_hw->machwcap & 0xffff));
33058c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
33068c2ecf20Sopenharmony_ci			   (u16) ((wlc_hw->
33078c2ecf20Sopenharmony_ci				      machwcap >> 16) & 0xffff));
33088c2ecf20Sopenharmony_ci
33098c2ecf20Sopenharmony_ci	/* write retry limits to SCR, this done after PSM init */
33108c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr),
33118c2ecf20Sopenharmony_ci		     OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
33128c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
33138c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
33148c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objaddr),
33158c2ecf20Sopenharmony_ci		     OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
33168c2ecf20Sopenharmony_ci	(void)bcma_read32(core, D11REGOFFS(objaddr));
33178c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
33188c2ecf20Sopenharmony_ci
33198c2ecf20Sopenharmony_ci	/* write rate fallback retry limits */
33208c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
33218c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
33228c2ecf20Sopenharmony_ci
33238c2ecf20Sopenharmony_ci	bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
33248c2ecf20Sopenharmony_ci	bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
33258c2ecf20Sopenharmony_ci
33268c2ecf20Sopenharmony_ci	/* init the tx dma engines */
33278c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++) {
33288c2ecf20Sopenharmony_ci		if (wlc_hw->di[i])
33298c2ecf20Sopenharmony_ci			dma_txinit(wlc_hw->di[i]);
33308c2ecf20Sopenharmony_ci	}
33318c2ecf20Sopenharmony_ci
33328c2ecf20Sopenharmony_ci	/* init the rx dma engine(s) and post receive buffers */
33338c2ecf20Sopenharmony_ci	dma_rxinit(wlc_hw->di[RX_FIFO]);
33348c2ecf20Sopenharmony_ci	dma_rxfill(wlc_hw->di[RX_FIFO]);
33358c2ecf20Sopenharmony_ci}
33368c2ecf20Sopenharmony_ci
33378c2ecf20Sopenharmony_cistatic void brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec)
33388c2ecf20Sopenharmony_ci{
33398c2ecf20Sopenharmony_ci	u32 macintmask;
33408c2ecf20Sopenharmony_ci	bool fastclk;
33418c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = wlc_hw->wlc;
33428c2ecf20Sopenharmony_ci
33438c2ecf20Sopenharmony_ci	/* request FAST clock if not on */
33448c2ecf20Sopenharmony_ci	fastclk = wlc_hw->forcefastclk;
33458c2ecf20Sopenharmony_ci	if (!fastclk)
33468c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
33478c2ecf20Sopenharmony_ci
33488c2ecf20Sopenharmony_ci	/* disable interrupts */
33498c2ecf20Sopenharmony_ci	macintmask = brcms_intrsoff(wlc->wl);
33508c2ecf20Sopenharmony_ci
33518c2ecf20Sopenharmony_ci	/* set up the specified band and chanspec */
33528c2ecf20Sopenharmony_ci	brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
33538c2ecf20Sopenharmony_ci	wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
33548c2ecf20Sopenharmony_ci
33558c2ecf20Sopenharmony_ci	/* do one-time phy inits and calibration */
33568c2ecf20Sopenharmony_ci	wlc_phy_cal_init(wlc_hw->band->pi);
33578c2ecf20Sopenharmony_ci
33588c2ecf20Sopenharmony_ci	/* core-specific initialization */
33598c2ecf20Sopenharmony_ci	brcms_b_coreinit(wlc);
33608c2ecf20Sopenharmony_ci
33618c2ecf20Sopenharmony_ci	/* band-specific inits */
33628c2ecf20Sopenharmony_ci	brcms_b_bsinit(wlc, chanspec);
33638c2ecf20Sopenharmony_ci
33648c2ecf20Sopenharmony_ci	/* restore macintmask */
33658c2ecf20Sopenharmony_ci	brcms_intrsrestore(wlc->wl, macintmask);
33668c2ecf20Sopenharmony_ci
33678c2ecf20Sopenharmony_ci	/* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
33688c2ecf20Sopenharmony_ci	 * is suspended and brcms_c_enable_mac() will clear this override bit.
33698c2ecf20Sopenharmony_ci	 */
33708c2ecf20Sopenharmony_ci	mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
33718c2ecf20Sopenharmony_ci
33728c2ecf20Sopenharmony_ci	/*
33738c2ecf20Sopenharmony_ci	 * initialize mac_suspend_depth to 1 to match ucode
33748c2ecf20Sopenharmony_ci	 * initial suspended state
33758c2ecf20Sopenharmony_ci	 */
33768c2ecf20Sopenharmony_ci	wlc_hw->mac_suspend_depth = 1;
33778c2ecf20Sopenharmony_ci
33788c2ecf20Sopenharmony_ci	/* restore the clk */
33798c2ecf20Sopenharmony_ci	if (!fastclk)
33808c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
33818c2ecf20Sopenharmony_ci}
33828c2ecf20Sopenharmony_ci
33838c2ecf20Sopenharmony_cistatic void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
33848c2ecf20Sopenharmony_ci				     u16 chanspec)
33858c2ecf20Sopenharmony_ci{
33868c2ecf20Sopenharmony_ci	/* Save our copy of the chanspec */
33878c2ecf20Sopenharmony_ci	wlc->chanspec = chanspec;
33888c2ecf20Sopenharmony_ci
33898c2ecf20Sopenharmony_ci	/* Set the chanspec and power limits for this locale */
33908c2ecf20Sopenharmony_ci	brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
33918c2ecf20Sopenharmony_ci
33928c2ecf20Sopenharmony_ci	if (wlc->stf->ss_algosel_auto)
33938c2ecf20Sopenharmony_ci		brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
33948c2ecf20Sopenharmony_ci					    chanspec);
33958c2ecf20Sopenharmony_ci
33968c2ecf20Sopenharmony_ci	brcms_c_stf_ss_update(wlc, wlc->band);
33978c2ecf20Sopenharmony_ci}
33988c2ecf20Sopenharmony_ci
33998c2ecf20Sopenharmony_cistatic void
34008c2ecf20Sopenharmony_cibrcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
34018c2ecf20Sopenharmony_ci{
34028c2ecf20Sopenharmony_ci	brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
34038c2ecf20Sopenharmony_ci		wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
34048c2ecf20Sopenharmony_ci		(bool) (wlc->pub->_n_enab & SUPPORT_11N),
34058c2ecf20Sopenharmony_ci		brcms_chspec_bw(wlc->default_bss->chanspec),
34068c2ecf20Sopenharmony_ci		wlc->stf->txstreams);
34078c2ecf20Sopenharmony_ci}
34088c2ecf20Sopenharmony_ci
34098c2ecf20Sopenharmony_ci/* derive wlc->band->basic_rate[] table from 'rateset' */
34108c2ecf20Sopenharmony_cistatic void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
34118c2ecf20Sopenharmony_ci			      struct brcms_c_rateset *rateset)
34128c2ecf20Sopenharmony_ci{
34138c2ecf20Sopenharmony_ci	u8 rate;
34148c2ecf20Sopenharmony_ci	u8 mandatory;
34158c2ecf20Sopenharmony_ci	u8 cck_basic = 0;
34168c2ecf20Sopenharmony_ci	u8 ofdm_basic = 0;
34178c2ecf20Sopenharmony_ci	u8 *br = wlc->band->basic_rate;
34188c2ecf20Sopenharmony_ci	uint i;
34198c2ecf20Sopenharmony_ci
34208c2ecf20Sopenharmony_ci	/* incoming rates are in 500kbps units as in 802.11 Supported Rates */
34218c2ecf20Sopenharmony_ci	memset(br, 0, BRCM_MAXRATE + 1);
34228c2ecf20Sopenharmony_ci
34238c2ecf20Sopenharmony_ci	/* For each basic rate in the rates list, make an entry in the
34248c2ecf20Sopenharmony_ci	 * best basic lookup.
34258c2ecf20Sopenharmony_ci	 */
34268c2ecf20Sopenharmony_ci	for (i = 0; i < rateset->count; i++) {
34278c2ecf20Sopenharmony_ci		/* only make an entry for a basic rate */
34288c2ecf20Sopenharmony_ci		if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
34298c2ecf20Sopenharmony_ci			continue;
34308c2ecf20Sopenharmony_ci
34318c2ecf20Sopenharmony_ci		/* mask off basic bit */
34328c2ecf20Sopenharmony_ci		rate = (rateset->rates[i] & BRCMS_RATE_MASK);
34338c2ecf20Sopenharmony_ci
34348c2ecf20Sopenharmony_ci		if (rate > BRCM_MAXRATE) {
34358c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
34368c2ecf20Sopenharmony_ci				  "invalid rate 0x%X in rate set\n",
34378c2ecf20Sopenharmony_ci				  rateset->rates[i]);
34388c2ecf20Sopenharmony_ci			continue;
34398c2ecf20Sopenharmony_ci		}
34408c2ecf20Sopenharmony_ci
34418c2ecf20Sopenharmony_ci		br[rate] = rate;
34428c2ecf20Sopenharmony_ci	}
34438c2ecf20Sopenharmony_ci
34448c2ecf20Sopenharmony_ci	/* The rate lookup table now has non-zero entries for each
34458c2ecf20Sopenharmony_ci	 * basic rate, equal to the basic rate: br[basicN] = basicN
34468c2ecf20Sopenharmony_ci	 *
34478c2ecf20Sopenharmony_ci	 * To look up the best basic rate corresponding to any
34488c2ecf20Sopenharmony_ci	 * particular rate, code can use the basic_rate table
34498c2ecf20Sopenharmony_ci	 * like this
34508c2ecf20Sopenharmony_ci	 *
34518c2ecf20Sopenharmony_ci	 * basic_rate = wlc->band->basic_rate[tx_rate]
34528c2ecf20Sopenharmony_ci	 *
34538c2ecf20Sopenharmony_ci	 * Make sure there is a best basic rate entry for
34548c2ecf20Sopenharmony_ci	 * every rate by walking up the table from low rates
34558c2ecf20Sopenharmony_ci	 * to high, filling in holes in the lookup table
34568c2ecf20Sopenharmony_ci	 */
34578c2ecf20Sopenharmony_ci
34588c2ecf20Sopenharmony_ci	for (i = 0; i < wlc->band->hw_rateset.count; i++) {
34598c2ecf20Sopenharmony_ci		rate = wlc->band->hw_rateset.rates[i];
34608c2ecf20Sopenharmony_ci
34618c2ecf20Sopenharmony_ci		if (br[rate] != 0) {
34628c2ecf20Sopenharmony_ci			/* This rate is a basic rate.
34638c2ecf20Sopenharmony_ci			 * Keep track of the best basic rate so far by
34648c2ecf20Sopenharmony_ci			 * modulation type.
34658c2ecf20Sopenharmony_ci			 */
34668c2ecf20Sopenharmony_ci			if (is_ofdm_rate(rate))
34678c2ecf20Sopenharmony_ci				ofdm_basic = rate;
34688c2ecf20Sopenharmony_ci			else
34698c2ecf20Sopenharmony_ci				cck_basic = rate;
34708c2ecf20Sopenharmony_ci
34718c2ecf20Sopenharmony_ci			continue;
34728c2ecf20Sopenharmony_ci		}
34738c2ecf20Sopenharmony_ci
34748c2ecf20Sopenharmony_ci		/* This rate is not a basic rate so figure out the
34758c2ecf20Sopenharmony_ci		 * best basic rate less than this rate and fill in
34768c2ecf20Sopenharmony_ci		 * the hole in the table
34778c2ecf20Sopenharmony_ci		 */
34788c2ecf20Sopenharmony_ci
34798c2ecf20Sopenharmony_ci		br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
34808c2ecf20Sopenharmony_ci
34818c2ecf20Sopenharmony_ci		if (br[rate] != 0)
34828c2ecf20Sopenharmony_ci			continue;
34838c2ecf20Sopenharmony_ci
34848c2ecf20Sopenharmony_ci		if (is_ofdm_rate(rate)) {
34858c2ecf20Sopenharmony_ci			/*
34868c2ecf20Sopenharmony_ci			 * In 11g and 11a, the OFDM mandatory rates
34878c2ecf20Sopenharmony_ci			 * are 6, 12, and 24 Mbps
34888c2ecf20Sopenharmony_ci			 */
34898c2ecf20Sopenharmony_ci			if (rate >= BRCM_RATE_24M)
34908c2ecf20Sopenharmony_ci				mandatory = BRCM_RATE_24M;
34918c2ecf20Sopenharmony_ci			else if (rate >= BRCM_RATE_12M)
34928c2ecf20Sopenharmony_ci				mandatory = BRCM_RATE_12M;
34938c2ecf20Sopenharmony_ci			else
34948c2ecf20Sopenharmony_ci				mandatory = BRCM_RATE_6M;
34958c2ecf20Sopenharmony_ci		} else {
34968c2ecf20Sopenharmony_ci			/* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
34978c2ecf20Sopenharmony_ci			mandatory = rate;
34988c2ecf20Sopenharmony_ci		}
34998c2ecf20Sopenharmony_ci
35008c2ecf20Sopenharmony_ci		br[rate] = mandatory;
35018c2ecf20Sopenharmony_ci	}
35028c2ecf20Sopenharmony_ci}
35038c2ecf20Sopenharmony_ci
35048c2ecf20Sopenharmony_cistatic void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
35058c2ecf20Sopenharmony_ci				     u16 chanspec)
35068c2ecf20Sopenharmony_ci{
35078c2ecf20Sopenharmony_ci	struct brcms_c_rateset default_rateset;
35088c2ecf20Sopenharmony_ci	uint parkband;
35098c2ecf20Sopenharmony_ci	uint i, band_order[2];
35108c2ecf20Sopenharmony_ci
35118c2ecf20Sopenharmony_ci	/*
35128c2ecf20Sopenharmony_ci	 * We might have been bandlocked during down and the chip
35138c2ecf20Sopenharmony_ci	 * power-cycled (hibernate). Figure out the right band to park on
35148c2ecf20Sopenharmony_ci	 */
35158c2ecf20Sopenharmony_ci	if (wlc->bandlocked || wlc->pub->_nbands == 1) {
35168c2ecf20Sopenharmony_ci		/* updated in brcms_c_bandlock() */
35178c2ecf20Sopenharmony_ci		parkband = wlc->band->bandunit;
35188c2ecf20Sopenharmony_ci		band_order[0] = band_order[1] = parkband;
35198c2ecf20Sopenharmony_ci	} else {
35208c2ecf20Sopenharmony_ci		/* park on the band of the specified chanspec */
35218c2ecf20Sopenharmony_ci		parkband = chspec_bandunit(chanspec);
35228c2ecf20Sopenharmony_ci
35238c2ecf20Sopenharmony_ci		/* order so that parkband initialize last */
35248c2ecf20Sopenharmony_ci		band_order[0] = parkband ^ 1;
35258c2ecf20Sopenharmony_ci		band_order[1] = parkband;
35268c2ecf20Sopenharmony_ci	}
35278c2ecf20Sopenharmony_ci
35288c2ecf20Sopenharmony_ci	/* make each band operational, software state init */
35298c2ecf20Sopenharmony_ci	for (i = 0; i < wlc->pub->_nbands; i++) {
35308c2ecf20Sopenharmony_ci		uint j = band_order[i];
35318c2ecf20Sopenharmony_ci
35328c2ecf20Sopenharmony_ci		wlc->band = wlc->bandstate[j];
35338c2ecf20Sopenharmony_ci
35348c2ecf20Sopenharmony_ci		brcms_default_rateset(wlc, &default_rateset);
35358c2ecf20Sopenharmony_ci
35368c2ecf20Sopenharmony_ci		/* fill in hw_rate */
35378c2ecf20Sopenharmony_ci		brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
35388c2ecf20Sopenharmony_ci				   false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
35398c2ecf20Sopenharmony_ci				   (bool) (wlc->pub->_n_enab & SUPPORT_11N));
35408c2ecf20Sopenharmony_ci
35418c2ecf20Sopenharmony_ci		/* init basic rate lookup */
35428c2ecf20Sopenharmony_ci		brcms_c_rate_lookup_init(wlc, &default_rateset);
35438c2ecf20Sopenharmony_ci	}
35448c2ecf20Sopenharmony_ci
35458c2ecf20Sopenharmony_ci	/* sync up phy/radio chanspec */
35468c2ecf20Sopenharmony_ci	brcms_c_set_phy_chanspec(wlc, chanspec);
35478c2ecf20Sopenharmony_ci}
35488c2ecf20Sopenharmony_ci
35498c2ecf20Sopenharmony_ci/*
35508c2ecf20Sopenharmony_ci * Set or clear filtering related maccontrol bits based on
35518c2ecf20Sopenharmony_ci * specified filter flags
35528c2ecf20Sopenharmony_ci */
35538c2ecf20Sopenharmony_civoid brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
35548c2ecf20Sopenharmony_ci{
35558c2ecf20Sopenharmony_ci	u32 promisc_bits = 0;
35568c2ecf20Sopenharmony_ci
35578c2ecf20Sopenharmony_ci	wlc->filter_flags = filter_flags;
35588c2ecf20Sopenharmony_ci
35598c2ecf20Sopenharmony_ci	if (filter_flags & FIF_OTHER_BSS)
35608c2ecf20Sopenharmony_ci		promisc_bits |= MCTL_PROMISC;
35618c2ecf20Sopenharmony_ci
35628c2ecf20Sopenharmony_ci	if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
35638c2ecf20Sopenharmony_ci		promisc_bits |= MCTL_BCNS_PROMISC;
35648c2ecf20Sopenharmony_ci
35658c2ecf20Sopenharmony_ci	if (filter_flags & FIF_FCSFAIL)
35668c2ecf20Sopenharmony_ci		promisc_bits |= MCTL_KEEPBADFCS;
35678c2ecf20Sopenharmony_ci
35688c2ecf20Sopenharmony_ci	if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
35698c2ecf20Sopenharmony_ci		promisc_bits |= MCTL_KEEPCONTROL;
35708c2ecf20Sopenharmony_ci
35718c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc->hw,
35728c2ecf20Sopenharmony_ci		MCTL_PROMISC | MCTL_BCNS_PROMISC |
35738c2ecf20Sopenharmony_ci		MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
35748c2ecf20Sopenharmony_ci		promisc_bits);
35758c2ecf20Sopenharmony_ci}
35768c2ecf20Sopenharmony_ci
35778c2ecf20Sopenharmony_ci/*
35788c2ecf20Sopenharmony_ci * ucode, hwmac update
35798c2ecf20Sopenharmony_ci *    Channel dependent updates for ucode and hw
35808c2ecf20Sopenharmony_ci */
35818c2ecf20Sopenharmony_cistatic void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
35828c2ecf20Sopenharmony_ci{
35838c2ecf20Sopenharmony_ci	/* enable or disable any active IBSSs depending on whether or not
35848c2ecf20Sopenharmony_ci	 * we are on the home channel
35858c2ecf20Sopenharmony_ci	 */
35868c2ecf20Sopenharmony_ci	if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
35878c2ecf20Sopenharmony_ci		if (wlc->pub->associated) {
35888c2ecf20Sopenharmony_ci			/*
35898c2ecf20Sopenharmony_ci			 * BMAC_NOTE: This is something that should be fixed
35908c2ecf20Sopenharmony_ci			 * in ucode inits. I think that the ucode inits set
35918c2ecf20Sopenharmony_ci			 * up the bcn templates and shm values with a bogus
35928c2ecf20Sopenharmony_ci			 * beacon. This should not be done in the inits. If
35938c2ecf20Sopenharmony_ci			 * ucode needs to set up a beacon for testing, the
35948c2ecf20Sopenharmony_ci			 * test routines should write it down, not expect the
35958c2ecf20Sopenharmony_ci			 * inits to populate a bogus beacon.
35968c2ecf20Sopenharmony_ci			 */
35978c2ecf20Sopenharmony_ci			if (BRCMS_PHY_11N_CAP(wlc->band))
35988c2ecf20Sopenharmony_ci				brcms_b_write_shm(wlc->hw,
35998c2ecf20Sopenharmony_ci						M_BCN_TXTSF_OFFSET, 0);
36008c2ecf20Sopenharmony_ci		}
36018c2ecf20Sopenharmony_ci	} else {
36028c2ecf20Sopenharmony_ci		/* disable an active IBSS if we are not on the home channel */
36038c2ecf20Sopenharmony_ci	}
36048c2ecf20Sopenharmony_ci}
36058c2ecf20Sopenharmony_ci
36068c2ecf20Sopenharmony_cistatic void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
36078c2ecf20Sopenharmony_ci				   u8 basic_rate)
36088c2ecf20Sopenharmony_ci{
36098c2ecf20Sopenharmony_ci	u8 phy_rate, index;
36108c2ecf20Sopenharmony_ci	u8 basic_phy_rate, basic_index;
36118c2ecf20Sopenharmony_ci	u16 dir_table, basic_table;
36128c2ecf20Sopenharmony_ci	u16 basic_ptr;
36138c2ecf20Sopenharmony_ci
36148c2ecf20Sopenharmony_ci	/* Shared memory address for the table we are reading */
36158c2ecf20Sopenharmony_ci	dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
36168c2ecf20Sopenharmony_ci
36178c2ecf20Sopenharmony_ci	/* Shared memory address for the table we are writing */
36188c2ecf20Sopenharmony_ci	basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
36198c2ecf20Sopenharmony_ci
36208c2ecf20Sopenharmony_ci	/*
36218c2ecf20Sopenharmony_ci	 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
36228c2ecf20Sopenharmony_ci	 * the index into the rate table.
36238c2ecf20Sopenharmony_ci	 */
36248c2ecf20Sopenharmony_ci	phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
36258c2ecf20Sopenharmony_ci	basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
36268c2ecf20Sopenharmony_ci	index = phy_rate & 0xf;
36278c2ecf20Sopenharmony_ci	basic_index = basic_phy_rate & 0xf;
36288c2ecf20Sopenharmony_ci
36298c2ecf20Sopenharmony_ci	/* Find the SHM pointer to the ACK rate entry by looking in the
36308c2ecf20Sopenharmony_ci	 * Direct-map Table
36318c2ecf20Sopenharmony_ci	 */
36328c2ecf20Sopenharmony_ci	basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
36338c2ecf20Sopenharmony_ci
36348c2ecf20Sopenharmony_ci	/* Update the SHM BSS-basic-rate-set mapping table with the pointer
36358c2ecf20Sopenharmony_ci	 * to the correct basic rate for the given incoming rate
36368c2ecf20Sopenharmony_ci	 */
36378c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
36388c2ecf20Sopenharmony_ci}
36398c2ecf20Sopenharmony_ci
36408c2ecf20Sopenharmony_cistatic const struct brcms_c_rateset *
36418c2ecf20Sopenharmony_cibrcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
36428c2ecf20Sopenharmony_ci{
36438c2ecf20Sopenharmony_ci	const struct brcms_c_rateset *rs_dflt;
36448c2ecf20Sopenharmony_ci
36458c2ecf20Sopenharmony_ci	if (BRCMS_PHY_11N_CAP(wlc->band)) {
36468c2ecf20Sopenharmony_ci		if (wlc->band->bandtype == BRCM_BAND_5G)
36478c2ecf20Sopenharmony_ci			rs_dflt = &ofdm_mimo_rates;
36488c2ecf20Sopenharmony_ci		else
36498c2ecf20Sopenharmony_ci			rs_dflt = &cck_ofdm_mimo_rates;
36508c2ecf20Sopenharmony_ci	} else if (wlc->band->gmode)
36518c2ecf20Sopenharmony_ci		rs_dflt = &cck_ofdm_rates;
36528c2ecf20Sopenharmony_ci	else
36538c2ecf20Sopenharmony_ci		rs_dflt = &cck_rates;
36548c2ecf20Sopenharmony_ci
36558c2ecf20Sopenharmony_ci	return rs_dflt;
36568c2ecf20Sopenharmony_ci}
36578c2ecf20Sopenharmony_ci
36588c2ecf20Sopenharmony_cistatic void brcms_c_set_ratetable(struct brcms_c_info *wlc)
36598c2ecf20Sopenharmony_ci{
36608c2ecf20Sopenharmony_ci	const struct brcms_c_rateset *rs_dflt;
36618c2ecf20Sopenharmony_ci	struct brcms_c_rateset rs;
36628c2ecf20Sopenharmony_ci	u8 rate, basic_rate;
36638c2ecf20Sopenharmony_ci	uint i;
36648c2ecf20Sopenharmony_ci
36658c2ecf20Sopenharmony_ci	rs_dflt = brcms_c_rateset_get_hwrs(wlc);
36668c2ecf20Sopenharmony_ci
36678c2ecf20Sopenharmony_ci	brcms_c_rateset_copy(rs_dflt, &rs);
36688c2ecf20Sopenharmony_ci	brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
36698c2ecf20Sopenharmony_ci
36708c2ecf20Sopenharmony_ci	/* walk the phy rate table and update SHM basic rate lookup table */
36718c2ecf20Sopenharmony_ci	for (i = 0; i < rs.count; i++) {
36728c2ecf20Sopenharmony_ci		rate = rs.rates[i] & BRCMS_RATE_MASK;
36738c2ecf20Sopenharmony_ci
36748c2ecf20Sopenharmony_ci		/* for a given rate brcms_basic_rate returns the rate at
36758c2ecf20Sopenharmony_ci		 * which a response ACK/CTS should be sent.
36768c2ecf20Sopenharmony_ci		 */
36778c2ecf20Sopenharmony_ci		basic_rate = brcms_basic_rate(wlc, rate);
36788c2ecf20Sopenharmony_ci		if (basic_rate == 0)
36798c2ecf20Sopenharmony_ci			/* This should only happen if we are using a
36808c2ecf20Sopenharmony_ci			 * restricted rateset.
36818c2ecf20Sopenharmony_ci			 */
36828c2ecf20Sopenharmony_ci			basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
36838c2ecf20Sopenharmony_ci
36848c2ecf20Sopenharmony_ci		brcms_c_write_rate_shm(wlc, rate, basic_rate);
36858c2ecf20Sopenharmony_ci	}
36868c2ecf20Sopenharmony_ci}
36878c2ecf20Sopenharmony_ci
36888c2ecf20Sopenharmony_ci/* band-specific init */
36898c2ecf20Sopenharmony_cistatic void brcms_c_bsinit(struct brcms_c_info *wlc)
36908c2ecf20Sopenharmony_ci{
36918c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
36928c2ecf20Sopenharmony_ci		       wlc->pub->unit, wlc->band->bandunit);
36938c2ecf20Sopenharmony_ci
36948c2ecf20Sopenharmony_ci	/* write ucode ACK/CTS rate table */
36958c2ecf20Sopenharmony_ci	brcms_c_set_ratetable(wlc);
36968c2ecf20Sopenharmony_ci
36978c2ecf20Sopenharmony_ci	/* update some band specific mac configuration */
36988c2ecf20Sopenharmony_ci	brcms_c_ucode_mac_upd(wlc);
36998c2ecf20Sopenharmony_ci
37008c2ecf20Sopenharmony_ci	/* init antenna selection */
37018c2ecf20Sopenharmony_ci	brcms_c_antsel_init(wlc->asi);
37028c2ecf20Sopenharmony_ci
37038c2ecf20Sopenharmony_ci}
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ci/* formula:  IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
37068c2ecf20Sopenharmony_cistatic int
37078c2ecf20Sopenharmony_cibrcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
37088c2ecf20Sopenharmony_ci		   bool writeToShm)
37098c2ecf20Sopenharmony_ci{
37108c2ecf20Sopenharmony_ci	int idle_busy_ratio_x_16 = 0;
37118c2ecf20Sopenharmony_ci	uint offset =
37128c2ecf20Sopenharmony_ci	    isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
37138c2ecf20Sopenharmony_ci	    M_TX_IDLE_BUSY_RATIO_X_16_CCK;
37148c2ecf20Sopenharmony_ci	if (duty_cycle > 100 || duty_cycle < 0) {
37158c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core,
37168c2ecf20Sopenharmony_ci			  "wl%d:  duty cycle value off limit\n",
37178c2ecf20Sopenharmony_ci			  wlc->pub->unit);
37188c2ecf20Sopenharmony_ci		return -EINVAL;
37198c2ecf20Sopenharmony_ci	}
37208c2ecf20Sopenharmony_ci	if (duty_cycle)
37218c2ecf20Sopenharmony_ci		idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
37228c2ecf20Sopenharmony_ci	/* Only write to shared memory  when wl is up */
37238c2ecf20Sopenharmony_ci	if (writeToShm)
37248c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
37258c2ecf20Sopenharmony_ci
37268c2ecf20Sopenharmony_ci	if (isOFDM)
37278c2ecf20Sopenharmony_ci		wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
37288c2ecf20Sopenharmony_ci	else
37298c2ecf20Sopenharmony_ci		wlc->tx_duty_cycle_cck = (u16) duty_cycle;
37308c2ecf20Sopenharmony_ci
37318c2ecf20Sopenharmony_ci	return 0;
37328c2ecf20Sopenharmony_ci}
37338c2ecf20Sopenharmony_ci
37348c2ecf20Sopenharmony_ci/* push sw hps and wake state through hardware */
37358c2ecf20Sopenharmony_cistatic void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
37368c2ecf20Sopenharmony_ci{
37378c2ecf20Sopenharmony_ci	u32 v1, v2;
37388c2ecf20Sopenharmony_ci	bool hps;
37398c2ecf20Sopenharmony_ci	bool awake_before;
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	hps = brcms_c_ps_allowed(wlc);
37428c2ecf20Sopenharmony_ci
37438c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
37448c2ecf20Sopenharmony_ci			   hps);
37458c2ecf20Sopenharmony_ci
37468c2ecf20Sopenharmony_ci	v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
37478c2ecf20Sopenharmony_ci	v2 = MCTL_WAKE;
37488c2ecf20Sopenharmony_ci	if (hps)
37498c2ecf20Sopenharmony_ci		v2 |= MCTL_HPS;
37508c2ecf20Sopenharmony_ci
37518c2ecf20Sopenharmony_ci	brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
37528c2ecf20Sopenharmony_ci
37538c2ecf20Sopenharmony_ci	awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
37548c2ecf20Sopenharmony_ci
37558c2ecf20Sopenharmony_ci	if (!awake_before)
37568c2ecf20Sopenharmony_ci		brcms_b_wait_for_wake(wlc->hw);
37578c2ecf20Sopenharmony_ci}
37588c2ecf20Sopenharmony_ci
37598c2ecf20Sopenharmony_ci/*
37608c2ecf20Sopenharmony_ci * Write this BSS config's MAC address to core.
37618c2ecf20Sopenharmony_ci * Updates RXE match engine.
37628c2ecf20Sopenharmony_ci */
37638c2ecf20Sopenharmony_cistatic void brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
37648c2ecf20Sopenharmony_ci{
37658c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = bsscfg->wlc;
37668c2ecf20Sopenharmony_ci
37678c2ecf20Sopenharmony_ci	/* enter the MAC addr into the RXE match registers */
37688c2ecf20Sopenharmony_ci	brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, wlc->pub->cur_etheraddr);
37698c2ecf20Sopenharmony_ci
37708c2ecf20Sopenharmony_ci	brcms_c_ampdu_macaddr_upd(wlc);
37718c2ecf20Sopenharmony_ci}
37728c2ecf20Sopenharmony_ci
37738c2ecf20Sopenharmony_ci/* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
37748c2ecf20Sopenharmony_ci * Updates RXE match engine.
37758c2ecf20Sopenharmony_ci */
37768c2ecf20Sopenharmony_cistatic void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
37778c2ecf20Sopenharmony_ci{
37788c2ecf20Sopenharmony_ci	/* we need to update BSSID in RXE match registers */
37798c2ecf20Sopenharmony_ci	brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
37808c2ecf20Sopenharmony_ci}
37818c2ecf20Sopenharmony_ci
37828c2ecf20Sopenharmony_civoid brcms_c_set_ssid(struct brcms_c_info *wlc, u8 *ssid, size_t ssid_len)
37838c2ecf20Sopenharmony_ci{
37848c2ecf20Sopenharmony_ci	u8 len = min_t(u8, sizeof(wlc->bsscfg->SSID), ssid_len);
37858c2ecf20Sopenharmony_ci	memset(wlc->bsscfg->SSID, 0, sizeof(wlc->bsscfg->SSID));
37868c2ecf20Sopenharmony_ci
37878c2ecf20Sopenharmony_ci	memcpy(wlc->bsscfg->SSID, ssid, len);
37888c2ecf20Sopenharmony_ci	wlc->bsscfg->SSID_len = len;
37898c2ecf20Sopenharmony_ci}
37908c2ecf20Sopenharmony_ci
37918c2ecf20Sopenharmony_cistatic void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
37928c2ecf20Sopenharmony_ci{
37938c2ecf20Sopenharmony_ci	wlc_hw->shortslot = shortslot;
37948c2ecf20Sopenharmony_ci
37958c2ecf20Sopenharmony_ci	if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
37968c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
37978c2ecf20Sopenharmony_ci		brcms_b_update_slot_timing(wlc_hw, shortslot);
37988c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc_hw->wlc);
37998c2ecf20Sopenharmony_ci	}
38008c2ecf20Sopenharmony_ci}
38018c2ecf20Sopenharmony_ci
38028c2ecf20Sopenharmony_ci/*
38038c2ecf20Sopenharmony_ci * Suspend the the MAC and update the slot timing
38048c2ecf20Sopenharmony_ci * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
38058c2ecf20Sopenharmony_ci */
38068c2ecf20Sopenharmony_cistatic void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
38078c2ecf20Sopenharmony_ci{
38088c2ecf20Sopenharmony_ci	/* use the override if it is set */
38098c2ecf20Sopenharmony_ci	if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
38108c2ecf20Sopenharmony_ci		shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
38118c2ecf20Sopenharmony_ci
38128c2ecf20Sopenharmony_ci	if (wlc->shortslot == shortslot)
38138c2ecf20Sopenharmony_ci		return;
38148c2ecf20Sopenharmony_ci
38158c2ecf20Sopenharmony_ci	wlc->shortslot = shortslot;
38168c2ecf20Sopenharmony_ci
38178c2ecf20Sopenharmony_ci	brcms_b_set_shortslot(wlc->hw, shortslot);
38188c2ecf20Sopenharmony_ci}
38198c2ecf20Sopenharmony_ci
38208c2ecf20Sopenharmony_cistatic void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
38218c2ecf20Sopenharmony_ci{
38228c2ecf20Sopenharmony_ci	if (wlc->home_chanspec != chanspec) {
38238c2ecf20Sopenharmony_ci		wlc->home_chanspec = chanspec;
38248c2ecf20Sopenharmony_ci
38258c2ecf20Sopenharmony_ci		if (wlc->pub->associated)
38268c2ecf20Sopenharmony_ci			wlc->bsscfg->current_bss->chanspec = chanspec;
38278c2ecf20Sopenharmony_ci	}
38288c2ecf20Sopenharmony_ci}
38298c2ecf20Sopenharmony_ci
38308c2ecf20Sopenharmony_civoid
38318c2ecf20Sopenharmony_cibrcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
38328c2ecf20Sopenharmony_ci		      bool mute_tx, struct txpwr_limits *txpwr)
38338c2ecf20Sopenharmony_ci{
38348c2ecf20Sopenharmony_ci	uint bandunit;
38358c2ecf20Sopenharmony_ci
38368c2ecf20Sopenharmony_ci	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
38378c2ecf20Sopenharmony_ci			   chanspec);
38388c2ecf20Sopenharmony_ci
38398c2ecf20Sopenharmony_ci	wlc_hw->chanspec = chanspec;
38408c2ecf20Sopenharmony_ci
38418c2ecf20Sopenharmony_ci	/* Switch bands if necessary */
38428c2ecf20Sopenharmony_ci	if (wlc_hw->_nbands > 1) {
38438c2ecf20Sopenharmony_ci		bandunit = chspec_bandunit(chanspec);
38448c2ecf20Sopenharmony_ci		if (wlc_hw->band->bandunit != bandunit) {
38458c2ecf20Sopenharmony_ci			/* brcms_b_setband disables other bandunit,
38468c2ecf20Sopenharmony_ci			 *  use light band switch if not up yet
38478c2ecf20Sopenharmony_ci			 */
38488c2ecf20Sopenharmony_ci			if (wlc_hw->up) {
38498c2ecf20Sopenharmony_ci				wlc_phy_chanspec_radio_set(wlc_hw->
38508c2ecf20Sopenharmony_ci							   bandstate[bandunit]->
38518c2ecf20Sopenharmony_ci							   pi, chanspec);
38528c2ecf20Sopenharmony_ci				brcms_b_setband(wlc_hw, bandunit, chanspec);
38538c2ecf20Sopenharmony_ci			} else {
38548c2ecf20Sopenharmony_ci				brcms_c_setxband(wlc_hw, bandunit);
38558c2ecf20Sopenharmony_ci			}
38568c2ecf20Sopenharmony_ci		}
38578c2ecf20Sopenharmony_ci	}
38588c2ecf20Sopenharmony_ci
38598c2ecf20Sopenharmony_ci	wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
38608c2ecf20Sopenharmony_ci
38618c2ecf20Sopenharmony_ci	if (!wlc_hw->up) {
38628c2ecf20Sopenharmony_ci		if (wlc_hw->clk)
38638c2ecf20Sopenharmony_ci			wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
38648c2ecf20Sopenharmony_ci						  chanspec);
38658c2ecf20Sopenharmony_ci		wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
38668c2ecf20Sopenharmony_ci	} else {
38678c2ecf20Sopenharmony_ci		wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
38688c2ecf20Sopenharmony_ci		wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
38698c2ecf20Sopenharmony_ci
38708c2ecf20Sopenharmony_ci		/* Update muting of the channel */
38718c2ecf20Sopenharmony_ci		brcms_b_mute(wlc_hw, mute_tx);
38728c2ecf20Sopenharmony_ci	}
38738c2ecf20Sopenharmony_ci}
38748c2ecf20Sopenharmony_ci
38758c2ecf20Sopenharmony_ci/* switch to and initialize new band */
38768c2ecf20Sopenharmony_cistatic void brcms_c_setband(struct brcms_c_info *wlc,
38778c2ecf20Sopenharmony_ci					   uint bandunit)
38788c2ecf20Sopenharmony_ci{
38798c2ecf20Sopenharmony_ci	wlc->band = wlc->bandstate[bandunit];
38808c2ecf20Sopenharmony_ci
38818c2ecf20Sopenharmony_ci	if (!wlc->pub->up)
38828c2ecf20Sopenharmony_ci		return;
38838c2ecf20Sopenharmony_ci
38848c2ecf20Sopenharmony_ci	/* wait for at least one beacon before entering sleeping state */
38858c2ecf20Sopenharmony_ci	brcms_c_set_ps_ctrl(wlc);
38868c2ecf20Sopenharmony_ci
38878c2ecf20Sopenharmony_ci	/* band-specific initializations */
38888c2ecf20Sopenharmony_ci	brcms_c_bsinit(wlc);
38898c2ecf20Sopenharmony_ci}
38908c2ecf20Sopenharmony_ci
38918c2ecf20Sopenharmony_cistatic void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
38928c2ecf20Sopenharmony_ci{
38938c2ecf20Sopenharmony_ci	uint bandunit;
38948c2ecf20Sopenharmony_ci	u16 old_chanspec = wlc->chanspec;
38958c2ecf20Sopenharmony_ci
38968c2ecf20Sopenharmony_ci	if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
38978c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
38988c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
38998c2ecf20Sopenharmony_ci		return;
39008c2ecf20Sopenharmony_ci	}
39018c2ecf20Sopenharmony_ci
39028c2ecf20Sopenharmony_ci	/* Switch bands if necessary */
39038c2ecf20Sopenharmony_ci	if (wlc->pub->_nbands > 1) {
39048c2ecf20Sopenharmony_ci		bandunit = chspec_bandunit(chanspec);
39058c2ecf20Sopenharmony_ci		if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
39068c2ecf20Sopenharmony_ci			if (wlc->bandlocked) {
39078c2ecf20Sopenharmony_ci				brcms_err(wlc->hw->d11core,
39088c2ecf20Sopenharmony_ci					  "wl%d: %s: chspec %d band is locked!\n",
39098c2ecf20Sopenharmony_ci					  wlc->pub->unit, __func__,
39108c2ecf20Sopenharmony_ci					  CHSPEC_CHANNEL(chanspec));
39118c2ecf20Sopenharmony_ci				return;
39128c2ecf20Sopenharmony_ci			}
39138c2ecf20Sopenharmony_ci			/*
39148c2ecf20Sopenharmony_ci			 * should the setband call come after the
39158c2ecf20Sopenharmony_ci			 * brcms_b_chanspec() ? if the setband updates
39168c2ecf20Sopenharmony_ci			 * (brcms_c_bsinit) use low level calls to inspect and
39178c2ecf20Sopenharmony_ci			 * set state, the state inspected may be from the wrong
39188c2ecf20Sopenharmony_ci			 * band, or the following brcms_b_set_chanspec() may
39198c2ecf20Sopenharmony_ci			 * undo the work.
39208c2ecf20Sopenharmony_ci			 */
39218c2ecf20Sopenharmony_ci			brcms_c_setband(wlc, bandunit);
39228c2ecf20Sopenharmony_ci		}
39238c2ecf20Sopenharmony_ci	}
39248c2ecf20Sopenharmony_ci
39258c2ecf20Sopenharmony_ci	/* sync up phy/radio chanspec */
39268c2ecf20Sopenharmony_ci	brcms_c_set_phy_chanspec(wlc, chanspec);
39278c2ecf20Sopenharmony_ci
39288c2ecf20Sopenharmony_ci	/* init antenna selection */
39298c2ecf20Sopenharmony_ci	if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
39308c2ecf20Sopenharmony_ci		brcms_c_antsel_init(wlc->asi);
39318c2ecf20Sopenharmony_ci
39328c2ecf20Sopenharmony_ci		/* Fix the hardware rateset based on bw.
39338c2ecf20Sopenharmony_ci		 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
39348c2ecf20Sopenharmony_ci		 */
39358c2ecf20Sopenharmony_ci		brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
39368c2ecf20Sopenharmony_ci			wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
39378c2ecf20Sopenharmony_ci	}
39388c2ecf20Sopenharmony_ci
39398c2ecf20Sopenharmony_ci	/* update some mac configuration since chanspec changed */
39408c2ecf20Sopenharmony_ci	brcms_c_ucode_mac_upd(wlc);
39418c2ecf20Sopenharmony_ci}
39428c2ecf20Sopenharmony_ci
39438c2ecf20Sopenharmony_ci/*
39448c2ecf20Sopenharmony_ci * This function changes the phytxctl for beacon based on current
39458c2ecf20Sopenharmony_ci * beacon ratespec AND txant setting as per this table:
39468c2ecf20Sopenharmony_ci *  ratespec     CCK		ant = wlc->stf->txant
39478c2ecf20Sopenharmony_ci *		OFDM		ant = 3
39488c2ecf20Sopenharmony_ci */
39498c2ecf20Sopenharmony_civoid brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
39508c2ecf20Sopenharmony_ci				       u32 bcn_rspec)
39518c2ecf20Sopenharmony_ci{
39528c2ecf20Sopenharmony_ci	u16 phyctl;
39538c2ecf20Sopenharmony_ci	u16 phytxant = wlc->stf->phytxant;
39548c2ecf20Sopenharmony_ci	u16 mask = PHY_TXC_ANT_MASK;
39558c2ecf20Sopenharmony_ci
39568c2ecf20Sopenharmony_ci	/* for non-siso rates or default setting, use the available chains */
39578c2ecf20Sopenharmony_ci	if (BRCMS_PHY_11N_CAP(wlc->band))
39588c2ecf20Sopenharmony_ci		phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
39598c2ecf20Sopenharmony_ci
39608c2ecf20Sopenharmony_ci	phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
39618c2ecf20Sopenharmony_ci	phyctl = (phyctl & ~mask) | phytxant;
39628c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
39638c2ecf20Sopenharmony_ci}
39648c2ecf20Sopenharmony_ci
39658c2ecf20Sopenharmony_ci/*
39668c2ecf20Sopenharmony_ci * centralized protection config change function to simplify debugging, no
39678c2ecf20Sopenharmony_ci * consistency checking this should be called only on changes to avoid overhead
39688c2ecf20Sopenharmony_ci * in periodic function
39698c2ecf20Sopenharmony_ci */
39708c2ecf20Sopenharmony_civoid brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
39718c2ecf20Sopenharmony_ci{
39728c2ecf20Sopenharmony_ci	/*
39738c2ecf20Sopenharmony_ci	 * Cannot use brcms_dbg_* here because this function is called
39748c2ecf20Sopenharmony_ci	 * before wlc is sufficiently initialized.
39758c2ecf20Sopenharmony_ci	 */
39768c2ecf20Sopenharmony_ci	BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
39778c2ecf20Sopenharmony_ci
39788c2ecf20Sopenharmony_ci	switch (idx) {
39798c2ecf20Sopenharmony_ci	case BRCMS_PROT_G_SPEC:
39808c2ecf20Sopenharmony_ci		wlc->protection->_g = (bool) val;
39818c2ecf20Sopenharmony_ci		break;
39828c2ecf20Sopenharmony_ci	case BRCMS_PROT_G_OVR:
39838c2ecf20Sopenharmony_ci		wlc->protection->g_override = (s8) val;
39848c2ecf20Sopenharmony_ci		break;
39858c2ecf20Sopenharmony_ci	case BRCMS_PROT_G_USER:
39868c2ecf20Sopenharmony_ci		wlc->protection->gmode_user = (u8) val;
39878c2ecf20Sopenharmony_ci		break;
39888c2ecf20Sopenharmony_ci	case BRCMS_PROT_OVERLAP:
39898c2ecf20Sopenharmony_ci		wlc->protection->overlap = (s8) val;
39908c2ecf20Sopenharmony_ci		break;
39918c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_USER:
39928c2ecf20Sopenharmony_ci		wlc->protection->nmode_user = (s8) val;
39938c2ecf20Sopenharmony_ci		break;
39948c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_CFG:
39958c2ecf20Sopenharmony_ci		wlc->protection->n_cfg = (s8) val;
39968c2ecf20Sopenharmony_ci		break;
39978c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_CFG_OVR:
39988c2ecf20Sopenharmony_ci		wlc->protection->n_cfg_override = (s8) val;
39998c2ecf20Sopenharmony_ci		break;
40008c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_NONGF:
40018c2ecf20Sopenharmony_ci		wlc->protection->nongf = (bool) val;
40028c2ecf20Sopenharmony_ci		break;
40038c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_NONGF_OVR:
40048c2ecf20Sopenharmony_ci		wlc->protection->nongf_override = (s8) val;
40058c2ecf20Sopenharmony_ci		break;
40068c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_PAM_OVR:
40078c2ecf20Sopenharmony_ci		wlc->protection->n_pam_override = (s8) val;
40088c2ecf20Sopenharmony_ci		break;
40098c2ecf20Sopenharmony_ci	case BRCMS_PROT_N_OBSS:
40108c2ecf20Sopenharmony_ci		wlc->protection->n_obss = (bool) val;
40118c2ecf20Sopenharmony_ci		break;
40128c2ecf20Sopenharmony_ci
40138c2ecf20Sopenharmony_ci	default:
40148c2ecf20Sopenharmony_ci		break;
40158c2ecf20Sopenharmony_ci	}
40168c2ecf20Sopenharmony_ci
40178c2ecf20Sopenharmony_ci}
40188c2ecf20Sopenharmony_ci
40198c2ecf20Sopenharmony_cistatic void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
40208c2ecf20Sopenharmony_ci{
40218c2ecf20Sopenharmony_ci	if (wlc->pub->up) {
40228c2ecf20Sopenharmony_ci		brcms_c_update_beacon(wlc);
40238c2ecf20Sopenharmony_ci		brcms_c_update_probe_resp(wlc, true);
40248c2ecf20Sopenharmony_ci	}
40258c2ecf20Sopenharmony_ci}
40268c2ecf20Sopenharmony_ci
40278c2ecf20Sopenharmony_cistatic void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
40288c2ecf20Sopenharmony_ci{
40298c2ecf20Sopenharmony_ci	wlc->stf->ldpc = val;
40308c2ecf20Sopenharmony_ci
40318c2ecf20Sopenharmony_ci	if (wlc->pub->up) {
40328c2ecf20Sopenharmony_ci		brcms_c_update_beacon(wlc);
40338c2ecf20Sopenharmony_ci		brcms_c_update_probe_resp(wlc, true);
40348c2ecf20Sopenharmony_ci		wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
40358c2ecf20Sopenharmony_ci	}
40368c2ecf20Sopenharmony_ci}
40378c2ecf20Sopenharmony_ci
40388c2ecf20Sopenharmony_civoid brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
40398c2ecf20Sopenharmony_ci		       const struct ieee80211_tx_queue_params *params,
40408c2ecf20Sopenharmony_ci		       bool suspend)
40418c2ecf20Sopenharmony_ci{
40428c2ecf20Sopenharmony_ci	int i;
40438c2ecf20Sopenharmony_ci	struct shm_acparams acp_shm;
40448c2ecf20Sopenharmony_ci	u16 *shm_entry;
40458c2ecf20Sopenharmony_ci
40468c2ecf20Sopenharmony_ci	/* Only apply params if the core is out of reset and has clocks */
40478c2ecf20Sopenharmony_ci	if (!wlc->clk) {
40488c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
40498c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__);
40508c2ecf20Sopenharmony_ci		return;
40518c2ecf20Sopenharmony_ci	}
40528c2ecf20Sopenharmony_ci
40538c2ecf20Sopenharmony_ci	memset(&acp_shm, 0, sizeof(struct shm_acparams));
40548c2ecf20Sopenharmony_ci	/* fill in shm ac params struct */
40558c2ecf20Sopenharmony_ci	acp_shm.txop = params->txop;
40568c2ecf20Sopenharmony_ci	/* convert from units of 32us to us for ucode */
40578c2ecf20Sopenharmony_ci	wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
40588c2ecf20Sopenharmony_ci	    EDCF_TXOP2USEC(acp_shm.txop);
40598c2ecf20Sopenharmony_ci	acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
40608c2ecf20Sopenharmony_ci
40618c2ecf20Sopenharmony_ci	if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
40628c2ecf20Sopenharmony_ci	    && acp_shm.aifs < EDCF_AIFSN_MAX)
40638c2ecf20Sopenharmony_ci		acp_shm.aifs++;
40648c2ecf20Sopenharmony_ci
40658c2ecf20Sopenharmony_ci	if (acp_shm.aifs < EDCF_AIFSN_MIN
40668c2ecf20Sopenharmony_ci	    || acp_shm.aifs > EDCF_AIFSN_MAX) {
40678c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
40688c2ecf20Sopenharmony_ci			  "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
40698c2ecf20Sopenharmony_ci	} else {
40708c2ecf20Sopenharmony_ci		acp_shm.cwmin = params->cw_min;
40718c2ecf20Sopenharmony_ci		acp_shm.cwmax = params->cw_max;
40728c2ecf20Sopenharmony_ci		acp_shm.cwcur = acp_shm.cwmin;
40738c2ecf20Sopenharmony_ci		acp_shm.bslots =
40748c2ecf20Sopenharmony_ci			bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
40758c2ecf20Sopenharmony_ci			acp_shm.cwcur;
40768c2ecf20Sopenharmony_ci		acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
40778c2ecf20Sopenharmony_ci		/* Indicate the new params to the ucode */
40788c2ecf20Sopenharmony_ci		acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
40798c2ecf20Sopenharmony_ci						  wme_ac2fifo[aci] *
40808c2ecf20Sopenharmony_ci						  M_EDCF_QLEN +
40818c2ecf20Sopenharmony_ci						  M_EDCF_STATUS_OFF));
40828c2ecf20Sopenharmony_ci		acp_shm.status |= WME_STATUS_NEWAC;
40838c2ecf20Sopenharmony_ci
40848c2ecf20Sopenharmony_ci		/* Fill in shm acparam table */
40858c2ecf20Sopenharmony_ci		shm_entry = (u16 *) &acp_shm;
40868c2ecf20Sopenharmony_ci		for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
40878c2ecf20Sopenharmony_ci			brcms_b_write_shm(wlc->hw,
40888c2ecf20Sopenharmony_ci					  M_EDCF_QINFO +
40898c2ecf20Sopenharmony_ci					  wme_ac2fifo[aci] * M_EDCF_QLEN + i,
40908c2ecf20Sopenharmony_ci					  *shm_entry++);
40918c2ecf20Sopenharmony_ci	}
40928c2ecf20Sopenharmony_ci
40938c2ecf20Sopenharmony_ci	if (suspend)
40948c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc);
40958c2ecf20Sopenharmony_ci
40968c2ecf20Sopenharmony_ci	brcms_c_update_beacon(wlc);
40978c2ecf20Sopenharmony_ci	brcms_c_update_probe_resp(wlc, false);
40988c2ecf20Sopenharmony_ci
40998c2ecf20Sopenharmony_ci	if (suspend)
41008c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc);
41018c2ecf20Sopenharmony_ci}
41028c2ecf20Sopenharmony_ci
41038c2ecf20Sopenharmony_cistatic void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
41048c2ecf20Sopenharmony_ci{
41058c2ecf20Sopenharmony_ci	u16 aci;
41068c2ecf20Sopenharmony_ci	int i_ac;
41078c2ecf20Sopenharmony_ci	struct ieee80211_tx_queue_params txq_pars;
41088c2ecf20Sopenharmony_ci	static const struct edcf_acparam default_edcf_acparams[] = {
41098c2ecf20Sopenharmony_ci		 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
41108c2ecf20Sopenharmony_ci		 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
41118c2ecf20Sopenharmony_ci		 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
41128c2ecf20Sopenharmony_ci		 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
41138c2ecf20Sopenharmony_ci	}; /* ucode needs these parameters during its initialization */
41148c2ecf20Sopenharmony_ci	const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
41158c2ecf20Sopenharmony_ci
41168c2ecf20Sopenharmony_ci	for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
41178c2ecf20Sopenharmony_ci		/* find out which ac this set of params applies to */
41188c2ecf20Sopenharmony_ci		aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
41198c2ecf20Sopenharmony_ci
41208c2ecf20Sopenharmony_ci		/* fill in shm ac params struct */
41218c2ecf20Sopenharmony_ci		txq_pars.txop = edcf_acp->TXOP;
41228c2ecf20Sopenharmony_ci		txq_pars.aifs = edcf_acp->ACI;
41238c2ecf20Sopenharmony_ci
41248c2ecf20Sopenharmony_ci		/* CWmin = 2^(ECWmin) - 1 */
41258c2ecf20Sopenharmony_ci		txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
41268c2ecf20Sopenharmony_ci		/* CWmax = 2^(ECWmax) - 1 */
41278c2ecf20Sopenharmony_ci		txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
41288c2ecf20Sopenharmony_ci					    >> EDCF_ECWMAX_SHIFT);
41298c2ecf20Sopenharmony_ci		brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
41308c2ecf20Sopenharmony_ci	}
41318c2ecf20Sopenharmony_ci
41328c2ecf20Sopenharmony_ci	if (suspend) {
41338c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc);
41348c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc);
41358c2ecf20Sopenharmony_ci	}
41368c2ecf20Sopenharmony_ci}
41378c2ecf20Sopenharmony_ci
41388c2ecf20Sopenharmony_cistatic void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
41398c2ecf20Sopenharmony_ci{
41408c2ecf20Sopenharmony_ci	/* Don't start the timer if HWRADIO feature is disabled */
41418c2ecf20Sopenharmony_ci	if (wlc->radio_monitor)
41428c2ecf20Sopenharmony_ci		return;
41438c2ecf20Sopenharmony_ci
41448c2ecf20Sopenharmony_ci	wlc->radio_monitor = true;
41458c2ecf20Sopenharmony_ci	brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
41468c2ecf20Sopenharmony_ci	brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
41478c2ecf20Sopenharmony_ci}
41488c2ecf20Sopenharmony_ci
41498c2ecf20Sopenharmony_cistatic bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
41508c2ecf20Sopenharmony_ci{
41518c2ecf20Sopenharmony_ci	if (!wlc->radio_monitor)
41528c2ecf20Sopenharmony_ci		return true;
41538c2ecf20Sopenharmony_ci
41548c2ecf20Sopenharmony_ci	wlc->radio_monitor = false;
41558c2ecf20Sopenharmony_ci	brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
41568c2ecf20Sopenharmony_ci	return brcms_del_timer(wlc->radio_timer);
41578c2ecf20Sopenharmony_ci}
41588c2ecf20Sopenharmony_ci
41598c2ecf20Sopenharmony_ci/* read hwdisable state and propagate to wlc flag */
41608c2ecf20Sopenharmony_cistatic void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
41618c2ecf20Sopenharmony_ci{
41628c2ecf20Sopenharmony_ci	if (wlc->pub->hw_off)
41638c2ecf20Sopenharmony_ci		return;
41648c2ecf20Sopenharmony_ci
41658c2ecf20Sopenharmony_ci	if (brcms_b_radio_read_hwdisabled(wlc->hw))
41668c2ecf20Sopenharmony_ci		mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
41678c2ecf20Sopenharmony_ci	else
41688c2ecf20Sopenharmony_ci		mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
41698c2ecf20Sopenharmony_ci}
41708c2ecf20Sopenharmony_ci
41718c2ecf20Sopenharmony_ci/* update hwradio status and return it */
41728c2ecf20Sopenharmony_cibool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
41738c2ecf20Sopenharmony_ci{
41748c2ecf20Sopenharmony_ci	brcms_c_radio_hwdisable_upd(wlc);
41758c2ecf20Sopenharmony_ci
41768c2ecf20Sopenharmony_ci	return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
41778c2ecf20Sopenharmony_ci			true : false;
41788c2ecf20Sopenharmony_ci}
41798c2ecf20Sopenharmony_ci
41808c2ecf20Sopenharmony_ci/* periodical query hw radio button while driver is "down" */
41818c2ecf20Sopenharmony_cistatic void brcms_c_radio_timer(void *arg)
41828c2ecf20Sopenharmony_ci{
41838c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
41848c2ecf20Sopenharmony_ci
41858c2ecf20Sopenharmony_ci	if (brcms_deviceremoved(wlc)) {
41868c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
41878c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__);
41888c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
41898c2ecf20Sopenharmony_ci		return;
41908c2ecf20Sopenharmony_ci	}
41918c2ecf20Sopenharmony_ci
41928c2ecf20Sopenharmony_ci	brcms_c_radio_hwdisable_upd(wlc);
41938c2ecf20Sopenharmony_ci}
41948c2ecf20Sopenharmony_ci
41958c2ecf20Sopenharmony_ci/* common low-level watchdog code */
41968c2ecf20Sopenharmony_cistatic void brcms_b_watchdog(struct brcms_c_info *wlc)
41978c2ecf20Sopenharmony_ci{
41988c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
41998c2ecf20Sopenharmony_ci
42008c2ecf20Sopenharmony_ci	if (!wlc_hw->up)
42018c2ecf20Sopenharmony_ci		return;
42028c2ecf20Sopenharmony_ci
42038c2ecf20Sopenharmony_ci	/* increment second count */
42048c2ecf20Sopenharmony_ci	wlc_hw->now++;
42058c2ecf20Sopenharmony_ci
42068c2ecf20Sopenharmony_ci	/* Check for FIFO error interrupts */
42078c2ecf20Sopenharmony_ci	brcms_b_fifoerrors(wlc_hw);
42088c2ecf20Sopenharmony_ci
42098c2ecf20Sopenharmony_ci	/* make sure RX dma has buffers */
42108c2ecf20Sopenharmony_ci	dma_rxfill(wlc->hw->di[RX_FIFO]);
42118c2ecf20Sopenharmony_ci
42128c2ecf20Sopenharmony_ci	wlc_phy_watchdog(wlc_hw->band->pi);
42138c2ecf20Sopenharmony_ci}
42148c2ecf20Sopenharmony_ci
42158c2ecf20Sopenharmony_ci/* common watchdog code */
42168c2ecf20Sopenharmony_cistatic void brcms_c_watchdog(struct brcms_c_info *wlc)
42178c2ecf20Sopenharmony_ci{
42188c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
42198c2ecf20Sopenharmony_ci
42208c2ecf20Sopenharmony_ci	if (!wlc->pub->up)
42218c2ecf20Sopenharmony_ci		return;
42228c2ecf20Sopenharmony_ci
42238c2ecf20Sopenharmony_ci	if (brcms_deviceremoved(wlc)) {
42248c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
42258c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__);
42268c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
42278c2ecf20Sopenharmony_ci		return;
42288c2ecf20Sopenharmony_ci	}
42298c2ecf20Sopenharmony_ci
42308c2ecf20Sopenharmony_ci	/* increment second count */
42318c2ecf20Sopenharmony_ci	wlc->pub->now++;
42328c2ecf20Sopenharmony_ci
42338c2ecf20Sopenharmony_ci	brcms_c_radio_hwdisable_upd(wlc);
42348c2ecf20Sopenharmony_ci	/* if radio is disable, driver may be down, quit here */
42358c2ecf20Sopenharmony_ci	if (wlc->pub->radio_disabled)
42368c2ecf20Sopenharmony_ci		return;
42378c2ecf20Sopenharmony_ci
42388c2ecf20Sopenharmony_ci	brcms_b_watchdog(wlc);
42398c2ecf20Sopenharmony_ci
42408c2ecf20Sopenharmony_ci	/*
42418c2ecf20Sopenharmony_ci	 * occasionally sample mac stat counters to
42428c2ecf20Sopenharmony_ci	 * detect 16-bit counter wrap
42438c2ecf20Sopenharmony_ci	 */
42448c2ecf20Sopenharmony_ci	if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
42458c2ecf20Sopenharmony_ci		brcms_c_statsupd(wlc);
42468c2ecf20Sopenharmony_ci
42478c2ecf20Sopenharmony_ci	if (BRCMS_ISNPHY(wlc->band) &&
42488c2ecf20Sopenharmony_ci	    ((wlc->pub->now - wlc->tempsense_lasttime) >=
42498c2ecf20Sopenharmony_ci	     BRCMS_TEMPSENSE_PERIOD)) {
42508c2ecf20Sopenharmony_ci		wlc->tempsense_lasttime = wlc->pub->now;
42518c2ecf20Sopenharmony_ci		brcms_c_tempsense_upd(wlc);
42528c2ecf20Sopenharmony_ci	}
42538c2ecf20Sopenharmony_ci}
42548c2ecf20Sopenharmony_ci
42558c2ecf20Sopenharmony_cistatic void brcms_c_watchdog_by_timer(void *arg)
42568c2ecf20Sopenharmony_ci{
42578c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
42588c2ecf20Sopenharmony_ci
42598c2ecf20Sopenharmony_ci	brcms_c_watchdog(wlc);
42608c2ecf20Sopenharmony_ci}
42618c2ecf20Sopenharmony_ci
42628c2ecf20Sopenharmony_cistatic bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
42638c2ecf20Sopenharmony_ci{
42648c2ecf20Sopenharmony_ci	wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
42658c2ecf20Sopenharmony_ci		wlc, "watchdog");
42668c2ecf20Sopenharmony_ci	if (!wlc->wdtimer) {
42678c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for wdtimer "
42688c2ecf20Sopenharmony_ci			  "failed\n", unit);
42698c2ecf20Sopenharmony_ci		goto fail;
42708c2ecf20Sopenharmony_ci	}
42718c2ecf20Sopenharmony_ci
42728c2ecf20Sopenharmony_ci	wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
42738c2ecf20Sopenharmony_ci		wlc, "radio");
42748c2ecf20Sopenharmony_ci	if (!wlc->radio_timer) {
42758c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for radio_timer "
42768c2ecf20Sopenharmony_ci			  "failed\n", unit);
42778c2ecf20Sopenharmony_ci		goto fail;
42788c2ecf20Sopenharmony_ci	}
42798c2ecf20Sopenharmony_ci
42808c2ecf20Sopenharmony_ci	return true;
42818c2ecf20Sopenharmony_ci
42828c2ecf20Sopenharmony_ci fail:
42838c2ecf20Sopenharmony_ci	return false;
42848c2ecf20Sopenharmony_ci}
42858c2ecf20Sopenharmony_ci
42868c2ecf20Sopenharmony_ci/*
42878c2ecf20Sopenharmony_ci * Initialize brcms_c_info default values ...
42888c2ecf20Sopenharmony_ci * may get overrides later in this function
42898c2ecf20Sopenharmony_ci */
42908c2ecf20Sopenharmony_cistatic void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
42918c2ecf20Sopenharmony_ci{
42928c2ecf20Sopenharmony_ci	int i;
42938c2ecf20Sopenharmony_ci
42948c2ecf20Sopenharmony_ci	/* Save our copy of the chanspec */
42958c2ecf20Sopenharmony_ci	wlc->chanspec = ch20mhz_chspec(1);
42968c2ecf20Sopenharmony_ci
42978c2ecf20Sopenharmony_ci	/* various 802.11g modes */
42988c2ecf20Sopenharmony_ci	wlc->shortslot = false;
42998c2ecf20Sopenharmony_ci	wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
43008c2ecf20Sopenharmony_ci
43018c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
43028c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
43038c2ecf20Sopenharmony_ci
43048c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
43058c2ecf20Sopenharmony_ci			       BRCMS_PROTECTION_AUTO);
43068c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
43078c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
43088c2ecf20Sopenharmony_ci			       BRCMS_PROTECTION_AUTO);
43098c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
43108c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
43118c2ecf20Sopenharmony_ci
43128c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
43138c2ecf20Sopenharmony_ci			       BRCMS_PROTECTION_CTL_OVERLAP);
43148c2ecf20Sopenharmony_ci
43158c2ecf20Sopenharmony_ci	/* 802.11g draft 4.0 NonERP elt advertisement */
43168c2ecf20Sopenharmony_ci	wlc->include_legacy_erp = true;
43178c2ecf20Sopenharmony_ci
43188c2ecf20Sopenharmony_ci	wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
43198c2ecf20Sopenharmony_ci	wlc->stf->txant = ANT_TX_DEF;
43208c2ecf20Sopenharmony_ci
43218c2ecf20Sopenharmony_ci	wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
43228c2ecf20Sopenharmony_ci
43238c2ecf20Sopenharmony_ci	wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
43248c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++)
43258c2ecf20Sopenharmony_ci		wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
43268c2ecf20Sopenharmony_ci	wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
43278c2ecf20Sopenharmony_ci
43288c2ecf20Sopenharmony_ci	/* default rate fallback retry limits */
43298c2ecf20Sopenharmony_ci	wlc->SFBL = RETRY_SHORT_FB;
43308c2ecf20Sopenharmony_ci	wlc->LFBL = RETRY_LONG_FB;
43318c2ecf20Sopenharmony_ci
43328c2ecf20Sopenharmony_ci	/* default mac retry limits */
43338c2ecf20Sopenharmony_ci	wlc->SRL = RETRY_SHORT_DEF;
43348c2ecf20Sopenharmony_ci	wlc->LRL = RETRY_LONG_DEF;
43358c2ecf20Sopenharmony_ci
43368c2ecf20Sopenharmony_ci	/* WME QoS mode is Auto by default */
43378c2ecf20Sopenharmony_ci	wlc->pub->_ampdu = AMPDU_AGG_HOST;
43388c2ecf20Sopenharmony_ci}
43398c2ecf20Sopenharmony_ci
43408c2ecf20Sopenharmony_cistatic uint brcms_c_attach_module(struct brcms_c_info *wlc)
43418c2ecf20Sopenharmony_ci{
43428c2ecf20Sopenharmony_ci	uint err = 0;
43438c2ecf20Sopenharmony_ci	uint unit;
43448c2ecf20Sopenharmony_ci	unit = wlc->pub->unit;
43458c2ecf20Sopenharmony_ci
43468c2ecf20Sopenharmony_ci	wlc->asi = brcms_c_antsel_attach(wlc);
43478c2ecf20Sopenharmony_ci	if (wlc->asi == NULL) {
43488c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
43498c2ecf20Sopenharmony_ci			  "failed\n", unit);
43508c2ecf20Sopenharmony_ci		err = 44;
43518c2ecf20Sopenharmony_ci		goto fail;
43528c2ecf20Sopenharmony_ci	}
43538c2ecf20Sopenharmony_ci
43548c2ecf20Sopenharmony_ci	wlc->ampdu = brcms_c_ampdu_attach(wlc);
43558c2ecf20Sopenharmony_ci	if (wlc->ampdu == NULL) {
43568c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
43578c2ecf20Sopenharmony_ci			  "failed\n", unit);
43588c2ecf20Sopenharmony_ci		err = 50;
43598c2ecf20Sopenharmony_ci		goto fail;
43608c2ecf20Sopenharmony_ci	}
43618c2ecf20Sopenharmony_ci
43628c2ecf20Sopenharmony_ci	if ((brcms_c_stf_attach(wlc) != 0)) {
43638c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
43648c2ecf20Sopenharmony_ci			  "failed\n", unit);
43658c2ecf20Sopenharmony_ci		err = 68;
43668c2ecf20Sopenharmony_ci		goto fail;
43678c2ecf20Sopenharmony_ci	}
43688c2ecf20Sopenharmony_ci fail:
43698c2ecf20Sopenharmony_ci	return err;
43708c2ecf20Sopenharmony_ci}
43718c2ecf20Sopenharmony_ci
43728c2ecf20Sopenharmony_cistruct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
43738c2ecf20Sopenharmony_ci{
43748c2ecf20Sopenharmony_ci	return wlc->pub;
43758c2ecf20Sopenharmony_ci}
43768c2ecf20Sopenharmony_ci
43778c2ecf20Sopenharmony_ci/* low level attach
43788c2ecf20Sopenharmony_ci *    run backplane attach, init nvram
43798c2ecf20Sopenharmony_ci *    run phy attach
43808c2ecf20Sopenharmony_ci *    initialize software state for each core and band
43818c2ecf20Sopenharmony_ci *    put the whole chip in reset(driver down state), no clock
43828c2ecf20Sopenharmony_ci */
43838c2ecf20Sopenharmony_cistatic int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
43848c2ecf20Sopenharmony_ci			  uint unit, bool piomode)
43858c2ecf20Sopenharmony_ci{
43868c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw;
43878c2ecf20Sopenharmony_ci	uint err = 0;
43888c2ecf20Sopenharmony_ci	uint j;
43898c2ecf20Sopenharmony_ci	bool wme = false;
43908c2ecf20Sopenharmony_ci	struct shared_phy_params sha_params;
43918c2ecf20Sopenharmony_ci	struct wiphy *wiphy = wlc->wiphy;
43928c2ecf20Sopenharmony_ci	struct pci_dev *pcidev = core->bus->host_pci;
43938c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = &core->bus->sprom;
43948c2ecf20Sopenharmony_ci
43958c2ecf20Sopenharmony_ci	if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
43968c2ecf20Sopenharmony_ci		brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
43978c2ecf20Sopenharmony_ci			       pcidev->vendor,
43988c2ecf20Sopenharmony_ci			       pcidev->device);
43998c2ecf20Sopenharmony_ci	else
44008c2ecf20Sopenharmony_ci		brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
44018c2ecf20Sopenharmony_ci			       core->bus->boardinfo.vendor,
44028c2ecf20Sopenharmony_ci			       core->bus->boardinfo.type);
44038c2ecf20Sopenharmony_ci
44048c2ecf20Sopenharmony_ci	wme = true;
44058c2ecf20Sopenharmony_ci
44068c2ecf20Sopenharmony_ci	wlc_hw = wlc->hw;
44078c2ecf20Sopenharmony_ci	wlc_hw->wlc = wlc;
44088c2ecf20Sopenharmony_ci	wlc_hw->unit = unit;
44098c2ecf20Sopenharmony_ci	wlc_hw->band = wlc_hw->bandstate[0];
44108c2ecf20Sopenharmony_ci	wlc_hw->_piomode = piomode;
44118c2ecf20Sopenharmony_ci
44128c2ecf20Sopenharmony_ci	/* populate struct brcms_hardware with default values  */
44138c2ecf20Sopenharmony_ci	brcms_b_info_init(wlc_hw);
44148c2ecf20Sopenharmony_ci
44158c2ecf20Sopenharmony_ci	/*
44168c2ecf20Sopenharmony_ci	 * Do the hardware portion of the attach. Also initialize software
44178c2ecf20Sopenharmony_ci	 * state that depends on the particular hardware we are running.
44188c2ecf20Sopenharmony_ci	 */
44198c2ecf20Sopenharmony_ci	wlc_hw->sih = ai_attach(core->bus);
44208c2ecf20Sopenharmony_ci	if (wlc_hw->sih == NULL) {
44218c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
44228c2ecf20Sopenharmony_ci			  unit);
44238c2ecf20Sopenharmony_ci		err = 11;
44248c2ecf20Sopenharmony_ci		goto fail;
44258c2ecf20Sopenharmony_ci	}
44268c2ecf20Sopenharmony_ci
44278c2ecf20Sopenharmony_ci	/* verify again the device is supported */
44288c2ecf20Sopenharmony_ci	if (!brcms_c_chipmatch(core)) {
44298c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
44308c2ecf20Sopenharmony_ci			 unit);
44318c2ecf20Sopenharmony_ci		err = 12;
44328c2ecf20Sopenharmony_ci		goto fail;
44338c2ecf20Sopenharmony_ci	}
44348c2ecf20Sopenharmony_ci
44358c2ecf20Sopenharmony_ci	if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
44368c2ecf20Sopenharmony_ci		wlc_hw->vendorid = pcidev->vendor;
44378c2ecf20Sopenharmony_ci		wlc_hw->deviceid = pcidev->device;
44388c2ecf20Sopenharmony_ci	} else {
44398c2ecf20Sopenharmony_ci		wlc_hw->vendorid = core->bus->boardinfo.vendor;
44408c2ecf20Sopenharmony_ci		wlc_hw->deviceid = core->bus->boardinfo.type;
44418c2ecf20Sopenharmony_ci	}
44428c2ecf20Sopenharmony_ci
44438c2ecf20Sopenharmony_ci	wlc_hw->d11core = core;
44448c2ecf20Sopenharmony_ci	wlc_hw->corerev = core->id.rev;
44458c2ecf20Sopenharmony_ci
44468c2ecf20Sopenharmony_ci	/* validate chip, chiprev and corerev */
44478c2ecf20Sopenharmony_ci	if (!brcms_c_isgoodchip(wlc_hw)) {
44488c2ecf20Sopenharmony_ci		err = 13;
44498c2ecf20Sopenharmony_ci		goto fail;
44508c2ecf20Sopenharmony_ci	}
44518c2ecf20Sopenharmony_ci
44528c2ecf20Sopenharmony_ci	/* initialize power control registers */
44538c2ecf20Sopenharmony_ci	ai_clkctl_init(wlc_hw->sih);
44548c2ecf20Sopenharmony_ci
44558c2ecf20Sopenharmony_ci	/* request fastclock and force fastclock for the rest of attach
44568c2ecf20Sopenharmony_ci	 * bring the d11 core out of reset.
44578c2ecf20Sopenharmony_ci	 *   For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
44588c2ecf20Sopenharmony_ci	 *   is still false; But it will be called again inside wlc_corereset,
44598c2ecf20Sopenharmony_ci	 *   after d11 is out of reset.
44608c2ecf20Sopenharmony_ci	 */
44618c2ecf20Sopenharmony_ci	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
44628c2ecf20Sopenharmony_ci	brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
44638c2ecf20Sopenharmony_ci
44648c2ecf20Sopenharmony_ci	if (!brcms_b_validate_chip_access(wlc_hw)) {
44658c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
44668c2ecf20Sopenharmony_ci			"failed\n", unit);
44678c2ecf20Sopenharmony_ci		err = 14;
44688c2ecf20Sopenharmony_ci		goto fail;
44698c2ecf20Sopenharmony_ci	}
44708c2ecf20Sopenharmony_ci
44718c2ecf20Sopenharmony_ci	/* get the board rev, used just below */
44728c2ecf20Sopenharmony_ci	j = sprom->board_rev;
44738c2ecf20Sopenharmony_ci	/* promote srom boardrev of 0xFF to 1 */
44748c2ecf20Sopenharmony_ci	if (j == BOARDREV_PROMOTABLE)
44758c2ecf20Sopenharmony_ci		j = BOARDREV_PROMOTED;
44768c2ecf20Sopenharmony_ci	wlc_hw->boardrev = (u16) j;
44778c2ecf20Sopenharmony_ci	if (!brcms_c_validboardtype(wlc_hw)) {
44788c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
44798c2ecf20Sopenharmony_ci			  "board type (0x%x)" " or revision level (0x%x)\n",
44808c2ecf20Sopenharmony_ci			  unit, ai_get_boardtype(wlc_hw->sih),
44818c2ecf20Sopenharmony_ci			  wlc_hw->boardrev);
44828c2ecf20Sopenharmony_ci		err = 15;
44838c2ecf20Sopenharmony_ci		goto fail;
44848c2ecf20Sopenharmony_ci	}
44858c2ecf20Sopenharmony_ci	wlc_hw->sromrev = sprom->revision;
44868c2ecf20Sopenharmony_ci	wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
44878c2ecf20Sopenharmony_ci	wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
44888c2ecf20Sopenharmony_ci
44898c2ecf20Sopenharmony_ci	if (wlc_hw->boardflags & BFL_NOPLLDOWN)
44908c2ecf20Sopenharmony_ci		brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
44918c2ecf20Sopenharmony_ci
44928c2ecf20Sopenharmony_ci	/* check device id(srom, nvram etc.) to set bands */
44938c2ecf20Sopenharmony_ci	if (wlc_hw->deviceid == BCM43224_D11N_ID ||
44948c2ecf20Sopenharmony_ci	    wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
44958c2ecf20Sopenharmony_ci	    wlc_hw->deviceid == BCM43224_CHIP_ID)
44968c2ecf20Sopenharmony_ci		/* Dualband boards */
44978c2ecf20Sopenharmony_ci		wlc_hw->_nbands = 2;
44988c2ecf20Sopenharmony_ci	else
44998c2ecf20Sopenharmony_ci		wlc_hw->_nbands = 1;
45008c2ecf20Sopenharmony_ci
45018c2ecf20Sopenharmony_ci	if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
45028c2ecf20Sopenharmony_ci		wlc_hw->_nbands = 1;
45038c2ecf20Sopenharmony_ci
45048c2ecf20Sopenharmony_ci	/* BMAC_NOTE: remove init of pub values when brcms_c_attach()
45058c2ecf20Sopenharmony_ci	 * unconditionally does the init of these values
45068c2ecf20Sopenharmony_ci	 */
45078c2ecf20Sopenharmony_ci	wlc->vendorid = wlc_hw->vendorid;
45088c2ecf20Sopenharmony_ci	wlc->deviceid = wlc_hw->deviceid;
45098c2ecf20Sopenharmony_ci	wlc->pub->sih = wlc_hw->sih;
45108c2ecf20Sopenharmony_ci	wlc->pub->corerev = wlc_hw->corerev;
45118c2ecf20Sopenharmony_ci	wlc->pub->sromrev = wlc_hw->sromrev;
45128c2ecf20Sopenharmony_ci	wlc->pub->boardrev = wlc_hw->boardrev;
45138c2ecf20Sopenharmony_ci	wlc->pub->boardflags = wlc_hw->boardflags;
45148c2ecf20Sopenharmony_ci	wlc->pub->boardflags2 = wlc_hw->boardflags2;
45158c2ecf20Sopenharmony_ci	wlc->pub->_nbands = wlc_hw->_nbands;
45168c2ecf20Sopenharmony_ci
45178c2ecf20Sopenharmony_ci	wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
45188c2ecf20Sopenharmony_ci
45198c2ecf20Sopenharmony_ci	if (wlc_hw->physhim == NULL) {
45208c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
45218c2ecf20Sopenharmony_ci			"failed\n", unit);
45228c2ecf20Sopenharmony_ci		err = 25;
45238c2ecf20Sopenharmony_ci		goto fail;
45248c2ecf20Sopenharmony_ci	}
45258c2ecf20Sopenharmony_ci
45268c2ecf20Sopenharmony_ci	/* pass all the parameters to wlc_phy_shared_attach in one struct */
45278c2ecf20Sopenharmony_ci	sha_params.sih = wlc_hw->sih;
45288c2ecf20Sopenharmony_ci	sha_params.physhim = wlc_hw->physhim;
45298c2ecf20Sopenharmony_ci	sha_params.unit = unit;
45308c2ecf20Sopenharmony_ci	sha_params.corerev = wlc_hw->corerev;
45318c2ecf20Sopenharmony_ci	sha_params.vid = wlc_hw->vendorid;
45328c2ecf20Sopenharmony_ci	sha_params.did = wlc_hw->deviceid;
45338c2ecf20Sopenharmony_ci	sha_params.chip = ai_get_chip_id(wlc_hw->sih);
45348c2ecf20Sopenharmony_ci	sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
45358c2ecf20Sopenharmony_ci	sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
45368c2ecf20Sopenharmony_ci	sha_params.sromrev = wlc_hw->sromrev;
45378c2ecf20Sopenharmony_ci	sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
45388c2ecf20Sopenharmony_ci	sha_params.boardrev = wlc_hw->boardrev;
45398c2ecf20Sopenharmony_ci	sha_params.boardflags = wlc_hw->boardflags;
45408c2ecf20Sopenharmony_ci	sha_params.boardflags2 = wlc_hw->boardflags2;
45418c2ecf20Sopenharmony_ci
45428c2ecf20Sopenharmony_ci	/* alloc and save pointer to shared phy state area */
45438c2ecf20Sopenharmony_ci	wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
45448c2ecf20Sopenharmony_ci	if (!wlc_hw->phy_sh) {
45458c2ecf20Sopenharmony_ci		err = 16;
45468c2ecf20Sopenharmony_ci		goto fail;
45478c2ecf20Sopenharmony_ci	}
45488c2ecf20Sopenharmony_ci
45498c2ecf20Sopenharmony_ci	/* initialize software state for each core and band */
45508c2ecf20Sopenharmony_ci	for (j = 0; j < wlc_hw->_nbands; j++) {
45518c2ecf20Sopenharmony_ci		/*
45528c2ecf20Sopenharmony_ci		 * band0 is always 2.4Ghz
45538c2ecf20Sopenharmony_ci		 * band1, if present, is 5Ghz
45548c2ecf20Sopenharmony_ci		 */
45558c2ecf20Sopenharmony_ci
45568c2ecf20Sopenharmony_ci		brcms_c_setxband(wlc_hw, j);
45578c2ecf20Sopenharmony_ci
45588c2ecf20Sopenharmony_ci		wlc_hw->band->bandunit = j;
45598c2ecf20Sopenharmony_ci		wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
45608c2ecf20Sopenharmony_ci		wlc->band->bandunit = j;
45618c2ecf20Sopenharmony_ci		wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
45628c2ecf20Sopenharmony_ci		wlc->core->coreidx = core->core_index;
45638c2ecf20Sopenharmony_ci
45648c2ecf20Sopenharmony_ci		wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
45658c2ecf20Sopenharmony_ci		wlc_hw->machwcap_backup = wlc_hw->machwcap;
45668c2ecf20Sopenharmony_ci
45678c2ecf20Sopenharmony_ci		/* init tx fifo size */
45688c2ecf20Sopenharmony_ci		WARN_ON(wlc_hw->corerev < XMTFIFOTBL_STARTREV ||
45698c2ecf20Sopenharmony_ci			(wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
45708c2ecf20Sopenharmony_ci				ARRAY_SIZE(xmtfifo_sz));
45718c2ecf20Sopenharmony_ci		wlc_hw->xmtfifo_sz =
45728c2ecf20Sopenharmony_ci		    xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
45738c2ecf20Sopenharmony_ci		WARN_ON(!wlc_hw->xmtfifo_sz[0]);
45748c2ecf20Sopenharmony_ci
45758c2ecf20Sopenharmony_ci		/* Get a phy for this band */
45768c2ecf20Sopenharmony_ci		wlc_hw->band->pi =
45778c2ecf20Sopenharmony_ci			wlc_phy_attach(wlc_hw->phy_sh, core,
45788c2ecf20Sopenharmony_ci				       wlc_hw->band->bandtype,
45798c2ecf20Sopenharmony_ci				       wlc->wiphy);
45808c2ecf20Sopenharmony_ci		if (wlc_hw->band->pi == NULL) {
45818c2ecf20Sopenharmony_ci			wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
45828c2ecf20Sopenharmony_ci				  "attach failed\n", unit);
45838c2ecf20Sopenharmony_ci			err = 17;
45848c2ecf20Sopenharmony_ci			goto fail;
45858c2ecf20Sopenharmony_ci		}
45868c2ecf20Sopenharmony_ci
45878c2ecf20Sopenharmony_ci		wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
45888c2ecf20Sopenharmony_ci
45898c2ecf20Sopenharmony_ci		wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
45908c2ecf20Sopenharmony_ci				       &wlc_hw->band->phyrev,
45918c2ecf20Sopenharmony_ci				       &wlc_hw->band->radioid,
45928c2ecf20Sopenharmony_ci				       &wlc_hw->band->radiorev);
45938c2ecf20Sopenharmony_ci		wlc_hw->band->abgphy_encore =
45948c2ecf20Sopenharmony_ci		    wlc_phy_get_encore(wlc_hw->band->pi);
45958c2ecf20Sopenharmony_ci		wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
45968c2ecf20Sopenharmony_ci		wlc_hw->band->core_flags =
45978c2ecf20Sopenharmony_ci		    wlc_phy_get_coreflags(wlc_hw->band->pi);
45988c2ecf20Sopenharmony_ci
45998c2ecf20Sopenharmony_ci		/* verify good phy_type & supported phy revision */
46008c2ecf20Sopenharmony_ci		if (BRCMS_ISNPHY(wlc_hw->band)) {
46018c2ecf20Sopenharmony_ci			if (NCONF_HAS(wlc_hw->band->phyrev))
46028c2ecf20Sopenharmony_ci				goto good_phy;
46038c2ecf20Sopenharmony_ci			else
46048c2ecf20Sopenharmony_ci				goto bad_phy;
46058c2ecf20Sopenharmony_ci		} else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
46068c2ecf20Sopenharmony_ci			if (LCNCONF_HAS(wlc_hw->band->phyrev))
46078c2ecf20Sopenharmony_ci				goto good_phy;
46088c2ecf20Sopenharmony_ci			else
46098c2ecf20Sopenharmony_ci				goto bad_phy;
46108c2ecf20Sopenharmony_ci		} else {
46118c2ecf20Sopenharmony_ci bad_phy:
46128c2ecf20Sopenharmony_ci			wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
46138c2ecf20Sopenharmony_ci				  "phy type/rev (%d/%d)\n", unit,
46148c2ecf20Sopenharmony_ci				  wlc_hw->band->phytype, wlc_hw->band->phyrev);
46158c2ecf20Sopenharmony_ci			err = 18;
46168c2ecf20Sopenharmony_ci			goto fail;
46178c2ecf20Sopenharmony_ci		}
46188c2ecf20Sopenharmony_ci
46198c2ecf20Sopenharmony_ci good_phy:
46208c2ecf20Sopenharmony_ci		/*
46218c2ecf20Sopenharmony_ci		 * BMAC_NOTE: wlc->band->pi should not be set below and should
46228c2ecf20Sopenharmony_ci		 * be done in the high level attach. However we can not make
46238c2ecf20Sopenharmony_ci		 * that change until all low level access is changed to
46248c2ecf20Sopenharmony_ci		 * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
46258c2ecf20Sopenharmony_ci		 * keeping wlc_hw->band->pi as well for incremental update of
46268c2ecf20Sopenharmony_ci		 * low level fns, and cut over low only init when all fns
46278c2ecf20Sopenharmony_ci		 * updated.
46288c2ecf20Sopenharmony_ci		 */
46298c2ecf20Sopenharmony_ci		wlc->band->pi = wlc_hw->band->pi;
46308c2ecf20Sopenharmony_ci		wlc->band->phytype = wlc_hw->band->phytype;
46318c2ecf20Sopenharmony_ci		wlc->band->phyrev = wlc_hw->band->phyrev;
46328c2ecf20Sopenharmony_ci		wlc->band->radioid = wlc_hw->band->radioid;
46338c2ecf20Sopenharmony_ci		wlc->band->radiorev = wlc_hw->band->radiorev;
46348c2ecf20Sopenharmony_ci		brcms_dbg_info(core, "wl%d: phy %u/%u radio %x/%u\n", unit,
46358c2ecf20Sopenharmony_ci			       wlc->band->phytype, wlc->band->phyrev,
46368c2ecf20Sopenharmony_ci			       wlc->band->radioid, wlc->band->radiorev);
46378c2ecf20Sopenharmony_ci		/* default contention windows size limits */
46388c2ecf20Sopenharmony_ci		wlc_hw->band->CWmin = APHY_CWMIN;
46398c2ecf20Sopenharmony_ci		wlc_hw->band->CWmax = PHY_CWMAX;
46408c2ecf20Sopenharmony_ci
46418c2ecf20Sopenharmony_ci		if (!brcms_b_attach_dmapio(wlc, j, wme)) {
46428c2ecf20Sopenharmony_ci			err = 19;
46438c2ecf20Sopenharmony_ci			goto fail;
46448c2ecf20Sopenharmony_ci		}
46458c2ecf20Sopenharmony_ci	}
46468c2ecf20Sopenharmony_ci
46478c2ecf20Sopenharmony_ci	/* disable core to match driver "down" state */
46488c2ecf20Sopenharmony_ci	brcms_c_coredisable(wlc_hw);
46498c2ecf20Sopenharmony_ci
46508c2ecf20Sopenharmony_ci	/* Match driver "down" state */
46518c2ecf20Sopenharmony_ci	bcma_host_pci_down(wlc_hw->d11core->bus);
46528c2ecf20Sopenharmony_ci
46538c2ecf20Sopenharmony_ci	/* turn off pll and xtal to match driver "down" state */
46548c2ecf20Sopenharmony_ci	brcms_b_xtal(wlc_hw, OFF);
46558c2ecf20Sopenharmony_ci
46568c2ecf20Sopenharmony_ci	/* *******************************************************************
46578c2ecf20Sopenharmony_ci	 * The hardware is in the DOWN state at this point. D11 core
46588c2ecf20Sopenharmony_ci	 * or cores are in reset with clocks off, and the board PLLs
46598c2ecf20Sopenharmony_ci	 * are off if possible.
46608c2ecf20Sopenharmony_ci	 *
46618c2ecf20Sopenharmony_ci	 * Beyond this point, wlc->sbclk == false and chip registers
46628c2ecf20Sopenharmony_ci	 * should not be touched.
46638c2ecf20Sopenharmony_ci	 *********************************************************************
46648c2ecf20Sopenharmony_ci	 */
46658c2ecf20Sopenharmony_ci
46668c2ecf20Sopenharmony_ci	/* init etheraddr state variables */
46678c2ecf20Sopenharmony_ci	brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
46688c2ecf20Sopenharmony_ci
46698c2ecf20Sopenharmony_ci	if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
46708c2ecf20Sopenharmony_ci	    is_zero_ether_addr(wlc_hw->etheraddr)) {
46718c2ecf20Sopenharmony_ci		wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
46728c2ecf20Sopenharmony_ci			  unit);
46738c2ecf20Sopenharmony_ci		err = 22;
46748c2ecf20Sopenharmony_ci		goto fail;
46758c2ecf20Sopenharmony_ci	}
46768c2ecf20Sopenharmony_ci
46778c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
46788c2ecf20Sopenharmony_ci		       wlc_hw->deviceid, wlc_hw->_nbands,
46798c2ecf20Sopenharmony_ci		       ai_get_boardtype(wlc_hw->sih));
46808c2ecf20Sopenharmony_ci
46818c2ecf20Sopenharmony_ci	return err;
46828c2ecf20Sopenharmony_ci
46838c2ecf20Sopenharmony_ci fail:
46848c2ecf20Sopenharmony_ci	wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
46858c2ecf20Sopenharmony_ci		  err);
46868c2ecf20Sopenharmony_ci	return err;
46878c2ecf20Sopenharmony_ci}
46888c2ecf20Sopenharmony_ci
46898c2ecf20Sopenharmony_cistatic bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
46908c2ecf20Sopenharmony_ci{
46918c2ecf20Sopenharmony_ci	int aa;
46928c2ecf20Sopenharmony_ci	uint unit;
46938c2ecf20Sopenharmony_ci	int bandtype;
46948c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
46958c2ecf20Sopenharmony_ci
46968c2ecf20Sopenharmony_ci	unit = wlc->pub->unit;
46978c2ecf20Sopenharmony_ci	bandtype = wlc->band->bandtype;
46988c2ecf20Sopenharmony_ci
46998c2ecf20Sopenharmony_ci	/* get antennas available */
47008c2ecf20Sopenharmony_ci	if (bandtype == BRCM_BAND_5G)
47018c2ecf20Sopenharmony_ci		aa = sprom->ant_available_a;
47028c2ecf20Sopenharmony_ci	else
47038c2ecf20Sopenharmony_ci		aa = sprom->ant_available_bg;
47048c2ecf20Sopenharmony_ci
47058c2ecf20Sopenharmony_ci	if ((aa < 1) || (aa > 15)) {
47068c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
47078c2ecf20Sopenharmony_ci			  " srom (0x%x), using 3\n", unit, __func__, aa);
47088c2ecf20Sopenharmony_ci		aa = 3;
47098c2ecf20Sopenharmony_ci	}
47108c2ecf20Sopenharmony_ci
47118c2ecf20Sopenharmony_ci	/* reset the defaults if we have a single antenna */
47128c2ecf20Sopenharmony_ci	if (aa == 1) {
47138c2ecf20Sopenharmony_ci		wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
47148c2ecf20Sopenharmony_ci		wlc->stf->txant = ANT_TX_FORCE_0;
47158c2ecf20Sopenharmony_ci	} else if (aa == 2) {
47168c2ecf20Sopenharmony_ci		wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
47178c2ecf20Sopenharmony_ci		wlc->stf->txant = ANT_TX_FORCE_1;
47188c2ecf20Sopenharmony_ci	} else {
47198c2ecf20Sopenharmony_ci	}
47208c2ecf20Sopenharmony_ci
47218c2ecf20Sopenharmony_ci	/* Compute Antenna Gain */
47228c2ecf20Sopenharmony_ci	if (bandtype == BRCM_BAND_5G)
47238c2ecf20Sopenharmony_ci		wlc->band->antgain = sprom->antenna_gain.a1;
47248c2ecf20Sopenharmony_ci	else
47258c2ecf20Sopenharmony_ci		wlc->band->antgain = sprom->antenna_gain.a0;
47268c2ecf20Sopenharmony_ci
47278c2ecf20Sopenharmony_ci	return true;
47288c2ecf20Sopenharmony_ci}
47298c2ecf20Sopenharmony_ci
47308c2ecf20Sopenharmony_cistatic void brcms_c_bss_default_init(struct brcms_c_info *wlc)
47318c2ecf20Sopenharmony_ci{
47328c2ecf20Sopenharmony_ci	u16 chanspec;
47338c2ecf20Sopenharmony_ci	struct brcms_band *band;
47348c2ecf20Sopenharmony_ci	struct brcms_bss_info *bi = wlc->default_bss;
47358c2ecf20Sopenharmony_ci
47368c2ecf20Sopenharmony_ci	/* init default and target BSS with some sane initial values */
47378c2ecf20Sopenharmony_ci	memset(bi, 0, sizeof(*bi));
47388c2ecf20Sopenharmony_ci	bi->beacon_period = BEACON_INTERVAL_DEFAULT;
47398c2ecf20Sopenharmony_ci
47408c2ecf20Sopenharmony_ci	/* fill the default channel as the first valid channel
47418c2ecf20Sopenharmony_ci	 * starting from the 2G channels
47428c2ecf20Sopenharmony_ci	 */
47438c2ecf20Sopenharmony_ci	chanspec = ch20mhz_chspec(1);
47448c2ecf20Sopenharmony_ci	wlc->home_chanspec = bi->chanspec = chanspec;
47458c2ecf20Sopenharmony_ci
47468c2ecf20Sopenharmony_ci	/* find the band of our default channel */
47478c2ecf20Sopenharmony_ci	band = wlc->band;
47488c2ecf20Sopenharmony_ci	if (wlc->pub->_nbands > 1 &&
47498c2ecf20Sopenharmony_ci	    band->bandunit != chspec_bandunit(chanspec))
47508c2ecf20Sopenharmony_ci		band = wlc->bandstate[OTHERBANDUNIT(wlc)];
47518c2ecf20Sopenharmony_ci
47528c2ecf20Sopenharmony_ci	/* init bss rates to the band specific default rate set */
47538c2ecf20Sopenharmony_ci	brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
47548c2ecf20Sopenharmony_ci		band->bandtype, false, BRCMS_RATE_MASK_FULL,
47558c2ecf20Sopenharmony_ci		(bool) (wlc->pub->_n_enab & SUPPORT_11N),
47568c2ecf20Sopenharmony_ci		brcms_chspec_bw(chanspec), wlc->stf->txstreams);
47578c2ecf20Sopenharmony_ci
47588c2ecf20Sopenharmony_ci	if (wlc->pub->_n_enab & SUPPORT_11N)
47598c2ecf20Sopenharmony_ci		bi->flags |= BRCMS_BSS_HT;
47608c2ecf20Sopenharmony_ci}
47618c2ecf20Sopenharmony_ci
47628c2ecf20Sopenharmony_cistatic void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
47638c2ecf20Sopenharmony_ci{
47648c2ecf20Sopenharmony_ci	uint i;
47658c2ecf20Sopenharmony_ci	struct brcms_band *band;
47668c2ecf20Sopenharmony_ci
47678c2ecf20Sopenharmony_ci	for (i = 0; i < wlc->pub->_nbands; i++) {
47688c2ecf20Sopenharmony_ci		band = wlc->bandstate[i];
47698c2ecf20Sopenharmony_ci		if (band->bandtype == BRCM_BAND_5G) {
47708c2ecf20Sopenharmony_ci			if ((bwcap == BRCMS_N_BW_40ALL)
47718c2ecf20Sopenharmony_ci			    || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
47728c2ecf20Sopenharmony_ci				band->mimo_cap_40 = true;
47738c2ecf20Sopenharmony_ci			else
47748c2ecf20Sopenharmony_ci				band->mimo_cap_40 = false;
47758c2ecf20Sopenharmony_ci		} else {
47768c2ecf20Sopenharmony_ci			if (bwcap == BRCMS_N_BW_40ALL)
47778c2ecf20Sopenharmony_ci				band->mimo_cap_40 = true;
47788c2ecf20Sopenharmony_ci			else
47798c2ecf20Sopenharmony_ci				band->mimo_cap_40 = false;
47808c2ecf20Sopenharmony_ci		}
47818c2ecf20Sopenharmony_ci	}
47828c2ecf20Sopenharmony_ci}
47838c2ecf20Sopenharmony_ci
47848c2ecf20Sopenharmony_cistatic void brcms_c_timers_deinit(struct brcms_c_info *wlc)
47858c2ecf20Sopenharmony_ci{
47868c2ecf20Sopenharmony_ci	/* free timer state */
47878c2ecf20Sopenharmony_ci	if (wlc->wdtimer) {
47888c2ecf20Sopenharmony_ci		brcms_free_timer(wlc->wdtimer);
47898c2ecf20Sopenharmony_ci		wlc->wdtimer = NULL;
47908c2ecf20Sopenharmony_ci	}
47918c2ecf20Sopenharmony_ci	if (wlc->radio_timer) {
47928c2ecf20Sopenharmony_ci		brcms_free_timer(wlc->radio_timer);
47938c2ecf20Sopenharmony_ci		wlc->radio_timer = NULL;
47948c2ecf20Sopenharmony_ci	}
47958c2ecf20Sopenharmony_ci}
47968c2ecf20Sopenharmony_ci
47978c2ecf20Sopenharmony_cistatic void brcms_c_detach_module(struct brcms_c_info *wlc)
47988c2ecf20Sopenharmony_ci{
47998c2ecf20Sopenharmony_ci	if (wlc->asi) {
48008c2ecf20Sopenharmony_ci		brcms_c_antsel_detach(wlc->asi);
48018c2ecf20Sopenharmony_ci		wlc->asi = NULL;
48028c2ecf20Sopenharmony_ci	}
48038c2ecf20Sopenharmony_ci
48048c2ecf20Sopenharmony_ci	if (wlc->ampdu) {
48058c2ecf20Sopenharmony_ci		brcms_c_ampdu_detach(wlc->ampdu);
48068c2ecf20Sopenharmony_ci		wlc->ampdu = NULL;
48078c2ecf20Sopenharmony_ci	}
48088c2ecf20Sopenharmony_ci
48098c2ecf20Sopenharmony_ci	brcms_c_stf_detach(wlc);
48108c2ecf20Sopenharmony_ci}
48118c2ecf20Sopenharmony_ci
48128c2ecf20Sopenharmony_ci/*
48138c2ecf20Sopenharmony_ci * low level detach
48148c2ecf20Sopenharmony_ci */
48158c2ecf20Sopenharmony_cistatic void brcms_b_detach(struct brcms_c_info *wlc)
48168c2ecf20Sopenharmony_ci{
48178c2ecf20Sopenharmony_ci	uint i;
48188c2ecf20Sopenharmony_ci	struct brcms_hw_band *band;
48198c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
48208c2ecf20Sopenharmony_ci
48218c2ecf20Sopenharmony_ci	brcms_b_detach_dmapio(wlc_hw);
48228c2ecf20Sopenharmony_ci
48238c2ecf20Sopenharmony_ci	band = wlc_hw->band;
48248c2ecf20Sopenharmony_ci	for (i = 0; i < wlc_hw->_nbands; i++) {
48258c2ecf20Sopenharmony_ci		if (band->pi) {
48268c2ecf20Sopenharmony_ci			/* Detach this band's phy */
48278c2ecf20Sopenharmony_ci			wlc_phy_detach(band->pi);
48288c2ecf20Sopenharmony_ci			band->pi = NULL;
48298c2ecf20Sopenharmony_ci		}
48308c2ecf20Sopenharmony_ci		band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
48318c2ecf20Sopenharmony_ci	}
48328c2ecf20Sopenharmony_ci
48338c2ecf20Sopenharmony_ci	/* Free shared phy state */
48348c2ecf20Sopenharmony_ci	kfree(wlc_hw->phy_sh);
48358c2ecf20Sopenharmony_ci
48368c2ecf20Sopenharmony_ci	wlc_phy_shim_detach(wlc_hw->physhim);
48378c2ecf20Sopenharmony_ci
48388c2ecf20Sopenharmony_ci	if (wlc_hw->sih) {
48398c2ecf20Sopenharmony_ci		ai_detach(wlc_hw->sih);
48408c2ecf20Sopenharmony_ci		wlc_hw->sih = NULL;
48418c2ecf20Sopenharmony_ci	}
48428c2ecf20Sopenharmony_ci}
48438c2ecf20Sopenharmony_ci
48448c2ecf20Sopenharmony_ci/*
48458c2ecf20Sopenharmony_ci * Return a count of the number of driver callbacks still pending.
48468c2ecf20Sopenharmony_ci *
48478c2ecf20Sopenharmony_ci * General policy is that brcms_c_detach can only dealloc/free software states.
48488c2ecf20Sopenharmony_ci * It can NOT touch hardware registers since the d11core may be in reset and
48498c2ecf20Sopenharmony_ci * clock may not be available.
48508c2ecf20Sopenharmony_ci * One exception is sb register access, which is possible if crystal is turned
48518c2ecf20Sopenharmony_ci * on after "down" state, driver should avoid software timer with the exception
48528c2ecf20Sopenharmony_ci * of radio_monitor.
48538c2ecf20Sopenharmony_ci */
48548c2ecf20Sopenharmony_ciuint brcms_c_detach(struct brcms_c_info *wlc)
48558c2ecf20Sopenharmony_ci{
48568c2ecf20Sopenharmony_ci	uint callbacks;
48578c2ecf20Sopenharmony_ci
48588c2ecf20Sopenharmony_ci	if (wlc == NULL)
48598c2ecf20Sopenharmony_ci		return 0;
48608c2ecf20Sopenharmony_ci
48618c2ecf20Sopenharmony_ci	brcms_b_detach(wlc);
48628c2ecf20Sopenharmony_ci
48638c2ecf20Sopenharmony_ci	/* delete software timers */
48648c2ecf20Sopenharmony_ci	callbacks = 0;
48658c2ecf20Sopenharmony_ci	if (!brcms_c_radio_monitor_stop(wlc))
48668c2ecf20Sopenharmony_ci		callbacks++;
48678c2ecf20Sopenharmony_ci
48688c2ecf20Sopenharmony_ci	brcms_c_channel_mgr_detach(wlc->cmi);
48698c2ecf20Sopenharmony_ci
48708c2ecf20Sopenharmony_ci	brcms_c_timers_deinit(wlc);
48718c2ecf20Sopenharmony_ci
48728c2ecf20Sopenharmony_ci	brcms_c_detach_module(wlc);
48738c2ecf20Sopenharmony_ci
48748c2ecf20Sopenharmony_ci	brcms_c_detach_mfree(wlc);
48758c2ecf20Sopenharmony_ci	return callbacks;
48768c2ecf20Sopenharmony_ci}
48778c2ecf20Sopenharmony_ci
48788c2ecf20Sopenharmony_ci/* update state that depends on the current value of "ap" */
48798c2ecf20Sopenharmony_cistatic void brcms_c_ap_upd(struct brcms_c_info *wlc)
48808c2ecf20Sopenharmony_ci{
48818c2ecf20Sopenharmony_ci	/* STA-BSS; short capable */
48828c2ecf20Sopenharmony_ci	wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
48838c2ecf20Sopenharmony_ci}
48848c2ecf20Sopenharmony_ci
48858c2ecf20Sopenharmony_ci/* Initialize just the hardware when coming out of POR or S3/S5 system states */
48868c2ecf20Sopenharmony_cistatic void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
48878c2ecf20Sopenharmony_ci{
48888c2ecf20Sopenharmony_ci	if (wlc_hw->wlc->pub->hw_up)
48898c2ecf20Sopenharmony_ci		return;
48908c2ecf20Sopenharmony_ci
48918c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
48928c2ecf20Sopenharmony_ci
48938c2ecf20Sopenharmony_ci	/*
48948c2ecf20Sopenharmony_ci	 * Enable pll and xtal, initialize the power control registers,
48958c2ecf20Sopenharmony_ci	 * and force fastclock for the remainder of brcms_c_up().
48968c2ecf20Sopenharmony_ci	 */
48978c2ecf20Sopenharmony_ci	brcms_b_xtal(wlc_hw, ON);
48988c2ecf20Sopenharmony_ci	ai_clkctl_init(wlc_hw->sih);
48998c2ecf20Sopenharmony_ci	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
49008c2ecf20Sopenharmony_ci
49018c2ecf20Sopenharmony_ci	/*
49028c2ecf20Sopenharmony_ci	 * TODO: test suspend/resume
49038c2ecf20Sopenharmony_ci	 *
49048c2ecf20Sopenharmony_ci	 * AI chip doesn't restore bar0win2 on
49058c2ecf20Sopenharmony_ci	 * hibernation/resume, need sw fixup
49068c2ecf20Sopenharmony_ci	 */
49078c2ecf20Sopenharmony_ci
49088c2ecf20Sopenharmony_ci	/*
49098c2ecf20Sopenharmony_ci	 * Inform phy that a POR reset has occurred so
49108c2ecf20Sopenharmony_ci	 * it does a complete phy init
49118c2ecf20Sopenharmony_ci	 */
49128c2ecf20Sopenharmony_ci	wlc_phy_por_inform(wlc_hw->band->pi);
49138c2ecf20Sopenharmony_ci
49148c2ecf20Sopenharmony_ci	wlc_hw->ucode_loaded = false;
49158c2ecf20Sopenharmony_ci	wlc_hw->wlc->pub->hw_up = true;
49168c2ecf20Sopenharmony_ci
49178c2ecf20Sopenharmony_ci	if ((wlc_hw->boardflags & BFL_FEM)
49188c2ecf20Sopenharmony_ci	    && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
49198c2ecf20Sopenharmony_ci		if (!
49208c2ecf20Sopenharmony_ci		    (wlc_hw->boardrev >= 0x1250
49218c2ecf20Sopenharmony_ci		     && (wlc_hw->boardflags & BFL_FEM_BT)))
49228c2ecf20Sopenharmony_ci			ai_epa_4313war(wlc_hw->sih);
49238c2ecf20Sopenharmony_ci	}
49248c2ecf20Sopenharmony_ci}
49258c2ecf20Sopenharmony_ci
49268c2ecf20Sopenharmony_cistatic int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
49278c2ecf20Sopenharmony_ci{
49288c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
49298c2ecf20Sopenharmony_ci
49308c2ecf20Sopenharmony_ci	/*
49318c2ecf20Sopenharmony_ci	 * Enable pll and xtal, initialize the power control registers,
49328c2ecf20Sopenharmony_ci	 * and force fastclock for the remainder of brcms_c_up().
49338c2ecf20Sopenharmony_ci	 */
49348c2ecf20Sopenharmony_ci	brcms_b_xtal(wlc_hw, ON);
49358c2ecf20Sopenharmony_ci	ai_clkctl_init(wlc_hw->sih);
49368c2ecf20Sopenharmony_ci	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
49378c2ecf20Sopenharmony_ci
49388c2ecf20Sopenharmony_ci	/*
49398c2ecf20Sopenharmony_ci	 * Configure pci/pcmcia here instead of in brcms_c_attach()
49408c2ecf20Sopenharmony_ci	 * to allow mfg hotswap:  down, hotswap (chip power cycle), up.
49418c2ecf20Sopenharmony_ci	 */
49428c2ecf20Sopenharmony_ci	bcma_host_pci_irq_ctl(wlc_hw->d11core->bus, wlc_hw->d11core,
49438c2ecf20Sopenharmony_ci			      true);
49448c2ecf20Sopenharmony_ci
49458c2ecf20Sopenharmony_ci	/*
49468c2ecf20Sopenharmony_ci	 * Need to read the hwradio status here to cover the case where the
49478c2ecf20Sopenharmony_ci	 * system is loaded with the hw radio disabled. We do not want to
49488c2ecf20Sopenharmony_ci	 * bring the driver up in this case.
49498c2ecf20Sopenharmony_ci	 */
49508c2ecf20Sopenharmony_ci	if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
49518c2ecf20Sopenharmony_ci		/* put SB PCI in down state again */
49528c2ecf20Sopenharmony_ci		bcma_host_pci_down(wlc_hw->d11core->bus);
49538c2ecf20Sopenharmony_ci		brcms_b_xtal(wlc_hw, OFF);
49548c2ecf20Sopenharmony_ci		return -ENOMEDIUM;
49558c2ecf20Sopenharmony_ci	}
49568c2ecf20Sopenharmony_ci
49578c2ecf20Sopenharmony_ci	bcma_host_pci_up(wlc_hw->d11core->bus);
49588c2ecf20Sopenharmony_ci
49598c2ecf20Sopenharmony_ci	/* reset the d11 core */
49608c2ecf20Sopenharmony_ci	brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
49618c2ecf20Sopenharmony_ci
49628c2ecf20Sopenharmony_ci	return 0;
49638c2ecf20Sopenharmony_ci}
49648c2ecf20Sopenharmony_ci
49658c2ecf20Sopenharmony_cistatic int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
49668c2ecf20Sopenharmony_ci{
49678c2ecf20Sopenharmony_ci	wlc_hw->up = true;
49688c2ecf20Sopenharmony_ci	wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
49698c2ecf20Sopenharmony_ci
49708c2ecf20Sopenharmony_ci	/* FULLY enable dynamic power control and d11 core interrupt */
49718c2ecf20Sopenharmony_ci	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
49728c2ecf20Sopenharmony_ci	brcms_intrson(wlc_hw->wlc->wl);
49738c2ecf20Sopenharmony_ci	return 0;
49748c2ecf20Sopenharmony_ci}
49758c2ecf20Sopenharmony_ci
49768c2ecf20Sopenharmony_ci/*
49778c2ecf20Sopenharmony_ci * Write WME tunable parameters for retransmit/max rate
49788c2ecf20Sopenharmony_ci * from wlc struct to ucode
49798c2ecf20Sopenharmony_ci */
49808c2ecf20Sopenharmony_cistatic void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
49818c2ecf20Sopenharmony_ci{
49828c2ecf20Sopenharmony_ci	int ac;
49838c2ecf20Sopenharmony_ci
49848c2ecf20Sopenharmony_ci	/* Need clock to do this */
49858c2ecf20Sopenharmony_ci	if (!wlc->clk)
49868c2ecf20Sopenharmony_ci		return;
49878c2ecf20Sopenharmony_ci
49888c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
49898c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
49908c2ecf20Sopenharmony_ci				  wlc->wme_retries[ac]);
49918c2ecf20Sopenharmony_ci}
49928c2ecf20Sopenharmony_ci
49938c2ecf20Sopenharmony_ci/* make interface operational */
49948c2ecf20Sopenharmony_ciint brcms_c_up(struct brcms_c_info *wlc)
49958c2ecf20Sopenharmony_ci{
49968c2ecf20Sopenharmony_ci	struct ieee80211_channel *ch;
49978c2ecf20Sopenharmony_ci
49988c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
49998c2ecf20Sopenharmony_ci
50008c2ecf20Sopenharmony_ci	/* HW is turned off so don't try to access it */
50018c2ecf20Sopenharmony_ci	if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
50028c2ecf20Sopenharmony_ci		return -ENOMEDIUM;
50038c2ecf20Sopenharmony_ci
50048c2ecf20Sopenharmony_ci	if (!wlc->pub->hw_up) {
50058c2ecf20Sopenharmony_ci		brcms_b_hw_up(wlc->hw);
50068c2ecf20Sopenharmony_ci		wlc->pub->hw_up = true;
50078c2ecf20Sopenharmony_ci	}
50088c2ecf20Sopenharmony_ci
50098c2ecf20Sopenharmony_ci	if ((wlc->pub->boardflags & BFL_FEM)
50108c2ecf20Sopenharmony_ci	    && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
50118c2ecf20Sopenharmony_ci		if (wlc->pub->boardrev >= 0x1250
50128c2ecf20Sopenharmony_ci		    && (wlc->pub->boardflags & BFL_FEM_BT))
50138c2ecf20Sopenharmony_ci			brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
50148c2ecf20Sopenharmony_ci				MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
50158c2ecf20Sopenharmony_ci		else
50168c2ecf20Sopenharmony_ci			brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
50178c2ecf20Sopenharmony_ci				    MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
50188c2ecf20Sopenharmony_ci	}
50198c2ecf20Sopenharmony_ci
50208c2ecf20Sopenharmony_ci	/*
50218c2ecf20Sopenharmony_ci	 * Need to read the hwradio status here to cover the case where the
50228c2ecf20Sopenharmony_ci	 * system is loaded with the hw radio disabled. We do not want to bring
50238c2ecf20Sopenharmony_ci	 * the driver up in this case. If radio is disabled, abort up, lower
50248c2ecf20Sopenharmony_ci	 * power, start radio timer and return 0(for NDIS) don't call
50258c2ecf20Sopenharmony_ci	 * radio_update to avoid looping brcms_c_up.
50268c2ecf20Sopenharmony_ci	 *
50278c2ecf20Sopenharmony_ci	 * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
50288c2ecf20Sopenharmony_ci	 */
50298c2ecf20Sopenharmony_ci	if (!wlc->pub->radio_disabled) {
50308c2ecf20Sopenharmony_ci		int status = brcms_b_up_prep(wlc->hw);
50318c2ecf20Sopenharmony_ci		if (status == -ENOMEDIUM) {
50328c2ecf20Sopenharmony_ci			if (!mboolisset
50338c2ecf20Sopenharmony_ci			    (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
50348c2ecf20Sopenharmony_ci				struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
50358c2ecf20Sopenharmony_ci				mboolset(wlc->pub->radio_disabled,
50368c2ecf20Sopenharmony_ci					 WL_RADIO_HW_DISABLE);
50378c2ecf20Sopenharmony_ci				if (bsscfg->type == BRCMS_TYPE_STATION ||
50388c2ecf20Sopenharmony_ci				    bsscfg->type == BRCMS_TYPE_ADHOC)
50398c2ecf20Sopenharmony_ci					brcms_err(wlc->hw->d11core,
50408c2ecf20Sopenharmony_ci						  "wl%d: up: rfdisable -> "
50418c2ecf20Sopenharmony_ci						  "bsscfg_disable()\n",
50428c2ecf20Sopenharmony_ci						   wlc->pub->unit);
50438c2ecf20Sopenharmony_ci			}
50448c2ecf20Sopenharmony_ci		}
50458c2ecf20Sopenharmony_ci	}
50468c2ecf20Sopenharmony_ci
50478c2ecf20Sopenharmony_ci	if (wlc->pub->radio_disabled) {
50488c2ecf20Sopenharmony_ci		brcms_c_radio_monitor_start(wlc);
50498c2ecf20Sopenharmony_ci		return 0;
50508c2ecf20Sopenharmony_ci	}
50518c2ecf20Sopenharmony_ci
50528c2ecf20Sopenharmony_ci	/* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
50538c2ecf20Sopenharmony_ci	wlc->clk = true;
50548c2ecf20Sopenharmony_ci
50558c2ecf20Sopenharmony_ci	brcms_c_radio_monitor_stop(wlc);
50568c2ecf20Sopenharmony_ci
50578c2ecf20Sopenharmony_ci	/* Set EDCF hostflags */
50588c2ecf20Sopenharmony_ci	brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
50598c2ecf20Sopenharmony_ci
50608c2ecf20Sopenharmony_ci	brcms_init(wlc->wl);
50618c2ecf20Sopenharmony_ci	wlc->pub->up = true;
50628c2ecf20Sopenharmony_ci
50638c2ecf20Sopenharmony_ci	if (wlc->bandinit_pending) {
50648c2ecf20Sopenharmony_ci		ch = wlc->pub->ieee_hw->conf.chandef.chan;
50658c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc);
50668c2ecf20Sopenharmony_ci		brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
50678c2ecf20Sopenharmony_ci		wlc->bandinit_pending = false;
50688c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc);
50698c2ecf20Sopenharmony_ci	}
50708c2ecf20Sopenharmony_ci
50718c2ecf20Sopenharmony_ci	brcms_b_up_finish(wlc->hw);
50728c2ecf20Sopenharmony_ci
50738c2ecf20Sopenharmony_ci	/* Program the TX wme params with the current settings */
50748c2ecf20Sopenharmony_ci	brcms_c_wme_retries_write(wlc);
50758c2ecf20Sopenharmony_ci
50768c2ecf20Sopenharmony_ci	/* start one second watchdog timer */
50778c2ecf20Sopenharmony_ci	brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
50788c2ecf20Sopenharmony_ci	wlc->WDarmed = true;
50798c2ecf20Sopenharmony_ci
50808c2ecf20Sopenharmony_ci	/* ensure antenna config is up to date */
50818c2ecf20Sopenharmony_ci	brcms_c_stf_phy_txant_upd(wlc);
50828c2ecf20Sopenharmony_ci	/* ensure LDPC config is in sync */
50838c2ecf20Sopenharmony_ci	brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
50848c2ecf20Sopenharmony_ci
50858c2ecf20Sopenharmony_ci	return 0;
50868c2ecf20Sopenharmony_ci}
50878c2ecf20Sopenharmony_ci
50888c2ecf20Sopenharmony_cistatic int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
50898c2ecf20Sopenharmony_ci{
50908c2ecf20Sopenharmony_ci	bool dev_gone;
50918c2ecf20Sopenharmony_ci	uint callbacks = 0;
50928c2ecf20Sopenharmony_ci
50938c2ecf20Sopenharmony_ci	if (!wlc_hw->up)
50948c2ecf20Sopenharmony_ci		return callbacks;
50958c2ecf20Sopenharmony_ci
50968c2ecf20Sopenharmony_ci	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
50978c2ecf20Sopenharmony_ci
50988c2ecf20Sopenharmony_ci	/* disable interrupts */
50998c2ecf20Sopenharmony_ci	if (dev_gone)
51008c2ecf20Sopenharmony_ci		wlc_hw->wlc->macintmask = 0;
51018c2ecf20Sopenharmony_ci	else {
51028c2ecf20Sopenharmony_ci		/* now disable interrupts */
51038c2ecf20Sopenharmony_ci		brcms_intrsoff(wlc_hw->wlc->wl);
51048c2ecf20Sopenharmony_ci
51058c2ecf20Sopenharmony_ci		/* ensure we're running on the pll clock again */
51068c2ecf20Sopenharmony_ci		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
51078c2ecf20Sopenharmony_ci	}
51088c2ecf20Sopenharmony_ci	/* down phy at the last of this stage */
51098c2ecf20Sopenharmony_ci	callbacks += wlc_phy_down(wlc_hw->band->pi);
51108c2ecf20Sopenharmony_ci
51118c2ecf20Sopenharmony_ci	return callbacks;
51128c2ecf20Sopenharmony_ci}
51138c2ecf20Sopenharmony_ci
51148c2ecf20Sopenharmony_cistatic int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
51158c2ecf20Sopenharmony_ci{
51168c2ecf20Sopenharmony_ci	uint callbacks = 0;
51178c2ecf20Sopenharmony_ci	bool dev_gone;
51188c2ecf20Sopenharmony_ci
51198c2ecf20Sopenharmony_ci	if (!wlc_hw->up)
51208c2ecf20Sopenharmony_ci		return callbacks;
51218c2ecf20Sopenharmony_ci
51228c2ecf20Sopenharmony_ci	wlc_hw->up = false;
51238c2ecf20Sopenharmony_ci	wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
51248c2ecf20Sopenharmony_ci
51258c2ecf20Sopenharmony_ci	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
51268c2ecf20Sopenharmony_ci
51278c2ecf20Sopenharmony_ci	if (dev_gone) {
51288c2ecf20Sopenharmony_ci		wlc_hw->sbclk = false;
51298c2ecf20Sopenharmony_ci		wlc_hw->clk = false;
51308c2ecf20Sopenharmony_ci		wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
51318c2ecf20Sopenharmony_ci
51328c2ecf20Sopenharmony_ci		/* reclaim any posted packets */
51338c2ecf20Sopenharmony_ci		brcms_c_flushqueues(wlc_hw->wlc);
51348c2ecf20Sopenharmony_ci	} else {
51358c2ecf20Sopenharmony_ci
51368c2ecf20Sopenharmony_ci		/* Reset and disable the core */
51378c2ecf20Sopenharmony_ci		if (bcma_core_is_enabled(wlc_hw->d11core)) {
51388c2ecf20Sopenharmony_ci			if (bcma_read32(wlc_hw->d11core,
51398c2ecf20Sopenharmony_ci					D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
51408c2ecf20Sopenharmony_ci				brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
51418c2ecf20Sopenharmony_ci			callbacks += brcms_reset(wlc_hw->wlc->wl);
51428c2ecf20Sopenharmony_ci			brcms_c_coredisable(wlc_hw);
51438c2ecf20Sopenharmony_ci		}
51448c2ecf20Sopenharmony_ci
51458c2ecf20Sopenharmony_ci		/* turn off primary xtal and pll */
51468c2ecf20Sopenharmony_ci		if (!wlc_hw->noreset) {
51478c2ecf20Sopenharmony_ci			bcma_host_pci_down(wlc_hw->d11core->bus);
51488c2ecf20Sopenharmony_ci			brcms_b_xtal(wlc_hw, OFF);
51498c2ecf20Sopenharmony_ci		}
51508c2ecf20Sopenharmony_ci	}
51518c2ecf20Sopenharmony_ci
51528c2ecf20Sopenharmony_ci	return callbacks;
51538c2ecf20Sopenharmony_ci}
51548c2ecf20Sopenharmony_ci
51558c2ecf20Sopenharmony_ci/*
51568c2ecf20Sopenharmony_ci * Mark the interface nonoperational, stop the software mechanisms,
51578c2ecf20Sopenharmony_ci * disable the hardware, free any transient buffer state.
51588c2ecf20Sopenharmony_ci * Return a count of the number of driver callbacks still pending.
51598c2ecf20Sopenharmony_ci */
51608c2ecf20Sopenharmony_ciuint brcms_c_down(struct brcms_c_info *wlc)
51618c2ecf20Sopenharmony_ci{
51628c2ecf20Sopenharmony_ci
51638c2ecf20Sopenharmony_ci	uint callbacks = 0;
51648c2ecf20Sopenharmony_ci	int i;
51658c2ecf20Sopenharmony_ci
51668c2ecf20Sopenharmony_ci	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
51678c2ecf20Sopenharmony_ci
51688c2ecf20Sopenharmony_ci	/* check if we are already in the going down path */
51698c2ecf20Sopenharmony_ci	if (wlc->going_down) {
51708c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core,
51718c2ecf20Sopenharmony_ci			  "wl%d: %s: Driver going down so return\n",
51728c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__);
51738c2ecf20Sopenharmony_ci		return 0;
51748c2ecf20Sopenharmony_ci	}
51758c2ecf20Sopenharmony_ci	if (!wlc->pub->up)
51768c2ecf20Sopenharmony_ci		return callbacks;
51778c2ecf20Sopenharmony_ci
51788c2ecf20Sopenharmony_ci	wlc->going_down = true;
51798c2ecf20Sopenharmony_ci
51808c2ecf20Sopenharmony_ci	callbacks += brcms_b_bmac_down_prep(wlc->hw);
51818c2ecf20Sopenharmony_ci
51828c2ecf20Sopenharmony_ci	brcms_deviceremoved(wlc);
51838c2ecf20Sopenharmony_ci
51848c2ecf20Sopenharmony_ci	/* Call any registered down handlers */
51858c2ecf20Sopenharmony_ci	for (i = 0; i < BRCMS_MAXMODULES; i++) {
51868c2ecf20Sopenharmony_ci		if (wlc->modulecb[i].down_fn)
51878c2ecf20Sopenharmony_ci			callbacks +=
51888c2ecf20Sopenharmony_ci			    wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
51898c2ecf20Sopenharmony_ci	}
51908c2ecf20Sopenharmony_ci
51918c2ecf20Sopenharmony_ci	/* cancel the watchdog timer */
51928c2ecf20Sopenharmony_ci	if (wlc->WDarmed) {
51938c2ecf20Sopenharmony_ci		if (!brcms_del_timer(wlc->wdtimer))
51948c2ecf20Sopenharmony_ci			callbacks++;
51958c2ecf20Sopenharmony_ci		wlc->WDarmed = false;
51968c2ecf20Sopenharmony_ci	}
51978c2ecf20Sopenharmony_ci
51988c2ecf20Sopenharmony_ci	wlc->pub->up = false;
51998c2ecf20Sopenharmony_ci
52008c2ecf20Sopenharmony_ci	wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
52018c2ecf20Sopenharmony_ci
52028c2ecf20Sopenharmony_ci	callbacks += brcms_b_down_finish(wlc->hw);
52038c2ecf20Sopenharmony_ci
52048c2ecf20Sopenharmony_ci	/* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
52058c2ecf20Sopenharmony_ci	wlc->clk = false;
52068c2ecf20Sopenharmony_ci
52078c2ecf20Sopenharmony_ci	wlc->going_down = false;
52088c2ecf20Sopenharmony_ci	return callbacks;
52098c2ecf20Sopenharmony_ci}
52108c2ecf20Sopenharmony_ci
52118c2ecf20Sopenharmony_ci/* Set the current gmode configuration */
52128c2ecf20Sopenharmony_ciint brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
52138c2ecf20Sopenharmony_ci{
52148c2ecf20Sopenharmony_ci	int ret = 0;
52158c2ecf20Sopenharmony_ci	uint i;
52168c2ecf20Sopenharmony_ci	struct brcms_c_rateset rs;
52178c2ecf20Sopenharmony_ci	/* Default to 54g Auto */
52188c2ecf20Sopenharmony_ci	/* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
52198c2ecf20Sopenharmony_ci	s8 shortslot = BRCMS_SHORTSLOT_AUTO;
52208c2ecf20Sopenharmony_ci	bool ofdm_basic = false;	/* Make 6, 12, and 24 basic rates */
52218c2ecf20Sopenharmony_ci	struct brcms_band *band;
52228c2ecf20Sopenharmony_ci
52238c2ecf20Sopenharmony_ci	/* if N-support is enabled, allow Gmode set as long as requested
52248c2ecf20Sopenharmony_ci	 * Gmode is not GMODE_LEGACY_B
52258c2ecf20Sopenharmony_ci	 */
52268c2ecf20Sopenharmony_ci	if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
52278c2ecf20Sopenharmony_ci		return -ENOTSUPP;
52288c2ecf20Sopenharmony_ci
52298c2ecf20Sopenharmony_ci	/* verify that we are dealing with 2G band and grab the band pointer */
52308c2ecf20Sopenharmony_ci	if (wlc->band->bandtype == BRCM_BAND_2G)
52318c2ecf20Sopenharmony_ci		band = wlc->band;
52328c2ecf20Sopenharmony_ci	else if ((wlc->pub->_nbands > 1) &&
52338c2ecf20Sopenharmony_ci		 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
52348c2ecf20Sopenharmony_ci		band = wlc->bandstate[OTHERBANDUNIT(wlc)];
52358c2ecf20Sopenharmony_ci	else
52368c2ecf20Sopenharmony_ci		return -EINVAL;
52378c2ecf20Sopenharmony_ci
52388c2ecf20Sopenharmony_ci	/* update configuration value */
52398c2ecf20Sopenharmony_ci	if (config)
52408c2ecf20Sopenharmony_ci		brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
52418c2ecf20Sopenharmony_ci
52428c2ecf20Sopenharmony_ci	/* Clear rateset override */
52438c2ecf20Sopenharmony_ci	memset(&rs, 0, sizeof(rs));
52448c2ecf20Sopenharmony_ci
52458c2ecf20Sopenharmony_ci	switch (gmode) {
52468c2ecf20Sopenharmony_ci	case GMODE_LEGACY_B:
52478c2ecf20Sopenharmony_ci		shortslot = BRCMS_SHORTSLOT_OFF;
52488c2ecf20Sopenharmony_ci		brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
52498c2ecf20Sopenharmony_ci
52508c2ecf20Sopenharmony_ci		break;
52518c2ecf20Sopenharmony_ci
52528c2ecf20Sopenharmony_ci	case GMODE_LRS:
52538c2ecf20Sopenharmony_ci		break;
52548c2ecf20Sopenharmony_ci
52558c2ecf20Sopenharmony_ci	case GMODE_AUTO:
52568c2ecf20Sopenharmony_ci		/* Accept defaults */
52578c2ecf20Sopenharmony_ci		break;
52588c2ecf20Sopenharmony_ci
52598c2ecf20Sopenharmony_ci	case GMODE_ONLY:
52608c2ecf20Sopenharmony_ci		ofdm_basic = true;
52618c2ecf20Sopenharmony_ci		break;
52628c2ecf20Sopenharmony_ci
52638c2ecf20Sopenharmony_ci	case GMODE_PERFORMANCE:
52648c2ecf20Sopenharmony_ci		shortslot = BRCMS_SHORTSLOT_ON;
52658c2ecf20Sopenharmony_ci		ofdm_basic = true;
52668c2ecf20Sopenharmony_ci		break;
52678c2ecf20Sopenharmony_ci
52688c2ecf20Sopenharmony_ci	default:
52698c2ecf20Sopenharmony_ci		/* Error */
52708c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
52718c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__, gmode);
52728c2ecf20Sopenharmony_ci		return -ENOTSUPP;
52738c2ecf20Sopenharmony_ci	}
52748c2ecf20Sopenharmony_ci
52758c2ecf20Sopenharmony_ci	band->gmode = gmode;
52768c2ecf20Sopenharmony_ci
52778c2ecf20Sopenharmony_ci	wlc->shortslot_override = shortslot;
52788c2ecf20Sopenharmony_ci
52798c2ecf20Sopenharmony_ci	/* Use the default 11g rateset */
52808c2ecf20Sopenharmony_ci	if (!rs.count)
52818c2ecf20Sopenharmony_ci		brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
52828c2ecf20Sopenharmony_ci
52838c2ecf20Sopenharmony_ci	if (ofdm_basic) {
52848c2ecf20Sopenharmony_ci		for (i = 0; i < rs.count; i++) {
52858c2ecf20Sopenharmony_ci			if (rs.rates[i] == BRCM_RATE_6M
52868c2ecf20Sopenharmony_ci			    || rs.rates[i] == BRCM_RATE_12M
52878c2ecf20Sopenharmony_ci			    || rs.rates[i] == BRCM_RATE_24M)
52888c2ecf20Sopenharmony_ci				rs.rates[i] |= BRCMS_RATE_FLAG;
52898c2ecf20Sopenharmony_ci		}
52908c2ecf20Sopenharmony_ci	}
52918c2ecf20Sopenharmony_ci
52928c2ecf20Sopenharmony_ci	/* Set default bss rateset */
52938c2ecf20Sopenharmony_ci	wlc->default_bss->rateset.count = rs.count;
52948c2ecf20Sopenharmony_ci	memcpy(wlc->default_bss->rateset.rates, rs.rates,
52958c2ecf20Sopenharmony_ci	       sizeof(wlc->default_bss->rateset.rates));
52968c2ecf20Sopenharmony_ci
52978c2ecf20Sopenharmony_ci	return ret;
52988c2ecf20Sopenharmony_ci}
52998c2ecf20Sopenharmony_ci
53008c2ecf20Sopenharmony_ciint brcms_c_set_nmode(struct brcms_c_info *wlc)
53018c2ecf20Sopenharmony_ci{
53028c2ecf20Sopenharmony_ci	uint i;
53038c2ecf20Sopenharmony_ci	s32 nmode = AUTO;
53048c2ecf20Sopenharmony_ci
53058c2ecf20Sopenharmony_ci	if (wlc->stf->txstreams == WL_11N_3x3)
53068c2ecf20Sopenharmony_ci		nmode = WL_11N_3x3;
53078c2ecf20Sopenharmony_ci	else
53088c2ecf20Sopenharmony_ci		nmode = WL_11N_2x2;
53098c2ecf20Sopenharmony_ci
53108c2ecf20Sopenharmony_ci	/* force GMODE_AUTO if NMODE is ON */
53118c2ecf20Sopenharmony_ci	brcms_c_set_gmode(wlc, GMODE_AUTO, true);
53128c2ecf20Sopenharmony_ci	if (nmode == WL_11N_3x3)
53138c2ecf20Sopenharmony_ci		wlc->pub->_n_enab = SUPPORT_HT;
53148c2ecf20Sopenharmony_ci	else
53158c2ecf20Sopenharmony_ci		wlc->pub->_n_enab = SUPPORT_11N;
53168c2ecf20Sopenharmony_ci	wlc->default_bss->flags |= BRCMS_BSS_HT;
53178c2ecf20Sopenharmony_ci	/* add the mcs rates to the default and hw ratesets */
53188c2ecf20Sopenharmony_ci	brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
53198c2ecf20Sopenharmony_ci			      wlc->stf->txstreams);
53208c2ecf20Sopenharmony_ci	for (i = 0; i < wlc->pub->_nbands; i++)
53218c2ecf20Sopenharmony_ci		memcpy(wlc->bandstate[i]->hw_rateset.mcs,
53228c2ecf20Sopenharmony_ci		       wlc->default_bss->rateset.mcs, MCSSET_LEN);
53238c2ecf20Sopenharmony_ci
53248c2ecf20Sopenharmony_ci	return 0;
53258c2ecf20Sopenharmony_ci}
53268c2ecf20Sopenharmony_ci
53278c2ecf20Sopenharmony_cistatic int
53288c2ecf20Sopenharmony_cibrcms_c_set_internal_rateset(struct brcms_c_info *wlc,
53298c2ecf20Sopenharmony_ci			     struct brcms_c_rateset *rs_arg)
53308c2ecf20Sopenharmony_ci{
53318c2ecf20Sopenharmony_ci	struct brcms_c_rateset rs, new;
53328c2ecf20Sopenharmony_ci	uint bandunit;
53338c2ecf20Sopenharmony_ci
53348c2ecf20Sopenharmony_ci	memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
53358c2ecf20Sopenharmony_ci
53368c2ecf20Sopenharmony_ci	/* check for bad count value */
53378c2ecf20Sopenharmony_ci	if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
53388c2ecf20Sopenharmony_ci		return -EINVAL;
53398c2ecf20Sopenharmony_ci
53408c2ecf20Sopenharmony_ci	/* try the current band */
53418c2ecf20Sopenharmony_ci	bandunit = wlc->band->bandunit;
53428c2ecf20Sopenharmony_ci	memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
53438c2ecf20Sopenharmony_ci	if (brcms_c_rate_hwrs_filter_sort_validate
53448c2ecf20Sopenharmony_ci	    (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
53458c2ecf20Sopenharmony_ci	     wlc->stf->txstreams))
53468c2ecf20Sopenharmony_ci		goto good;
53478c2ecf20Sopenharmony_ci
53488c2ecf20Sopenharmony_ci	/* try the other band */
53498c2ecf20Sopenharmony_ci	if (brcms_is_mband_unlocked(wlc)) {
53508c2ecf20Sopenharmony_ci		bandunit = OTHERBANDUNIT(wlc);
53518c2ecf20Sopenharmony_ci		memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
53528c2ecf20Sopenharmony_ci		if (brcms_c_rate_hwrs_filter_sort_validate(&new,
53538c2ecf20Sopenharmony_ci						       &wlc->
53548c2ecf20Sopenharmony_ci						       bandstate[bandunit]->
53558c2ecf20Sopenharmony_ci						       hw_rateset, true,
53568c2ecf20Sopenharmony_ci						       wlc->stf->txstreams))
53578c2ecf20Sopenharmony_ci			goto good;
53588c2ecf20Sopenharmony_ci	}
53598c2ecf20Sopenharmony_ci
53608c2ecf20Sopenharmony_ci	return -EBADE;
53618c2ecf20Sopenharmony_ci
53628c2ecf20Sopenharmony_ci good:
53638c2ecf20Sopenharmony_ci	/* apply new rateset */
53648c2ecf20Sopenharmony_ci	memcpy(&wlc->default_bss->rateset, &new,
53658c2ecf20Sopenharmony_ci	       sizeof(struct brcms_c_rateset));
53668c2ecf20Sopenharmony_ci	memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
53678c2ecf20Sopenharmony_ci	       sizeof(struct brcms_c_rateset));
53688c2ecf20Sopenharmony_ci	return 0;
53698c2ecf20Sopenharmony_ci}
53708c2ecf20Sopenharmony_ci
53718c2ecf20Sopenharmony_cistatic void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
53728c2ecf20Sopenharmony_ci{
53738c2ecf20Sopenharmony_ci	wlc_phy_ofdm_rateset_war(wlc->band->pi, false);
53748c2ecf20Sopenharmony_ci}
53758c2ecf20Sopenharmony_ci
53768c2ecf20Sopenharmony_ciint brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
53778c2ecf20Sopenharmony_ci{
53788c2ecf20Sopenharmony_ci	u16 chspec = ch20mhz_chspec(channel);
53798c2ecf20Sopenharmony_ci
53808c2ecf20Sopenharmony_ci	if (channel > MAXCHANNEL)
53818c2ecf20Sopenharmony_ci		return -EINVAL;
53828c2ecf20Sopenharmony_ci
53838c2ecf20Sopenharmony_ci	if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
53848c2ecf20Sopenharmony_ci		return -EINVAL;
53858c2ecf20Sopenharmony_ci
53868c2ecf20Sopenharmony_ci
53878c2ecf20Sopenharmony_ci	if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
53888c2ecf20Sopenharmony_ci		if (wlc->band->bandunit != chspec_bandunit(chspec))
53898c2ecf20Sopenharmony_ci			wlc->bandinit_pending = true;
53908c2ecf20Sopenharmony_ci		else
53918c2ecf20Sopenharmony_ci			wlc->bandinit_pending = false;
53928c2ecf20Sopenharmony_ci	}
53938c2ecf20Sopenharmony_ci
53948c2ecf20Sopenharmony_ci	wlc->default_bss->chanspec = chspec;
53958c2ecf20Sopenharmony_ci	/* brcms_c_BSSinit() will sanitize the rateset before
53968c2ecf20Sopenharmony_ci	 * using it.. */
53978c2ecf20Sopenharmony_ci	if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
53988c2ecf20Sopenharmony_ci		brcms_c_set_home_chanspec(wlc, chspec);
53998c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc);
54008c2ecf20Sopenharmony_ci		brcms_c_set_chanspec(wlc, chspec);
54018c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc);
54028c2ecf20Sopenharmony_ci	}
54038c2ecf20Sopenharmony_ci	return 0;
54048c2ecf20Sopenharmony_ci}
54058c2ecf20Sopenharmony_ci
54068c2ecf20Sopenharmony_ciint brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
54078c2ecf20Sopenharmony_ci{
54088c2ecf20Sopenharmony_ci	int ac;
54098c2ecf20Sopenharmony_ci
54108c2ecf20Sopenharmony_ci	if (srl < 1 || srl > RETRY_SHORT_MAX ||
54118c2ecf20Sopenharmony_ci	    lrl < 1 || lrl > RETRY_SHORT_MAX)
54128c2ecf20Sopenharmony_ci		return -EINVAL;
54138c2ecf20Sopenharmony_ci
54148c2ecf20Sopenharmony_ci	wlc->SRL = srl;
54158c2ecf20Sopenharmony_ci	wlc->LRL = lrl;
54168c2ecf20Sopenharmony_ci
54178c2ecf20Sopenharmony_ci	brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
54188c2ecf20Sopenharmony_ci
54198c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
54208c2ecf20Sopenharmony_ci		wlc->wme_retries[ac] =	SFIELD(wlc->wme_retries[ac],
54218c2ecf20Sopenharmony_ci					       EDCF_SHORT,  wlc->SRL);
54228c2ecf20Sopenharmony_ci		wlc->wme_retries[ac] =	SFIELD(wlc->wme_retries[ac],
54238c2ecf20Sopenharmony_ci					       EDCF_LONG, wlc->LRL);
54248c2ecf20Sopenharmony_ci	}
54258c2ecf20Sopenharmony_ci	brcms_c_wme_retries_write(wlc);
54268c2ecf20Sopenharmony_ci
54278c2ecf20Sopenharmony_ci	return 0;
54288c2ecf20Sopenharmony_ci}
54298c2ecf20Sopenharmony_ci
54308c2ecf20Sopenharmony_civoid brcms_c_get_current_rateset(struct brcms_c_info *wlc,
54318c2ecf20Sopenharmony_ci				 struct brcm_rateset *currs)
54328c2ecf20Sopenharmony_ci{
54338c2ecf20Sopenharmony_ci	struct brcms_c_rateset *rs;
54348c2ecf20Sopenharmony_ci
54358c2ecf20Sopenharmony_ci	if (wlc->pub->associated)
54368c2ecf20Sopenharmony_ci		rs = &wlc->bsscfg->current_bss->rateset;
54378c2ecf20Sopenharmony_ci	else
54388c2ecf20Sopenharmony_ci		rs = &wlc->default_bss->rateset;
54398c2ecf20Sopenharmony_ci
54408c2ecf20Sopenharmony_ci	/* Copy only legacy rateset section */
54418c2ecf20Sopenharmony_ci	currs->count = rs->count;
54428c2ecf20Sopenharmony_ci	memcpy(&currs->rates, &rs->rates, rs->count);
54438c2ecf20Sopenharmony_ci}
54448c2ecf20Sopenharmony_ci
54458c2ecf20Sopenharmony_ciint brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
54468c2ecf20Sopenharmony_ci{
54478c2ecf20Sopenharmony_ci	struct brcms_c_rateset internal_rs;
54488c2ecf20Sopenharmony_ci	int bcmerror;
54498c2ecf20Sopenharmony_ci
54508c2ecf20Sopenharmony_ci	if (rs->count > BRCMS_NUMRATES)
54518c2ecf20Sopenharmony_ci		return -ENOBUFS;
54528c2ecf20Sopenharmony_ci
54538c2ecf20Sopenharmony_ci	memset(&internal_rs, 0, sizeof(internal_rs));
54548c2ecf20Sopenharmony_ci
54558c2ecf20Sopenharmony_ci	/* Copy only legacy rateset section */
54568c2ecf20Sopenharmony_ci	internal_rs.count = rs->count;
54578c2ecf20Sopenharmony_ci	memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
54588c2ecf20Sopenharmony_ci
54598c2ecf20Sopenharmony_ci	/* merge rateset coming in with the current mcsset */
54608c2ecf20Sopenharmony_ci	if (wlc->pub->_n_enab & SUPPORT_11N) {
54618c2ecf20Sopenharmony_ci		struct brcms_bss_info *mcsset_bss;
54628c2ecf20Sopenharmony_ci		if (wlc->pub->associated)
54638c2ecf20Sopenharmony_ci			mcsset_bss = wlc->bsscfg->current_bss;
54648c2ecf20Sopenharmony_ci		else
54658c2ecf20Sopenharmony_ci			mcsset_bss = wlc->default_bss;
54668c2ecf20Sopenharmony_ci		memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
54678c2ecf20Sopenharmony_ci		       MCSSET_LEN);
54688c2ecf20Sopenharmony_ci	}
54698c2ecf20Sopenharmony_ci
54708c2ecf20Sopenharmony_ci	bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
54718c2ecf20Sopenharmony_ci	if (!bcmerror)
54728c2ecf20Sopenharmony_ci		brcms_c_ofdm_rateset_war(wlc);
54738c2ecf20Sopenharmony_ci
54748c2ecf20Sopenharmony_ci	return bcmerror;
54758c2ecf20Sopenharmony_ci}
54768c2ecf20Sopenharmony_ci
54778c2ecf20Sopenharmony_cistatic void brcms_c_time_lock(struct brcms_c_info *wlc)
54788c2ecf20Sopenharmony_ci{
54798c2ecf20Sopenharmony_ci	bcma_set32(wlc->hw->d11core, D11REGOFFS(maccontrol), MCTL_TBTTHOLD);
54808c2ecf20Sopenharmony_ci	/* Commit the write */
54818c2ecf20Sopenharmony_ci	bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
54828c2ecf20Sopenharmony_ci}
54838c2ecf20Sopenharmony_ci
54848c2ecf20Sopenharmony_cistatic void brcms_c_time_unlock(struct brcms_c_info *wlc)
54858c2ecf20Sopenharmony_ci{
54868c2ecf20Sopenharmony_ci	bcma_mask32(wlc->hw->d11core, D11REGOFFS(maccontrol), ~MCTL_TBTTHOLD);
54878c2ecf20Sopenharmony_ci	/* Commit the write */
54888c2ecf20Sopenharmony_ci	bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
54898c2ecf20Sopenharmony_ci}
54908c2ecf20Sopenharmony_ci
54918c2ecf20Sopenharmony_ciint brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
54928c2ecf20Sopenharmony_ci{
54938c2ecf20Sopenharmony_ci	u32 bcnint_us;
54948c2ecf20Sopenharmony_ci
54958c2ecf20Sopenharmony_ci	if (period == 0)
54968c2ecf20Sopenharmony_ci		return -EINVAL;
54978c2ecf20Sopenharmony_ci
54988c2ecf20Sopenharmony_ci	wlc->default_bss->beacon_period = period;
54998c2ecf20Sopenharmony_ci
55008c2ecf20Sopenharmony_ci	bcnint_us = period << 10;
55018c2ecf20Sopenharmony_ci	brcms_c_time_lock(wlc);
55028c2ecf20Sopenharmony_ci	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfprep),
55038c2ecf20Sopenharmony_ci		     (bcnint_us << CFPREP_CBI_SHIFT));
55048c2ecf20Sopenharmony_ci	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfpstart), bcnint_us);
55058c2ecf20Sopenharmony_ci	brcms_c_time_unlock(wlc);
55068c2ecf20Sopenharmony_ci
55078c2ecf20Sopenharmony_ci	return 0;
55088c2ecf20Sopenharmony_ci}
55098c2ecf20Sopenharmony_ci
55108c2ecf20Sopenharmony_ciu16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
55118c2ecf20Sopenharmony_ci{
55128c2ecf20Sopenharmony_ci	return wlc->band->phytype;
55138c2ecf20Sopenharmony_ci}
55148c2ecf20Sopenharmony_ci
55158c2ecf20Sopenharmony_civoid brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
55168c2ecf20Sopenharmony_ci{
55178c2ecf20Sopenharmony_ci	wlc->shortslot_override = sslot_override;
55188c2ecf20Sopenharmony_ci
55198c2ecf20Sopenharmony_ci	/*
55208c2ecf20Sopenharmony_ci	 * shortslot is an 11g feature, so no more work if we are
55218c2ecf20Sopenharmony_ci	 * currently on the 5G band
55228c2ecf20Sopenharmony_ci	 */
55238c2ecf20Sopenharmony_ci	if (wlc->band->bandtype == BRCM_BAND_5G)
55248c2ecf20Sopenharmony_ci		return;
55258c2ecf20Sopenharmony_ci
55268c2ecf20Sopenharmony_ci	if (wlc->pub->up && wlc->pub->associated) {
55278c2ecf20Sopenharmony_ci		/* let watchdog or beacon processing update shortslot */
55288c2ecf20Sopenharmony_ci	} else if (wlc->pub->up) {
55298c2ecf20Sopenharmony_ci		/* unassociated shortslot is off */
55308c2ecf20Sopenharmony_ci		brcms_c_switch_shortslot(wlc, false);
55318c2ecf20Sopenharmony_ci	} else {
55328c2ecf20Sopenharmony_ci		/* driver is down, so just update the brcms_c_info
55338c2ecf20Sopenharmony_ci		 * value */
55348c2ecf20Sopenharmony_ci		if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
55358c2ecf20Sopenharmony_ci			wlc->shortslot = false;
55368c2ecf20Sopenharmony_ci		else
55378c2ecf20Sopenharmony_ci			wlc->shortslot =
55388c2ecf20Sopenharmony_ci			    (wlc->shortslot_override ==
55398c2ecf20Sopenharmony_ci			     BRCMS_SHORTSLOT_ON);
55408c2ecf20Sopenharmony_ci	}
55418c2ecf20Sopenharmony_ci}
55428c2ecf20Sopenharmony_ci
55438c2ecf20Sopenharmony_ci/*
55448c2ecf20Sopenharmony_ci * register watchdog and down handlers.
55458c2ecf20Sopenharmony_ci */
55468c2ecf20Sopenharmony_ciint brcms_c_module_register(struct brcms_pub *pub,
55478c2ecf20Sopenharmony_ci			    const char *name, struct brcms_info *hdl,
55488c2ecf20Sopenharmony_ci			    int (*d_fn)(void *handle))
55498c2ecf20Sopenharmony_ci{
55508c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
55518c2ecf20Sopenharmony_ci	int i;
55528c2ecf20Sopenharmony_ci
55538c2ecf20Sopenharmony_ci	/* find an empty entry and just add, no duplication check! */
55548c2ecf20Sopenharmony_ci	for (i = 0; i < BRCMS_MAXMODULES; i++) {
55558c2ecf20Sopenharmony_ci		if (wlc->modulecb[i].name[0] == '\0') {
55568c2ecf20Sopenharmony_ci			strncpy(wlc->modulecb[i].name, name,
55578c2ecf20Sopenharmony_ci				sizeof(wlc->modulecb[i].name) - 1);
55588c2ecf20Sopenharmony_ci			wlc->modulecb[i].hdl = hdl;
55598c2ecf20Sopenharmony_ci			wlc->modulecb[i].down_fn = d_fn;
55608c2ecf20Sopenharmony_ci			return 0;
55618c2ecf20Sopenharmony_ci		}
55628c2ecf20Sopenharmony_ci	}
55638c2ecf20Sopenharmony_ci
55648c2ecf20Sopenharmony_ci	return -ENOSR;
55658c2ecf20Sopenharmony_ci}
55668c2ecf20Sopenharmony_ci
55678c2ecf20Sopenharmony_ci/* unregister module callbacks */
55688c2ecf20Sopenharmony_ciint brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
55698c2ecf20Sopenharmony_ci			      struct brcms_info *hdl)
55708c2ecf20Sopenharmony_ci{
55718c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
55728c2ecf20Sopenharmony_ci	int i;
55738c2ecf20Sopenharmony_ci
55748c2ecf20Sopenharmony_ci	if (wlc == NULL)
55758c2ecf20Sopenharmony_ci		return -ENODATA;
55768c2ecf20Sopenharmony_ci
55778c2ecf20Sopenharmony_ci	for (i = 0; i < BRCMS_MAXMODULES; i++) {
55788c2ecf20Sopenharmony_ci		if (!strcmp(wlc->modulecb[i].name, name) &&
55798c2ecf20Sopenharmony_ci		    (wlc->modulecb[i].hdl == hdl)) {
55808c2ecf20Sopenharmony_ci			memset(&wlc->modulecb[i], 0, sizeof(wlc->modulecb[i]));
55818c2ecf20Sopenharmony_ci			return 0;
55828c2ecf20Sopenharmony_ci		}
55838c2ecf20Sopenharmony_ci	}
55848c2ecf20Sopenharmony_ci
55858c2ecf20Sopenharmony_ci	/* table not found! */
55868c2ecf20Sopenharmony_ci	return -ENODATA;
55878c2ecf20Sopenharmony_ci}
55888c2ecf20Sopenharmony_ci
55898c2ecf20Sopenharmony_cistatic bool brcms_c_chipmatch_pci(struct bcma_device *core)
55908c2ecf20Sopenharmony_ci{
55918c2ecf20Sopenharmony_ci	struct pci_dev *pcidev = core->bus->host_pci;
55928c2ecf20Sopenharmony_ci	u16 vendor = pcidev->vendor;
55938c2ecf20Sopenharmony_ci	u16 device = pcidev->device;
55948c2ecf20Sopenharmony_ci
55958c2ecf20Sopenharmony_ci	if (vendor != PCI_VENDOR_ID_BROADCOM) {
55968c2ecf20Sopenharmony_ci		pr_err("unknown vendor id %04x\n", vendor);
55978c2ecf20Sopenharmony_ci		return false;
55988c2ecf20Sopenharmony_ci	}
55998c2ecf20Sopenharmony_ci
56008c2ecf20Sopenharmony_ci	if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
56018c2ecf20Sopenharmony_ci		return true;
56028c2ecf20Sopenharmony_ci	if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
56038c2ecf20Sopenharmony_ci		return true;
56048c2ecf20Sopenharmony_ci	if (device == BCM4313_D11N2G_ID || device == BCM4313_CHIP_ID)
56058c2ecf20Sopenharmony_ci		return true;
56068c2ecf20Sopenharmony_ci	if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
56078c2ecf20Sopenharmony_ci		return true;
56088c2ecf20Sopenharmony_ci
56098c2ecf20Sopenharmony_ci	pr_err("unknown device id %04x\n", device);
56108c2ecf20Sopenharmony_ci	return false;
56118c2ecf20Sopenharmony_ci}
56128c2ecf20Sopenharmony_ci
56138c2ecf20Sopenharmony_cistatic bool brcms_c_chipmatch_soc(struct bcma_device *core)
56148c2ecf20Sopenharmony_ci{
56158c2ecf20Sopenharmony_ci	struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
56168c2ecf20Sopenharmony_ci
56178c2ecf20Sopenharmony_ci	if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
56188c2ecf20Sopenharmony_ci		return true;
56198c2ecf20Sopenharmony_ci
56208c2ecf20Sopenharmony_ci	pr_err("unknown chip id %04x\n", chipinfo->id);
56218c2ecf20Sopenharmony_ci	return false;
56228c2ecf20Sopenharmony_ci}
56238c2ecf20Sopenharmony_ci
56248c2ecf20Sopenharmony_cibool brcms_c_chipmatch(struct bcma_device *core)
56258c2ecf20Sopenharmony_ci{
56268c2ecf20Sopenharmony_ci	switch (core->bus->hosttype) {
56278c2ecf20Sopenharmony_ci	case BCMA_HOSTTYPE_PCI:
56288c2ecf20Sopenharmony_ci		return brcms_c_chipmatch_pci(core);
56298c2ecf20Sopenharmony_ci	case BCMA_HOSTTYPE_SOC:
56308c2ecf20Sopenharmony_ci		return brcms_c_chipmatch_soc(core);
56318c2ecf20Sopenharmony_ci	default:
56328c2ecf20Sopenharmony_ci		pr_err("unknown host type: %i\n", core->bus->hosttype);
56338c2ecf20Sopenharmony_ci		return false;
56348c2ecf20Sopenharmony_ci	}
56358c2ecf20Sopenharmony_ci}
56368c2ecf20Sopenharmony_ci
56378c2ecf20Sopenharmony_ciu16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
56388c2ecf20Sopenharmony_ci{
56398c2ecf20Sopenharmony_ci	u16 table_ptr;
56408c2ecf20Sopenharmony_ci	u8 phy_rate, index;
56418c2ecf20Sopenharmony_ci
56428c2ecf20Sopenharmony_ci	/* get the phy specific rate encoding for the PLCP SIGNAL field */
56438c2ecf20Sopenharmony_ci	if (is_ofdm_rate(rate))
56448c2ecf20Sopenharmony_ci		table_ptr = M_RT_DIRMAP_A;
56458c2ecf20Sopenharmony_ci	else
56468c2ecf20Sopenharmony_ci		table_ptr = M_RT_DIRMAP_B;
56478c2ecf20Sopenharmony_ci
56488c2ecf20Sopenharmony_ci	/* for a given rate, the LS-nibble of the PLCP SIGNAL field is
56498c2ecf20Sopenharmony_ci	 * the index into the rate table.
56508c2ecf20Sopenharmony_ci	 */
56518c2ecf20Sopenharmony_ci	phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
56528c2ecf20Sopenharmony_ci	index = phy_rate & 0xf;
56538c2ecf20Sopenharmony_ci
56548c2ecf20Sopenharmony_ci	/* Find the SHM pointer to the rate table entry by looking in the
56558c2ecf20Sopenharmony_ci	 * Direct-map Table
56568c2ecf20Sopenharmony_ci	 */
56578c2ecf20Sopenharmony_ci	return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
56588c2ecf20Sopenharmony_ci}
56598c2ecf20Sopenharmony_ci
56608c2ecf20Sopenharmony_ci/*
56618c2ecf20Sopenharmony_ci * bcmc_fid_generate:
56628c2ecf20Sopenharmony_ci * Generate frame ID for a BCMC packet.  The frag field is not used
56638c2ecf20Sopenharmony_ci * for MC frames so is used as part of the sequence number.
56648c2ecf20Sopenharmony_ci */
56658c2ecf20Sopenharmony_cistatic inline u16
56668c2ecf20Sopenharmony_cibcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
56678c2ecf20Sopenharmony_ci		  struct d11txh *txh)
56688c2ecf20Sopenharmony_ci{
56698c2ecf20Sopenharmony_ci	u16 frameid;
56708c2ecf20Sopenharmony_ci
56718c2ecf20Sopenharmony_ci	frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
56728c2ecf20Sopenharmony_ci						  TXFID_QUEUE_MASK);
56738c2ecf20Sopenharmony_ci	frameid |=
56748c2ecf20Sopenharmony_ci	    (((wlc->
56758c2ecf20Sopenharmony_ci	       mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
56768c2ecf20Sopenharmony_ci	    TX_BCMC_FIFO;
56778c2ecf20Sopenharmony_ci
56788c2ecf20Sopenharmony_ci	return frameid;
56798c2ecf20Sopenharmony_ci}
56808c2ecf20Sopenharmony_ci
56818c2ecf20Sopenharmony_cistatic uint
56828c2ecf20Sopenharmony_cibrcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
56838c2ecf20Sopenharmony_ci		      u8 preamble_type)
56848c2ecf20Sopenharmony_ci{
56858c2ecf20Sopenharmony_ci	uint dur = 0;
56868c2ecf20Sopenharmony_ci
56878c2ecf20Sopenharmony_ci	/*
56888c2ecf20Sopenharmony_ci	 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
56898c2ecf20Sopenharmony_ci	 * is less than or equal to the rate of the immediately previous
56908c2ecf20Sopenharmony_ci	 * frame in the FES
56918c2ecf20Sopenharmony_ci	 */
56928c2ecf20Sopenharmony_ci	rspec = brcms_basic_rate(wlc, rspec);
56938c2ecf20Sopenharmony_ci	/* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
56948c2ecf20Sopenharmony_ci	dur =
56958c2ecf20Sopenharmony_ci	    brcms_c_calc_frame_time(wlc, rspec, preamble_type,
56968c2ecf20Sopenharmony_ci				(DOT11_ACK_LEN + FCS_LEN));
56978c2ecf20Sopenharmony_ci	return dur;
56988c2ecf20Sopenharmony_ci}
56998c2ecf20Sopenharmony_ci
57008c2ecf20Sopenharmony_cistatic uint
57018c2ecf20Sopenharmony_cibrcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
57028c2ecf20Sopenharmony_ci		      u8 preamble_type)
57038c2ecf20Sopenharmony_ci{
57048c2ecf20Sopenharmony_ci	return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
57058c2ecf20Sopenharmony_ci}
57068c2ecf20Sopenharmony_ci
57078c2ecf20Sopenharmony_cistatic uint
57088c2ecf20Sopenharmony_cibrcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
57098c2ecf20Sopenharmony_ci		     u8 preamble_type)
57108c2ecf20Sopenharmony_ci{
57118c2ecf20Sopenharmony_ci	/*
57128c2ecf20Sopenharmony_ci	 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
57138c2ecf20Sopenharmony_ci	 * is less than or equal to the rate of the immediately previous
57148c2ecf20Sopenharmony_ci	 * frame in the FES
57158c2ecf20Sopenharmony_ci	 */
57168c2ecf20Sopenharmony_ci	rspec = brcms_basic_rate(wlc, rspec);
57178c2ecf20Sopenharmony_ci	/* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
57188c2ecf20Sopenharmony_ci	return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
57198c2ecf20Sopenharmony_ci				   (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
57208c2ecf20Sopenharmony_ci				    FCS_LEN));
57218c2ecf20Sopenharmony_ci}
57228c2ecf20Sopenharmony_ci
57238c2ecf20Sopenharmony_ci/* brcms_c_compute_frame_dur()
57248c2ecf20Sopenharmony_ci *
57258c2ecf20Sopenharmony_ci * Calculate the 802.11 MAC header DUR field for MPDU
57268c2ecf20Sopenharmony_ci * DUR for a single frame = 1 SIFS + 1 ACK
57278c2ecf20Sopenharmony_ci * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
57288c2ecf20Sopenharmony_ci *
57298c2ecf20Sopenharmony_ci * rate			MPDU rate in unit of 500kbps
57308c2ecf20Sopenharmony_ci * next_frag_len	next MPDU length in bytes
57318c2ecf20Sopenharmony_ci * preamble_type	use short/GF or long/MM PLCP header
57328c2ecf20Sopenharmony_ci */
57338c2ecf20Sopenharmony_cistatic u16
57348c2ecf20Sopenharmony_cibrcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
57358c2ecf20Sopenharmony_ci		      u8 preamble_type, uint next_frag_len)
57368c2ecf20Sopenharmony_ci{
57378c2ecf20Sopenharmony_ci	u16 dur, sifs;
57388c2ecf20Sopenharmony_ci
57398c2ecf20Sopenharmony_ci	sifs = get_sifs(wlc->band);
57408c2ecf20Sopenharmony_ci
57418c2ecf20Sopenharmony_ci	dur = sifs;
57428c2ecf20Sopenharmony_ci	dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
57438c2ecf20Sopenharmony_ci
57448c2ecf20Sopenharmony_ci	if (next_frag_len) {
57458c2ecf20Sopenharmony_ci		/* Double the current DUR to get 2 SIFS + 2 ACKs */
57468c2ecf20Sopenharmony_ci		dur *= 2;
57478c2ecf20Sopenharmony_ci		/* add another SIFS and the frag time */
57488c2ecf20Sopenharmony_ci		dur += sifs;
57498c2ecf20Sopenharmony_ci		dur +=
57508c2ecf20Sopenharmony_ci		    (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
57518c2ecf20Sopenharmony_ci						 next_frag_len);
57528c2ecf20Sopenharmony_ci	}
57538c2ecf20Sopenharmony_ci	return dur;
57548c2ecf20Sopenharmony_ci}
57558c2ecf20Sopenharmony_ci
57568c2ecf20Sopenharmony_ci/* The opposite of brcms_c_calc_frame_time */
57578c2ecf20Sopenharmony_cistatic uint
57588c2ecf20Sopenharmony_cibrcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
57598c2ecf20Sopenharmony_ci		   u8 preamble_type, uint dur)
57608c2ecf20Sopenharmony_ci{
57618c2ecf20Sopenharmony_ci	uint nsyms, mac_len, Ndps, kNdps;
57628c2ecf20Sopenharmony_ci	uint rate = rspec2rate(ratespec);
57638c2ecf20Sopenharmony_ci
57648c2ecf20Sopenharmony_ci	if (is_mcs_rate(ratespec)) {
57658c2ecf20Sopenharmony_ci		uint mcs = ratespec & RSPEC_RATE_MASK;
57668c2ecf20Sopenharmony_ci		int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
57678c2ecf20Sopenharmony_ci		dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
57688c2ecf20Sopenharmony_ci		/* payload calculation matches that of regular ofdm */
57698c2ecf20Sopenharmony_ci		if (wlc->band->bandtype == BRCM_BAND_2G)
57708c2ecf20Sopenharmony_ci			dur -= DOT11_OFDM_SIGNAL_EXTENSION;
57718c2ecf20Sopenharmony_ci		/* kNdbps = kbps * 4 */
57728c2ecf20Sopenharmony_ci		kNdps =	mcs_2_rate(mcs, rspec_is40mhz(ratespec),
57738c2ecf20Sopenharmony_ci				   rspec_issgi(ratespec)) * 4;
57748c2ecf20Sopenharmony_ci		nsyms = dur / APHY_SYMBOL_TIME;
57758c2ecf20Sopenharmony_ci		mac_len =
57768c2ecf20Sopenharmony_ci		    ((nsyms * kNdps) -
57778c2ecf20Sopenharmony_ci		     ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
57788c2ecf20Sopenharmony_ci	} else if (is_ofdm_rate(ratespec)) {
57798c2ecf20Sopenharmony_ci		dur -= APHY_PREAMBLE_TIME;
57808c2ecf20Sopenharmony_ci		dur -= APHY_SIGNAL_TIME;
57818c2ecf20Sopenharmony_ci		/* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
57828c2ecf20Sopenharmony_ci		Ndps = rate * 2;
57838c2ecf20Sopenharmony_ci		nsyms = dur / APHY_SYMBOL_TIME;
57848c2ecf20Sopenharmony_ci		mac_len =
57858c2ecf20Sopenharmony_ci		    ((nsyms * Ndps) -
57868c2ecf20Sopenharmony_ci		     (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
57878c2ecf20Sopenharmony_ci	} else {
57888c2ecf20Sopenharmony_ci		if (preamble_type & BRCMS_SHORT_PREAMBLE)
57898c2ecf20Sopenharmony_ci			dur -= BPHY_PLCP_SHORT_TIME;
57908c2ecf20Sopenharmony_ci		else
57918c2ecf20Sopenharmony_ci			dur -= BPHY_PLCP_TIME;
57928c2ecf20Sopenharmony_ci		mac_len = dur * rate;
57938c2ecf20Sopenharmony_ci		/* divide out factor of 2 in rate (1/2 mbps) */
57948c2ecf20Sopenharmony_ci		mac_len = mac_len / 8 / 2;
57958c2ecf20Sopenharmony_ci	}
57968c2ecf20Sopenharmony_ci	return mac_len;
57978c2ecf20Sopenharmony_ci}
57988c2ecf20Sopenharmony_ci
57998c2ecf20Sopenharmony_ci/*
58008c2ecf20Sopenharmony_ci * Return true if the specified rate is supported by the specified band.
58018c2ecf20Sopenharmony_ci * BRCM_BAND_AUTO indicates the current band.
58028c2ecf20Sopenharmony_ci */
58038c2ecf20Sopenharmony_cistatic bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
58048c2ecf20Sopenharmony_ci		    bool verbose)
58058c2ecf20Sopenharmony_ci{
58068c2ecf20Sopenharmony_ci	struct brcms_c_rateset *hw_rateset;
58078c2ecf20Sopenharmony_ci	uint i;
58088c2ecf20Sopenharmony_ci
58098c2ecf20Sopenharmony_ci	if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
58108c2ecf20Sopenharmony_ci		hw_rateset = &wlc->band->hw_rateset;
58118c2ecf20Sopenharmony_ci	else if (wlc->pub->_nbands > 1)
58128c2ecf20Sopenharmony_ci		hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
58138c2ecf20Sopenharmony_ci	else
58148c2ecf20Sopenharmony_ci		/* other band specified and we are a single band device */
58158c2ecf20Sopenharmony_ci		return false;
58168c2ecf20Sopenharmony_ci
58178c2ecf20Sopenharmony_ci	/* check if this is a mimo rate */
58188c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec)) {
58198c2ecf20Sopenharmony_ci		if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
58208c2ecf20Sopenharmony_ci			goto error;
58218c2ecf20Sopenharmony_ci
58228c2ecf20Sopenharmony_ci		return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
58238c2ecf20Sopenharmony_ci	}
58248c2ecf20Sopenharmony_ci
58258c2ecf20Sopenharmony_ci	for (i = 0; i < hw_rateset->count; i++)
58268c2ecf20Sopenharmony_ci		if (hw_rateset->rates[i] == rspec2rate(rspec))
58278c2ecf20Sopenharmony_ci			return true;
58288c2ecf20Sopenharmony_ci error:
58298c2ecf20Sopenharmony_ci	if (verbose)
58308c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
58318c2ecf20Sopenharmony_ci			  "not in hw_rateset\n", wlc->pub->unit, rspec);
58328c2ecf20Sopenharmony_ci
58338c2ecf20Sopenharmony_ci	return false;
58348c2ecf20Sopenharmony_ci}
58358c2ecf20Sopenharmony_ci
58368c2ecf20Sopenharmony_cistatic u32
58378c2ecf20Sopenharmony_cimac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
58388c2ecf20Sopenharmony_ci		       u32 int_val)
58398c2ecf20Sopenharmony_ci{
58408c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc->hw->d11core;
58418c2ecf20Sopenharmony_ci	u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
58428c2ecf20Sopenharmony_ci	u8 rate = int_val & NRATE_RATE_MASK;
58438c2ecf20Sopenharmony_ci	u32 rspec;
58448c2ecf20Sopenharmony_ci	bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
58458c2ecf20Sopenharmony_ci	bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
58468c2ecf20Sopenharmony_ci	bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
58478c2ecf20Sopenharmony_ci				  == NRATE_OVERRIDE_MCS_ONLY);
58488c2ecf20Sopenharmony_ci
58498c2ecf20Sopenharmony_ci	if (!ismcs)
58508c2ecf20Sopenharmony_ci		return (u32) rate;
58518c2ecf20Sopenharmony_ci
58528c2ecf20Sopenharmony_ci	/* validate the combination of rate/mcs/stf is allowed */
58538c2ecf20Sopenharmony_ci	if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
58548c2ecf20Sopenharmony_ci		/* mcs only allowed when nmode */
58558c2ecf20Sopenharmony_ci		if (stf > PHY_TXC1_MODE_SDM) {
58568c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: %s: Invalid stf\n",
58578c2ecf20Sopenharmony_ci				  wlc->pub->unit, __func__);
58588c2ecf20Sopenharmony_ci			goto done;
58598c2ecf20Sopenharmony_ci		}
58608c2ecf20Sopenharmony_ci
58618c2ecf20Sopenharmony_ci		/* mcs 32 is a special case, DUP mode 40 only */
58628c2ecf20Sopenharmony_ci		if (rate == 32) {
58638c2ecf20Sopenharmony_ci			if (!CHSPEC_IS40(wlc->home_chanspec) ||
58648c2ecf20Sopenharmony_ci			    ((stf != PHY_TXC1_MODE_SISO)
58658c2ecf20Sopenharmony_ci			     && (stf != PHY_TXC1_MODE_CDD))) {
58668c2ecf20Sopenharmony_ci				brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
58678c2ecf20Sopenharmony_ci					  wlc->pub->unit, __func__);
58688c2ecf20Sopenharmony_ci				goto done;
58698c2ecf20Sopenharmony_ci			}
58708c2ecf20Sopenharmony_ci			/* mcs > 7 must use stf SDM */
58718c2ecf20Sopenharmony_ci		} else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
58728c2ecf20Sopenharmony_ci			/* mcs > 7 must use stf SDM */
58738c2ecf20Sopenharmony_ci			if (stf != PHY_TXC1_MODE_SDM) {
58748c2ecf20Sopenharmony_ci				brcms_dbg_mac80211(core, "wl%d: enabling "
58758c2ecf20Sopenharmony_ci						   "SDM mode for mcs %d\n",
58768c2ecf20Sopenharmony_ci						   wlc->pub->unit, rate);
58778c2ecf20Sopenharmony_ci				stf = PHY_TXC1_MODE_SDM;
58788c2ecf20Sopenharmony_ci			}
58798c2ecf20Sopenharmony_ci		} else {
58808c2ecf20Sopenharmony_ci			/*
58818c2ecf20Sopenharmony_ci			 * MCS 0-7 may use SISO, CDD, and for
58828c2ecf20Sopenharmony_ci			 * phy_rev >= 3 STBC
58838c2ecf20Sopenharmony_ci			 */
58848c2ecf20Sopenharmony_ci			if ((stf > PHY_TXC1_MODE_STBC) ||
58858c2ecf20Sopenharmony_ci			    (!BRCMS_STBC_CAP_PHY(wlc)
58868c2ecf20Sopenharmony_ci			     && (stf == PHY_TXC1_MODE_STBC))) {
58878c2ecf20Sopenharmony_ci				brcms_err(core, "wl%d: %s: Invalid STBC\n",
58888c2ecf20Sopenharmony_ci					  wlc->pub->unit, __func__);
58898c2ecf20Sopenharmony_ci				goto done;
58908c2ecf20Sopenharmony_ci			}
58918c2ecf20Sopenharmony_ci		}
58928c2ecf20Sopenharmony_ci	} else if (is_ofdm_rate(rate)) {
58938c2ecf20Sopenharmony_ci		if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
58948c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: %s: Invalid OFDM\n",
58958c2ecf20Sopenharmony_ci				  wlc->pub->unit, __func__);
58968c2ecf20Sopenharmony_ci			goto done;
58978c2ecf20Sopenharmony_ci		}
58988c2ecf20Sopenharmony_ci	} else if (is_cck_rate(rate)) {
58998c2ecf20Sopenharmony_ci		if ((cur_band->bandtype != BRCM_BAND_2G)
59008c2ecf20Sopenharmony_ci		    || (stf != PHY_TXC1_MODE_SISO)) {
59018c2ecf20Sopenharmony_ci			brcms_err(core, "wl%d: %s: Invalid CCK\n",
59028c2ecf20Sopenharmony_ci				  wlc->pub->unit, __func__);
59038c2ecf20Sopenharmony_ci			goto done;
59048c2ecf20Sopenharmony_ci		}
59058c2ecf20Sopenharmony_ci	} else {
59068c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: Unknown rate type\n",
59078c2ecf20Sopenharmony_ci			  wlc->pub->unit, __func__);
59088c2ecf20Sopenharmony_ci		goto done;
59098c2ecf20Sopenharmony_ci	}
59108c2ecf20Sopenharmony_ci	/* make sure multiple antennae are available for non-siso rates */
59118c2ecf20Sopenharmony_ci	if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
59128c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
59138c2ecf20Sopenharmony_ci			  "request\n", wlc->pub->unit, __func__);
59148c2ecf20Sopenharmony_ci		goto done;
59158c2ecf20Sopenharmony_ci	}
59168c2ecf20Sopenharmony_ci
59178c2ecf20Sopenharmony_ci	rspec = rate;
59188c2ecf20Sopenharmony_ci	if (ismcs) {
59198c2ecf20Sopenharmony_ci		rspec |= RSPEC_MIMORATE;
59208c2ecf20Sopenharmony_ci		/* For STBC populate the STC field of the ratespec */
59218c2ecf20Sopenharmony_ci		if (stf == PHY_TXC1_MODE_STBC) {
59228c2ecf20Sopenharmony_ci			u8 stc;
59238c2ecf20Sopenharmony_ci			stc = 1;	/* Nss for single stream is always 1 */
59248c2ecf20Sopenharmony_ci			rspec |= (stc << RSPEC_STC_SHIFT);
59258c2ecf20Sopenharmony_ci		}
59268c2ecf20Sopenharmony_ci	}
59278c2ecf20Sopenharmony_ci
59288c2ecf20Sopenharmony_ci	rspec |= (stf << RSPEC_STF_SHIFT);
59298c2ecf20Sopenharmony_ci
59308c2ecf20Sopenharmony_ci	if (override_mcs_only)
59318c2ecf20Sopenharmony_ci		rspec |= RSPEC_OVERRIDE_MCS_ONLY;
59328c2ecf20Sopenharmony_ci
59338c2ecf20Sopenharmony_ci	if (issgi)
59348c2ecf20Sopenharmony_ci		rspec |= RSPEC_SHORT_GI;
59358c2ecf20Sopenharmony_ci
59368c2ecf20Sopenharmony_ci	if ((rate != 0)
59378c2ecf20Sopenharmony_ci	    && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
59388c2ecf20Sopenharmony_ci		return rate;
59398c2ecf20Sopenharmony_ci
59408c2ecf20Sopenharmony_ci	return rspec;
59418c2ecf20Sopenharmony_cidone:
59428c2ecf20Sopenharmony_ci	return rate;
59438c2ecf20Sopenharmony_ci}
59448c2ecf20Sopenharmony_ci
59458c2ecf20Sopenharmony_ci/*
59468c2ecf20Sopenharmony_ci * Compute PLCP, but only requires actual rate and length of pkt.
59478c2ecf20Sopenharmony_ci * Rate is given in the driver standard multiple of 500 kbps.
59488c2ecf20Sopenharmony_ci * le is set for 11 Mbps rate if necessary.
59498c2ecf20Sopenharmony_ci * Broken out for PRQ.
59508c2ecf20Sopenharmony_ci */
59518c2ecf20Sopenharmony_ci
59528c2ecf20Sopenharmony_cistatic void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
59538c2ecf20Sopenharmony_ci			     uint length, u8 *plcp)
59548c2ecf20Sopenharmony_ci{
59558c2ecf20Sopenharmony_ci	u16 usec = 0;
59568c2ecf20Sopenharmony_ci	u8 le = 0;
59578c2ecf20Sopenharmony_ci
59588c2ecf20Sopenharmony_ci	switch (rate_500) {
59598c2ecf20Sopenharmony_ci	case BRCM_RATE_1M:
59608c2ecf20Sopenharmony_ci		usec = length << 3;
59618c2ecf20Sopenharmony_ci		break;
59628c2ecf20Sopenharmony_ci	case BRCM_RATE_2M:
59638c2ecf20Sopenharmony_ci		usec = length << 2;
59648c2ecf20Sopenharmony_ci		break;
59658c2ecf20Sopenharmony_ci	case BRCM_RATE_5M5:
59668c2ecf20Sopenharmony_ci		usec = (length << 4) / 11;
59678c2ecf20Sopenharmony_ci		if ((length << 4) - (usec * 11) > 0)
59688c2ecf20Sopenharmony_ci			usec++;
59698c2ecf20Sopenharmony_ci		break;
59708c2ecf20Sopenharmony_ci	case BRCM_RATE_11M:
59718c2ecf20Sopenharmony_ci		usec = (length << 3) / 11;
59728c2ecf20Sopenharmony_ci		if ((length << 3) - (usec * 11) > 0) {
59738c2ecf20Sopenharmony_ci			usec++;
59748c2ecf20Sopenharmony_ci			if ((usec * 11) - (length << 3) >= 8)
59758c2ecf20Sopenharmony_ci				le = D11B_PLCP_SIGNAL_LE;
59768c2ecf20Sopenharmony_ci		}
59778c2ecf20Sopenharmony_ci		break;
59788c2ecf20Sopenharmony_ci
59798c2ecf20Sopenharmony_ci	default:
59808c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core,
59818c2ecf20Sopenharmony_ci			  "brcms_c_cck_plcp_set: unsupported rate %d\n",
59828c2ecf20Sopenharmony_ci			  rate_500);
59838c2ecf20Sopenharmony_ci		rate_500 = BRCM_RATE_1M;
59848c2ecf20Sopenharmony_ci		usec = length << 3;
59858c2ecf20Sopenharmony_ci		break;
59868c2ecf20Sopenharmony_ci	}
59878c2ecf20Sopenharmony_ci	/* PLCP signal byte */
59888c2ecf20Sopenharmony_ci	plcp[0] = rate_500 * 5;	/* r (500kbps) * 5 == r (100kbps) */
59898c2ecf20Sopenharmony_ci	/* PLCP service byte */
59908c2ecf20Sopenharmony_ci	plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
59918c2ecf20Sopenharmony_ci	/* PLCP length u16, little endian */
59928c2ecf20Sopenharmony_ci	plcp[2] = usec & 0xff;
59938c2ecf20Sopenharmony_ci	plcp[3] = (usec >> 8) & 0xff;
59948c2ecf20Sopenharmony_ci	/* PLCP CRC16 */
59958c2ecf20Sopenharmony_ci	plcp[4] = 0;
59968c2ecf20Sopenharmony_ci	plcp[5] = 0;
59978c2ecf20Sopenharmony_ci}
59988c2ecf20Sopenharmony_ci
59998c2ecf20Sopenharmony_ci/* Rate: 802.11 rate code, length: PSDU length in octets */
60008c2ecf20Sopenharmony_cistatic void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
60018c2ecf20Sopenharmony_ci{
60028c2ecf20Sopenharmony_ci	u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
60038c2ecf20Sopenharmony_ci	plcp[0] = mcs;
60048c2ecf20Sopenharmony_ci	if (rspec_is40mhz(rspec) || (mcs == 32))
60058c2ecf20Sopenharmony_ci		plcp[0] |= MIMO_PLCP_40MHZ;
60068c2ecf20Sopenharmony_ci	BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
60078c2ecf20Sopenharmony_ci	plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
60088c2ecf20Sopenharmony_ci	plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
60098c2ecf20Sopenharmony_ci	plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
60108c2ecf20Sopenharmony_ci	plcp[5] = 0;
60118c2ecf20Sopenharmony_ci}
60128c2ecf20Sopenharmony_ci
60138c2ecf20Sopenharmony_ci/* Rate: 802.11 rate code, length: PSDU length in octets */
60148c2ecf20Sopenharmony_cistatic void
60158c2ecf20Sopenharmony_cibrcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
60168c2ecf20Sopenharmony_ci{
60178c2ecf20Sopenharmony_ci	u8 rate_signal;
60188c2ecf20Sopenharmony_ci	u32 tmp = 0;
60198c2ecf20Sopenharmony_ci	int rate = rspec2rate(rspec);
60208c2ecf20Sopenharmony_ci
60218c2ecf20Sopenharmony_ci	/*
60228c2ecf20Sopenharmony_ci	 * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
60238c2ecf20Sopenharmony_ci	 * transmitted first
60248c2ecf20Sopenharmony_ci	 */
60258c2ecf20Sopenharmony_ci	rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
60268c2ecf20Sopenharmony_ci	memset(plcp, 0, D11_PHY_HDR_LEN);
60278c2ecf20Sopenharmony_ci	D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
60288c2ecf20Sopenharmony_ci
60298c2ecf20Sopenharmony_ci	tmp = (length & 0xfff) << 5;
60308c2ecf20Sopenharmony_ci	plcp[2] |= (tmp >> 16) & 0xff;
60318c2ecf20Sopenharmony_ci	plcp[1] |= (tmp >> 8) & 0xff;
60328c2ecf20Sopenharmony_ci	plcp[0] |= tmp & 0xff;
60338c2ecf20Sopenharmony_ci}
60348c2ecf20Sopenharmony_ci
60358c2ecf20Sopenharmony_ci/* Rate: 802.11 rate code, length: PSDU length in octets */
60368c2ecf20Sopenharmony_cistatic void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
60378c2ecf20Sopenharmony_ci				 uint length, u8 *plcp)
60388c2ecf20Sopenharmony_ci{
60398c2ecf20Sopenharmony_ci	int rate = rspec2rate(rspec);
60408c2ecf20Sopenharmony_ci
60418c2ecf20Sopenharmony_ci	brcms_c_cck_plcp_set(wlc, rate, length, plcp);
60428c2ecf20Sopenharmony_ci}
60438c2ecf20Sopenharmony_ci
60448c2ecf20Sopenharmony_cistatic void
60458c2ecf20Sopenharmony_cibrcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
60468c2ecf20Sopenharmony_ci		     uint length, u8 *plcp)
60478c2ecf20Sopenharmony_ci{
60488c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec))
60498c2ecf20Sopenharmony_ci		brcms_c_compute_mimo_plcp(rspec, length, plcp);
60508c2ecf20Sopenharmony_ci	else if (is_ofdm_rate(rspec))
60518c2ecf20Sopenharmony_ci		brcms_c_compute_ofdm_plcp(rspec, length, plcp);
60528c2ecf20Sopenharmony_ci	else
60538c2ecf20Sopenharmony_ci		brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
60548c2ecf20Sopenharmony_ci}
60558c2ecf20Sopenharmony_ci
60568c2ecf20Sopenharmony_ci/* brcms_c_compute_rtscts_dur()
60578c2ecf20Sopenharmony_ci *
60588c2ecf20Sopenharmony_ci * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
60598c2ecf20Sopenharmony_ci * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
60608c2ecf20Sopenharmony_ci * DUR for CTS-TO-SELF w/ frame    = 2 SIFS         + next frame time + 1 ACK
60618c2ecf20Sopenharmony_ci *
60628c2ecf20Sopenharmony_ci * cts			cts-to-self or rts/cts
60638c2ecf20Sopenharmony_ci * rts_rate		rts or cts rate in unit of 500kbps
60648c2ecf20Sopenharmony_ci * rate			next MPDU rate in unit of 500kbps
60658c2ecf20Sopenharmony_ci * frame_len		next MPDU frame length in bytes
60668c2ecf20Sopenharmony_ci */
60678c2ecf20Sopenharmony_ciu16
60688c2ecf20Sopenharmony_cibrcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
60698c2ecf20Sopenharmony_ci			   u32 rts_rate,
60708c2ecf20Sopenharmony_ci			   u32 frame_rate, u8 rts_preamble_type,
60718c2ecf20Sopenharmony_ci			   u8 frame_preamble_type, uint frame_len, bool ba)
60728c2ecf20Sopenharmony_ci{
60738c2ecf20Sopenharmony_ci	u16 dur, sifs;
60748c2ecf20Sopenharmony_ci
60758c2ecf20Sopenharmony_ci	sifs = get_sifs(wlc->band);
60768c2ecf20Sopenharmony_ci
60778c2ecf20Sopenharmony_ci	if (!cts_only) {
60788c2ecf20Sopenharmony_ci		/* RTS/CTS */
60798c2ecf20Sopenharmony_ci		dur = 3 * sifs;
60808c2ecf20Sopenharmony_ci		dur +=
60818c2ecf20Sopenharmony_ci		    (u16) brcms_c_calc_cts_time(wlc, rts_rate,
60828c2ecf20Sopenharmony_ci					       rts_preamble_type);
60838c2ecf20Sopenharmony_ci	} else {
60848c2ecf20Sopenharmony_ci		/* CTS-TO-SELF */
60858c2ecf20Sopenharmony_ci		dur = 2 * sifs;
60868c2ecf20Sopenharmony_ci	}
60878c2ecf20Sopenharmony_ci
60888c2ecf20Sopenharmony_ci	dur +=
60898c2ecf20Sopenharmony_ci	    (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
60908c2ecf20Sopenharmony_ci					 frame_len);
60918c2ecf20Sopenharmony_ci	if (ba)
60928c2ecf20Sopenharmony_ci		dur +=
60938c2ecf20Sopenharmony_ci		    (u16) brcms_c_calc_ba_time(wlc, frame_rate,
60948c2ecf20Sopenharmony_ci					      BRCMS_SHORT_PREAMBLE);
60958c2ecf20Sopenharmony_ci	else
60968c2ecf20Sopenharmony_ci		dur +=
60978c2ecf20Sopenharmony_ci		    (u16) brcms_c_calc_ack_time(wlc, frame_rate,
60988c2ecf20Sopenharmony_ci					       frame_preamble_type);
60998c2ecf20Sopenharmony_ci	return dur;
61008c2ecf20Sopenharmony_ci}
61018c2ecf20Sopenharmony_ci
61028c2ecf20Sopenharmony_cistatic u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
61038c2ecf20Sopenharmony_ci{
61048c2ecf20Sopenharmony_ci	u16 phyctl1 = 0;
61058c2ecf20Sopenharmony_ci	u16 bw;
61068c2ecf20Sopenharmony_ci
61078c2ecf20Sopenharmony_ci	if (BRCMS_ISLCNPHY(wlc->band)) {
61088c2ecf20Sopenharmony_ci		bw = PHY_TXC1_BW_20MHZ;
61098c2ecf20Sopenharmony_ci	} else {
61108c2ecf20Sopenharmony_ci		bw = rspec_get_bw(rspec);
61118c2ecf20Sopenharmony_ci		/* 10Mhz is not supported yet */
61128c2ecf20Sopenharmony_ci		if (bw < PHY_TXC1_BW_20MHZ) {
61138c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
61148c2ecf20Sopenharmony_ci				  "not supported yet, set to 20L\n", bw);
61158c2ecf20Sopenharmony_ci			bw = PHY_TXC1_BW_20MHZ;
61168c2ecf20Sopenharmony_ci		}
61178c2ecf20Sopenharmony_ci	}
61188c2ecf20Sopenharmony_ci
61198c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec)) {
61208c2ecf20Sopenharmony_ci		uint mcs = rspec & RSPEC_RATE_MASK;
61218c2ecf20Sopenharmony_ci
61228c2ecf20Sopenharmony_ci		/* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
61238c2ecf20Sopenharmony_ci		phyctl1 = rspec_phytxbyte2(rspec);
61248c2ecf20Sopenharmony_ci		/* set the upper byte of phyctl1 */
61258c2ecf20Sopenharmony_ci		phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
61268c2ecf20Sopenharmony_ci	} else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
61278c2ecf20Sopenharmony_ci		   && !BRCMS_ISSSLPNPHY(wlc->band)) {
61288c2ecf20Sopenharmony_ci		/*
61298c2ecf20Sopenharmony_ci		 * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
61308c2ecf20Sopenharmony_ci		 * Data Rate. Eventually MIMOPHY would also be converted to
61318c2ecf20Sopenharmony_ci		 * this format
61328c2ecf20Sopenharmony_ci		 */
61338c2ecf20Sopenharmony_ci		/* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
61348c2ecf20Sopenharmony_ci		phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
61358c2ecf20Sopenharmony_ci	} else {		/* legacy OFDM/CCK */
61368c2ecf20Sopenharmony_ci		s16 phycfg;
61378c2ecf20Sopenharmony_ci		/* get the phyctl byte from rate phycfg table */
61388c2ecf20Sopenharmony_ci		phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
61398c2ecf20Sopenharmony_ci		if (phycfg == -1) {
61408c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
61418c2ecf20Sopenharmony_ci				  "legacy OFDM/CCK rate\n");
61428c2ecf20Sopenharmony_ci			phycfg = 0;
61438c2ecf20Sopenharmony_ci		}
61448c2ecf20Sopenharmony_ci		/* set the upper byte of phyctl1 */
61458c2ecf20Sopenharmony_ci		phyctl1 =
61468c2ecf20Sopenharmony_ci		    (bw | (phycfg << 8) |
61478c2ecf20Sopenharmony_ci		     (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
61488c2ecf20Sopenharmony_ci	}
61498c2ecf20Sopenharmony_ci	return phyctl1;
61508c2ecf20Sopenharmony_ci}
61518c2ecf20Sopenharmony_ci
61528c2ecf20Sopenharmony_ci/*
61538c2ecf20Sopenharmony_ci * Add struct d11txh, struct cck_phy_hdr.
61548c2ecf20Sopenharmony_ci *
61558c2ecf20Sopenharmony_ci * 'p' data must start with 802.11 MAC header
61568c2ecf20Sopenharmony_ci * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
61578c2ecf20Sopenharmony_ci *
61588c2ecf20Sopenharmony_ci * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
61598c2ecf20Sopenharmony_ci *
61608c2ecf20Sopenharmony_ci */
61618c2ecf20Sopenharmony_cistatic u16
61628c2ecf20Sopenharmony_cibrcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
61638c2ecf20Sopenharmony_ci		     struct sk_buff *p, struct scb *scb, uint frag,
61648c2ecf20Sopenharmony_ci		     uint nfrags, uint queue, uint next_frag_len)
61658c2ecf20Sopenharmony_ci{
61668c2ecf20Sopenharmony_ci	struct ieee80211_hdr *h;
61678c2ecf20Sopenharmony_ci	struct d11txh *txh;
61688c2ecf20Sopenharmony_ci	u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
61698c2ecf20Sopenharmony_ci	int len, phylen, rts_phylen;
61708c2ecf20Sopenharmony_ci	u16 mch, phyctl, xfts, mainrates;
61718c2ecf20Sopenharmony_ci	u16 seq = 0, mcl = 0, status = 0, frameid = 0;
61728c2ecf20Sopenharmony_ci	u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
61738c2ecf20Sopenharmony_ci	u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
61748c2ecf20Sopenharmony_ci	bool use_rts = false;
61758c2ecf20Sopenharmony_ci	bool use_cts = false;
61768c2ecf20Sopenharmony_ci	bool use_rifs = false;
61778c2ecf20Sopenharmony_ci	u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
61788c2ecf20Sopenharmony_ci	u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
61798c2ecf20Sopenharmony_ci	u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
61808c2ecf20Sopenharmony_ci	struct ieee80211_rts *rts = NULL;
61818c2ecf20Sopenharmony_ci	bool qos;
61828c2ecf20Sopenharmony_ci	uint ac;
61838c2ecf20Sopenharmony_ci	bool hwtkmic = false;
61848c2ecf20Sopenharmony_ci	u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
61858c2ecf20Sopenharmony_ci#define ANTCFG_NONE 0xFF
61868c2ecf20Sopenharmony_ci	u8 antcfg = ANTCFG_NONE;
61878c2ecf20Sopenharmony_ci	u8 fbantcfg = ANTCFG_NONE;
61888c2ecf20Sopenharmony_ci	uint phyctl1_stf = 0;
61898c2ecf20Sopenharmony_ci	u16 durid = 0;
61908c2ecf20Sopenharmony_ci	struct ieee80211_tx_rate *txrate[2];
61918c2ecf20Sopenharmony_ci	int k;
61928c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info;
61938c2ecf20Sopenharmony_ci	bool is_mcs;
61948c2ecf20Sopenharmony_ci	u16 mimo_txbw;
61958c2ecf20Sopenharmony_ci	u8 mimo_preamble_type;
61968c2ecf20Sopenharmony_ci
61978c2ecf20Sopenharmony_ci	/* locate 802.11 MAC header */
61988c2ecf20Sopenharmony_ci	h = (struct ieee80211_hdr *)(p->data);
61998c2ecf20Sopenharmony_ci	qos = ieee80211_is_data_qos(h->frame_control);
62008c2ecf20Sopenharmony_ci
62018c2ecf20Sopenharmony_ci	/* compute length of frame in bytes for use in PLCP computations */
62028c2ecf20Sopenharmony_ci	len = p->len;
62038c2ecf20Sopenharmony_ci	phylen = len + FCS_LEN;
62048c2ecf20Sopenharmony_ci
62058c2ecf20Sopenharmony_ci	/* Get tx_info */
62068c2ecf20Sopenharmony_ci	tx_info = IEEE80211_SKB_CB(p);
62078c2ecf20Sopenharmony_ci
62088c2ecf20Sopenharmony_ci	/* add PLCP */
62098c2ecf20Sopenharmony_ci	plcp = skb_push(p, D11_PHY_HDR_LEN);
62108c2ecf20Sopenharmony_ci
62118c2ecf20Sopenharmony_ci	/* add Broadcom tx descriptor header */
62128c2ecf20Sopenharmony_ci	txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
62138c2ecf20Sopenharmony_ci	memset(txh, 0, D11_TXH_LEN);
62148c2ecf20Sopenharmony_ci
62158c2ecf20Sopenharmony_ci	/* setup frameid */
62168c2ecf20Sopenharmony_ci	if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
62178c2ecf20Sopenharmony_ci		/* non-AP STA should never use BCMC queue */
62188c2ecf20Sopenharmony_ci		if (queue == TX_BCMC_FIFO) {
62198c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core,
62208c2ecf20Sopenharmony_ci				  "wl%d: %s: ASSERT queue == TX_BCMC!\n",
62218c2ecf20Sopenharmony_ci				  wlc->pub->unit, __func__);
62228c2ecf20Sopenharmony_ci			frameid = bcmc_fid_generate(wlc, NULL, txh);
62238c2ecf20Sopenharmony_ci		} else {
62248c2ecf20Sopenharmony_ci			/* Increment the counter for first fragment */
62258c2ecf20Sopenharmony_ci			if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
62268c2ecf20Sopenharmony_ci				scb->seqnum[p->priority]++;
62278c2ecf20Sopenharmony_ci
62288c2ecf20Sopenharmony_ci			/* extract fragment number from frame first */
62298c2ecf20Sopenharmony_ci			seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
62308c2ecf20Sopenharmony_ci			seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
62318c2ecf20Sopenharmony_ci			h->seq_ctrl = cpu_to_le16(seq);
62328c2ecf20Sopenharmony_ci
62338c2ecf20Sopenharmony_ci			frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
62348c2ecf20Sopenharmony_ci			    (queue & TXFID_QUEUE_MASK);
62358c2ecf20Sopenharmony_ci		}
62368c2ecf20Sopenharmony_ci	}
62378c2ecf20Sopenharmony_ci	frameid |= queue & TXFID_QUEUE_MASK;
62388c2ecf20Sopenharmony_ci
62398c2ecf20Sopenharmony_ci	/* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
62408c2ecf20Sopenharmony_ci	if (ieee80211_is_beacon(h->frame_control))
62418c2ecf20Sopenharmony_ci		mcl |= TXC_IGNOREPMQ;
62428c2ecf20Sopenharmony_ci
62438c2ecf20Sopenharmony_ci	txrate[0] = tx_info->control.rates;
62448c2ecf20Sopenharmony_ci	txrate[1] = txrate[0] + 1;
62458c2ecf20Sopenharmony_ci
62468c2ecf20Sopenharmony_ci	/*
62478c2ecf20Sopenharmony_ci	 * if rate control algorithm didn't give us a fallback
62488c2ecf20Sopenharmony_ci	 * rate, use the primary rate
62498c2ecf20Sopenharmony_ci	 */
62508c2ecf20Sopenharmony_ci	if (txrate[1]->idx < 0)
62518c2ecf20Sopenharmony_ci		txrate[1] = txrate[0];
62528c2ecf20Sopenharmony_ci
62538c2ecf20Sopenharmony_ci	for (k = 0; k < hw->max_rates; k++) {
62548c2ecf20Sopenharmony_ci		is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
62558c2ecf20Sopenharmony_ci		if (!is_mcs) {
62568c2ecf20Sopenharmony_ci			if ((txrate[k]->idx >= 0)
62578c2ecf20Sopenharmony_ci			    && (txrate[k]->idx <
62588c2ecf20Sopenharmony_ci				hw->wiphy->bands[tx_info->band]->n_bitrates)) {
62598c2ecf20Sopenharmony_ci				rspec[k] =
62608c2ecf20Sopenharmony_ci				    hw->wiphy->bands[tx_info->band]->
62618c2ecf20Sopenharmony_ci				    bitrates[txrate[k]->idx].hw_value;
62628c2ecf20Sopenharmony_ci			} else {
62638c2ecf20Sopenharmony_ci				rspec[k] = BRCM_RATE_1M;
62648c2ecf20Sopenharmony_ci			}
62658c2ecf20Sopenharmony_ci		} else {
62668c2ecf20Sopenharmony_ci			rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
62678c2ecf20Sopenharmony_ci					NRATE_MCS_INUSE | txrate[k]->idx);
62688c2ecf20Sopenharmony_ci		}
62698c2ecf20Sopenharmony_ci
62708c2ecf20Sopenharmony_ci		/*
62718c2ecf20Sopenharmony_ci		 * Currently only support same setting for primay and
62728c2ecf20Sopenharmony_ci		 * fallback rates. Unify flags for each rate into a
62738c2ecf20Sopenharmony_ci		 * single value for the frame
62748c2ecf20Sopenharmony_ci		 */
62758c2ecf20Sopenharmony_ci		use_rts |=
62768c2ecf20Sopenharmony_ci		    txrate[k]->
62778c2ecf20Sopenharmony_ci		    flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
62788c2ecf20Sopenharmony_ci		use_cts |=
62798c2ecf20Sopenharmony_ci		    txrate[k]->
62808c2ecf20Sopenharmony_ci		    flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
62818c2ecf20Sopenharmony_ci
62828c2ecf20Sopenharmony_ci
62838c2ecf20Sopenharmony_ci		/*
62848c2ecf20Sopenharmony_ci		 * (1) RATE:
62858c2ecf20Sopenharmony_ci		 *   determine and validate primary rate
62868c2ecf20Sopenharmony_ci		 *   and fallback rates
62878c2ecf20Sopenharmony_ci		 */
62888c2ecf20Sopenharmony_ci		if (!rspec_active(rspec[k])) {
62898c2ecf20Sopenharmony_ci			rspec[k] = BRCM_RATE_1M;
62908c2ecf20Sopenharmony_ci		} else {
62918c2ecf20Sopenharmony_ci			if (!is_multicast_ether_addr(h->addr1)) {
62928c2ecf20Sopenharmony_ci				/* set tx antenna config */
62938c2ecf20Sopenharmony_ci				brcms_c_antsel_antcfg_get(wlc->asi, false,
62948c2ecf20Sopenharmony_ci					false, 0, 0, &antcfg, &fbantcfg);
62958c2ecf20Sopenharmony_ci			}
62968c2ecf20Sopenharmony_ci		}
62978c2ecf20Sopenharmony_ci	}
62988c2ecf20Sopenharmony_ci
62998c2ecf20Sopenharmony_ci	phyctl1_stf = wlc->stf->ss_opmode;
63008c2ecf20Sopenharmony_ci
63018c2ecf20Sopenharmony_ci	if (wlc->pub->_n_enab & SUPPORT_11N) {
63028c2ecf20Sopenharmony_ci		for (k = 0; k < hw->max_rates; k++) {
63038c2ecf20Sopenharmony_ci			/*
63048c2ecf20Sopenharmony_ci			 * apply siso/cdd to single stream mcs's or ofdm
63058c2ecf20Sopenharmony_ci			 * if rspec is auto selected
63068c2ecf20Sopenharmony_ci			 */
63078c2ecf20Sopenharmony_ci			if (((is_mcs_rate(rspec[k]) &&
63088c2ecf20Sopenharmony_ci			      is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
63098c2ecf20Sopenharmony_ci			     is_ofdm_rate(rspec[k]))
63108c2ecf20Sopenharmony_ci			    && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
63118c2ecf20Sopenharmony_ci				|| !(rspec[k] & RSPEC_OVERRIDE))) {
63128c2ecf20Sopenharmony_ci				rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
63138c2ecf20Sopenharmony_ci
63148c2ecf20Sopenharmony_ci				/* For SISO MCS use STBC if possible */
63158c2ecf20Sopenharmony_ci				if (is_mcs_rate(rspec[k])
63168c2ecf20Sopenharmony_ci				    && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
63178c2ecf20Sopenharmony_ci					u8 stc;
63188c2ecf20Sopenharmony_ci
63198c2ecf20Sopenharmony_ci					/* Nss for single stream is always 1 */
63208c2ecf20Sopenharmony_ci					stc = 1;
63218c2ecf20Sopenharmony_ci					rspec[k] |= (PHY_TXC1_MODE_STBC <<
63228c2ecf20Sopenharmony_ci							RSPEC_STF_SHIFT) |
63238c2ecf20Sopenharmony_ci						    (stc << RSPEC_STC_SHIFT);
63248c2ecf20Sopenharmony_ci				} else
63258c2ecf20Sopenharmony_ci					rspec[k] |=
63268c2ecf20Sopenharmony_ci					    (phyctl1_stf << RSPEC_STF_SHIFT);
63278c2ecf20Sopenharmony_ci			}
63288c2ecf20Sopenharmony_ci
63298c2ecf20Sopenharmony_ci			/*
63308c2ecf20Sopenharmony_ci			 * Is the phy configured to use 40MHZ frames? If
63318c2ecf20Sopenharmony_ci			 * so then pick the desired txbw
63328c2ecf20Sopenharmony_ci			 */
63338c2ecf20Sopenharmony_ci			if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
63348c2ecf20Sopenharmony_ci				/* default txbw is 20in40 SB */
63358c2ecf20Sopenharmony_ci				mimo_ctlchbw = mimo_txbw =
63368c2ecf20Sopenharmony_ci				   CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
63378c2ecf20Sopenharmony_ci								 wlc->band->pi))
63388c2ecf20Sopenharmony_ci				   ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
63398c2ecf20Sopenharmony_ci
63408c2ecf20Sopenharmony_ci				if (is_mcs_rate(rspec[k])) {
63418c2ecf20Sopenharmony_ci					/* mcs 32 must be 40b/w DUP */
63428c2ecf20Sopenharmony_ci					if ((rspec[k] & RSPEC_RATE_MASK)
63438c2ecf20Sopenharmony_ci					    == 32) {
63448c2ecf20Sopenharmony_ci						mimo_txbw =
63458c2ecf20Sopenharmony_ci						    PHY_TXC1_BW_40MHZ_DUP;
63468c2ecf20Sopenharmony_ci						/* use override */
63478c2ecf20Sopenharmony_ci					} else if (wlc->mimo_40txbw != AUTO)
63488c2ecf20Sopenharmony_ci						mimo_txbw = wlc->mimo_40txbw;
63498c2ecf20Sopenharmony_ci					/* else check if dst is using 40 Mhz */
63508c2ecf20Sopenharmony_ci					else if (scb->flags & SCB_IS40)
63518c2ecf20Sopenharmony_ci						mimo_txbw = PHY_TXC1_BW_40MHZ;
63528c2ecf20Sopenharmony_ci				} else if (is_ofdm_rate(rspec[k])) {
63538c2ecf20Sopenharmony_ci					if (wlc->ofdm_40txbw != AUTO)
63548c2ecf20Sopenharmony_ci						mimo_txbw = wlc->ofdm_40txbw;
63558c2ecf20Sopenharmony_ci				} else if (wlc->cck_40txbw != AUTO) {
63568c2ecf20Sopenharmony_ci					mimo_txbw = wlc->cck_40txbw;
63578c2ecf20Sopenharmony_ci				}
63588c2ecf20Sopenharmony_ci			} else {
63598c2ecf20Sopenharmony_ci				/*
63608c2ecf20Sopenharmony_ci				 * mcs32 is 40 b/w only.
63618c2ecf20Sopenharmony_ci				 * This is possible for probe packets on
63628c2ecf20Sopenharmony_ci				 * a STA during SCAN
63638c2ecf20Sopenharmony_ci				 */
63648c2ecf20Sopenharmony_ci				if ((rspec[k] & RSPEC_RATE_MASK) == 32)
63658c2ecf20Sopenharmony_ci					/* mcs 0 */
63668c2ecf20Sopenharmony_ci					rspec[k] = RSPEC_MIMORATE;
63678c2ecf20Sopenharmony_ci
63688c2ecf20Sopenharmony_ci				mimo_txbw = PHY_TXC1_BW_20MHZ;
63698c2ecf20Sopenharmony_ci			}
63708c2ecf20Sopenharmony_ci
63718c2ecf20Sopenharmony_ci			/* Set channel width */
63728c2ecf20Sopenharmony_ci			rspec[k] &= ~RSPEC_BW_MASK;
63738c2ecf20Sopenharmony_ci			if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
63748c2ecf20Sopenharmony_ci				rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
63758c2ecf20Sopenharmony_ci			else
63768c2ecf20Sopenharmony_ci				rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
63778c2ecf20Sopenharmony_ci
63788c2ecf20Sopenharmony_ci			/* Disable short GI, not supported yet */
63798c2ecf20Sopenharmony_ci			rspec[k] &= ~RSPEC_SHORT_GI;
63808c2ecf20Sopenharmony_ci
63818c2ecf20Sopenharmony_ci			mimo_preamble_type = BRCMS_MM_PREAMBLE;
63828c2ecf20Sopenharmony_ci			if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
63838c2ecf20Sopenharmony_ci				mimo_preamble_type = BRCMS_GF_PREAMBLE;
63848c2ecf20Sopenharmony_ci
63858c2ecf20Sopenharmony_ci			if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
63868c2ecf20Sopenharmony_ci			    && (!is_mcs_rate(rspec[k]))) {
63878c2ecf20Sopenharmony_ci				brcms_warn(wlc->hw->d11core,
63888c2ecf20Sopenharmony_ci					   "wl%d: %s: IEEE80211_TX_RC_MCS != is_mcs_rate(rspec)\n",
63898c2ecf20Sopenharmony_ci					   wlc->pub->unit, __func__);
63908c2ecf20Sopenharmony_ci			}
63918c2ecf20Sopenharmony_ci
63928c2ecf20Sopenharmony_ci			if (is_mcs_rate(rspec[k])) {
63938c2ecf20Sopenharmony_ci				preamble_type[k] = mimo_preamble_type;
63948c2ecf20Sopenharmony_ci
63958c2ecf20Sopenharmony_ci				/*
63968c2ecf20Sopenharmony_ci				 * if SGI is selected, then forced mm
63978c2ecf20Sopenharmony_ci				 * for single stream
63988c2ecf20Sopenharmony_ci				 */
63998c2ecf20Sopenharmony_ci				if ((rspec[k] & RSPEC_SHORT_GI)
64008c2ecf20Sopenharmony_ci				    && is_single_stream(rspec[k] &
64018c2ecf20Sopenharmony_ci							RSPEC_RATE_MASK))
64028c2ecf20Sopenharmony_ci					preamble_type[k] = BRCMS_MM_PREAMBLE;
64038c2ecf20Sopenharmony_ci			}
64048c2ecf20Sopenharmony_ci
64058c2ecf20Sopenharmony_ci			/* should be better conditionalized */
64068c2ecf20Sopenharmony_ci			if (!is_mcs_rate(rspec[0])
64078c2ecf20Sopenharmony_ci			    && (tx_info->control.rates[0].
64088c2ecf20Sopenharmony_ci				flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
64098c2ecf20Sopenharmony_ci				preamble_type[k] = BRCMS_SHORT_PREAMBLE;
64108c2ecf20Sopenharmony_ci		}
64118c2ecf20Sopenharmony_ci	} else {
64128c2ecf20Sopenharmony_ci		for (k = 0; k < hw->max_rates; k++) {
64138c2ecf20Sopenharmony_ci			/* Set ctrlchbw as 20Mhz */
64148c2ecf20Sopenharmony_ci			rspec[k] &= ~RSPEC_BW_MASK;
64158c2ecf20Sopenharmony_ci			rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
64168c2ecf20Sopenharmony_ci
64178c2ecf20Sopenharmony_ci			/* for nphy, stf of ofdm frames must follow policies */
64188c2ecf20Sopenharmony_ci			if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
64198c2ecf20Sopenharmony_ci				rspec[k] &= ~RSPEC_STF_MASK;
64208c2ecf20Sopenharmony_ci				rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
64218c2ecf20Sopenharmony_ci			}
64228c2ecf20Sopenharmony_ci		}
64238c2ecf20Sopenharmony_ci	}
64248c2ecf20Sopenharmony_ci
64258c2ecf20Sopenharmony_ci	/* Reset these for use with AMPDU's */
64268c2ecf20Sopenharmony_ci	txrate[0]->count = 0;
64278c2ecf20Sopenharmony_ci	txrate[1]->count = 0;
64288c2ecf20Sopenharmony_ci
64298c2ecf20Sopenharmony_ci	/* (2) PROTECTION, may change rspec */
64308c2ecf20Sopenharmony_ci	if ((ieee80211_is_data(h->frame_control) ||
64318c2ecf20Sopenharmony_ci	    ieee80211_is_mgmt(h->frame_control)) &&
64328c2ecf20Sopenharmony_ci	    (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
64338c2ecf20Sopenharmony_ci		use_rts = true;
64348c2ecf20Sopenharmony_ci
64358c2ecf20Sopenharmony_ci	/* (3) PLCP: determine PLCP header and MAC duration,
64368c2ecf20Sopenharmony_ci	 * fill struct d11txh */
64378c2ecf20Sopenharmony_ci	brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
64388c2ecf20Sopenharmony_ci	brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
64398c2ecf20Sopenharmony_ci	memcpy(&txh->FragPLCPFallback,
64408c2ecf20Sopenharmony_ci	       plcp_fallback, sizeof(txh->FragPLCPFallback));
64418c2ecf20Sopenharmony_ci
64428c2ecf20Sopenharmony_ci	/* Length field now put in CCK FBR CRC field */
64438c2ecf20Sopenharmony_ci	if (is_cck_rate(rspec[1])) {
64448c2ecf20Sopenharmony_ci		txh->FragPLCPFallback[4] = phylen & 0xff;
64458c2ecf20Sopenharmony_ci		txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
64468c2ecf20Sopenharmony_ci	}
64478c2ecf20Sopenharmony_ci
64488c2ecf20Sopenharmony_ci	/* MIMO-RATE: need validation ?? */
64498c2ecf20Sopenharmony_ci	mainrates = is_ofdm_rate(rspec[0]) ?
64508c2ecf20Sopenharmony_ci			D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
64518c2ecf20Sopenharmony_ci			plcp[0];
64528c2ecf20Sopenharmony_ci
64538c2ecf20Sopenharmony_ci	/* DUR field for main rate */
64548c2ecf20Sopenharmony_ci	if (!ieee80211_is_pspoll(h->frame_control) &&
64558c2ecf20Sopenharmony_ci	    !is_multicast_ether_addr(h->addr1) && !use_rifs) {
64568c2ecf20Sopenharmony_ci		durid =
64578c2ecf20Sopenharmony_ci		    brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
64588c2ecf20Sopenharmony_ci					  next_frag_len);
64598c2ecf20Sopenharmony_ci		h->duration_id = cpu_to_le16(durid);
64608c2ecf20Sopenharmony_ci	} else if (use_rifs) {
64618c2ecf20Sopenharmony_ci		/* NAV protect to end of next max packet size */
64628c2ecf20Sopenharmony_ci		durid =
64638c2ecf20Sopenharmony_ci		    (u16) brcms_c_calc_frame_time(wlc, rspec[0],
64648c2ecf20Sopenharmony_ci						 preamble_type[0],
64658c2ecf20Sopenharmony_ci						 DOT11_MAX_FRAG_LEN);
64668c2ecf20Sopenharmony_ci		durid += RIFS_11N_TIME;
64678c2ecf20Sopenharmony_ci		h->duration_id = cpu_to_le16(durid);
64688c2ecf20Sopenharmony_ci	}
64698c2ecf20Sopenharmony_ci
64708c2ecf20Sopenharmony_ci	/* DUR field for fallback rate */
64718c2ecf20Sopenharmony_ci	if (ieee80211_is_pspoll(h->frame_control))
64728c2ecf20Sopenharmony_ci		txh->FragDurFallback = h->duration_id;
64738c2ecf20Sopenharmony_ci	else if (is_multicast_ether_addr(h->addr1) || use_rifs)
64748c2ecf20Sopenharmony_ci		txh->FragDurFallback = 0;
64758c2ecf20Sopenharmony_ci	else {
64768c2ecf20Sopenharmony_ci		durid = brcms_c_compute_frame_dur(wlc, rspec[1],
64778c2ecf20Sopenharmony_ci					      preamble_type[1], next_frag_len);
64788c2ecf20Sopenharmony_ci		txh->FragDurFallback = cpu_to_le16(durid);
64798c2ecf20Sopenharmony_ci	}
64808c2ecf20Sopenharmony_ci
64818c2ecf20Sopenharmony_ci	/* (4) MAC-HDR: MacTxControlLow */
64828c2ecf20Sopenharmony_ci	if (frag == 0)
64838c2ecf20Sopenharmony_ci		mcl |= TXC_STARTMSDU;
64848c2ecf20Sopenharmony_ci
64858c2ecf20Sopenharmony_ci	if (!is_multicast_ether_addr(h->addr1))
64868c2ecf20Sopenharmony_ci		mcl |= TXC_IMMEDACK;
64878c2ecf20Sopenharmony_ci
64888c2ecf20Sopenharmony_ci	if (wlc->band->bandtype == BRCM_BAND_5G)
64898c2ecf20Sopenharmony_ci		mcl |= TXC_FREQBAND_5G;
64908c2ecf20Sopenharmony_ci
64918c2ecf20Sopenharmony_ci	if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
64928c2ecf20Sopenharmony_ci		mcl |= TXC_BW_40;
64938c2ecf20Sopenharmony_ci
64948c2ecf20Sopenharmony_ci	/* set AMIC bit if using hardware TKIP MIC */
64958c2ecf20Sopenharmony_ci	if (hwtkmic)
64968c2ecf20Sopenharmony_ci		mcl |= TXC_AMIC;
64978c2ecf20Sopenharmony_ci
64988c2ecf20Sopenharmony_ci	txh->MacTxControlLow = cpu_to_le16(mcl);
64998c2ecf20Sopenharmony_ci
65008c2ecf20Sopenharmony_ci	/* MacTxControlHigh */
65018c2ecf20Sopenharmony_ci	mch = 0;
65028c2ecf20Sopenharmony_ci
65038c2ecf20Sopenharmony_ci	/* Set fallback rate preamble type */
65048c2ecf20Sopenharmony_ci	if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
65058c2ecf20Sopenharmony_ci	    (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
65068c2ecf20Sopenharmony_ci		if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
65078c2ecf20Sopenharmony_ci			mch |= TXC_PREAMBLE_DATA_FB_SHORT;
65088c2ecf20Sopenharmony_ci	}
65098c2ecf20Sopenharmony_ci
65108c2ecf20Sopenharmony_ci	/* MacFrameControl */
65118c2ecf20Sopenharmony_ci	memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
65128c2ecf20Sopenharmony_ci	txh->TxFesTimeNormal = cpu_to_le16(0);
65138c2ecf20Sopenharmony_ci
65148c2ecf20Sopenharmony_ci	txh->TxFesTimeFallback = cpu_to_le16(0);
65158c2ecf20Sopenharmony_ci
65168c2ecf20Sopenharmony_ci	/* TxFrameRA */
65178c2ecf20Sopenharmony_ci	memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
65188c2ecf20Sopenharmony_ci
65198c2ecf20Sopenharmony_ci	/* TxFrameID */
65208c2ecf20Sopenharmony_ci	txh->TxFrameID = cpu_to_le16(frameid);
65218c2ecf20Sopenharmony_ci
65228c2ecf20Sopenharmony_ci	/*
65238c2ecf20Sopenharmony_ci	 * TxStatus, Note the case of recreating the first frag of a suppressed
65248c2ecf20Sopenharmony_ci	 * frame then we may need to reset the retry cnt's via the status reg
65258c2ecf20Sopenharmony_ci	 */
65268c2ecf20Sopenharmony_ci	txh->TxStatus = cpu_to_le16(status);
65278c2ecf20Sopenharmony_ci
65288c2ecf20Sopenharmony_ci	/*
65298c2ecf20Sopenharmony_ci	 * extra fields for ucode AMPDU aggregation, the new fields are added to
65308c2ecf20Sopenharmony_ci	 * the END of previous structure so that it's compatible in driver.
65318c2ecf20Sopenharmony_ci	 */
65328c2ecf20Sopenharmony_ci	txh->MaxNMpdus = cpu_to_le16(0);
65338c2ecf20Sopenharmony_ci	txh->MaxABytes_MRT = cpu_to_le16(0);
65348c2ecf20Sopenharmony_ci	txh->MaxABytes_FBR = cpu_to_le16(0);
65358c2ecf20Sopenharmony_ci	txh->MinMBytes = cpu_to_le16(0);
65368c2ecf20Sopenharmony_ci
65378c2ecf20Sopenharmony_ci	/* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
65388c2ecf20Sopenharmony_ci	 * furnish struct d11txh */
65398c2ecf20Sopenharmony_ci	/* RTS PLCP header and RTS frame */
65408c2ecf20Sopenharmony_ci	if (use_rts || use_cts) {
65418c2ecf20Sopenharmony_ci		if (use_rts && use_cts)
65428c2ecf20Sopenharmony_ci			use_cts = false;
65438c2ecf20Sopenharmony_ci
65448c2ecf20Sopenharmony_ci		for (k = 0; k < 2; k++) {
65458c2ecf20Sopenharmony_ci			rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
65468c2ecf20Sopenharmony_ci							      false,
65478c2ecf20Sopenharmony_ci							      mimo_ctlchbw);
65488c2ecf20Sopenharmony_ci		}
65498c2ecf20Sopenharmony_ci
65508c2ecf20Sopenharmony_ci		if (!is_ofdm_rate(rts_rspec[0]) &&
65518c2ecf20Sopenharmony_ci		    !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
65528c2ecf20Sopenharmony_ci		      (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
65538c2ecf20Sopenharmony_ci			rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
65548c2ecf20Sopenharmony_ci			mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
65558c2ecf20Sopenharmony_ci		}
65568c2ecf20Sopenharmony_ci
65578c2ecf20Sopenharmony_ci		if (!is_ofdm_rate(rts_rspec[1]) &&
65588c2ecf20Sopenharmony_ci		    !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
65598c2ecf20Sopenharmony_ci		      (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
65608c2ecf20Sopenharmony_ci			rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
65618c2ecf20Sopenharmony_ci			mch |= TXC_PREAMBLE_RTS_FB_SHORT;
65628c2ecf20Sopenharmony_ci		}
65638c2ecf20Sopenharmony_ci
65648c2ecf20Sopenharmony_ci		/* RTS/CTS additions to MacTxControlLow */
65658c2ecf20Sopenharmony_ci		if (use_cts) {
65668c2ecf20Sopenharmony_ci			txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
65678c2ecf20Sopenharmony_ci		} else {
65688c2ecf20Sopenharmony_ci			txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
65698c2ecf20Sopenharmony_ci			txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
65708c2ecf20Sopenharmony_ci		}
65718c2ecf20Sopenharmony_ci
65728c2ecf20Sopenharmony_ci		/* RTS PLCP header */
65738c2ecf20Sopenharmony_ci		rts_plcp = txh->RTSPhyHeader;
65748c2ecf20Sopenharmony_ci		if (use_cts)
65758c2ecf20Sopenharmony_ci			rts_phylen = DOT11_CTS_LEN + FCS_LEN;
65768c2ecf20Sopenharmony_ci		else
65778c2ecf20Sopenharmony_ci			rts_phylen = DOT11_RTS_LEN + FCS_LEN;
65788c2ecf20Sopenharmony_ci
65798c2ecf20Sopenharmony_ci		brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
65808c2ecf20Sopenharmony_ci
65818c2ecf20Sopenharmony_ci		/* fallback rate version of RTS PLCP header */
65828c2ecf20Sopenharmony_ci		brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
65838c2ecf20Sopenharmony_ci				 rts_plcp_fallback);
65848c2ecf20Sopenharmony_ci		memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
65858c2ecf20Sopenharmony_ci		       sizeof(txh->RTSPLCPFallback));
65868c2ecf20Sopenharmony_ci
65878c2ecf20Sopenharmony_ci		/* RTS frame fields... */
65888c2ecf20Sopenharmony_ci		rts = (struct ieee80211_rts *)&txh->rts_frame;
65898c2ecf20Sopenharmony_ci
65908c2ecf20Sopenharmony_ci		durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
65918c2ecf20Sopenharmony_ci					       rspec[0], rts_preamble_type[0],
65928c2ecf20Sopenharmony_ci					       preamble_type[0], phylen, false);
65938c2ecf20Sopenharmony_ci		rts->duration = cpu_to_le16(durid);
65948c2ecf20Sopenharmony_ci		/* fallback rate version of RTS DUR field */
65958c2ecf20Sopenharmony_ci		durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
65968c2ecf20Sopenharmony_ci					       rts_rspec[1], rspec[1],
65978c2ecf20Sopenharmony_ci					       rts_preamble_type[1],
65988c2ecf20Sopenharmony_ci					       preamble_type[1], phylen, false);
65998c2ecf20Sopenharmony_ci		txh->RTSDurFallback = cpu_to_le16(durid);
66008c2ecf20Sopenharmony_ci
66018c2ecf20Sopenharmony_ci		if (use_cts) {
66028c2ecf20Sopenharmony_ci			rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
66038c2ecf20Sopenharmony_ci							 IEEE80211_STYPE_CTS);
66048c2ecf20Sopenharmony_ci
66058c2ecf20Sopenharmony_ci			memcpy(&rts->ra, &h->addr2, ETH_ALEN);
66068c2ecf20Sopenharmony_ci		} else {
66078c2ecf20Sopenharmony_ci			rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
66088c2ecf20Sopenharmony_ci							 IEEE80211_STYPE_RTS);
66098c2ecf20Sopenharmony_ci
66108c2ecf20Sopenharmony_ci			memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
66118c2ecf20Sopenharmony_ci		}
66128c2ecf20Sopenharmony_ci
66138c2ecf20Sopenharmony_ci		/* mainrate
66148c2ecf20Sopenharmony_ci		 *    low 8 bits: main frag rate/mcs,
66158c2ecf20Sopenharmony_ci		 *    high 8 bits: rts/cts rate/mcs
66168c2ecf20Sopenharmony_ci		 */
66178c2ecf20Sopenharmony_ci		mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
66188c2ecf20Sopenharmony_ci				D11A_PHY_HDR_GRATE(
66198c2ecf20Sopenharmony_ci					(struct ofdm_phy_hdr *) rts_plcp) :
66208c2ecf20Sopenharmony_ci				rts_plcp[0]) << 8;
66218c2ecf20Sopenharmony_ci	} else {
66228c2ecf20Sopenharmony_ci		memset(txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
66238c2ecf20Sopenharmony_ci		memset(&txh->rts_frame, 0, sizeof(struct ieee80211_rts));
66248c2ecf20Sopenharmony_ci		memset(txh->RTSPLCPFallback, 0, sizeof(txh->RTSPLCPFallback));
66258c2ecf20Sopenharmony_ci		txh->RTSDurFallback = 0;
66268c2ecf20Sopenharmony_ci	}
66278c2ecf20Sopenharmony_ci
66288c2ecf20Sopenharmony_ci#ifdef SUPPORT_40MHZ
66298c2ecf20Sopenharmony_ci	/* add null delimiter count */
66308c2ecf20Sopenharmony_ci	if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
66318c2ecf20Sopenharmony_ci		txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
66328c2ecf20Sopenharmony_ci		   brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
66338c2ecf20Sopenharmony_ci
66348c2ecf20Sopenharmony_ci#endif
66358c2ecf20Sopenharmony_ci
66368c2ecf20Sopenharmony_ci	/*
66378c2ecf20Sopenharmony_ci	 * Now that RTS/RTS FB preamble types are updated, write
66388c2ecf20Sopenharmony_ci	 * the final value
66398c2ecf20Sopenharmony_ci	 */
66408c2ecf20Sopenharmony_ci	txh->MacTxControlHigh = cpu_to_le16(mch);
66418c2ecf20Sopenharmony_ci
66428c2ecf20Sopenharmony_ci	/*
66438c2ecf20Sopenharmony_ci	 * MainRates (both the rts and frag plcp rates have
66448c2ecf20Sopenharmony_ci	 * been calculated now)
66458c2ecf20Sopenharmony_ci	 */
66468c2ecf20Sopenharmony_ci	txh->MainRates = cpu_to_le16(mainrates);
66478c2ecf20Sopenharmony_ci
66488c2ecf20Sopenharmony_ci	/* XtraFrameTypes */
66498c2ecf20Sopenharmony_ci	xfts = frametype(rspec[1], wlc->mimoft);
66508c2ecf20Sopenharmony_ci	xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
66518c2ecf20Sopenharmony_ci	xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
66528c2ecf20Sopenharmony_ci	xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
66538c2ecf20Sopenharmony_ci							     XFTS_CHANNEL_SHIFT;
66548c2ecf20Sopenharmony_ci	txh->XtraFrameTypes = cpu_to_le16(xfts);
66558c2ecf20Sopenharmony_ci
66568c2ecf20Sopenharmony_ci	/* PhyTxControlWord */
66578c2ecf20Sopenharmony_ci	phyctl = frametype(rspec[0], wlc->mimoft);
66588c2ecf20Sopenharmony_ci	if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
66598c2ecf20Sopenharmony_ci	    (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
66608c2ecf20Sopenharmony_ci		if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
66618c2ecf20Sopenharmony_ci			phyctl |= PHY_TXC_SHORT_HDR;
66628c2ecf20Sopenharmony_ci	}
66638c2ecf20Sopenharmony_ci
66648c2ecf20Sopenharmony_ci	/* phytxant is properly bit shifted */
66658c2ecf20Sopenharmony_ci	phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
66668c2ecf20Sopenharmony_ci	txh->PhyTxControlWord = cpu_to_le16(phyctl);
66678c2ecf20Sopenharmony_ci
66688c2ecf20Sopenharmony_ci	/* PhyTxControlWord_1 */
66698c2ecf20Sopenharmony_ci	if (BRCMS_PHY_11N_CAP(wlc->band)) {
66708c2ecf20Sopenharmony_ci		u16 phyctl1 = 0;
66718c2ecf20Sopenharmony_ci
66728c2ecf20Sopenharmony_ci		phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
66738c2ecf20Sopenharmony_ci		txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
66748c2ecf20Sopenharmony_ci		phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
66758c2ecf20Sopenharmony_ci		txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
66768c2ecf20Sopenharmony_ci
66778c2ecf20Sopenharmony_ci		if (use_rts || use_cts) {
66788c2ecf20Sopenharmony_ci			phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
66798c2ecf20Sopenharmony_ci			txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
66808c2ecf20Sopenharmony_ci			phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
66818c2ecf20Sopenharmony_ci			txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
66828c2ecf20Sopenharmony_ci		}
66838c2ecf20Sopenharmony_ci
66848c2ecf20Sopenharmony_ci		/*
66858c2ecf20Sopenharmony_ci		 * For mcs frames, if mixedmode(overloaded with long preamble)
66868c2ecf20Sopenharmony_ci		 * is going to be set, fill in non-zero MModeLen and/or
66878c2ecf20Sopenharmony_ci		 * MModeFbrLen it will be unnecessary if they are separated
66888c2ecf20Sopenharmony_ci		 */
66898c2ecf20Sopenharmony_ci		if (is_mcs_rate(rspec[0]) &&
66908c2ecf20Sopenharmony_ci		    (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
66918c2ecf20Sopenharmony_ci			u16 mmodelen =
66928c2ecf20Sopenharmony_ci			    brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
66938c2ecf20Sopenharmony_ci			txh->MModeLen = cpu_to_le16(mmodelen);
66948c2ecf20Sopenharmony_ci		}
66958c2ecf20Sopenharmony_ci
66968c2ecf20Sopenharmony_ci		if (is_mcs_rate(rspec[1]) &&
66978c2ecf20Sopenharmony_ci		    (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
66988c2ecf20Sopenharmony_ci			u16 mmodefbrlen =
66998c2ecf20Sopenharmony_ci			    brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
67008c2ecf20Sopenharmony_ci			txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
67018c2ecf20Sopenharmony_ci		}
67028c2ecf20Sopenharmony_ci	}
67038c2ecf20Sopenharmony_ci
67048c2ecf20Sopenharmony_ci	ac = skb_get_queue_mapping(p);
67058c2ecf20Sopenharmony_ci	if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
67068c2ecf20Sopenharmony_ci		uint frag_dur, dur, dur_fallback;
67078c2ecf20Sopenharmony_ci
67088c2ecf20Sopenharmony_ci		/* WME: Update TXOP threshold */
67098c2ecf20Sopenharmony_ci		if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
67108c2ecf20Sopenharmony_ci			frag_dur =
67118c2ecf20Sopenharmony_ci			    brcms_c_calc_frame_time(wlc, rspec[0],
67128c2ecf20Sopenharmony_ci					preamble_type[0], phylen);
67138c2ecf20Sopenharmony_ci
67148c2ecf20Sopenharmony_ci			if (rts) {
67158c2ecf20Sopenharmony_ci				/* 1 RTS or CTS-to-self frame */
67168c2ecf20Sopenharmony_ci				dur =
67178c2ecf20Sopenharmony_ci				    brcms_c_calc_cts_time(wlc, rts_rspec[0],
67188c2ecf20Sopenharmony_ci						      rts_preamble_type[0]);
67198c2ecf20Sopenharmony_ci				dur_fallback =
67208c2ecf20Sopenharmony_ci				    brcms_c_calc_cts_time(wlc, rts_rspec[1],
67218c2ecf20Sopenharmony_ci						      rts_preamble_type[1]);
67228c2ecf20Sopenharmony_ci				/* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
67238c2ecf20Sopenharmony_ci				dur += le16_to_cpu(rts->duration);
67248c2ecf20Sopenharmony_ci				dur_fallback +=
67258c2ecf20Sopenharmony_ci					le16_to_cpu(txh->RTSDurFallback);
67268c2ecf20Sopenharmony_ci			} else if (use_rifs) {
67278c2ecf20Sopenharmony_ci				dur = frag_dur;
67288c2ecf20Sopenharmony_ci				dur_fallback = 0;
67298c2ecf20Sopenharmony_ci			} else {
67308c2ecf20Sopenharmony_ci				/* frame + SIFS + ACK */
67318c2ecf20Sopenharmony_ci				dur = frag_dur;
67328c2ecf20Sopenharmony_ci				dur +=
67338c2ecf20Sopenharmony_ci				    brcms_c_compute_frame_dur(wlc, rspec[0],
67348c2ecf20Sopenharmony_ci							  preamble_type[0], 0);
67358c2ecf20Sopenharmony_ci
67368c2ecf20Sopenharmony_ci				dur_fallback =
67378c2ecf20Sopenharmony_ci				    brcms_c_calc_frame_time(wlc, rspec[1],
67388c2ecf20Sopenharmony_ci							preamble_type[1],
67398c2ecf20Sopenharmony_ci							phylen);
67408c2ecf20Sopenharmony_ci				dur_fallback +=
67418c2ecf20Sopenharmony_ci				    brcms_c_compute_frame_dur(wlc, rspec[1],
67428c2ecf20Sopenharmony_ci							  preamble_type[1], 0);
67438c2ecf20Sopenharmony_ci			}
67448c2ecf20Sopenharmony_ci			/* NEED to set TxFesTimeNormal (hard) */
67458c2ecf20Sopenharmony_ci			txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
67468c2ecf20Sopenharmony_ci			/*
67478c2ecf20Sopenharmony_ci			 * NEED to set fallback rate version of
67488c2ecf20Sopenharmony_ci			 * TxFesTimeNormal (hard)
67498c2ecf20Sopenharmony_ci			 */
67508c2ecf20Sopenharmony_ci			txh->TxFesTimeFallback =
67518c2ecf20Sopenharmony_ci				cpu_to_le16((u16) dur_fallback);
67528c2ecf20Sopenharmony_ci
67538c2ecf20Sopenharmony_ci			/*
67548c2ecf20Sopenharmony_ci			 * update txop byte threshold (txop minus intraframe
67558c2ecf20Sopenharmony_ci			 * overhead)
67568c2ecf20Sopenharmony_ci			 */
67578c2ecf20Sopenharmony_ci			if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
67588c2ecf20Sopenharmony_ci				uint newfragthresh;
67598c2ecf20Sopenharmony_ci
67608c2ecf20Sopenharmony_ci				newfragthresh =
67618c2ecf20Sopenharmony_ci				    brcms_c_calc_frame_len(wlc,
67628c2ecf20Sopenharmony_ci					rspec[0], preamble_type[0],
67638c2ecf20Sopenharmony_ci					(wlc->edcf_txop[ac] -
67648c2ecf20Sopenharmony_ci						(dur - frag_dur)));
67658c2ecf20Sopenharmony_ci				/* range bound the fragthreshold */
67668c2ecf20Sopenharmony_ci				if (newfragthresh < DOT11_MIN_FRAG_LEN)
67678c2ecf20Sopenharmony_ci					newfragthresh =
67688c2ecf20Sopenharmony_ci					    DOT11_MIN_FRAG_LEN;
67698c2ecf20Sopenharmony_ci				else if (newfragthresh >
67708c2ecf20Sopenharmony_ci					 wlc->usr_fragthresh)
67718c2ecf20Sopenharmony_ci					newfragthresh =
67728c2ecf20Sopenharmony_ci					    wlc->usr_fragthresh;
67738c2ecf20Sopenharmony_ci				/* update the fragthresh and do txc update */
67748c2ecf20Sopenharmony_ci				if (wlc->fragthresh[queue] !=
67758c2ecf20Sopenharmony_ci				    (u16) newfragthresh)
67768c2ecf20Sopenharmony_ci					wlc->fragthresh[queue] =
67778c2ecf20Sopenharmony_ci					    (u16) newfragthresh;
67788c2ecf20Sopenharmony_ci			} else {
67798c2ecf20Sopenharmony_ci				brcms_warn(wlc->hw->d11core,
67808c2ecf20Sopenharmony_ci					   "wl%d: %s txop invalid for rate %d\n",
67818c2ecf20Sopenharmony_ci					   wlc->pub->unit, fifo_names[queue],
67828c2ecf20Sopenharmony_ci					   rspec2rate(rspec[0]));
67838c2ecf20Sopenharmony_ci			}
67848c2ecf20Sopenharmony_ci
67858c2ecf20Sopenharmony_ci			if (dur > wlc->edcf_txop[ac])
67868c2ecf20Sopenharmony_ci				brcms_warn(wlc->hw->d11core,
67878c2ecf20Sopenharmony_ci					   "wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
67888c2ecf20Sopenharmony_ci					   wlc->pub->unit, __func__,
67898c2ecf20Sopenharmony_ci					   fifo_names[queue],
67908c2ecf20Sopenharmony_ci					   phylen, wlc->fragthresh[queue],
67918c2ecf20Sopenharmony_ci					   dur, wlc->edcf_txop[ac]);
67928c2ecf20Sopenharmony_ci		}
67938c2ecf20Sopenharmony_ci	}
67948c2ecf20Sopenharmony_ci
67958c2ecf20Sopenharmony_ci	return 0;
67968c2ecf20Sopenharmony_ci}
67978c2ecf20Sopenharmony_ci
67988c2ecf20Sopenharmony_cistatic int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
67998c2ecf20Sopenharmony_ci{
68008c2ecf20Sopenharmony_ci	struct dma_pub *dma;
68018c2ecf20Sopenharmony_ci	int fifo, ret = -ENOSPC;
68028c2ecf20Sopenharmony_ci	struct d11txh *txh;
68038c2ecf20Sopenharmony_ci	u16 frameid = INVALIDFID;
68048c2ecf20Sopenharmony_ci
68058c2ecf20Sopenharmony_ci	fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
68068c2ecf20Sopenharmony_ci	dma = wlc->hw->di[fifo];
68078c2ecf20Sopenharmony_ci	txh = (struct d11txh *)(skb->data);
68088c2ecf20Sopenharmony_ci
68098c2ecf20Sopenharmony_ci	if (dma->txavail == 0) {
68108c2ecf20Sopenharmony_ci		/*
68118c2ecf20Sopenharmony_ci		 * We sometimes get a frame from mac80211 after stopping
68128c2ecf20Sopenharmony_ci		 * the queues. This only ever seems to be a single frame
68138c2ecf20Sopenharmony_ci		 * and is seems likely to be a race. TX_HEADROOM should
68148c2ecf20Sopenharmony_ci		 * ensure that we have enough space to handle these stray
68158c2ecf20Sopenharmony_ci		 * packets, so warn if there isn't. If we're out of space
68168c2ecf20Sopenharmony_ci		 * in the tx ring and the tx queue isn't stopped then
68178c2ecf20Sopenharmony_ci		 * we've really got a bug; warn loudly if that happens.
68188c2ecf20Sopenharmony_ci		 */
68198c2ecf20Sopenharmony_ci		brcms_warn(wlc->hw->d11core,
68208c2ecf20Sopenharmony_ci			   "Received frame for tx with no space in DMA ring\n");
68218c2ecf20Sopenharmony_ci		WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
68228c2ecf20Sopenharmony_ci						 skb_get_queue_mapping(skb)));
68238c2ecf20Sopenharmony_ci		return -ENOSPC;
68248c2ecf20Sopenharmony_ci	}
68258c2ecf20Sopenharmony_ci
68268c2ecf20Sopenharmony_ci	/* When a BC/MC frame is being committed to the BCMC fifo
68278c2ecf20Sopenharmony_ci	 * via DMA (NOT PIO), update ucode or BSS info as appropriate.
68288c2ecf20Sopenharmony_ci	 */
68298c2ecf20Sopenharmony_ci	if (fifo == TX_BCMC_FIFO)
68308c2ecf20Sopenharmony_ci		frameid = le16_to_cpu(txh->TxFrameID);
68318c2ecf20Sopenharmony_ci
68328c2ecf20Sopenharmony_ci	/* Commit BCMC sequence number in the SHM frame ID location */
68338c2ecf20Sopenharmony_ci	if (frameid != INVALIDFID) {
68348c2ecf20Sopenharmony_ci		/*
68358c2ecf20Sopenharmony_ci		 * To inform the ucode of the last mcast frame posted
68368c2ecf20Sopenharmony_ci		 * so that it can clear moredata bit
68378c2ecf20Sopenharmony_ci		 */
68388c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
68398c2ecf20Sopenharmony_ci	}
68408c2ecf20Sopenharmony_ci
68418c2ecf20Sopenharmony_ci	ret = brcms_c_txfifo(wlc, fifo, skb);
68428c2ecf20Sopenharmony_ci	/*
68438c2ecf20Sopenharmony_ci	 * The only reason for brcms_c_txfifo to fail is because
68448c2ecf20Sopenharmony_ci	 * there weren't any DMA descriptors, but we've already
68458c2ecf20Sopenharmony_ci	 * checked for that. So if it does fail yell loudly.
68468c2ecf20Sopenharmony_ci	 */
68478c2ecf20Sopenharmony_ci	WARN_ON_ONCE(ret);
68488c2ecf20Sopenharmony_ci
68498c2ecf20Sopenharmony_ci	return ret;
68508c2ecf20Sopenharmony_ci}
68518c2ecf20Sopenharmony_ci
68528c2ecf20Sopenharmony_cibool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
68538c2ecf20Sopenharmony_ci			      struct ieee80211_hw *hw)
68548c2ecf20Sopenharmony_ci{
68558c2ecf20Sopenharmony_ci	uint fifo;
68568c2ecf20Sopenharmony_ci	struct scb *scb = &wlc->pri_scb;
68578c2ecf20Sopenharmony_ci
68588c2ecf20Sopenharmony_ci	fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
68598c2ecf20Sopenharmony_ci	brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
68608c2ecf20Sopenharmony_ci	if (!brcms_c_tx(wlc, sdu))
68618c2ecf20Sopenharmony_ci		return true;
68628c2ecf20Sopenharmony_ci
68638c2ecf20Sopenharmony_ci	/* packet discarded */
68648c2ecf20Sopenharmony_ci	dev_kfree_skb_any(sdu);
68658c2ecf20Sopenharmony_ci	return false;
68668c2ecf20Sopenharmony_ci}
68678c2ecf20Sopenharmony_ci
68688c2ecf20Sopenharmony_ciint
68698c2ecf20Sopenharmony_cibrcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
68708c2ecf20Sopenharmony_ci{
68718c2ecf20Sopenharmony_ci	struct dma_pub *dma = wlc->hw->di[fifo];
68728c2ecf20Sopenharmony_ci	int ret;
68738c2ecf20Sopenharmony_ci	u16 queue;
68748c2ecf20Sopenharmony_ci
68758c2ecf20Sopenharmony_ci	ret = dma_txfast(wlc, dma, p);
68768c2ecf20Sopenharmony_ci	if (ret	< 0)
68778c2ecf20Sopenharmony_ci		wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
68788c2ecf20Sopenharmony_ci
68798c2ecf20Sopenharmony_ci	/*
68808c2ecf20Sopenharmony_ci	 * Stop queue if DMA ring is full. Reserve some free descriptors,
68818c2ecf20Sopenharmony_ci	 * as we sometimes receive a frame from mac80211 after the queues
68828c2ecf20Sopenharmony_ci	 * are stopped.
68838c2ecf20Sopenharmony_ci	 */
68848c2ecf20Sopenharmony_ci	queue = skb_get_queue_mapping(p);
68858c2ecf20Sopenharmony_ci	if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
68868c2ecf20Sopenharmony_ci	    !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
68878c2ecf20Sopenharmony_ci		ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
68888c2ecf20Sopenharmony_ci
68898c2ecf20Sopenharmony_ci	return ret;
68908c2ecf20Sopenharmony_ci}
68918c2ecf20Sopenharmony_ci
68928c2ecf20Sopenharmony_ciu32
68938c2ecf20Sopenharmony_cibrcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
68948c2ecf20Sopenharmony_ci			   bool use_rspec, u16 mimo_ctlchbw)
68958c2ecf20Sopenharmony_ci{
68968c2ecf20Sopenharmony_ci	u32 rts_rspec = 0;
68978c2ecf20Sopenharmony_ci
68988c2ecf20Sopenharmony_ci	if (use_rspec)
68998c2ecf20Sopenharmony_ci		/* use frame rate as rts rate */
69008c2ecf20Sopenharmony_ci		rts_rspec = rspec;
69018c2ecf20Sopenharmony_ci	else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
69028c2ecf20Sopenharmony_ci		/* Use 11Mbps as the g protection RTS target rate and fallback.
69038c2ecf20Sopenharmony_ci		 * Use the brcms_basic_rate() lookup to find the best basic rate
69048c2ecf20Sopenharmony_ci		 * under the target in case 11 Mbps is not Basic.
69058c2ecf20Sopenharmony_ci		 * 6 and 9 Mbps are not usually selected by rate selection, but
69068c2ecf20Sopenharmony_ci		 * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
69078c2ecf20Sopenharmony_ci		 * is more robust.
69088c2ecf20Sopenharmony_ci		 */
69098c2ecf20Sopenharmony_ci		rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
69108c2ecf20Sopenharmony_ci	else
69118c2ecf20Sopenharmony_ci		/* calculate RTS rate and fallback rate based on the frame rate
69128c2ecf20Sopenharmony_ci		 * RTS must be sent at a basic rate since it is a
69138c2ecf20Sopenharmony_ci		 * control frame, sec 9.6 of 802.11 spec
69148c2ecf20Sopenharmony_ci		 */
69158c2ecf20Sopenharmony_ci		rts_rspec = brcms_basic_rate(wlc, rspec);
69168c2ecf20Sopenharmony_ci
69178c2ecf20Sopenharmony_ci	if (BRCMS_PHY_11N_CAP(wlc->band)) {
69188c2ecf20Sopenharmony_ci		/* set rts txbw to correct side band */
69198c2ecf20Sopenharmony_ci		rts_rspec &= ~RSPEC_BW_MASK;
69208c2ecf20Sopenharmony_ci
69218c2ecf20Sopenharmony_ci		/*
69228c2ecf20Sopenharmony_ci		 * if rspec/rspec_fallback is 40MHz, then send RTS on both
69238c2ecf20Sopenharmony_ci		 * 20MHz channel (DUP), otherwise send RTS on control channel
69248c2ecf20Sopenharmony_ci		 */
69258c2ecf20Sopenharmony_ci		if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
69268c2ecf20Sopenharmony_ci			rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
69278c2ecf20Sopenharmony_ci		else
69288c2ecf20Sopenharmony_ci			rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
69298c2ecf20Sopenharmony_ci
69308c2ecf20Sopenharmony_ci		/* pick siso/cdd as default for ofdm */
69318c2ecf20Sopenharmony_ci		if (is_ofdm_rate(rts_rspec)) {
69328c2ecf20Sopenharmony_ci			rts_rspec &= ~RSPEC_STF_MASK;
69338c2ecf20Sopenharmony_ci			rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
69348c2ecf20Sopenharmony_ci		}
69358c2ecf20Sopenharmony_ci	}
69368c2ecf20Sopenharmony_ci	return rts_rspec;
69378c2ecf20Sopenharmony_ci}
69388c2ecf20Sopenharmony_ci
69398c2ecf20Sopenharmony_ci/* Update beacon listen interval in shared memory */
69408c2ecf20Sopenharmony_cistatic void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
69418c2ecf20Sopenharmony_ci{
69428c2ecf20Sopenharmony_ci	/* wake up every DTIM is the default */
69438c2ecf20Sopenharmony_ci	if (wlc->bcn_li_dtim == 1)
69448c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
69458c2ecf20Sopenharmony_ci	else
69468c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, M_BCN_LI,
69478c2ecf20Sopenharmony_ci			      (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
69488c2ecf20Sopenharmony_ci}
69498c2ecf20Sopenharmony_ci
69508c2ecf20Sopenharmony_cistatic void
69518c2ecf20Sopenharmony_cibrcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
69528c2ecf20Sopenharmony_ci		  u32 *tsf_h_ptr)
69538c2ecf20Sopenharmony_ci{
69548c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
69558c2ecf20Sopenharmony_ci
69568c2ecf20Sopenharmony_ci	/* read the tsf timer low, then high to get an atomic read */
69578c2ecf20Sopenharmony_ci	*tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
69588c2ecf20Sopenharmony_ci	*tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
69598c2ecf20Sopenharmony_ci}
69608c2ecf20Sopenharmony_ci
69618c2ecf20Sopenharmony_ci/*
69628c2ecf20Sopenharmony_ci * recover 64bit TSF value from the 16bit TSF value in the rx header
69638c2ecf20Sopenharmony_ci * given the assumption that the TSF passed in header is within 65ms
69648c2ecf20Sopenharmony_ci * of the current tsf.
69658c2ecf20Sopenharmony_ci *
69668c2ecf20Sopenharmony_ci * 6       5       4       4       3       2       1
69678c2ecf20Sopenharmony_ci * 3.......6.......8.......0.......2.......4.......6.......8......0
69688c2ecf20Sopenharmony_ci * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
69698c2ecf20Sopenharmony_ci *
69708c2ecf20Sopenharmony_ci * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
69718c2ecf20Sopenharmony_ci * tsf_l is filled in by brcms_b_recv, which is done earlier in the
69728c2ecf20Sopenharmony_ci * receive call sequence after rx interrupt. Only the higher 16 bits
69738c2ecf20Sopenharmony_ci * are used. Finally, the tsf_h is read from the tsf register.
69748c2ecf20Sopenharmony_ci */
69758c2ecf20Sopenharmony_cistatic u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
69768c2ecf20Sopenharmony_ci				 struct d11rxhdr *rxh)
69778c2ecf20Sopenharmony_ci{
69788c2ecf20Sopenharmony_ci	u32 tsf_h, tsf_l;
69798c2ecf20Sopenharmony_ci	u16 rx_tsf_0_15, rx_tsf_16_31;
69808c2ecf20Sopenharmony_ci
69818c2ecf20Sopenharmony_ci	brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
69828c2ecf20Sopenharmony_ci
69838c2ecf20Sopenharmony_ci	rx_tsf_16_31 = (u16)(tsf_l >> 16);
69848c2ecf20Sopenharmony_ci	rx_tsf_0_15 = rxh->RxTSFTime;
69858c2ecf20Sopenharmony_ci
69868c2ecf20Sopenharmony_ci	/*
69878c2ecf20Sopenharmony_ci	 * a greater tsf time indicates the low 16 bits of
69888c2ecf20Sopenharmony_ci	 * tsf_l wrapped, so decrement the high 16 bits.
69898c2ecf20Sopenharmony_ci	 */
69908c2ecf20Sopenharmony_ci	if ((u16)tsf_l < rx_tsf_0_15) {
69918c2ecf20Sopenharmony_ci		rx_tsf_16_31 -= 1;
69928c2ecf20Sopenharmony_ci		if (rx_tsf_16_31 == 0xffff)
69938c2ecf20Sopenharmony_ci			tsf_h -= 1;
69948c2ecf20Sopenharmony_ci	}
69958c2ecf20Sopenharmony_ci
69968c2ecf20Sopenharmony_ci	return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
69978c2ecf20Sopenharmony_ci}
69988c2ecf20Sopenharmony_ci
69998c2ecf20Sopenharmony_cistatic void
70008c2ecf20Sopenharmony_ciprep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
70018c2ecf20Sopenharmony_ci		     struct sk_buff *p,
70028c2ecf20Sopenharmony_ci		     struct ieee80211_rx_status *rx_status)
70038c2ecf20Sopenharmony_ci{
70048c2ecf20Sopenharmony_ci	int channel;
70058c2ecf20Sopenharmony_ci	u32 rspec;
70068c2ecf20Sopenharmony_ci	unsigned char *plcp;
70078c2ecf20Sopenharmony_ci
70088c2ecf20Sopenharmony_ci	/* fill in TSF and flag its presence */
70098c2ecf20Sopenharmony_ci	rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
70108c2ecf20Sopenharmony_ci	rx_status->flag |= RX_FLAG_MACTIME_START;
70118c2ecf20Sopenharmony_ci
70128c2ecf20Sopenharmony_ci	channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
70138c2ecf20Sopenharmony_ci
70148c2ecf20Sopenharmony_ci	rx_status->band =
70158c2ecf20Sopenharmony_ci		channel > 14 ? NL80211_BAND_5GHZ : NL80211_BAND_2GHZ;
70168c2ecf20Sopenharmony_ci	rx_status->freq =
70178c2ecf20Sopenharmony_ci		ieee80211_channel_to_frequency(channel, rx_status->band);
70188c2ecf20Sopenharmony_ci
70198c2ecf20Sopenharmony_ci	rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
70208c2ecf20Sopenharmony_ci
70218c2ecf20Sopenharmony_ci	/* noise */
70228c2ecf20Sopenharmony_ci	/* qual */
70238c2ecf20Sopenharmony_ci	rx_status->antenna =
70248c2ecf20Sopenharmony_ci		(rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
70258c2ecf20Sopenharmony_ci
70268c2ecf20Sopenharmony_ci	plcp = p->data;
70278c2ecf20Sopenharmony_ci
70288c2ecf20Sopenharmony_ci	rspec = brcms_c_compute_rspec(rxh, plcp);
70298c2ecf20Sopenharmony_ci	if (is_mcs_rate(rspec)) {
70308c2ecf20Sopenharmony_ci		rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
70318c2ecf20Sopenharmony_ci		rx_status->encoding = RX_ENC_HT;
70328c2ecf20Sopenharmony_ci		if (rspec_is40mhz(rspec))
70338c2ecf20Sopenharmony_ci			rx_status->bw = RATE_INFO_BW_40;
70348c2ecf20Sopenharmony_ci	} else {
70358c2ecf20Sopenharmony_ci		switch (rspec2rate(rspec)) {
70368c2ecf20Sopenharmony_ci		case BRCM_RATE_1M:
70378c2ecf20Sopenharmony_ci			rx_status->rate_idx = 0;
70388c2ecf20Sopenharmony_ci			break;
70398c2ecf20Sopenharmony_ci		case BRCM_RATE_2M:
70408c2ecf20Sopenharmony_ci			rx_status->rate_idx = 1;
70418c2ecf20Sopenharmony_ci			break;
70428c2ecf20Sopenharmony_ci		case BRCM_RATE_5M5:
70438c2ecf20Sopenharmony_ci			rx_status->rate_idx = 2;
70448c2ecf20Sopenharmony_ci			break;
70458c2ecf20Sopenharmony_ci		case BRCM_RATE_11M:
70468c2ecf20Sopenharmony_ci			rx_status->rate_idx = 3;
70478c2ecf20Sopenharmony_ci			break;
70488c2ecf20Sopenharmony_ci		case BRCM_RATE_6M:
70498c2ecf20Sopenharmony_ci			rx_status->rate_idx = 4;
70508c2ecf20Sopenharmony_ci			break;
70518c2ecf20Sopenharmony_ci		case BRCM_RATE_9M:
70528c2ecf20Sopenharmony_ci			rx_status->rate_idx = 5;
70538c2ecf20Sopenharmony_ci			break;
70548c2ecf20Sopenharmony_ci		case BRCM_RATE_12M:
70558c2ecf20Sopenharmony_ci			rx_status->rate_idx = 6;
70568c2ecf20Sopenharmony_ci			break;
70578c2ecf20Sopenharmony_ci		case BRCM_RATE_18M:
70588c2ecf20Sopenharmony_ci			rx_status->rate_idx = 7;
70598c2ecf20Sopenharmony_ci			break;
70608c2ecf20Sopenharmony_ci		case BRCM_RATE_24M:
70618c2ecf20Sopenharmony_ci			rx_status->rate_idx = 8;
70628c2ecf20Sopenharmony_ci			break;
70638c2ecf20Sopenharmony_ci		case BRCM_RATE_36M:
70648c2ecf20Sopenharmony_ci			rx_status->rate_idx = 9;
70658c2ecf20Sopenharmony_ci			break;
70668c2ecf20Sopenharmony_ci		case BRCM_RATE_48M:
70678c2ecf20Sopenharmony_ci			rx_status->rate_idx = 10;
70688c2ecf20Sopenharmony_ci			break;
70698c2ecf20Sopenharmony_ci		case BRCM_RATE_54M:
70708c2ecf20Sopenharmony_ci			rx_status->rate_idx = 11;
70718c2ecf20Sopenharmony_ci			break;
70728c2ecf20Sopenharmony_ci		default:
70738c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core,
70748c2ecf20Sopenharmony_ci				  "%s: Unknown rate\n", __func__);
70758c2ecf20Sopenharmony_ci		}
70768c2ecf20Sopenharmony_ci
70778c2ecf20Sopenharmony_ci		/*
70788c2ecf20Sopenharmony_ci		 * For 5GHz, we should decrease the index as it is
70798c2ecf20Sopenharmony_ci		 * a subset of the 2.4G rates. See bitrates field
70808c2ecf20Sopenharmony_ci		 * of brcms_band_5GHz_nphy (in mac80211_if.c).
70818c2ecf20Sopenharmony_ci		 */
70828c2ecf20Sopenharmony_ci		if (rx_status->band == NL80211_BAND_5GHZ)
70838c2ecf20Sopenharmony_ci			rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
70848c2ecf20Sopenharmony_ci
70858c2ecf20Sopenharmony_ci		/* Determine short preamble and rate_idx */
70868c2ecf20Sopenharmony_ci		if (is_cck_rate(rspec)) {
70878c2ecf20Sopenharmony_ci			if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
70888c2ecf20Sopenharmony_ci				rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
70898c2ecf20Sopenharmony_ci		} else if (is_ofdm_rate(rspec)) {
70908c2ecf20Sopenharmony_ci			rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
70918c2ecf20Sopenharmony_ci		} else {
70928c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
70938c2ecf20Sopenharmony_ci				  __func__);
70948c2ecf20Sopenharmony_ci		}
70958c2ecf20Sopenharmony_ci	}
70968c2ecf20Sopenharmony_ci
70978c2ecf20Sopenharmony_ci	if (plcp3_issgi(plcp[3]))
70988c2ecf20Sopenharmony_ci		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
70998c2ecf20Sopenharmony_ci
71008c2ecf20Sopenharmony_ci	if (rxh->RxStatus1 & RXS_DECERR) {
71018c2ecf20Sopenharmony_ci		rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
71028c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "%s:  RX_FLAG_FAILED_PLCP_CRC\n",
71038c2ecf20Sopenharmony_ci			  __func__);
71048c2ecf20Sopenharmony_ci	}
71058c2ecf20Sopenharmony_ci	if (rxh->RxStatus1 & RXS_FCSERR) {
71068c2ecf20Sopenharmony_ci		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
71078c2ecf20Sopenharmony_ci		brcms_err(wlc->hw->d11core, "%s:  RX_FLAG_FAILED_FCS_CRC\n",
71088c2ecf20Sopenharmony_ci			  __func__);
71098c2ecf20Sopenharmony_ci	}
71108c2ecf20Sopenharmony_ci}
71118c2ecf20Sopenharmony_ci
71128c2ecf20Sopenharmony_cistatic void
71138c2ecf20Sopenharmony_cibrcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
71148c2ecf20Sopenharmony_ci		struct sk_buff *p)
71158c2ecf20Sopenharmony_ci{
71168c2ecf20Sopenharmony_ci	int len_mpdu;
71178c2ecf20Sopenharmony_ci	struct ieee80211_rx_status rx_status;
71188c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr;
71198c2ecf20Sopenharmony_ci
71208c2ecf20Sopenharmony_ci	memset(&rx_status, 0, sizeof(rx_status));
71218c2ecf20Sopenharmony_ci	prep_mac80211_status(wlc, rxh, p, &rx_status);
71228c2ecf20Sopenharmony_ci
71238c2ecf20Sopenharmony_ci	/* mac header+body length, exclude CRC and plcp header */
71248c2ecf20Sopenharmony_ci	len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
71258c2ecf20Sopenharmony_ci	skb_pull(p, D11_PHY_HDR_LEN);
71268c2ecf20Sopenharmony_ci	__skb_trim(p, len_mpdu);
71278c2ecf20Sopenharmony_ci
71288c2ecf20Sopenharmony_ci	/* unmute transmit */
71298c2ecf20Sopenharmony_ci	if (wlc->hw->suspended_fifos) {
71308c2ecf20Sopenharmony_ci		hdr = (struct ieee80211_hdr *)p->data;
71318c2ecf20Sopenharmony_ci		if (ieee80211_is_beacon(hdr->frame_control))
71328c2ecf20Sopenharmony_ci			brcms_b_mute(wlc->hw, false);
71338c2ecf20Sopenharmony_ci	}
71348c2ecf20Sopenharmony_ci
71358c2ecf20Sopenharmony_ci	memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
71368c2ecf20Sopenharmony_ci	ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
71378c2ecf20Sopenharmony_ci}
71388c2ecf20Sopenharmony_ci
71398c2ecf20Sopenharmony_ci/* calculate frame duration for Mixed-mode L-SIG spoofing, return
71408c2ecf20Sopenharmony_ci * number of bytes goes in the length field
71418c2ecf20Sopenharmony_ci *
71428c2ecf20Sopenharmony_ci * Formula given by HT PHY Spec v 1.13
71438c2ecf20Sopenharmony_ci *   len = 3(nsyms + nstream + 3) - 3
71448c2ecf20Sopenharmony_ci */
71458c2ecf20Sopenharmony_ciu16
71468c2ecf20Sopenharmony_cibrcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
71478c2ecf20Sopenharmony_ci		      uint mac_len)
71488c2ecf20Sopenharmony_ci{
71498c2ecf20Sopenharmony_ci	uint nsyms, len = 0, kNdps;
71508c2ecf20Sopenharmony_ci
71518c2ecf20Sopenharmony_ci	if (is_mcs_rate(ratespec)) {
71528c2ecf20Sopenharmony_ci		uint mcs = ratespec & RSPEC_RATE_MASK;
71538c2ecf20Sopenharmony_ci		int tot_streams = (mcs_2_txstreams(mcs) + 1) +
71548c2ecf20Sopenharmony_ci				  rspec_stc(ratespec);
71558c2ecf20Sopenharmony_ci
71568c2ecf20Sopenharmony_ci		/*
71578c2ecf20Sopenharmony_ci		 * the payload duration calculation matches that
71588c2ecf20Sopenharmony_ci		 * of regular ofdm
71598c2ecf20Sopenharmony_ci		 */
71608c2ecf20Sopenharmony_ci		/* 1000Ndbps = kbps * 4 */
71618c2ecf20Sopenharmony_ci		kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
71628c2ecf20Sopenharmony_ci				   rspec_issgi(ratespec)) * 4;
71638c2ecf20Sopenharmony_ci
71648c2ecf20Sopenharmony_ci		if (rspec_stc(ratespec) == 0)
71658c2ecf20Sopenharmony_ci			nsyms =
71668c2ecf20Sopenharmony_ci			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
71678c2ecf20Sopenharmony_ci				  APHY_TAIL_NBITS) * 1000, kNdps);
71688c2ecf20Sopenharmony_ci		else
71698c2ecf20Sopenharmony_ci			/* STBC needs to have even number of symbols */
71708c2ecf20Sopenharmony_ci			nsyms =
71718c2ecf20Sopenharmony_ci			    2 *
71728c2ecf20Sopenharmony_ci			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
71738c2ecf20Sopenharmony_ci				  APHY_TAIL_NBITS) * 1000, 2 * kNdps);
71748c2ecf20Sopenharmony_ci
71758c2ecf20Sopenharmony_ci		/* (+3) account for HT-SIG(2) and HT-STF(1) */
71768c2ecf20Sopenharmony_ci		nsyms += (tot_streams + 3);
71778c2ecf20Sopenharmony_ci		/*
71788c2ecf20Sopenharmony_ci		 * 3 bytes/symbol @ legacy 6Mbps rate
71798c2ecf20Sopenharmony_ci		 * (-3) excluding service bits and tail bits
71808c2ecf20Sopenharmony_ci		 */
71818c2ecf20Sopenharmony_ci		len = (3 * nsyms) - 3;
71828c2ecf20Sopenharmony_ci	}
71838c2ecf20Sopenharmony_ci
71848c2ecf20Sopenharmony_ci	return (u16) len;
71858c2ecf20Sopenharmony_ci}
71868c2ecf20Sopenharmony_ci
71878c2ecf20Sopenharmony_cistatic void
71888c2ecf20Sopenharmony_cibrcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
71898c2ecf20Sopenharmony_ci{
71908c2ecf20Sopenharmony_ci	const struct brcms_c_rateset *rs_dflt;
71918c2ecf20Sopenharmony_ci	struct brcms_c_rateset rs;
71928c2ecf20Sopenharmony_ci	u8 rate;
71938c2ecf20Sopenharmony_ci	u16 entry_ptr;
71948c2ecf20Sopenharmony_ci	u8 plcp[D11_PHY_HDR_LEN];
71958c2ecf20Sopenharmony_ci	u16 dur, sifs;
71968c2ecf20Sopenharmony_ci	uint i;
71978c2ecf20Sopenharmony_ci
71988c2ecf20Sopenharmony_ci	sifs = get_sifs(wlc->band);
71998c2ecf20Sopenharmony_ci
72008c2ecf20Sopenharmony_ci	rs_dflt = brcms_c_rateset_get_hwrs(wlc);
72018c2ecf20Sopenharmony_ci
72028c2ecf20Sopenharmony_ci	brcms_c_rateset_copy(rs_dflt, &rs);
72038c2ecf20Sopenharmony_ci	brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
72048c2ecf20Sopenharmony_ci
72058c2ecf20Sopenharmony_ci	/*
72068c2ecf20Sopenharmony_ci	 * walk the phy rate table and update MAC core SHM
72078c2ecf20Sopenharmony_ci	 * basic rate table entries
72088c2ecf20Sopenharmony_ci	 */
72098c2ecf20Sopenharmony_ci	for (i = 0; i < rs.count; i++) {
72108c2ecf20Sopenharmony_ci		rate = rs.rates[i] & BRCMS_RATE_MASK;
72118c2ecf20Sopenharmony_ci
72128c2ecf20Sopenharmony_ci		entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
72138c2ecf20Sopenharmony_ci
72148c2ecf20Sopenharmony_ci		/* Calculate the Probe Response PLCP for the given rate */
72158c2ecf20Sopenharmony_ci		brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
72168c2ecf20Sopenharmony_ci
72178c2ecf20Sopenharmony_ci		/*
72188c2ecf20Sopenharmony_ci		 * Calculate the duration of the Probe Response
72198c2ecf20Sopenharmony_ci		 * frame plus SIFS for the MAC
72208c2ecf20Sopenharmony_ci		 */
72218c2ecf20Sopenharmony_ci		dur = (u16) brcms_c_calc_frame_time(wlc, rate,
72228c2ecf20Sopenharmony_ci						BRCMS_LONG_PREAMBLE, frame_len);
72238c2ecf20Sopenharmony_ci		dur += sifs;
72248c2ecf20Sopenharmony_ci
72258c2ecf20Sopenharmony_ci		/* Update the SHM Rate Table entry Probe Response values */
72268c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
72278c2ecf20Sopenharmony_ci			      (u16) (plcp[0] + (plcp[1] << 8)));
72288c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
72298c2ecf20Sopenharmony_ci			      (u16) (plcp[2] + (plcp[3] << 8)));
72308c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
72318c2ecf20Sopenharmony_ci	}
72328c2ecf20Sopenharmony_ci}
72338c2ecf20Sopenharmony_ci
72348c2ecf20Sopenharmony_ciint brcms_c_get_header_len(void)
72358c2ecf20Sopenharmony_ci{
72368c2ecf20Sopenharmony_ci	return TXOFF;
72378c2ecf20Sopenharmony_ci}
72388c2ecf20Sopenharmony_ci
72398c2ecf20Sopenharmony_cistatic void brcms_c_beacon_write(struct brcms_c_info *wlc,
72408c2ecf20Sopenharmony_ci				 struct sk_buff *beacon, u16 tim_offset,
72418c2ecf20Sopenharmony_ci				 u16 dtim_period, bool bcn0, bool bcn1)
72428c2ecf20Sopenharmony_ci{
72438c2ecf20Sopenharmony_ci	size_t len;
72448c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info;
72458c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
72468c2ecf20Sopenharmony_ci	struct ieee80211_hw *ieee_hw = brcms_c_pub(wlc)->ieee_hw;
72478c2ecf20Sopenharmony_ci
72488c2ecf20Sopenharmony_ci	/* Get tx_info */
72498c2ecf20Sopenharmony_ci	tx_info = IEEE80211_SKB_CB(beacon);
72508c2ecf20Sopenharmony_ci
72518c2ecf20Sopenharmony_ci	len = min_t(size_t, beacon->len, BCN_TMPL_LEN);
72528c2ecf20Sopenharmony_ci	wlc->bcn_rspec = ieee80211_get_tx_rate(ieee_hw, tx_info)->hw_value;
72538c2ecf20Sopenharmony_ci
72548c2ecf20Sopenharmony_ci	brcms_c_compute_plcp(wlc, wlc->bcn_rspec,
72558c2ecf20Sopenharmony_ci			     len + FCS_LEN - D11_PHY_HDR_LEN, beacon->data);
72568c2ecf20Sopenharmony_ci
72578c2ecf20Sopenharmony_ci	/* "Regular" and 16 MBSS but not for 4 MBSS */
72588c2ecf20Sopenharmony_ci	/* Update the phytxctl for the beacon based on the rspec */
72598c2ecf20Sopenharmony_ci	brcms_c_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
72608c2ecf20Sopenharmony_ci
72618c2ecf20Sopenharmony_ci	if (bcn0) {
72628c2ecf20Sopenharmony_ci		/* write the probe response into the template region */
72638c2ecf20Sopenharmony_ci		brcms_b_write_template_ram(wlc_hw, T_BCN0_TPL_BASE,
72648c2ecf20Sopenharmony_ci					    (len + 3) & ~3, beacon->data);
72658c2ecf20Sopenharmony_ci
72668c2ecf20Sopenharmony_ci		/* write beacon length to SCR */
72678c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_BCN0_FRM_BYTESZ, (u16) len);
72688c2ecf20Sopenharmony_ci	}
72698c2ecf20Sopenharmony_ci	if (bcn1) {
72708c2ecf20Sopenharmony_ci		/* write the probe response into the template region */
72718c2ecf20Sopenharmony_ci		brcms_b_write_template_ram(wlc_hw, T_BCN1_TPL_BASE,
72728c2ecf20Sopenharmony_ci					    (len + 3) & ~3, beacon->data);
72738c2ecf20Sopenharmony_ci
72748c2ecf20Sopenharmony_ci		/* write beacon length to SCR */
72758c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_BCN1_FRM_BYTESZ, (u16) len);
72768c2ecf20Sopenharmony_ci	}
72778c2ecf20Sopenharmony_ci
72788c2ecf20Sopenharmony_ci	if (tim_offset != 0) {
72798c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
72808c2ecf20Sopenharmony_ci				  tim_offset + D11B_PHY_HDR_LEN);
72818c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, dtim_period);
72828c2ecf20Sopenharmony_ci	} else {
72838c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
72848c2ecf20Sopenharmony_ci				  len + D11B_PHY_HDR_LEN);
72858c2ecf20Sopenharmony_ci		brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, 0);
72868c2ecf20Sopenharmony_ci	}
72878c2ecf20Sopenharmony_ci}
72888c2ecf20Sopenharmony_ci
72898c2ecf20Sopenharmony_cistatic void brcms_c_update_beacon_hw(struct brcms_c_info *wlc,
72908c2ecf20Sopenharmony_ci				     struct sk_buff *beacon, u16 tim_offset,
72918c2ecf20Sopenharmony_ci				     u16 dtim_period)
72928c2ecf20Sopenharmony_ci{
72938c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
72948c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
72958c2ecf20Sopenharmony_ci
72968c2ecf20Sopenharmony_ci	/* Hardware beaconing for this config */
72978c2ecf20Sopenharmony_ci	u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
72988c2ecf20Sopenharmony_ci
72998c2ecf20Sopenharmony_ci	/* Check if both templates are in use, if so sched. an interrupt
73008c2ecf20Sopenharmony_ci	 *      that will call back into this routine
73018c2ecf20Sopenharmony_ci	 */
73028c2ecf20Sopenharmony_ci	if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid)
73038c2ecf20Sopenharmony_ci		/* clear any previous status */
73048c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(macintstatus), MI_BCNTPL);
73058c2ecf20Sopenharmony_ci
73068c2ecf20Sopenharmony_ci	if (wlc->beacon_template_virgin) {
73078c2ecf20Sopenharmony_ci		wlc->beacon_template_virgin = false;
73088c2ecf20Sopenharmony_ci		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
73098c2ecf20Sopenharmony_ci				     true);
73108c2ecf20Sopenharmony_ci		/* mark beacon0 valid */
73118c2ecf20Sopenharmony_ci		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
73128c2ecf20Sopenharmony_ci		return;
73138c2ecf20Sopenharmony_ci	}
73148c2ecf20Sopenharmony_ci
73158c2ecf20Sopenharmony_ci	/* Check that after scheduling the interrupt both of the
73168c2ecf20Sopenharmony_ci	 *      templates are still busy. if not clear the int. & remask
73178c2ecf20Sopenharmony_ci	 */
73188c2ecf20Sopenharmony_ci	if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid) {
73198c2ecf20Sopenharmony_ci		wlc->defmacintmask |= MI_BCNTPL;
73208c2ecf20Sopenharmony_ci		return;
73218c2ecf20Sopenharmony_ci	}
73228c2ecf20Sopenharmony_ci
73238c2ecf20Sopenharmony_ci	if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN0VLD)) {
73248c2ecf20Sopenharmony_ci		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
73258c2ecf20Sopenharmony_ci				     false);
73268c2ecf20Sopenharmony_ci		/* mark beacon0 valid */
73278c2ecf20Sopenharmony_ci		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
73288c2ecf20Sopenharmony_ci		return;
73298c2ecf20Sopenharmony_ci	}
73308c2ecf20Sopenharmony_ci	if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN1VLD)) {
73318c2ecf20Sopenharmony_ci		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period,
73328c2ecf20Sopenharmony_ci				     false, true);
73338c2ecf20Sopenharmony_ci		/* mark beacon0 valid */
73348c2ecf20Sopenharmony_ci		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN1VLD);
73358c2ecf20Sopenharmony_ci	}
73368c2ecf20Sopenharmony_ci}
73378c2ecf20Sopenharmony_ci
73388c2ecf20Sopenharmony_ci/*
73398c2ecf20Sopenharmony_ci * Update all beacons for the system.
73408c2ecf20Sopenharmony_ci */
73418c2ecf20Sopenharmony_civoid brcms_c_update_beacon(struct brcms_c_info *wlc)
73428c2ecf20Sopenharmony_ci{
73438c2ecf20Sopenharmony_ci	struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
73448c2ecf20Sopenharmony_ci
73458c2ecf20Sopenharmony_ci	if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
73468c2ecf20Sopenharmony_ci			     bsscfg->type == BRCMS_TYPE_ADHOC)) {
73478c2ecf20Sopenharmony_ci		/* Clear the soft intmask */
73488c2ecf20Sopenharmony_ci		wlc->defmacintmask &= ~MI_BCNTPL;
73498c2ecf20Sopenharmony_ci		if (!wlc->beacon)
73508c2ecf20Sopenharmony_ci			return;
73518c2ecf20Sopenharmony_ci		brcms_c_update_beacon_hw(wlc, wlc->beacon,
73528c2ecf20Sopenharmony_ci					 wlc->beacon_tim_offset,
73538c2ecf20Sopenharmony_ci					 wlc->beacon_dtim_period);
73548c2ecf20Sopenharmony_ci	}
73558c2ecf20Sopenharmony_ci}
73568c2ecf20Sopenharmony_ci
73578c2ecf20Sopenharmony_civoid brcms_c_set_new_beacon(struct brcms_c_info *wlc, struct sk_buff *beacon,
73588c2ecf20Sopenharmony_ci			    u16 tim_offset, u16 dtim_period)
73598c2ecf20Sopenharmony_ci{
73608c2ecf20Sopenharmony_ci	if (!beacon)
73618c2ecf20Sopenharmony_ci		return;
73628c2ecf20Sopenharmony_ci	if (wlc->beacon)
73638c2ecf20Sopenharmony_ci		dev_kfree_skb_any(wlc->beacon);
73648c2ecf20Sopenharmony_ci	wlc->beacon = beacon;
73658c2ecf20Sopenharmony_ci
73668c2ecf20Sopenharmony_ci	/* add PLCP */
73678c2ecf20Sopenharmony_ci	skb_push(wlc->beacon, D11_PHY_HDR_LEN);
73688c2ecf20Sopenharmony_ci	wlc->beacon_tim_offset = tim_offset;
73698c2ecf20Sopenharmony_ci	wlc->beacon_dtim_period = dtim_period;
73708c2ecf20Sopenharmony_ci	brcms_c_update_beacon(wlc);
73718c2ecf20Sopenharmony_ci}
73728c2ecf20Sopenharmony_ci
73738c2ecf20Sopenharmony_civoid brcms_c_set_new_probe_resp(struct brcms_c_info *wlc,
73748c2ecf20Sopenharmony_ci				struct sk_buff *probe_resp)
73758c2ecf20Sopenharmony_ci{
73768c2ecf20Sopenharmony_ci	if (!probe_resp)
73778c2ecf20Sopenharmony_ci		return;
73788c2ecf20Sopenharmony_ci	if (wlc->probe_resp)
73798c2ecf20Sopenharmony_ci		dev_kfree_skb_any(wlc->probe_resp);
73808c2ecf20Sopenharmony_ci	wlc->probe_resp = probe_resp;
73818c2ecf20Sopenharmony_ci
73828c2ecf20Sopenharmony_ci	/* add PLCP */
73838c2ecf20Sopenharmony_ci	skb_push(wlc->probe_resp, D11_PHY_HDR_LEN);
73848c2ecf20Sopenharmony_ci	brcms_c_update_probe_resp(wlc, false);
73858c2ecf20Sopenharmony_ci}
73868c2ecf20Sopenharmony_ci
73878c2ecf20Sopenharmony_civoid brcms_c_enable_probe_resp(struct brcms_c_info *wlc, bool enable)
73888c2ecf20Sopenharmony_ci{
73898c2ecf20Sopenharmony_ci	/*
73908c2ecf20Sopenharmony_ci	 * prevent ucode from sending probe responses by setting the timeout
73918c2ecf20Sopenharmony_ci	 * to 1, it can not send it in that time frame.
73928c2ecf20Sopenharmony_ci	 */
73938c2ecf20Sopenharmony_ci	wlc->prb_resp_timeout = enable ? BRCMS_PRB_RESP_TIMEOUT : 1;
73948c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
73958c2ecf20Sopenharmony_ci	/* TODO: if (enable) => also deactivate receiving of probe request */
73968c2ecf20Sopenharmony_ci}
73978c2ecf20Sopenharmony_ci
73988c2ecf20Sopenharmony_ci/* Write ssid into shared memory */
73998c2ecf20Sopenharmony_cistatic void
74008c2ecf20Sopenharmony_cibrcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
74018c2ecf20Sopenharmony_ci{
74028c2ecf20Sopenharmony_ci	u8 *ssidptr = cfg->SSID;
74038c2ecf20Sopenharmony_ci	u16 base = M_SSID;
74048c2ecf20Sopenharmony_ci	u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
74058c2ecf20Sopenharmony_ci
74068c2ecf20Sopenharmony_ci	/* padding the ssid with zero and copy it into shm */
74078c2ecf20Sopenharmony_ci	memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
74088c2ecf20Sopenharmony_ci	memcpy(ssidbuf, ssidptr, cfg->SSID_len);
74098c2ecf20Sopenharmony_ci
74108c2ecf20Sopenharmony_ci	brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
74118c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
74128c2ecf20Sopenharmony_ci}
74138c2ecf20Sopenharmony_ci
74148c2ecf20Sopenharmony_cistatic void
74158c2ecf20Sopenharmony_cibrcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
74168c2ecf20Sopenharmony_ci			      struct brcms_bss_cfg *cfg,
74178c2ecf20Sopenharmony_ci			      struct sk_buff *probe_resp,
74188c2ecf20Sopenharmony_ci			      bool suspend)
74198c2ecf20Sopenharmony_ci{
74208c2ecf20Sopenharmony_ci	int len;
74218c2ecf20Sopenharmony_ci
74228c2ecf20Sopenharmony_ci	len = min_t(size_t, probe_resp->len, BCN_TMPL_LEN);
74238c2ecf20Sopenharmony_ci
74248c2ecf20Sopenharmony_ci	if (suspend)
74258c2ecf20Sopenharmony_ci		brcms_c_suspend_mac_and_wait(wlc);
74268c2ecf20Sopenharmony_ci
74278c2ecf20Sopenharmony_ci	/* write the probe response into the template region */
74288c2ecf20Sopenharmony_ci	brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
74298c2ecf20Sopenharmony_ci				    (len + 3) & ~3, probe_resp->data);
74308c2ecf20Sopenharmony_ci
74318c2ecf20Sopenharmony_ci	/* write the length of the probe response frame (+PLCP/-FCS) */
74328c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
74338c2ecf20Sopenharmony_ci
74348c2ecf20Sopenharmony_ci	/* write the SSID and SSID length */
74358c2ecf20Sopenharmony_ci	brcms_c_shm_ssid_upd(wlc, cfg);
74368c2ecf20Sopenharmony_ci
74378c2ecf20Sopenharmony_ci	/*
74388c2ecf20Sopenharmony_ci	 * Write PLCP headers and durations for probe response frames
74398c2ecf20Sopenharmony_ci	 * at all rates. Use the actual frame length covered by the
74408c2ecf20Sopenharmony_ci	 * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
74418c2ecf20Sopenharmony_ci	 * by subtracting the PLCP len and adding the FCS.
74428c2ecf20Sopenharmony_ci	 */
74438c2ecf20Sopenharmony_ci	brcms_c_mod_prb_rsp_rate_table(wlc,
74448c2ecf20Sopenharmony_ci				      (u16)len + FCS_LEN - D11_PHY_HDR_LEN);
74458c2ecf20Sopenharmony_ci
74468c2ecf20Sopenharmony_ci	if (suspend)
74478c2ecf20Sopenharmony_ci		brcms_c_enable_mac(wlc);
74488c2ecf20Sopenharmony_ci}
74498c2ecf20Sopenharmony_ci
74508c2ecf20Sopenharmony_civoid brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
74518c2ecf20Sopenharmony_ci{
74528c2ecf20Sopenharmony_ci	struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
74538c2ecf20Sopenharmony_ci
74548c2ecf20Sopenharmony_ci	/* update AP or IBSS probe responses */
74558c2ecf20Sopenharmony_ci	if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
74568c2ecf20Sopenharmony_ci			     bsscfg->type == BRCMS_TYPE_ADHOC)) {
74578c2ecf20Sopenharmony_ci		if (!wlc->probe_resp)
74588c2ecf20Sopenharmony_ci			return;
74598c2ecf20Sopenharmony_ci		brcms_c_bss_update_probe_resp(wlc, bsscfg, wlc->probe_resp,
74608c2ecf20Sopenharmony_ci					      suspend);
74618c2ecf20Sopenharmony_ci	}
74628c2ecf20Sopenharmony_ci}
74638c2ecf20Sopenharmony_ci
74648c2ecf20Sopenharmony_ciint brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
74658c2ecf20Sopenharmony_ci			   uint *blocks)
74668c2ecf20Sopenharmony_ci{
74678c2ecf20Sopenharmony_ci	if (fifo >= NFIFO)
74688c2ecf20Sopenharmony_ci		return -EINVAL;
74698c2ecf20Sopenharmony_ci
74708c2ecf20Sopenharmony_ci	*blocks = wlc_hw->xmtfifo_sz[fifo];
74718c2ecf20Sopenharmony_ci
74728c2ecf20Sopenharmony_ci	return 0;
74738c2ecf20Sopenharmony_ci}
74748c2ecf20Sopenharmony_ci
74758c2ecf20Sopenharmony_civoid
74768c2ecf20Sopenharmony_cibrcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
74778c2ecf20Sopenharmony_ci		  const u8 *addr)
74788c2ecf20Sopenharmony_ci{
74798c2ecf20Sopenharmony_ci	brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
74808c2ecf20Sopenharmony_ci	if (match_reg_offset == RCM_BSSID_OFFSET)
74818c2ecf20Sopenharmony_ci		memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
74828c2ecf20Sopenharmony_ci}
74838c2ecf20Sopenharmony_ci
74848c2ecf20Sopenharmony_ci/*
74858c2ecf20Sopenharmony_ci * Flag 'scan in progress' to withhold dynamic phy calibration
74868c2ecf20Sopenharmony_ci */
74878c2ecf20Sopenharmony_civoid brcms_c_scan_start(struct brcms_c_info *wlc)
74888c2ecf20Sopenharmony_ci{
74898c2ecf20Sopenharmony_ci	wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
74908c2ecf20Sopenharmony_ci}
74918c2ecf20Sopenharmony_ci
74928c2ecf20Sopenharmony_civoid brcms_c_scan_stop(struct brcms_c_info *wlc)
74938c2ecf20Sopenharmony_ci{
74948c2ecf20Sopenharmony_ci	wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
74958c2ecf20Sopenharmony_ci}
74968c2ecf20Sopenharmony_ci
74978c2ecf20Sopenharmony_civoid brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
74988c2ecf20Sopenharmony_ci{
74998c2ecf20Sopenharmony_ci	wlc->pub->associated = state;
75008c2ecf20Sopenharmony_ci}
75018c2ecf20Sopenharmony_ci
75028c2ecf20Sopenharmony_ci/*
75038c2ecf20Sopenharmony_ci * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
75048c2ecf20Sopenharmony_ci * AMPDU traffic, packets pending in hardware have to be invalidated so that
75058c2ecf20Sopenharmony_ci * when later on hardware releases them, they can be handled appropriately.
75068c2ecf20Sopenharmony_ci */
75078c2ecf20Sopenharmony_civoid brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
75088c2ecf20Sopenharmony_ci			       struct ieee80211_sta *sta,
75098c2ecf20Sopenharmony_ci			       void (*dma_callback_fn))
75108c2ecf20Sopenharmony_ci{
75118c2ecf20Sopenharmony_ci	struct dma_pub *dmah;
75128c2ecf20Sopenharmony_ci	int i;
75138c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++) {
75148c2ecf20Sopenharmony_ci		dmah = hw->di[i];
75158c2ecf20Sopenharmony_ci		if (dmah != NULL)
75168c2ecf20Sopenharmony_ci			dma_walk_packets(dmah, dma_callback_fn, sta);
75178c2ecf20Sopenharmony_ci	}
75188c2ecf20Sopenharmony_ci}
75198c2ecf20Sopenharmony_ci
75208c2ecf20Sopenharmony_ciint brcms_c_get_curband(struct brcms_c_info *wlc)
75218c2ecf20Sopenharmony_ci{
75228c2ecf20Sopenharmony_ci	return wlc->band->bandunit;
75238c2ecf20Sopenharmony_ci}
75248c2ecf20Sopenharmony_ci
75258c2ecf20Sopenharmony_cibool brcms_c_tx_flush_completed(struct brcms_c_info *wlc)
75268c2ecf20Sopenharmony_ci{
75278c2ecf20Sopenharmony_ci	int i;
75288c2ecf20Sopenharmony_ci
75298c2ecf20Sopenharmony_ci	/* Kick DMA to send any pending AMPDU */
75308c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
75318c2ecf20Sopenharmony_ci		if (wlc->hw->di[i])
75328c2ecf20Sopenharmony_ci			dma_kick_tx(wlc->hw->di[i]);
75338c2ecf20Sopenharmony_ci
75348c2ecf20Sopenharmony_ci	return !brcms_txpktpendtot(wlc);
75358c2ecf20Sopenharmony_ci}
75368c2ecf20Sopenharmony_ci
75378c2ecf20Sopenharmony_civoid brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
75388c2ecf20Sopenharmony_ci{
75398c2ecf20Sopenharmony_ci	wlc->bcn_li_bcn = interval;
75408c2ecf20Sopenharmony_ci	if (wlc->pub->up)
75418c2ecf20Sopenharmony_ci		brcms_c_bcn_li_upd(wlc);
75428c2ecf20Sopenharmony_ci}
75438c2ecf20Sopenharmony_ci
75448c2ecf20Sopenharmony_ciu64 brcms_c_tsf_get(struct brcms_c_info *wlc)
75458c2ecf20Sopenharmony_ci{
75468c2ecf20Sopenharmony_ci	u32 tsf_h, tsf_l;
75478c2ecf20Sopenharmony_ci	u64 tsf;
75488c2ecf20Sopenharmony_ci
75498c2ecf20Sopenharmony_ci	brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
75508c2ecf20Sopenharmony_ci
75518c2ecf20Sopenharmony_ci	tsf = tsf_h;
75528c2ecf20Sopenharmony_ci	tsf <<= 32;
75538c2ecf20Sopenharmony_ci	tsf |= tsf_l;
75548c2ecf20Sopenharmony_ci
75558c2ecf20Sopenharmony_ci	return tsf;
75568c2ecf20Sopenharmony_ci}
75578c2ecf20Sopenharmony_ci
75588c2ecf20Sopenharmony_civoid brcms_c_tsf_set(struct brcms_c_info *wlc, u64 tsf)
75598c2ecf20Sopenharmony_ci{
75608c2ecf20Sopenharmony_ci	u32 tsf_h, tsf_l;
75618c2ecf20Sopenharmony_ci
75628c2ecf20Sopenharmony_ci	brcms_c_time_lock(wlc);
75638c2ecf20Sopenharmony_ci
75648c2ecf20Sopenharmony_ci	tsf_l = tsf;
75658c2ecf20Sopenharmony_ci	tsf_h = (tsf >> 32);
75668c2ecf20Sopenharmony_ci
75678c2ecf20Sopenharmony_ci	/* read the tsf timer low, then high to get an atomic read */
75688c2ecf20Sopenharmony_ci	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerlow), tsf_l);
75698c2ecf20Sopenharmony_ci	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerhigh), tsf_h);
75708c2ecf20Sopenharmony_ci
75718c2ecf20Sopenharmony_ci	brcms_c_time_unlock(wlc);
75728c2ecf20Sopenharmony_ci}
75738c2ecf20Sopenharmony_ci
75748c2ecf20Sopenharmony_ciint brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
75758c2ecf20Sopenharmony_ci{
75768c2ecf20Sopenharmony_ci	uint qdbm;
75778c2ecf20Sopenharmony_ci
75788c2ecf20Sopenharmony_ci	/* Remove override bit and clip to max qdbm value */
75798c2ecf20Sopenharmony_ci	qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
75808c2ecf20Sopenharmony_ci	return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
75818c2ecf20Sopenharmony_ci}
75828c2ecf20Sopenharmony_ci
75838c2ecf20Sopenharmony_ciint brcms_c_get_tx_power(struct brcms_c_info *wlc)
75848c2ecf20Sopenharmony_ci{
75858c2ecf20Sopenharmony_ci	uint qdbm;
75868c2ecf20Sopenharmony_ci	bool override;
75878c2ecf20Sopenharmony_ci
75888c2ecf20Sopenharmony_ci	wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
75898c2ecf20Sopenharmony_ci
75908c2ecf20Sopenharmony_ci	/* Return qdbm units */
75918c2ecf20Sopenharmony_ci	return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
75928c2ecf20Sopenharmony_ci}
75938c2ecf20Sopenharmony_ci
75948c2ecf20Sopenharmony_ci/* Process received frames */
75958c2ecf20Sopenharmony_ci/*
75968c2ecf20Sopenharmony_ci * Return true if more frames need to be processed. false otherwise.
75978c2ecf20Sopenharmony_ci * Param 'bound' indicates max. # frames to process before break out.
75988c2ecf20Sopenharmony_ci */
75998c2ecf20Sopenharmony_cistatic void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
76008c2ecf20Sopenharmony_ci{
76018c2ecf20Sopenharmony_ci	struct d11rxhdr *rxh;
76028c2ecf20Sopenharmony_ci	struct ieee80211_hdr *h;
76038c2ecf20Sopenharmony_ci	uint len;
76048c2ecf20Sopenharmony_ci	bool is_amsdu;
76058c2ecf20Sopenharmony_ci
76068c2ecf20Sopenharmony_ci	/* frame starts with rxhdr */
76078c2ecf20Sopenharmony_ci	rxh = (struct d11rxhdr *) (p->data);
76088c2ecf20Sopenharmony_ci
76098c2ecf20Sopenharmony_ci	/* strip off rxhdr */
76108c2ecf20Sopenharmony_ci	skb_pull(p, BRCMS_HWRXOFF);
76118c2ecf20Sopenharmony_ci
76128c2ecf20Sopenharmony_ci	/* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
76138c2ecf20Sopenharmony_ci	if (rxh->RxStatus1 & RXS_PBPRES) {
76148c2ecf20Sopenharmony_ci		if (p->len < 2) {
76158c2ecf20Sopenharmony_ci			brcms_err(wlc->hw->d11core,
76168c2ecf20Sopenharmony_ci				  "wl%d: recv: rcvd runt of len %d\n",
76178c2ecf20Sopenharmony_ci				  wlc->pub->unit, p->len);
76188c2ecf20Sopenharmony_ci			goto toss;
76198c2ecf20Sopenharmony_ci		}
76208c2ecf20Sopenharmony_ci		skb_pull(p, 2);
76218c2ecf20Sopenharmony_ci	}
76228c2ecf20Sopenharmony_ci
76238c2ecf20Sopenharmony_ci	h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
76248c2ecf20Sopenharmony_ci	len = p->len;
76258c2ecf20Sopenharmony_ci
76268c2ecf20Sopenharmony_ci	if (rxh->RxStatus1 & RXS_FCSERR) {
76278c2ecf20Sopenharmony_ci		if (!(wlc->filter_flags & FIF_FCSFAIL))
76288c2ecf20Sopenharmony_ci			goto toss;
76298c2ecf20Sopenharmony_ci	}
76308c2ecf20Sopenharmony_ci
76318c2ecf20Sopenharmony_ci	/* check received pkt has at least frame control field */
76328c2ecf20Sopenharmony_ci	if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
76338c2ecf20Sopenharmony_ci		goto toss;
76348c2ecf20Sopenharmony_ci
76358c2ecf20Sopenharmony_ci	/* not supporting A-MSDU */
76368c2ecf20Sopenharmony_ci	is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
76378c2ecf20Sopenharmony_ci	if (is_amsdu)
76388c2ecf20Sopenharmony_ci		goto toss;
76398c2ecf20Sopenharmony_ci
76408c2ecf20Sopenharmony_ci	brcms_c_recvctl(wlc, rxh, p);
76418c2ecf20Sopenharmony_ci	return;
76428c2ecf20Sopenharmony_ci
76438c2ecf20Sopenharmony_ci toss:
76448c2ecf20Sopenharmony_ci	brcmu_pkt_buf_free_skb(p);
76458c2ecf20Sopenharmony_ci}
76468c2ecf20Sopenharmony_ci
76478c2ecf20Sopenharmony_ci/* Process received frames */
76488c2ecf20Sopenharmony_ci/*
76498c2ecf20Sopenharmony_ci * Return true if more frames need to be processed. false otherwise.
76508c2ecf20Sopenharmony_ci * Param 'bound' indicates max. # frames to process before break out.
76518c2ecf20Sopenharmony_ci */
76528c2ecf20Sopenharmony_cistatic bool
76538c2ecf20Sopenharmony_cibrcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
76548c2ecf20Sopenharmony_ci{
76558c2ecf20Sopenharmony_ci	struct sk_buff *p;
76568c2ecf20Sopenharmony_ci	struct sk_buff *next = NULL;
76578c2ecf20Sopenharmony_ci	struct sk_buff_head recv_frames;
76588c2ecf20Sopenharmony_ci
76598c2ecf20Sopenharmony_ci	uint n = 0;
76608c2ecf20Sopenharmony_ci	uint bound_limit = bound ? RXBND : -1;
76618c2ecf20Sopenharmony_ci	bool morepending = false;
76628c2ecf20Sopenharmony_ci
76638c2ecf20Sopenharmony_ci	skb_queue_head_init(&recv_frames);
76648c2ecf20Sopenharmony_ci
76658c2ecf20Sopenharmony_ci	/* gather received frames */
76668c2ecf20Sopenharmony_ci	do {
76678c2ecf20Sopenharmony_ci		/* !give others some time to run! */
76688c2ecf20Sopenharmony_ci		if (n >= bound_limit)
76698c2ecf20Sopenharmony_ci			break;
76708c2ecf20Sopenharmony_ci
76718c2ecf20Sopenharmony_ci		morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
76728c2ecf20Sopenharmony_ci		n++;
76738c2ecf20Sopenharmony_ci	} while (morepending);
76748c2ecf20Sopenharmony_ci
76758c2ecf20Sopenharmony_ci	/* post more rbufs */
76768c2ecf20Sopenharmony_ci	dma_rxfill(wlc_hw->di[fifo]);
76778c2ecf20Sopenharmony_ci
76788c2ecf20Sopenharmony_ci	/* process each frame */
76798c2ecf20Sopenharmony_ci	skb_queue_walk_safe(&recv_frames, p, next) {
76808c2ecf20Sopenharmony_ci		struct d11rxhdr_le *rxh_le;
76818c2ecf20Sopenharmony_ci		struct d11rxhdr *rxh;
76828c2ecf20Sopenharmony_ci
76838c2ecf20Sopenharmony_ci		skb_unlink(p, &recv_frames);
76848c2ecf20Sopenharmony_ci		rxh_le = (struct d11rxhdr_le *)p->data;
76858c2ecf20Sopenharmony_ci		rxh = (struct d11rxhdr *)p->data;
76868c2ecf20Sopenharmony_ci
76878c2ecf20Sopenharmony_ci		/* fixup rx header endianness */
76888c2ecf20Sopenharmony_ci		rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
76898c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
76908c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
76918c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
76928c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
76938c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
76948c2ecf20Sopenharmony_ci		rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
76958c2ecf20Sopenharmony_ci		rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
76968c2ecf20Sopenharmony_ci		rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
76978c2ecf20Sopenharmony_ci		rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
76988c2ecf20Sopenharmony_ci		rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
76998c2ecf20Sopenharmony_ci
77008c2ecf20Sopenharmony_ci		brcms_c_recv(wlc_hw->wlc, p);
77018c2ecf20Sopenharmony_ci	}
77028c2ecf20Sopenharmony_ci
77038c2ecf20Sopenharmony_ci	return morepending;
77048c2ecf20Sopenharmony_ci}
77058c2ecf20Sopenharmony_ci
77068c2ecf20Sopenharmony_ci/* second-level interrupt processing
77078c2ecf20Sopenharmony_ci *   Return true if another dpc needs to be re-scheduled. false otherwise.
77088c2ecf20Sopenharmony_ci *   Param 'bounded' indicates if applicable loops should be bounded.
77098c2ecf20Sopenharmony_ci */
77108c2ecf20Sopenharmony_cibool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
77118c2ecf20Sopenharmony_ci{
77128c2ecf20Sopenharmony_ci	u32 macintstatus;
77138c2ecf20Sopenharmony_ci	struct brcms_hardware *wlc_hw = wlc->hw;
77148c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc_hw->d11core;
77158c2ecf20Sopenharmony_ci
77168c2ecf20Sopenharmony_ci	if (brcms_deviceremoved(wlc)) {
77178c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
77188c2ecf20Sopenharmony_ci			  __func__);
77198c2ecf20Sopenharmony_ci		brcms_down(wlc->wl);
77208c2ecf20Sopenharmony_ci		return false;
77218c2ecf20Sopenharmony_ci	}
77228c2ecf20Sopenharmony_ci
77238c2ecf20Sopenharmony_ci	/* grab and clear the saved software intstatus bits */
77248c2ecf20Sopenharmony_ci	macintstatus = wlc->macintstatus;
77258c2ecf20Sopenharmony_ci	wlc->macintstatus = 0;
77268c2ecf20Sopenharmony_ci
77278c2ecf20Sopenharmony_ci	brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
77288c2ecf20Sopenharmony_ci		      wlc_hw->unit, macintstatus);
77298c2ecf20Sopenharmony_ci
77308c2ecf20Sopenharmony_ci	WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
77318c2ecf20Sopenharmony_ci
77328c2ecf20Sopenharmony_ci	/* tx status */
77338c2ecf20Sopenharmony_ci	if (macintstatus & MI_TFS) {
77348c2ecf20Sopenharmony_ci		bool fatal;
77358c2ecf20Sopenharmony_ci		if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
77368c2ecf20Sopenharmony_ci			wlc->macintstatus |= MI_TFS;
77378c2ecf20Sopenharmony_ci		if (fatal) {
77388c2ecf20Sopenharmony_ci			brcms_err(core, "MI_TFS: fatal\n");
77398c2ecf20Sopenharmony_ci			goto fatal;
77408c2ecf20Sopenharmony_ci		}
77418c2ecf20Sopenharmony_ci	}
77428c2ecf20Sopenharmony_ci
77438c2ecf20Sopenharmony_ci	if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
77448c2ecf20Sopenharmony_ci		brcms_c_tbtt(wlc);
77458c2ecf20Sopenharmony_ci
77468c2ecf20Sopenharmony_ci	/* ATIM window end */
77478c2ecf20Sopenharmony_ci	if (macintstatus & MI_ATIMWINEND) {
77488c2ecf20Sopenharmony_ci		brcms_dbg_info(core, "end of ATIM window\n");
77498c2ecf20Sopenharmony_ci		bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
77508c2ecf20Sopenharmony_ci		wlc->qvalid = 0;
77518c2ecf20Sopenharmony_ci	}
77528c2ecf20Sopenharmony_ci
77538c2ecf20Sopenharmony_ci	/*
77548c2ecf20Sopenharmony_ci	 * received data or control frame, MI_DMAINT is
77558c2ecf20Sopenharmony_ci	 * indication of RX_FIFO interrupt
77568c2ecf20Sopenharmony_ci	 */
77578c2ecf20Sopenharmony_ci	if (macintstatus & MI_DMAINT)
77588c2ecf20Sopenharmony_ci		if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
77598c2ecf20Sopenharmony_ci			wlc->macintstatus |= MI_DMAINT;
77608c2ecf20Sopenharmony_ci
77618c2ecf20Sopenharmony_ci	/* noise sample collected */
77628c2ecf20Sopenharmony_ci	if (macintstatus & MI_BG_NOISE)
77638c2ecf20Sopenharmony_ci		wlc_phy_noise_sample_intr(wlc_hw->band->pi);
77648c2ecf20Sopenharmony_ci
77658c2ecf20Sopenharmony_ci	if (macintstatus & MI_GP0) {
77668c2ecf20Sopenharmony_ci		brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
77678c2ecf20Sopenharmony_ci			  "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
77688c2ecf20Sopenharmony_ci
77698c2ecf20Sopenharmony_ci		printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
77708c2ecf20Sopenharmony_ci			    __func__, ai_get_chip_id(wlc_hw->sih),
77718c2ecf20Sopenharmony_ci			    ai_get_chiprev(wlc_hw->sih));
77728c2ecf20Sopenharmony_ci		brcms_fatal_error(wlc_hw->wlc->wl);
77738c2ecf20Sopenharmony_ci	}
77748c2ecf20Sopenharmony_ci
77758c2ecf20Sopenharmony_ci	/* gptimer timeout */
77768c2ecf20Sopenharmony_ci	if (macintstatus & MI_TO)
77778c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(gptimer), 0);
77788c2ecf20Sopenharmony_ci
77798c2ecf20Sopenharmony_ci	if (macintstatus & MI_RFDISABLE) {
77808c2ecf20Sopenharmony_ci		brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
77818c2ecf20Sopenharmony_ci			       " RF Disable Input\n", wlc_hw->unit);
77828c2ecf20Sopenharmony_ci		brcms_rfkill_set_hw_state(wlc->wl);
77838c2ecf20Sopenharmony_ci	}
77848c2ecf20Sopenharmony_ci
77858c2ecf20Sopenharmony_ci	/* BCN template is available */
77868c2ecf20Sopenharmony_ci	if (macintstatus & MI_BCNTPL)
77878c2ecf20Sopenharmony_ci		brcms_c_update_beacon(wlc);
77888c2ecf20Sopenharmony_ci
77898c2ecf20Sopenharmony_ci	/* it isn't done and needs to be resched if macintstatus is non-zero */
77908c2ecf20Sopenharmony_ci	return wlc->macintstatus != 0;
77918c2ecf20Sopenharmony_ci
77928c2ecf20Sopenharmony_ci fatal:
77938c2ecf20Sopenharmony_ci	brcms_fatal_error(wlc_hw->wlc->wl);
77948c2ecf20Sopenharmony_ci	return wlc->macintstatus != 0;
77958c2ecf20Sopenharmony_ci}
77968c2ecf20Sopenharmony_ci
77978c2ecf20Sopenharmony_civoid brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
77988c2ecf20Sopenharmony_ci{
77998c2ecf20Sopenharmony_ci	struct bcma_device *core = wlc->hw->d11core;
78008c2ecf20Sopenharmony_ci	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.chandef.chan;
78018c2ecf20Sopenharmony_ci	u16 chanspec;
78028c2ecf20Sopenharmony_ci
78038c2ecf20Sopenharmony_ci	brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
78048c2ecf20Sopenharmony_ci
78058c2ecf20Sopenharmony_ci	chanspec = ch20mhz_chspec(ch->hw_value);
78068c2ecf20Sopenharmony_ci
78078c2ecf20Sopenharmony_ci	brcms_b_init(wlc->hw, chanspec);
78088c2ecf20Sopenharmony_ci
78098c2ecf20Sopenharmony_ci	/* update beacon listen interval */
78108c2ecf20Sopenharmony_ci	brcms_c_bcn_li_upd(wlc);
78118c2ecf20Sopenharmony_ci
78128c2ecf20Sopenharmony_ci	/* write ethernet address to core */
78138c2ecf20Sopenharmony_ci	brcms_c_set_mac(wlc->bsscfg);
78148c2ecf20Sopenharmony_ci	brcms_c_set_bssid(wlc->bsscfg);
78158c2ecf20Sopenharmony_ci
78168c2ecf20Sopenharmony_ci	/* Update tsf_cfprep if associated and up */
78178c2ecf20Sopenharmony_ci	if (wlc->pub->associated && wlc->pub->up) {
78188c2ecf20Sopenharmony_ci		u32 bi;
78198c2ecf20Sopenharmony_ci
78208c2ecf20Sopenharmony_ci		/* get beacon period and convert to uS */
78218c2ecf20Sopenharmony_ci		bi = wlc->bsscfg->current_bss->beacon_period << 10;
78228c2ecf20Sopenharmony_ci		/*
78238c2ecf20Sopenharmony_ci		 * update since init path would reset
78248c2ecf20Sopenharmony_ci		 * to default value
78258c2ecf20Sopenharmony_ci		 */
78268c2ecf20Sopenharmony_ci		bcma_write32(core, D11REGOFFS(tsf_cfprep),
78278c2ecf20Sopenharmony_ci			     bi << CFPREP_CBI_SHIFT);
78288c2ecf20Sopenharmony_ci
78298c2ecf20Sopenharmony_ci		/* Update maccontrol PM related bits */
78308c2ecf20Sopenharmony_ci		brcms_c_set_ps_ctrl(wlc);
78318c2ecf20Sopenharmony_ci	}
78328c2ecf20Sopenharmony_ci
78338c2ecf20Sopenharmony_ci	brcms_c_bandinit_ordered(wlc, chanspec);
78348c2ecf20Sopenharmony_ci
78358c2ecf20Sopenharmony_ci	/* init probe response timeout */
78368c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
78378c2ecf20Sopenharmony_ci
78388c2ecf20Sopenharmony_ci	/* init max burst txop (framebursting) */
78398c2ecf20Sopenharmony_ci	brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
78408c2ecf20Sopenharmony_ci		      (wlc->
78418c2ecf20Sopenharmony_ci		       _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
78428c2ecf20Sopenharmony_ci
78438c2ecf20Sopenharmony_ci	/* initialize maximum allowed duty cycle */
78448c2ecf20Sopenharmony_ci	brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
78458c2ecf20Sopenharmony_ci	brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
78468c2ecf20Sopenharmony_ci
78478c2ecf20Sopenharmony_ci	/*
78488c2ecf20Sopenharmony_ci	 * Update some shared memory locations related to
78498c2ecf20Sopenharmony_ci	 * max AMPDU size allowed to received
78508c2ecf20Sopenharmony_ci	 */
78518c2ecf20Sopenharmony_ci	brcms_c_ampdu_shm_upd(wlc->ampdu);
78528c2ecf20Sopenharmony_ci
78538c2ecf20Sopenharmony_ci	/* band-specific inits */
78548c2ecf20Sopenharmony_ci	brcms_c_bsinit(wlc);
78558c2ecf20Sopenharmony_ci
78568c2ecf20Sopenharmony_ci	/* Enable EDCF mode (while the MAC is suspended) */
78578c2ecf20Sopenharmony_ci	bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
78588c2ecf20Sopenharmony_ci	brcms_c_edcf_setparams(wlc, false);
78598c2ecf20Sopenharmony_ci
78608c2ecf20Sopenharmony_ci	/* read the ucode version if we have not yet done so */
78618c2ecf20Sopenharmony_ci	if (wlc->ucode_rev == 0) {
78628c2ecf20Sopenharmony_ci		u16 rev;
78638c2ecf20Sopenharmony_ci		u16 patch;
78648c2ecf20Sopenharmony_ci
78658c2ecf20Sopenharmony_ci		rev = brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR);
78668c2ecf20Sopenharmony_ci		patch = brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
78678c2ecf20Sopenharmony_ci		wlc->ucode_rev = (rev << NBITS(u16)) | patch;
78688c2ecf20Sopenharmony_ci		snprintf(wlc->wiphy->fw_version,
78698c2ecf20Sopenharmony_ci			 sizeof(wlc->wiphy->fw_version), "%u.%u", rev, patch);
78708c2ecf20Sopenharmony_ci	}
78718c2ecf20Sopenharmony_ci
78728c2ecf20Sopenharmony_ci	/* ..now really unleash hell (allow the MAC out of suspend) */
78738c2ecf20Sopenharmony_ci	brcms_c_enable_mac(wlc);
78748c2ecf20Sopenharmony_ci
78758c2ecf20Sopenharmony_ci	/* suspend the tx fifos and mute the phy for preism cac time */
78768c2ecf20Sopenharmony_ci	if (mute_tx)
78778c2ecf20Sopenharmony_ci		brcms_b_mute(wlc->hw, true);
78788c2ecf20Sopenharmony_ci
78798c2ecf20Sopenharmony_ci	/* enable the RF Disable Delay timer */
78808c2ecf20Sopenharmony_ci	bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
78818c2ecf20Sopenharmony_ci
78828c2ecf20Sopenharmony_ci	/*
78838c2ecf20Sopenharmony_ci	 * Initialize WME parameters; if they haven't been set by some other
78848c2ecf20Sopenharmony_ci	 * mechanism (IOVar, etc) then read them from the hardware.
78858c2ecf20Sopenharmony_ci	 */
78868c2ecf20Sopenharmony_ci	if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
78878c2ecf20Sopenharmony_ci		/* Uninitialized; read from HW */
78888c2ecf20Sopenharmony_ci		int ac;
78898c2ecf20Sopenharmony_ci
78908c2ecf20Sopenharmony_ci		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
78918c2ecf20Sopenharmony_ci			wlc->wme_retries[ac] =
78928c2ecf20Sopenharmony_ci			    brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
78938c2ecf20Sopenharmony_ci	}
78948c2ecf20Sopenharmony_ci}
78958c2ecf20Sopenharmony_ci
78968c2ecf20Sopenharmony_ci/*
78978c2ecf20Sopenharmony_ci * The common driver entry routine. Error codes should be unique
78988c2ecf20Sopenharmony_ci */
78998c2ecf20Sopenharmony_cistruct brcms_c_info *
79008c2ecf20Sopenharmony_cibrcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
79018c2ecf20Sopenharmony_ci	       bool piomode, uint *perr)
79028c2ecf20Sopenharmony_ci{
79038c2ecf20Sopenharmony_ci	struct brcms_c_info *wlc;
79048c2ecf20Sopenharmony_ci	uint err = 0;
79058c2ecf20Sopenharmony_ci	uint i, j;
79068c2ecf20Sopenharmony_ci	struct brcms_pub *pub;
79078c2ecf20Sopenharmony_ci
79088c2ecf20Sopenharmony_ci	/* allocate struct brcms_c_info state and its substructures */
79098c2ecf20Sopenharmony_ci	wlc = brcms_c_attach_malloc(unit, &err, 0);
79108c2ecf20Sopenharmony_ci	if (wlc == NULL)
79118c2ecf20Sopenharmony_ci		goto fail;
79128c2ecf20Sopenharmony_ci	wlc->wiphy = wl->wiphy;
79138c2ecf20Sopenharmony_ci	pub = wlc->pub;
79148c2ecf20Sopenharmony_ci
79158c2ecf20Sopenharmony_ci#if defined(DEBUG)
79168c2ecf20Sopenharmony_ci	wlc_info_dbg = wlc;
79178c2ecf20Sopenharmony_ci#endif
79188c2ecf20Sopenharmony_ci
79198c2ecf20Sopenharmony_ci	wlc->band = wlc->bandstate[0];
79208c2ecf20Sopenharmony_ci	wlc->core = wlc->corestate;
79218c2ecf20Sopenharmony_ci	wlc->wl = wl;
79228c2ecf20Sopenharmony_ci	pub->unit = unit;
79238c2ecf20Sopenharmony_ci	pub->_piomode = piomode;
79248c2ecf20Sopenharmony_ci	wlc->bandinit_pending = false;
79258c2ecf20Sopenharmony_ci	wlc->beacon_template_virgin = true;
79268c2ecf20Sopenharmony_ci
79278c2ecf20Sopenharmony_ci	/* populate struct brcms_c_info with default values  */
79288c2ecf20Sopenharmony_ci	brcms_c_info_init(wlc, unit);
79298c2ecf20Sopenharmony_ci
79308c2ecf20Sopenharmony_ci	/* update sta/ap related parameters */
79318c2ecf20Sopenharmony_ci	brcms_c_ap_upd(wlc);
79328c2ecf20Sopenharmony_ci
79338c2ecf20Sopenharmony_ci	/*
79348c2ecf20Sopenharmony_ci	 * low level attach steps(all hw accesses go
79358c2ecf20Sopenharmony_ci	 * inside, no more in rest of the attach)
79368c2ecf20Sopenharmony_ci	 */
79378c2ecf20Sopenharmony_ci	err = brcms_b_attach(wlc, core, unit, piomode);
79388c2ecf20Sopenharmony_ci	if (err)
79398c2ecf20Sopenharmony_ci		goto fail;
79408c2ecf20Sopenharmony_ci
79418c2ecf20Sopenharmony_ci	brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
79428c2ecf20Sopenharmony_ci
79438c2ecf20Sopenharmony_ci	pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
79448c2ecf20Sopenharmony_ci
79458c2ecf20Sopenharmony_ci	/* disable allowed duty cycle */
79468c2ecf20Sopenharmony_ci	wlc->tx_duty_cycle_ofdm = 0;
79478c2ecf20Sopenharmony_ci	wlc->tx_duty_cycle_cck = 0;
79488c2ecf20Sopenharmony_ci
79498c2ecf20Sopenharmony_ci	brcms_c_stf_phy_chain_calc(wlc);
79508c2ecf20Sopenharmony_ci
79518c2ecf20Sopenharmony_ci	/* txchain 1: txant 0, txchain 2: txant 1 */
79528c2ecf20Sopenharmony_ci	if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
79538c2ecf20Sopenharmony_ci		wlc->stf->txant = wlc->stf->hw_txchain - 1;
79548c2ecf20Sopenharmony_ci
79558c2ecf20Sopenharmony_ci	/* push to BMAC driver */
79568c2ecf20Sopenharmony_ci	wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
79578c2ecf20Sopenharmony_ci			       wlc->stf->hw_rxchain);
79588c2ecf20Sopenharmony_ci
79598c2ecf20Sopenharmony_ci	/* pull up some info resulting from the low attach */
79608c2ecf20Sopenharmony_ci	for (i = 0; i < NFIFO; i++)
79618c2ecf20Sopenharmony_ci		wlc->core->txavail[i] = wlc->hw->txavail[i];
79628c2ecf20Sopenharmony_ci
79638c2ecf20Sopenharmony_ci	memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
79648c2ecf20Sopenharmony_ci	memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
79658c2ecf20Sopenharmony_ci
79668c2ecf20Sopenharmony_ci	for (j = 0; j < wlc->pub->_nbands; j++) {
79678c2ecf20Sopenharmony_ci		wlc->band = wlc->bandstate[j];
79688c2ecf20Sopenharmony_ci
79698c2ecf20Sopenharmony_ci		if (!brcms_c_attach_stf_ant_init(wlc)) {
79708c2ecf20Sopenharmony_ci			err = 24;
79718c2ecf20Sopenharmony_ci			goto fail;
79728c2ecf20Sopenharmony_ci		}
79738c2ecf20Sopenharmony_ci
79748c2ecf20Sopenharmony_ci		/* default contention windows size limits */
79758c2ecf20Sopenharmony_ci		wlc->band->CWmin = APHY_CWMIN;
79768c2ecf20Sopenharmony_ci		wlc->band->CWmax = PHY_CWMAX;
79778c2ecf20Sopenharmony_ci
79788c2ecf20Sopenharmony_ci		/* init gmode value */
79798c2ecf20Sopenharmony_ci		if (wlc->band->bandtype == BRCM_BAND_2G) {
79808c2ecf20Sopenharmony_ci			wlc->band->gmode = GMODE_AUTO;
79818c2ecf20Sopenharmony_ci			brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
79828c2ecf20Sopenharmony_ci					   wlc->band->gmode);
79838c2ecf20Sopenharmony_ci		}
79848c2ecf20Sopenharmony_ci
79858c2ecf20Sopenharmony_ci		/* init _n_enab supported mode */
79868c2ecf20Sopenharmony_ci		if (BRCMS_PHY_11N_CAP(wlc->band)) {
79878c2ecf20Sopenharmony_ci			pub->_n_enab = SUPPORT_11N;
79888c2ecf20Sopenharmony_ci			brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
79898c2ecf20Sopenharmony_ci						   ((pub->_n_enab ==
79908c2ecf20Sopenharmony_ci						     SUPPORT_11N) ? WL_11N_2x2 :
79918c2ecf20Sopenharmony_ci						    WL_11N_3x3));
79928c2ecf20Sopenharmony_ci		}
79938c2ecf20Sopenharmony_ci
79948c2ecf20Sopenharmony_ci		/* init per-band default rateset, depend on band->gmode */
79958c2ecf20Sopenharmony_ci		brcms_default_rateset(wlc, &wlc->band->defrateset);
79968c2ecf20Sopenharmony_ci
79978c2ecf20Sopenharmony_ci		/* fill in hw_rateset */
79988c2ecf20Sopenharmony_ci		brcms_c_rateset_filter(&wlc->band->defrateset,
79998c2ecf20Sopenharmony_ci				   &wlc->band->hw_rateset, false,
80008c2ecf20Sopenharmony_ci				   BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
80018c2ecf20Sopenharmony_ci				   (bool) (wlc->pub->_n_enab & SUPPORT_11N));
80028c2ecf20Sopenharmony_ci	}
80038c2ecf20Sopenharmony_ci
80048c2ecf20Sopenharmony_ci	/*
80058c2ecf20Sopenharmony_ci	 * update antenna config due to
80068c2ecf20Sopenharmony_ci	 * wlc->stf->txant/txchain/ant_rx_ovr change
80078c2ecf20Sopenharmony_ci	 */
80088c2ecf20Sopenharmony_ci	brcms_c_stf_phy_txant_upd(wlc);
80098c2ecf20Sopenharmony_ci
80108c2ecf20Sopenharmony_ci	/* attach each modules */
80118c2ecf20Sopenharmony_ci	err = brcms_c_attach_module(wlc);
80128c2ecf20Sopenharmony_ci	if (err != 0)
80138c2ecf20Sopenharmony_ci		goto fail;
80148c2ecf20Sopenharmony_ci
80158c2ecf20Sopenharmony_ci	if (!brcms_c_timers_init(wlc, unit)) {
80168c2ecf20Sopenharmony_ci		wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
80178c2ecf20Sopenharmony_ci			  __func__);
80188c2ecf20Sopenharmony_ci		err = 32;
80198c2ecf20Sopenharmony_ci		goto fail;
80208c2ecf20Sopenharmony_ci	}
80218c2ecf20Sopenharmony_ci
80228c2ecf20Sopenharmony_ci	/* depend on rateset, gmode */
80238c2ecf20Sopenharmony_ci	wlc->cmi = brcms_c_channel_mgr_attach(wlc);
80248c2ecf20Sopenharmony_ci	if (!wlc->cmi) {
80258c2ecf20Sopenharmony_ci		wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
80268c2ecf20Sopenharmony_ci			  "\n", unit, __func__);
80278c2ecf20Sopenharmony_ci		err = 33;
80288c2ecf20Sopenharmony_ci		goto fail;
80298c2ecf20Sopenharmony_ci	}
80308c2ecf20Sopenharmony_ci
80318c2ecf20Sopenharmony_ci	/* init default when all parameters are ready, i.e. ->rateset */
80328c2ecf20Sopenharmony_ci	brcms_c_bss_default_init(wlc);
80338c2ecf20Sopenharmony_ci
80348c2ecf20Sopenharmony_ci	/*
80358c2ecf20Sopenharmony_ci	 * Complete the wlc default state initializations..
80368c2ecf20Sopenharmony_ci	 */
80378c2ecf20Sopenharmony_ci
80388c2ecf20Sopenharmony_ci	wlc->bsscfg->wlc = wlc;
80398c2ecf20Sopenharmony_ci
80408c2ecf20Sopenharmony_ci	wlc->mimoft = FT_HT;
80418c2ecf20Sopenharmony_ci	wlc->mimo_40txbw = AUTO;
80428c2ecf20Sopenharmony_ci	wlc->ofdm_40txbw = AUTO;
80438c2ecf20Sopenharmony_ci	wlc->cck_40txbw = AUTO;
80448c2ecf20Sopenharmony_ci	brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
80458c2ecf20Sopenharmony_ci
80468c2ecf20Sopenharmony_ci	/* Set default values of SGI */
80478c2ecf20Sopenharmony_ci	if (BRCMS_SGI_CAP_PHY(wlc)) {
80488c2ecf20Sopenharmony_ci		brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
80498c2ecf20Sopenharmony_ci					       BRCMS_N_SGI_40));
80508c2ecf20Sopenharmony_ci	} else if (BRCMS_ISSSLPNPHY(wlc->band)) {
80518c2ecf20Sopenharmony_ci		brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
80528c2ecf20Sopenharmony_ci					       BRCMS_N_SGI_40));
80538c2ecf20Sopenharmony_ci	} else {
80548c2ecf20Sopenharmony_ci		brcms_c_ht_update_sgi_rx(wlc, 0);
80558c2ecf20Sopenharmony_ci	}
80568c2ecf20Sopenharmony_ci
80578c2ecf20Sopenharmony_ci	brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
80588c2ecf20Sopenharmony_ci
80598c2ecf20Sopenharmony_ci	if (perr)
80608c2ecf20Sopenharmony_ci		*perr = 0;
80618c2ecf20Sopenharmony_ci
80628c2ecf20Sopenharmony_ci	return wlc;
80638c2ecf20Sopenharmony_ci
80648c2ecf20Sopenharmony_ci fail:
80658c2ecf20Sopenharmony_ci	wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
80668c2ecf20Sopenharmony_ci		  unit, __func__, err);
80678c2ecf20Sopenharmony_ci	if (wlc)
80688c2ecf20Sopenharmony_ci		brcms_c_detach(wlc);
80698c2ecf20Sopenharmony_ci
80708c2ecf20Sopenharmony_ci	if (perr)
80718c2ecf20Sopenharmony_ci		*perr = err;
80728c2ecf20Sopenharmony_ci	return NULL;
80738c2ecf20Sopenharmony_ci}
8074