blob: c7e7380a2e438266a1aae5b36fc8e965198d04b3 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Joe Perches02f77192012-01-15 00:38:44 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Arend van Spriel5b435de2011-10-05 13:19:03 +020019#include <linux/pci_ids.h>
20#include <linux/if_ether.h>
Seth Forsheeedc76512012-06-16 07:47:56 -050021#include <net/cfg80211.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020022#include <net/mac80211.h>
23#include <brcm_hw_ids.h>
24#include <aiutils.h>
25#include <chipcommon.h>
26#include "rate.h"
27#include "scb.h"
28#include "phy/phy_hal.h"
29#include "channel.h"
30#include "antsel.h"
31#include "stf.h"
32#include "ampdu.h"
33#include "mac80211_if.h"
34#include "ucode_loader.h"
35#include "main.h"
Alwin Beukers23038212011-10-18 14:02:58 +020036#include "soc.h"
Seth Forsheee041f652012-11-15 08:07:56 -060037#include "dma.h"
Seth Forsheeb353dda2012-11-15 08:08:03 -060038#include "debug.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020039
Arend van Spriel5b435de2011-10-05 13:19:03 +020040/* watchdog timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020041#define TIMER_INTERVAL_WATCHDOG 1000
Arend van Spriel5b435de2011-10-05 13:19:03 +020042/* radio monitor timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020043#define TIMER_INTERVAL_RADIOCHK 800
Arend van Spriel5b435de2011-10-05 13:19:03 +020044
Arend van Spriel5b435de2011-10-05 13:19:03 +020045/* beacon interval, in unit of 1024TU */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020046#define BEACON_INTERVAL_DEFAULT 100
Arend van Spriel5b435de2011-10-05 13:19:03 +020047
48/* n-mode support capability */
49/* 2x2 includes both 1x1 & 2x2 devices
50 * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
51 * control it independently
52 */
53#define WL_11N_2x2 1
54#define WL_11N_3x3 3
55#define WL_11N_4x4 4
56
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020057#define EDCF_ACI_MASK 0x60
58#define EDCF_ACI_SHIFT 5
59#define EDCF_ECWMIN_MASK 0x0f
60#define EDCF_ECWMAX_SHIFT 4
61#define EDCF_AIFSN_MASK 0x0f
62#define EDCF_AIFSN_MAX 15
63#define EDCF_ECWMAX_MASK 0xf0
Arend van Spriel5b435de2011-10-05 13:19:03 +020064
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020065#define EDCF_AC_BE_TXOP_STA 0x0000
66#define EDCF_AC_BK_TXOP_STA 0x0000
67#define EDCF_AC_VO_ACI_STA 0x62
68#define EDCF_AC_VO_ECW_STA 0x32
69#define EDCF_AC_VI_ACI_STA 0x42
70#define EDCF_AC_VI_ECW_STA 0x43
71#define EDCF_AC_BK_ECW_STA 0xA4
72#define EDCF_AC_VI_TXOP_STA 0x005e
73#define EDCF_AC_VO_TXOP_STA 0x002f
74#define EDCF_AC_BE_ACI_STA 0x03
75#define EDCF_AC_BE_ECW_STA 0xA4
76#define EDCF_AC_BK_ACI_STA 0x27
77#define EDCF_AC_VO_TXOP_AP 0x002f
Arend van Spriel5b435de2011-10-05 13:19:03 +020078
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020079#define EDCF_TXOP2USEC(txop) ((txop) << 5)
80#define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
Arend van Spriel5b435de2011-10-05 13:19:03 +020081
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020082#define APHY_SYMBOL_TIME 4
83#define APHY_PREAMBLE_TIME 16
84#define APHY_SIGNAL_TIME 4
85#define APHY_SIFS_TIME 16
86#define APHY_SERVICE_NBITS 16
87#define APHY_TAIL_NBITS 6
88#define BPHY_SIFS_TIME 10
89#define BPHY_PLCP_SHORT_TIME 96
Arend van Spriel5b435de2011-10-05 13:19:03 +020090
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020091#define PREN_PREAMBLE 24
92#define PREN_MM_EXT 12
93#define PREN_PREAMBLE_EXT 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020094
95#define DOT11_MAC_HDR_LEN 24
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020096#define DOT11_ACK_LEN 10
97#define DOT11_BA_LEN 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020098#define DOT11_OFDM_SIGNAL_EXTENSION 6
99#define DOT11_MIN_FRAG_LEN 256
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200100#define DOT11_RTS_LEN 16
101#define DOT11_CTS_LEN 10
Arend van Spriel5b435de2011-10-05 13:19:03 +0200102#define DOT11_BA_BITMAP_LEN 128
103#define DOT11_MIN_BEACON_PERIOD 1
104#define DOT11_MAX_BEACON_PERIOD 0xFFFF
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200105#define DOT11_MAXNUMFRAGS 16
Arend van Spriel5b435de2011-10-05 13:19:03 +0200106#define DOT11_MAX_FRAG_LEN 2346
107
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200108#define BPHY_PLCP_TIME 192
109#define RIFS_11N_TIME 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200110
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200111/* length of the BCN template area */
112#define BCN_TMPL_LEN 512
Arend van Spriel5b435de2011-10-05 13:19:03 +0200113
114/* brcms_bss_info flag bit values */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200115#define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200116
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200117/* chip rx buffer offset */
118#define BRCMS_HWRXOFF 38
Arend van Spriel5b435de2011-10-05 13:19:03 +0200119
120/* rfdisable delay timer 500 ms, runs of ALP clock */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200121#define RFDISABLE_DEFAULT 10000000
Arend van Spriel5b435de2011-10-05 13:19:03 +0200122
123#define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
124
125/* precedences numbers for wlc queues. These are twice as may levels as
126 * 802.1D priorities.
127 * Odd numbers are used for HI priority traffic at same precedence levels
128 * These constants are used ONLY by wlc_prio2prec_map. Do not use them
129 * elsewhere.
130 */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200131#define _BRCMS_PREC_NONE 0 /* None = - */
132#define _BRCMS_PREC_BK 2 /* BK - Background */
133#define _BRCMS_PREC_BE 4 /* BE - Best-effort */
134#define _BRCMS_PREC_EE 6 /* EE - Excellent-effort */
135#define _BRCMS_PREC_CL 8 /* CL - Controlled Load */
136#define _BRCMS_PREC_VI 10 /* Vi - Video */
137#define _BRCMS_PREC_VO 12 /* Vo - Voice */
138#define _BRCMS_PREC_NC 14 /* NC - Network Control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200139
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200140/* synthpu_dly times in us */
141#define SYNTHPU_DLY_APHY_US 3700
142#define SYNTHPU_DLY_BPHY_US 1050
143#define SYNTHPU_DLY_NPHY_US 2048
144#define SYNTHPU_DLY_LPPHY_US 300
Arend van Spriel5b435de2011-10-05 13:19:03 +0200145
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200146#define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200147
148/* Per-AC retry limit register definitions; uses defs.h bitfield macros */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200149#define EDCF_SHORT_S 0
150#define EDCF_SFB_S 4
151#define EDCF_LONG_S 8
152#define EDCF_LFB_S 12
153#define EDCF_SHORT_M BITFIELD_MASK(4)
154#define EDCF_SFB_M BITFIELD_MASK(4)
155#define EDCF_LONG_M BITFIELD_MASK(4)
156#define EDCF_LFB_M BITFIELD_MASK(4)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200157
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200158#define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
159#define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
160#define RETRY_LONG_DEF 4 /* Default Long retry count */
161#define RETRY_SHORT_FB 3 /* Short count for fb rate */
162#define RETRY_LONG_FB 2 /* Long count for fb rate */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200163
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200164#define APHY_CWMIN 15
165#define PHY_CWMAX 1023
Arend van Spriel5b435de2011-10-05 13:19:03 +0200166
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200167#define EDCF_AIFSN_MIN 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200168
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200169#define FRAGNUM_MASK 0xF
Arend van Spriel5b435de2011-10-05 13:19:03 +0200170
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200171#define APHY_SLOT_TIME 9
172#define BPHY_SLOT_TIME 20
Arend van Spriel5b435de2011-10-05 13:19:03 +0200173
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200174#define WL_SPURAVOID_OFF 0
175#define WL_SPURAVOID_ON1 1
176#define WL_SPURAVOID_ON2 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200177
178/* invalid core flags, use the saved coreflags */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200179#define BRCMS_USE_COREFLAGS 0xffffffff
Arend van Spriel5b435de2011-10-05 13:19:03 +0200180
181/* values for PLCPHdr_override */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200182#define BRCMS_PLCP_AUTO -1
183#define BRCMS_PLCP_SHORT 0
184#define BRCMS_PLCP_LONG 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200185
186/* values for g_protection_override and n_protection_override */
187#define BRCMS_PROTECTION_AUTO -1
188#define BRCMS_PROTECTION_OFF 0
189#define BRCMS_PROTECTION_ON 1
190#define BRCMS_PROTECTION_MMHDR_ONLY 2
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200191#define BRCMS_PROTECTION_CTS_ONLY 3
Arend van Spriel5b435de2011-10-05 13:19:03 +0200192
193/* values for g_protection_control and n_protection_control */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200194#define BRCMS_PROTECTION_CTL_OFF 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200195#define BRCMS_PROTECTION_CTL_LOCAL 1
196#define BRCMS_PROTECTION_CTL_OVERLAP 2
197
198/* values for n_protection */
199#define BRCMS_N_PROTECTION_OFF 0
200#define BRCMS_N_PROTECTION_OPTIONAL 1
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200201#define BRCMS_N_PROTECTION_20IN40 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200202#define BRCMS_N_PROTECTION_MIXEDMODE 3
203
204/* values for band specific 40MHz capabilities */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200205#define BRCMS_N_BW_20ALL 0
206#define BRCMS_N_BW_40ALL 1
207#define BRCMS_N_BW_20IN2G_40IN5G 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200208
209/* bitflags for SGI support (sgi_rx iovar) */
210#define BRCMS_N_SGI_20 0x01
211#define BRCMS_N_SGI_40 0x02
212
213/* defines used by the nrate iovar */
214/* MSC in use,indicates b0-6 holds an mcs */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200215#define NRATE_MCS_INUSE 0x00000080
Arend van Spriel5b435de2011-10-05 13:19:03 +0200216/* rate/mcs value */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200217#define NRATE_RATE_MASK 0x0000007f
Arend van Spriel5b435de2011-10-05 13:19:03 +0200218/* stf mode mask: siso, cdd, stbc, sdm */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200219#define NRATE_STF_MASK 0x0000ff00
Arend van Spriel5b435de2011-10-05 13:19:03 +0200220/* stf mode shift */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200221#define NRATE_STF_SHIFT 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222/* bit indicate to override mcs only */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200223#define NRATE_OVERRIDE_MCS_ONLY 0x40000000
224#define NRATE_SGI_MASK 0x00800000 /* sgi mode */
225#define NRATE_SGI_SHIFT 23 /* sgi mode */
226#define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
227#define NRATE_LDPC_SHIFT 22 /* ldpc shift */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200228
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200229#define NRATE_STF_SISO 0 /* stf mode SISO */
230#define NRATE_STF_CDD 1 /* stf mode CDD */
231#define NRATE_STF_STBC 2 /* stf mode STBC */
232#define NRATE_STF_SDM 3 /* stf mode SDM */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200233
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200234#define MAX_DMA_SEGS 4
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235
Seth Forshee75be3e22012-11-15 08:07:58 -0600236/* # of entries in Tx FIFO */
237#define NTXD 64
Arend van Spriel5b435de2011-10-05 13:19:03 +0200238/* Max # of entries in Rx FIFO based on 4kb page size */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200239#define NRXD 256
Arend van Spriel5b435de2011-10-05 13:19:03 +0200240
Seth Forsheee041f652012-11-15 08:07:56 -0600241/* Amount of headroom to leave in Tx FIFO */
242#define TX_HEADROOM 4
243
Arend van Spriel5b435de2011-10-05 13:19:03 +0200244/* try to keep this # rbufs posted to the chip */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200245#define NRXBUFPOST 32
Arend van Spriel5b435de2011-10-05 13:19:03 +0200246
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200247/* max # frames to process in brcms_c_recv() */
248#define RXBND 8
249/* max # tx status to process in wlc_txstatus() */
250#define TXSBND 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200251
Alwin Beukers44760652011-10-12 20:51:31 +0200252/* brcmu_format_flags() bit description structure */
253struct brcms_c_bit_desc {
254 u32 bit;
255 const char *name;
256};
257
Arend van Spriel5b435de2011-10-05 13:19:03 +0200258/*
259 * The following table lists the buffer memory allocated to xmt fifos in HW.
260 * the size is in units of 256bytes(one block), total size is HW dependent
261 * ucode has default fifo partition, sw can overwrite if necessary
262 *
263 * This is documented in twiki under the topic UcodeTxFifo. Please ensure
264 * the twiki is updated before making changes.
265 */
266
267/* Starting corerev for the fifo size table */
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200268#define XMTFIFOTBL_STARTREV 17
Arend van Spriel5b435de2011-10-05 13:19:03 +0200269
270struct d11init {
271 __le16 addr;
272 __le16 size;
273 __le32 value;
274};
275
Arend van Spriel5b435de2011-10-05 13:19:03 +0200276struct edcf_acparam {
277 u8 ACI;
278 u8 ECW;
279 u16 TXOP;
280} __packed;
281
Arend van Spriel5b435de2011-10-05 13:19:03 +0200282/* debug/trace */
Seth Forsheeb0341742012-11-15 08:08:01 -0600283uint brcm_msg_level;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200284
285/* TX FIFO number to WME/802.1E Access Category */
Arend van Sprielb7eec422011-11-10 20:30:18 +0100286static const u8 wme_fifo2ac[] = {
287 IEEE80211_AC_BK,
288 IEEE80211_AC_BE,
289 IEEE80211_AC_VI,
290 IEEE80211_AC_VO,
291 IEEE80211_AC_BE,
292 IEEE80211_AC_BE
293};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200294
Arend van Sprielb7eec422011-11-10 20:30:18 +0100295/* ieee80211 Access Category to TX FIFO number */
296static const u8 wme_ac2fifo[] = {
297 TX_AC_VO_FIFO,
298 TX_AC_VI_FIFO,
299 TX_AC_BE_FIFO,
300 TX_AC_BK_FIFO
301};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200302
Seth Forsheee041f652012-11-15 08:07:56 -0600303/* 802.1D Priority to precedence queue mapping */
304const u8 wlc_prio2prec_map[] = {
305 _BRCMS_PREC_BE, /* 0 BE - Best-effort */
306 _BRCMS_PREC_BK, /* 1 BK - Background */
307 _BRCMS_PREC_NONE, /* 2 None = - */
308 _BRCMS_PREC_EE, /* 3 EE - Excellent-effort */
309 _BRCMS_PREC_CL, /* 4 CL - Controlled Load */
310 _BRCMS_PREC_VI, /* 5 Vi - Video */
311 _BRCMS_PREC_VO, /* 6 Vo - Voice */
312 _BRCMS_PREC_NC, /* 7 NC - Network Control */
313};
314
Arend van Spriel5b435de2011-10-05 13:19:03 +0200315static const u16 xmtfifo_sz[][NFIFO] = {
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200316 /* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
317 {20, 192, 192, 21, 17, 5},
318 /* corerev 18: */
319 {0, 0, 0, 0, 0, 0},
320 /* corerev 19: */
321 {0, 0, 0, 0, 0, 0},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200322 /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
323 {20, 192, 192, 21, 17, 5},
324 /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
325 {9, 58, 22, 14, 14, 5},
326 /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
327 {20, 192, 192, 21, 17, 5},
328 /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
329 {20, 192, 192, 21, 17, 5},
330 /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
331 {9, 58, 22, 14, 14, 5},
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200332 /* corerev 25: */
333 {0, 0, 0, 0, 0, 0},
334 /* corerev 26: */
335 {0, 0, 0, 0, 0, 0},
336 /* corerev 27: */
337 {0, 0, 0, 0, 0, 0},
338 /* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
339 {9, 58, 22, 14, 14, 5},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200340};
341
Joe Perches8ae74652012-01-15 00:38:38 -0800342#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200343static const char * const fifo_names[] = {
344 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
345#else
346static const char fifo_names[6][0];
347#endif
348
Joe Perches8ae74652012-01-15 00:38:38 -0800349#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200350/* pointer to most recently allocated wl/wlc */
351static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
352#endif
353
Seth Forshee32d0f122012-11-15 08:07:55 -0600354/* Mapping of ieee80211 AC numbers to tx fifos */
355static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
356 [IEEE80211_AC_VO] = TX_AC_VO_FIFO,
357 [IEEE80211_AC_VI] = TX_AC_VI_FIFO,
358 [IEEE80211_AC_BE] = TX_AC_BE_FIFO,
359 [IEEE80211_AC_BK] = TX_AC_BK_FIFO,
360};
361
Seth Forsheee041f652012-11-15 08:07:56 -0600362/* Mapping of tx fifos to ieee80211 AC numbers */
363static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
364 [TX_AC_BK_FIFO] = IEEE80211_AC_BK,
365 [TX_AC_BE_FIFO] = IEEE80211_AC_BE,
366 [TX_AC_VI_FIFO] = IEEE80211_AC_VI,
367 [TX_AC_VO_FIFO] = IEEE80211_AC_VO,
368};
369
Seth Forshee32d0f122012-11-15 08:07:55 -0600370static u8 brcms_ac_to_fifo(u8 ac)
371{
372 if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
373 return TX_AC_BE_FIFO;
374 return ac_to_fifo_mapping[ac];
375}
376
Seth Forsheee041f652012-11-15 08:07:56 -0600377static u8 brcms_fifo_to_ac(u8 fifo)
378{
379 if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
380 return IEEE80211_AC_BE;
381 return fifo_to_ac_mapping[fifo];
382}
383
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200384/* Find basic rate for a given rate */
385static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
386{
387 if (is_mcs_rate(rspec))
388 return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
389 .leg_ofdm];
390 return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
391}
392
393static u16 frametype(u32 rspec, u8 mimoframe)
394{
395 if (is_mcs_rate(rspec))
396 return mimoframe;
397 return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
398}
399
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200400/* currently the best mechanism for determining SIFS is the band in use */
401static u16 get_sifs(struct brcms_band *band)
402{
403 return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
404 BPHY_SIFS_TIME;
405}
406
407/*
408 * Detect Card removed.
409 * Even checking an sbconfig register read will not false trigger when the core
410 * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
411 * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
412 * reg with fixed 0/1 pattern (some platforms return all 0).
413 * If clocks are present, call the sb routine which will figure out if the
414 * device is removed.
415 */
416static bool brcms_deviceremoved(struct brcms_c_info *wlc)
417{
Arend van Spriel16d28122011-12-08 15:06:51 -0800418 u32 macctrl;
419
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200420 if (!wlc->hw->clk)
421 return ai_deviceremoved(wlc->hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -0800422 macctrl = bcma_read32(wlc->hw->d11core,
423 D11REGOFFS(maccontrol));
424 return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200425}
426
427/* sum the individual fifo tx pending packet counts */
Seth Forsheee041f652012-11-15 08:07:56 -0600428static int brcms_txpktpendtot(struct brcms_c_info *wlc)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200429{
Seth Forsheee041f652012-11-15 08:07:56 -0600430 int i;
431 int pending = 0;
432
433 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
434 if (wlc->hw->di[i])
435 pending += dma_txpending(wlc->hw->di[i]);
436 return pending;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200437}
438
439static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
440{
441 return wlc->pub->_nbands > 1 && !wlc->bandlocked;
442}
443
444static int brcms_chspec_bw(u16 chanspec)
445{
446 if (CHSPEC_IS40(chanspec))
447 return BRCMS_40_MHZ;
448 if (CHSPEC_IS20(chanspec))
449 return BRCMS_20_MHZ;
450
451 return BRCMS_10_MHZ;
452}
453
Arend van Spriel5b435de2011-10-05 13:19:03 +0200454static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
455{
456 if (cfg == NULL)
457 return;
458
459 kfree(cfg->current_bss);
460 kfree(cfg);
461}
462
463static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
464{
465 if (wlc == NULL)
466 return;
467
468 brcms_c_bsscfg_mfree(wlc->bsscfg);
469 kfree(wlc->pub);
470 kfree(wlc->modulecb);
471 kfree(wlc->default_bss);
472 kfree(wlc->protection);
473 kfree(wlc->stf);
474 kfree(wlc->bandstate[0]);
475 kfree(wlc->corestate->macstat_snapshot);
476 kfree(wlc->corestate);
477 kfree(wlc->hw->bandstate[0]);
478 kfree(wlc->hw);
479
480 /* free the wlc */
481 kfree(wlc);
482 wlc = NULL;
483}
484
485static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
486{
487 struct brcms_bss_cfg *cfg;
488
489 cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
490 if (cfg == NULL)
491 goto fail;
492
493 cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
494 if (cfg->current_bss == NULL)
495 goto fail;
496
497 return cfg;
498
499 fail:
500 brcms_c_bsscfg_mfree(cfg);
501 return NULL;
502}
503
504static struct brcms_c_info *
505brcms_c_attach_malloc(uint unit, uint *err, uint devid)
506{
507 struct brcms_c_info *wlc;
508
509 wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
510 if (wlc == NULL) {
511 *err = 1002;
512 goto fail;
513 }
514
515 /* allocate struct brcms_c_pub state structure */
516 wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
517 if (wlc->pub == NULL) {
518 *err = 1003;
519 goto fail;
520 }
521 wlc->pub->wlc = wlc;
522
523 /* allocate struct brcms_hardware state structure */
524
525 wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
526 if (wlc->hw == NULL) {
527 *err = 1005;
528 goto fail;
529 }
530 wlc->hw->wlc = wlc;
531
532 wlc->hw->bandstate[0] =
533 kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
534 if (wlc->hw->bandstate[0] == NULL) {
535 *err = 1006;
536 goto fail;
537 } else {
538 int i;
539
540 for (i = 1; i < MAXBANDS; i++)
541 wlc->hw->bandstate[i] = (struct brcms_hw_band *)
542 ((unsigned long)wlc->hw->bandstate[0] +
543 (sizeof(struct brcms_hw_band) * i));
544 }
545
546 wlc->modulecb =
547 kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
548 if (wlc->modulecb == NULL) {
549 *err = 1009;
550 goto fail;
551 }
552
553 wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
554 if (wlc->default_bss == NULL) {
555 *err = 1010;
556 goto fail;
557 }
558
559 wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
560 if (wlc->bsscfg == NULL) {
561 *err = 1011;
562 goto fail;
563 }
564
565 wlc->protection = kzalloc(sizeof(struct brcms_protection),
566 GFP_ATOMIC);
567 if (wlc->protection == NULL) {
568 *err = 1016;
569 goto fail;
570 }
571
572 wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
573 if (wlc->stf == NULL) {
574 *err = 1017;
575 goto fail;
576 }
577
578 wlc->bandstate[0] =
579 kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
580 if (wlc->bandstate[0] == NULL) {
581 *err = 1025;
582 goto fail;
583 } else {
584 int i;
585
586 for (i = 1; i < MAXBANDS; i++)
587 wlc->bandstate[i] = (struct brcms_band *)
588 ((unsigned long)wlc->bandstate[0]
589 + (sizeof(struct brcms_band)*i));
590 }
591
592 wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
593 if (wlc->corestate == NULL) {
594 *err = 1026;
595 goto fail;
596 }
597
598 wlc->corestate->macstat_snapshot =
599 kzalloc(sizeof(struct macstat), GFP_ATOMIC);
600 if (wlc->corestate->macstat_snapshot == NULL) {
601 *err = 1027;
602 goto fail;
603 }
604
605 return wlc;
606
607 fail:
608 brcms_c_detach_mfree(wlc);
609 return NULL;
610}
611
612/*
613 * Update the slot timing for standard 11b/g (20us slots)
614 * or shortslot 11g (9us slots)
615 * The PSM needs to be suspended for this call.
616 */
617static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
618 bool shortslot)
619{
Arend van Spriel16d28122011-12-08 15:06:51 -0800620 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200621
622 if (shortslot) {
623 /* 11g short slot: 11a timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800624 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200625 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
626 } else {
627 /* 11g long slot: 11b timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800628 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200629 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
630 }
631}
632
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200633/*
634 * calculate frame duration of a given rate and length, return
635 * time in usec unit
636 */
Arend van Spriel094b1992011-10-18 14:03:07 +0200637static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
638 u8 preamble_type, uint mac_len)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200639{
640 uint nsyms, dur = 0, Ndps, kNdps;
641 uint rate = rspec2rate(ratespec);
642
643 if (rate == 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600644 brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200645 wlc->pub->unit);
646 rate = BRCM_RATE_1M;
647 }
648
649 BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, len%d\n",
650 wlc->pub->unit, ratespec, preamble_type, mac_len);
651
652 if (is_mcs_rate(ratespec)) {
653 uint mcs = ratespec & RSPEC_RATE_MASK;
654 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
655
656 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
657 if (preamble_type == BRCMS_MM_PREAMBLE)
658 dur += PREN_MM_EXT;
659 /* 1000Ndbps = kbps * 4 */
660 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
661 rspec_issgi(ratespec)) * 4;
662
663 if (rspec_stc(ratespec) == 0)
664 nsyms =
665 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
666 APHY_TAIL_NBITS) * 1000, kNdps);
667 else
668 /* STBC needs to have even number of symbols */
669 nsyms =
670 2 *
671 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
672 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
673
674 dur += APHY_SYMBOL_TIME * nsyms;
675 if (wlc->band->bandtype == BRCM_BAND_2G)
676 dur += DOT11_OFDM_SIGNAL_EXTENSION;
677 } else if (is_ofdm_rate(rate)) {
678 dur = APHY_PREAMBLE_TIME;
679 dur += APHY_SIGNAL_TIME;
680 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
681 Ndps = rate * 2;
682 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
683 nsyms =
684 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
685 Ndps);
686 dur += APHY_SYMBOL_TIME * nsyms;
687 if (wlc->band->bandtype == BRCM_BAND_2G)
688 dur += DOT11_OFDM_SIGNAL_EXTENSION;
689 } else {
690 /*
691 * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
692 * will divide out
693 */
694 mac_len = mac_len * 8 * 2;
695 /* calc ceiling of bits/rate = microseconds of air time */
696 dur = (mac_len + rate - 1) / rate;
697 if (preamble_type & BRCMS_SHORT_PREAMBLE)
698 dur += BPHY_PLCP_SHORT_TIME;
699 else
700 dur += BPHY_PLCP_TIME;
701 }
702 return dur;
703}
704
Arend van Spriel5b435de2011-10-05 13:19:03 +0200705static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
706 const struct d11init *inits)
707{
Arend van Spriel16d28122011-12-08 15:06:51 -0800708 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200709 int i;
Arend van Spriel16d28122011-12-08 15:06:51 -0800710 uint offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200711 u16 size;
712 u32 value;
713
Seth Forsheeb353dda2012-11-15 08:08:03 -0600714 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200715
Arend van Spriel5b435de2011-10-05 13:19:03 +0200716 for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
717 size = le16_to_cpu(inits[i].size);
Arend van Spriel16d28122011-12-08 15:06:51 -0800718 offset = le16_to_cpu(inits[i].addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200719 value = le32_to_cpu(inits[i].value);
720 if (size == 2)
Arend van Spriel16d28122011-12-08 15:06:51 -0800721 bcma_write16(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200722 else if (size == 4)
Arend van Spriel16d28122011-12-08 15:06:51 -0800723 bcma_write32(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200724 else
725 break;
726 }
727}
728
729static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
730{
731 u8 idx;
732 u16 addr[] = {
733 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
734 M_HOST_FLAGS5
735 };
736
737 for (idx = 0; idx < MHFMAX; idx++)
738 brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
739}
740
741static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
742{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200743 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
744
745 /* init microcode host flags */
746 brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
747
748 /* do band-specific ucode IHR, SHM, and SCR inits */
749 if (D11REV_IS(wlc_hw->corerev, 23)) {
750 if (BRCMS_ISNPHY(wlc_hw->band))
751 brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
752 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600753 brcms_err(wlc_hw->d11core,
754 "%s: wl%d: unsupported phy in corerev %d\n",
755 __func__, wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200756 wlc_hw->corerev);
757 } else {
758 if (D11REV_IS(wlc_hw->corerev, 24)) {
759 if (BRCMS_ISLCNPHY(wlc_hw->band))
760 brcms_c_write_inits(wlc_hw,
761 ucode->d11lcn0bsinitvals24);
762 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600763 brcms_err(wlc_hw->d11core,
764 "%s: wl%d: unsupported phy in core rev %d\n",
765 __func__, wlc_hw->unit,
766 wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200767 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600768 brcms_err(wlc_hw->d11core,
769 "%s: wl%d: unsupported corerev %d\n",
770 __func__, wlc_hw->unit, wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771 }
772 }
773}
774
Arend van Spriela8779e42011-12-08 15:06:58 -0800775static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
776{
777 struct bcma_device *core = wlc_hw->d11core;
778 u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
779
780 bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
781}
782
Arend van Spriel5b435de2011-10-05 13:19:03 +0200783static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
784{
Seth Forsheeb353dda2012-11-15 08:08:03 -0600785 brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200786
787 wlc_hw->phyclk = clk;
788
789 if (OFF == clk) { /* clear gmode bit, put phy into reset */
790
Arend van Spriela8779e42011-12-08 15:06:58 -0800791 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
792 (SICF_PRST | SICF_FGC));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200793 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800794 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795 udelay(1);
796
797 } else { /* take phy out of reset */
798
Arend van Spriela8779e42011-12-08 15:06:58 -0800799 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200800 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800801 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200802 udelay(1);
803
804 }
805}
806
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200807/* low-level band switch utility routine */
808static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
809{
810 BCMMSG(wlc_hw->wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
811 bandunit);
812
813 wlc_hw->band = wlc_hw->bandstate[bandunit];
814
815 /*
816 * BMAC_NOTE:
817 * until we eliminate need for wlc->band refs in low level code
818 */
819 wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
820
821 /* set gmode core flag */
Arend van Spriela8779e42011-12-08 15:06:58 -0800822 if (wlc_hw->sbclk && !wlc_hw->noreset) {
823 u32 gmode = 0;
824
825 if (bandunit == 0)
826 gmode = SICF_GMODE;
827
828 brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
829 }
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200830}
831
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832/* switch to new band but leave it inactive */
833static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
834{
835 struct brcms_hardware *wlc_hw = wlc->hw;
836 u32 macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -0800837 u32 macctrl;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200838
839 BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
Arend van Spriel16d28122011-12-08 15:06:51 -0800840 macctrl = bcma_read32(wlc_hw->d11core,
841 D11REGOFFS(maccontrol));
842 WARN_ON((macctrl & MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200843
844 /* disable interrupts */
845 macintmask = brcms_intrsoff(wlc->wl);
846
847 /* radio off */
848 wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
849
850 brcms_b_core_phy_clk(wlc_hw, OFF);
851
852 brcms_c_setxband(wlc_hw, bandunit);
853
854 return macintmask;
855}
856
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857/* process an individual struct tx_status */
858static bool
859brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
860{
Seth Forsheee041f652012-11-15 08:07:56 -0600861 struct sk_buff *p = NULL;
862 uint queue = NFIFO;
863 struct dma_pub *dma = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200864 struct d11txh *txh;
865 struct scb *scb = NULL;
866 bool free_pdu;
867 int tx_rts, tx_frame_count, tx_rts_count;
868 uint totlen, supr_status;
869 bool lastframe;
870 struct ieee80211_hdr *h;
871 u16 mcl;
872 struct ieee80211_tx_info *tx_info;
873 struct ieee80211_tx_rate *txrate;
874 int i;
Seth Forsheee041f652012-11-15 08:07:56 -0600875 bool fatal = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876
877 /* discard intermediate indications for ucode with one legitimate case:
878 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
879 * but the subsequent tx of DATA failed. so it will start rts/cts
880 * from the beginning (resetting the rts transmission count)
881 */
882 if (!(txs->status & TX_STATUS_AMPDU)
883 && (txs->status & TX_STATUS_INTERMEDIATE)) {
Eldad Zack6ead6292012-04-22 00:48:04 +0200884 BCMMSG(wlc->wiphy, "INTERMEDIATE but not AMPDU\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600885 fatal = false;
886 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 }
888
889 queue = txs->frameid & TXFID_QUEUE_MASK;
Seth Forsheee041f652012-11-15 08:07:56 -0600890 if (queue >= NFIFO)
891 goto out;
892
893 dma = wlc->hw->di[queue];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894
895 p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
896 if (p == NULL)
Seth Forsheee041f652012-11-15 08:07:56 -0600897 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200898
899 txh = (struct d11txh *) (p->data);
900 mcl = le16_to_cpu(txh->MacTxControlLow);
901
902 if (txs->phyerr) {
Seth Forshee1ca47e62012-11-15 08:08:00 -0600903 if (brcm_msg_level & BRCM_DL_INFO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600904 brcms_err(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905 txs->phyerr, txh->MainRates);
906 brcms_c_print_txdesc(txh);
907 }
908 brcms_c_print_txstatus(txs);
909 }
910
911 if (txs->frameid != le16_to_cpu(txh->TxFrameID))
Seth Forsheee041f652012-11-15 08:07:56 -0600912 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913 tx_info = IEEE80211_SKB_CB(p);
914 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
915
Thomas Huehn644e8c02012-07-10 14:01:37 +0200916 if (tx_info->rate_driver_data[0])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200917 scb = &wlc->pri_scb;
918
919 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
920 brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
Seth Forsheee041f652012-11-15 08:07:56 -0600921 fatal = false;
922 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200923 }
924
925 supr_status = txs->status & TX_STATUS_SUPR_MASK;
926 if (supr_status == TX_STATUS_SUPR_BADCH)
927 BCMMSG(wlc->wiphy,
928 "%s: Pkt tx suppressed, possibly channel %d\n",
929 __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
930
931 tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
932 tx_frame_count =
933 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
934 tx_rts_count =
935 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
936
937 lastframe = !ieee80211_has_morefrags(h->frame_control);
938
939 if (!lastframe) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600940 brcms_err(wlc->hw->d11core, "Not last frame!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200941 } else {
942 /*
943 * Set information to be consumed by Minstrel ht.
944 *
945 * The "fallback limit" is the number of tx attempts a given
946 * MPDU is sent at the "primary" rate. Tx attempts beyond that
947 * limit are sent at the "secondary" rate.
948 * A 'short frame' does not exceed RTS treshold.
949 */
950 u16 sfbl, /* Short Frame Rate Fallback Limit */
951 lfbl, /* Long Frame Rate Fallback Limit */
952 fbl;
953
Arend van Sprielb7eec422011-11-10 20:30:18 +0100954 if (queue < IEEE80211_NUM_ACS) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200955 sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
956 EDCF_SFB);
957 lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
958 EDCF_LFB);
959 } else {
960 sfbl = wlc->SFBL;
961 lfbl = wlc->LFBL;
962 }
963
964 txrate = tx_info->status.rates;
965 if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
966 fbl = lfbl;
967 else
968 fbl = sfbl;
969
970 ieee80211_tx_info_clear_status(tx_info);
971
972 if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
973 /*
974 * rate selection requested a fallback rate
975 * and we used it
976 */
977 txrate[0].count = fbl;
978 txrate[1].count = tx_frame_count - fbl;
979 } else {
980 /*
981 * rate selection did not request fallback rate, or
982 * we didn't need it
983 */
984 txrate[0].count = tx_frame_count;
985 /*
986 * rc80211_minstrel.c:minstrel_tx_status() expects
987 * unused rates to be marked with idx = -1
988 */
989 txrate[1].idx = -1;
990 txrate[1].count = 0;
991 }
992
993 /* clear the rest of the rates */
994 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
995 txrate[i].idx = -1;
996 txrate[i].count = 0;
997 }
998
999 if (txs->status & TX_STATUS_ACK_RCV)
1000 tx_info->flags |= IEEE80211_TX_STAT_ACK;
1001 }
1002
Arend van Sprielad4d71f2011-11-10 20:30:26 +01001003 totlen = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004 free_pdu = true;
1005
Arend van Spriel5b435de2011-10-05 13:19:03 +02001006 if (lastframe) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001007 /* remove PLCP & Broadcom tx descriptor header */
1008 skb_pull(p, D11_PHY_HDR_LEN);
1009 skb_pull(p, D11_TXH_LEN);
1010 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
1011 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001012 brcms_err(wlc->hw->d11core,
1013 "%s: Not last frame => not calling tx_status\n",
1014 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001015 }
1016
Seth Forsheee041f652012-11-15 08:07:56 -06001017 fatal = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001018
Seth Forsheee041f652012-11-15 08:07:56 -06001019 out:
1020 if (fatal && p)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001021 brcmu_pkt_buf_free_skb(p);
1022
Seth Forsheee041f652012-11-15 08:07:56 -06001023 if (dma && queue < NFIFO) {
1024 u16 ac_queue = brcms_fifo_to_ac(queue);
1025 if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
1026 ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
1027 ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
1028 dma_kick_tx(dma);
1029 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001030
Seth Forsheee041f652012-11-15 08:07:56 -06001031 return fatal;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001032}
1033
1034/* process tx completion events in BMAC
1035 * Return true if more tx status need to be processed. false otherwise.
1036 */
1037static bool
1038brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
1039{
1040 bool morepending = false;
1041 struct brcms_c_info *wlc = wlc_hw->wlc;
Arend van Spriel16d28122011-12-08 15:06:51 -08001042 struct bcma_device *core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001043 struct tx_status txstatus, *txs;
1044 u32 s1, s2;
1045 uint n = 0;
1046 /*
1047 * Param 'max_tx_num' indicates max. # tx status to process before
1048 * break out.
1049 */
1050 uint max_tx_num = bound ? TXSBND : -1;
1051
1052 BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
1053
1054 txs = &txstatus;
Arend van Spriel16d28122011-12-08 15:06:51 -08001055 core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001056 *fatal = false;
Arend van Spriel16d28122011-12-08 15:06:51 -08001057 s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001058 while (!(*fatal)
Arend van Spriel16d28122011-12-08 15:06:51 -08001059 && (s1 & TXS_V)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001060
1061 if (s1 == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001062 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
1063 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001064 return morepending;
1065 }
Arend van Spriel16d28122011-12-08 15:06:51 -08001066 s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001067
1068 txs->status = s1 & TXS_STATUS_MASK;
1069 txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
1070 txs->sequence = s2 & TXS_SEQ_MASK;
1071 txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
1072 txs->lasttxtime = 0;
1073
1074 *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
1075
1076 /* !give others some time to run! */
1077 if (++n >= max_tx_num)
1078 break;
Arend van Spriel16d28122011-12-08 15:06:51 -08001079 s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001080 }
1081
1082 if (*fatal)
1083 return 0;
1084
1085 if (n >= max_tx_num)
1086 morepending = true;
1087
Arend van Spriel5b435de2011-10-05 13:19:03 +02001088 return morepending;
1089}
1090
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001091static void brcms_c_tbtt(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001092{
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001093 if (!wlc->bsscfg->BSS)
1094 /*
1095 * DirFrmQ is now valid...defer setting until end
1096 * of ATIM window
1097 */
1098 wlc->qvalid |= MCMD_DIRFRMQVAL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001099}
1100
1101/* set initial host flags value */
1102static void
1103brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
1104{
1105 struct brcms_hardware *wlc_hw = wlc->hw;
1106
1107 memset(mhfs, 0, MHFMAX * sizeof(u16));
1108
1109 mhfs[MHF2] |= mhf2_init;
1110
1111 /* prohibit use of slowclock on multifunction boards */
1112 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
1113 mhfs[MHF1] |= MHF1_FORCEFASTCLK;
1114
1115 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
1116 mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
1117 mhfs[MHF1] |= MHF1_IQSWAP_WAR;
1118 }
1119}
1120
Arend van Spriele81da652011-12-08 15:06:53 -08001121static uint
1122dmareg(uint direction, uint fifonum)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001123{
1124 if (direction == DMA_TX)
Arend van Spriele81da652011-12-08 15:06:53 -08001125 return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
1126 return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001127}
1128
1129static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
1130{
1131 uint i;
1132 char name[8];
1133 /*
1134 * ucode host flag 2 needed for pio mode, independent of band and fifo
1135 */
1136 u16 pio_mhf2 = 0;
1137 struct brcms_hardware *wlc_hw = wlc->hw;
1138 uint unit = wlc_hw->unit;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001139
1140 /* name and offsets for dma_attach */
1141 snprintf(name, sizeof(name), "wl%d", unit);
1142
1143 if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
1144 int dma_attach_err = 0;
1145
1146 /*
1147 * FIFO 0
1148 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
1149 * RX: RX_FIFO (RX data packets)
1150 */
Seth Forsheee041f652012-11-15 08:07:56 -06001151 wlc_hw->di[0] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001152 (wme ? dmareg(DMA_TX, 0) : 0),
1153 dmareg(DMA_RX, 0),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001154 (wme ? NTXD : 0), NRXD,
1155 RXBUFSZ, -1, NRXBUFPOST,
1156 BRCMS_HWRXOFF, &brcm_msg_level);
1157 dma_attach_err |= (NULL == wlc_hw->di[0]);
1158
1159 /*
1160 * FIFO 1
1161 * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
1162 * (legacy) TX_DATA_FIFO (TX data packets)
1163 * RX: UNUSED
1164 */
Seth Forsheee041f652012-11-15 08:07:56 -06001165 wlc_hw->di[1] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001166 dmareg(DMA_TX, 1), 0,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001167 NTXD, 0, 0, -1, 0, 0,
1168 &brcm_msg_level);
1169 dma_attach_err |= (NULL == wlc_hw->di[1]);
1170
1171 /*
1172 * FIFO 2
1173 * TX: TX_AC_VI_FIFO (TX AC Video data packets)
1174 * RX: UNUSED
1175 */
Seth Forsheee041f652012-11-15 08:07:56 -06001176 wlc_hw->di[2] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001177 dmareg(DMA_TX, 2), 0,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001178 NTXD, 0, 0, -1, 0, 0,
1179 &brcm_msg_level);
1180 dma_attach_err |= (NULL == wlc_hw->di[2]);
1181 /*
1182 * FIFO 3
1183 * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
1184 * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
1185 */
Seth Forsheee041f652012-11-15 08:07:56 -06001186 wlc_hw->di[3] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001187 dmareg(DMA_TX, 3),
1188 0, NTXD, 0, 0, -1,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001189 0, 0, &brcm_msg_level);
1190 dma_attach_err |= (NULL == wlc_hw->di[3]);
1191/* Cleaner to leave this as if with AP defined */
1192
1193 if (dma_attach_err) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001194 brcms_err(wlc_hw->d11core,
1195 "wl%d: wlc_attach: dma_attach failed\n",
1196 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001197 return false;
1198 }
1199
1200 /* get pointer to dma engine tx flow control variable */
1201 for (i = 0; i < NFIFO; i++)
1202 if (wlc_hw->di[i])
1203 wlc_hw->txavail[i] =
1204 (uint *) dma_getvar(wlc_hw->di[i],
1205 "&txavail");
1206 }
1207
1208 /* initial ucode host flags */
1209 brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
1210
1211 return true;
1212}
1213
1214static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
1215{
1216 uint j;
1217
1218 for (j = 0; j < NFIFO; j++) {
1219 if (wlc_hw->di[j]) {
1220 dma_detach(wlc_hw->di[j]);
1221 wlc_hw->di[j] = NULL;
1222 }
1223 }
1224}
1225
1226/*
1227 * Initialize brcms_c_info default values ...
1228 * may get overrides later in this function
1229 * BMAC_NOTES, move low out and resolve the dangling ones
1230 */
1231static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
1232{
1233 struct brcms_c_info *wlc = wlc_hw->wlc;
1234
1235 /* set default sw macintmask value */
1236 wlc->defmacintmask = DEF_MACINTMASK;
1237
1238 /* various 802.11g modes */
1239 wlc_hw->shortslot = false;
1240
1241 wlc_hw->SFBL = RETRY_SHORT_FB;
1242 wlc_hw->LFBL = RETRY_LONG_FB;
1243
1244 /* default mac retry limits */
1245 wlc_hw->SRL = RETRY_SHORT_DEF;
1246 wlc_hw->LRL = RETRY_LONG_DEF;
1247 wlc_hw->chanspec = ch20mhz_chspec(1);
1248}
1249
1250static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
1251{
1252 /* delay before first read of ucode state */
1253 udelay(40);
1254
1255 /* wait until ucode is no longer asleep */
1256 SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
1257 DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
1258}
1259
1260/* control chip clock to save power, enable dynamic clock or force fast clock */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001261static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001262{
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001263 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001264 /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
1265 * on backplane, but mac core will still run on ALP(not HT) when
1266 * it enters powersave mode, which means the FCA bit may not be
1267 * set. Should wakeup mac if driver wants it to run on HT.
1268 */
1269
1270 if (wlc_hw->clk) {
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001271 if (mode == BCMA_CLKMODE_FAST) {
Arend van Spriel16d28122011-12-08 15:06:51 -08001272 bcma_set32(wlc_hw->d11core,
1273 D11REGOFFS(clk_ctl_st),
1274 CCS_FORCEHT);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001275
1276 udelay(64);
1277
Arend van Spriel16d28122011-12-08 15:06:51 -08001278 SPINWAIT(
1279 ((bcma_read32(wlc_hw->d11core,
1280 D11REGOFFS(clk_ctl_st)) &
1281 CCS_HTAVAIL) == 0),
1282 PMU_MAX_TRANSITION_DLY);
1283 WARN_ON(!(bcma_read32(wlc_hw->d11core,
1284 D11REGOFFS(clk_ctl_st)) &
1285 CCS_HTAVAIL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001286 } else {
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001287 if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
Arend van Spriel16d28122011-12-08 15:06:51 -08001288 (bcma_read32(wlc_hw->d11core,
1289 D11REGOFFS(clk_ctl_st)) &
1290 (CCS_FORCEHT | CCS_HTAREQ)))
1291 SPINWAIT(
1292 ((bcma_read32(wlc_hw->d11core,
1293 offsetof(struct d11regs,
1294 clk_ctl_st)) &
1295 CCS_HTAVAIL) == 0),
1296 PMU_MAX_TRANSITION_DLY);
1297 bcma_mask32(wlc_hw->d11core,
1298 D11REGOFFS(clk_ctl_st),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001299 ~CCS_FORCEHT);
1300 }
1301 }
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001302 wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001303 } else {
1304
1305 /* old chips w/o PMU, force HT through cc,
1306 * then use FCA to verify mac is running fast clock
1307 */
1308
1309 wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
1310
1311 /* check fast clock is available (if core is not in reset) */
1312 if (wlc_hw->forcefastclk && wlc_hw->clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001313 WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02001314 SISF_FCLKA));
1315
1316 /*
1317 * keep the ucode wake bit on if forcefastclk is on since we
1318 * do not want ucode to put us back to slow clock when it dozes
1319 * for PM mode. Code below matches the wake override bit with
1320 * current forcefastclk state. Only setting bit in wake_override
1321 * instead of waking ucode immediately since old code had this
1322 * behavior. Older code set wlc->forcefastclk but only had the
1323 * wake happen if the wakup_ucode work (protected by an up
1324 * check) was executed just below.
1325 */
1326 if (wlc_hw->forcefastclk)
1327 mboolset(wlc_hw->wake_override,
1328 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1329 else
1330 mboolclr(wlc_hw->wake_override,
1331 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1332 }
1333}
1334
1335/* set or clear ucode host flag bits
1336 * it has an optimization for no-change write
1337 * it only writes through shared memory when the core has clock;
1338 * pre-CLK changes should use wlc_write_mhf to get around the optimization
1339 *
1340 *
1341 * bands values are: BRCM_BAND_AUTO <--- Current band only
1342 * BRCM_BAND_5G <--- 5G band only
1343 * BRCM_BAND_2G <--- 2G band only
1344 * BRCM_BAND_ALL <--- All bands
1345 */
1346void
1347brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
1348 int bands)
1349{
1350 u16 save;
1351 u16 addr[MHFMAX] = {
1352 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
1353 M_HOST_FLAGS5
1354 };
1355 struct brcms_hw_band *band;
1356
1357 if ((val & ~mask) || idx >= MHFMAX)
1358 return; /* error condition */
1359
1360 switch (bands) {
1361 /* Current band only or all bands,
1362 * then set the band to current band
1363 */
1364 case BRCM_BAND_AUTO:
1365 case BRCM_BAND_ALL:
1366 band = wlc_hw->band;
1367 break;
1368 case BRCM_BAND_5G:
1369 band = wlc_hw->bandstate[BAND_5G_INDEX];
1370 break;
1371 case BRCM_BAND_2G:
1372 band = wlc_hw->bandstate[BAND_2G_INDEX];
1373 break;
1374 default:
1375 band = NULL; /* error condition */
1376 }
1377
1378 if (band) {
1379 save = band->mhfs[idx];
1380 band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
1381
1382 /* optimization: only write through if changed, and
1383 * changed band is the current band
1384 */
1385 if (wlc_hw->clk && (band->mhfs[idx] != save)
1386 && (band == wlc_hw->band))
1387 brcms_b_write_shm(wlc_hw, addr[idx],
1388 (u16) band->mhfs[idx]);
1389 }
1390
1391 if (bands == BRCM_BAND_ALL) {
1392 wlc_hw->bandstate[0]->mhfs[idx] =
1393 (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
1394 wlc_hw->bandstate[1]->mhfs[idx] =
1395 (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
1396 }
1397}
1398
1399/* set the maccontrol register to desired reset state and
1400 * initialize the sw cache of the register
1401 */
1402static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
1403{
1404 /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
1405 wlc_hw->maccontrol = 0;
1406 wlc_hw->suspended_fifos = 0;
1407 wlc_hw->wake_override = 0;
1408 wlc_hw->mute_override = 0;
1409 brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
1410}
1411
1412/*
1413 * write the software state of maccontrol and
1414 * overrides to the maccontrol register
1415 */
1416static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
1417{
1418 u32 maccontrol = wlc_hw->maccontrol;
1419
1420 /* OR in the wake bit if overridden */
1421 if (wlc_hw->wake_override)
1422 maccontrol |= MCTL_WAKE;
1423
1424 /* set AP and INFRA bits for mute if needed */
1425 if (wlc_hw->mute_override) {
1426 maccontrol &= ~(MCTL_AP);
1427 maccontrol |= MCTL_INFRA;
1428 }
1429
Arend van Spriel16d28122011-12-08 15:06:51 -08001430 bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
1431 maccontrol);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001432}
1433
1434/* set or clear maccontrol bits */
1435void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
1436{
1437 u32 maccontrol;
1438 u32 new_maccontrol;
1439
1440 if (val & ~mask)
1441 return; /* error condition */
1442 maccontrol = wlc_hw->maccontrol;
1443 new_maccontrol = (maccontrol & ~mask) | val;
1444
1445 /* if the new maccontrol value is the same as the old, nothing to do */
1446 if (new_maccontrol == maccontrol)
1447 return;
1448
1449 /* something changed, cache the new value */
1450 wlc_hw->maccontrol = new_maccontrol;
1451
1452 /* write the new values with overrides applied */
1453 brcms_c_mctrl_write(wlc_hw);
1454}
1455
1456void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
1457 u32 override_bit)
1458{
1459 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
1460 mboolset(wlc_hw->wake_override, override_bit);
1461 return;
1462 }
1463
1464 mboolset(wlc_hw->wake_override, override_bit);
1465
1466 brcms_c_mctrl_write(wlc_hw);
1467 brcms_b_wait_for_wake(wlc_hw);
1468}
1469
1470void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
1471 u32 override_bit)
1472{
1473 mboolclr(wlc_hw->wake_override, override_bit);
1474
1475 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
1476 return;
1477
1478 brcms_c_mctrl_write(wlc_hw);
1479}
1480
1481/* When driver needs ucode to stop beaconing, it has to make sure that
1482 * MCTL_AP is clear and MCTL_INFRA is set
1483 * Mode MCTL_AP MCTL_INFRA
1484 * AP 1 1
1485 * STA 0 1 <--- This will ensure no beacons
1486 * IBSS 0 0
1487 */
1488static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
1489{
1490 wlc_hw->mute_override = 1;
1491
1492 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1493 * override, then there is no change to write
1494 */
1495 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1496 return;
1497
1498 brcms_c_mctrl_write(wlc_hw);
1499}
1500
1501/* Clear the override on AP and INFRA bits */
1502static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
1503{
1504 if (wlc_hw->mute_override == 0)
1505 return;
1506
1507 wlc_hw->mute_override = 0;
1508
1509 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1510 * override, then there is no change to write
1511 */
1512 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1513 return;
1514
1515 brcms_c_mctrl_write(wlc_hw);
1516}
1517
1518/*
1519 * Write a MAC address to the given match reg offset in the RXE match engine.
1520 */
1521static void
1522brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
1523 const u8 *addr)
1524{
Arend van Spriel16d28122011-12-08 15:06:51 -08001525 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 u16 mac_l;
1527 u16 mac_m;
1528 u16 mac_h;
1529
1530 BCMMSG(wlc_hw->wlc->wiphy, "wl%d: brcms_b_set_addrmatch\n",
1531 wlc_hw->unit);
1532
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533 mac_l = addr[0] | (addr[1] << 8);
1534 mac_m = addr[2] | (addr[3] << 8);
1535 mac_h = addr[4] | (addr[5] << 8);
1536
1537 /* enter the MAC addr into the RXE match registers */
Arend van Spriel16d28122011-12-08 15:06:51 -08001538 bcma_write16(core, D11REGOFFS(rcm_ctl),
1539 RCM_INC_DATA | match_reg_offset);
1540 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
1541 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
1542 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001543}
1544
1545void
1546brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
1547 void *buf)
1548{
Arend van Spriel16d28122011-12-08 15:06:51 -08001549 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550 u32 word;
1551 __le32 word_le;
1552 __be32 word_be;
1553 bool be_bit;
Seth Forsheeb353dda2012-11-15 08:08:03 -06001554 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001555
Arend van Spriel16d28122011-12-08 15:06:51 -08001556 bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001557
1558 /* if MCTL_BIGEND bit set in mac control register,
1559 * the chip swaps data in fifo, as well as data in
1560 * template ram
1561 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001562 be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001563
1564 while (len > 0) {
1565 memcpy(&word, buf, sizeof(u32));
1566
1567 if (be_bit) {
1568 word_be = cpu_to_be32(word);
1569 word = *(u32 *)&word_be;
1570 } else {
1571 word_le = cpu_to_le32(word);
1572 word = *(u32 *)&word_le;
1573 }
1574
Arend van Spriel16d28122011-12-08 15:06:51 -08001575 bcma_write32(core, D11REGOFFS(tplatewrdata), word);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001576
1577 buf = (u8 *) buf + sizeof(u32);
1578 len -= sizeof(u32);
1579 }
1580}
1581
1582static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
1583{
1584 wlc_hw->band->CWmin = newmin;
1585
Arend van Spriel16d28122011-12-08 15:06:51 -08001586 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1587 OBJADDR_SCR_SEL | S_DOT11_CWMIN);
1588 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1589 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001590}
1591
1592static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
1593{
1594 wlc_hw->band->CWmax = newmax;
1595
Arend van Spriel16d28122011-12-08 15:06:51 -08001596 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1597 OBJADDR_SCR_SEL | S_DOT11_CWMAX);
1598 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1599 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001600}
1601
1602void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
1603{
1604 bool fastclk;
1605
1606 /* request FAST clock if not on */
1607 fastclk = wlc_hw->forcefastclk;
1608 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001609 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001610
1611 wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
1612
1613 brcms_b_phy_reset(wlc_hw);
1614 wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
1615
1616 /* restore the clk */
1617 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001618 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001619}
1620
1621static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
1622{
1623 u16 v;
1624 struct brcms_c_info *wlc = wlc_hw->wlc;
1625 /* update SYNTHPU_DLY */
1626
1627 if (BRCMS_ISLCNPHY(wlc->band))
1628 v = SYNTHPU_DLY_LPPHY_US;
1629 else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
1630 v = SYNTHPU_DLY_NPHY_US;
1631 else
1632 v = SYNTHPU_DLY_BPHY_US;
1633
1634 brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
1635}
1636
1637static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
1638{
1639 u16 phyctl;
1640 u16 phytxant = wlc_hw->bmac_phytxant;
1641 u16 mask = PHY_TXC_ANT_MASK;
1642
1643 /* set the Probe Response frame phy control word */
1644 phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
1645 phyctl = (phyctl & ~mask) | phytxant;
1646 brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
1647
1648 /* set the Response (ACK/CTS) frame phy control word */
1649 phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
1650 phyctl = (phyctl & ~mask) | phytxant;
1651 brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
1652}
1653
1654static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
1655 u8 rate)
1656{
1657 uint i;
1658 u8 plcp_rate = 0;
1659 struct plcp_signal_rate_lookup {
1660 u8 rate;
1661 u8 signal_rate;
1662 };
1663 /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
1664 const struct plcp_signal_rate_lookup rate_lookup[] = {
1665 {BRCM_RATE_6M, 0xB},
1666 {BRCM_RATE_9M, 0xF},
1667 {BRCM_RATE_12M, 0xA},
1668 {BRCM_RATE_18M, 0xE},
1669 {BRCM_RATE_24M, 0x9},
1670 {BRCM_RATE_36M, 0xD},
1671 {BRCM_RATE_48M, 0x8},
1672 {BRCM_RATE_54M, 0xC}
1673 };
1674
1675 for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
1676 if (rate == rate_lookup[i].rate) {
1677 plcp_rate = rate_lookup[i].signal_rate;
1678 break;
1679 }
1680 }
1681
1682 /* Find the SHM pointer to the rate table entry by looking in the
1683 * Direct-map Table
1684 */
1685 return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
1686}
1687
1688static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
1689{
1690 u8 rate;
1691 u8 rates[8] = {
1692 BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
1693 BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
1694 };
1695 u16 entry_ptr;
1696 u16 pctl1;
1697 uint i;
1698
1699 if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
1700 return;
1701
1702 /* walk the phy rate table and update the entries */
1703 for (i = 0; i < ARRAY_SIZE(rates); i++) {
1704 rate = rates[i];
1705
1706 entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
1707
1708 /* read the SHM Rate Table entry OFDM PCTL1 values */
1709 pctl1 =
1710 brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
1711
1712 /* modify the value */
1713 pctl1 &= ~PHY_TXC1_MODE_MASK;
1714 pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
1715
1716 /* Update the SHM Rate Table entry OFDM PCTL1 values */
1717 brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
1718 pctl1);
1719 }
1720}
1721
1722/* band-specific init */
1723static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
1724{
1725 struct brcms_hardware *wlc_hw = wlc->hw;
1726
1727 BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
1728 wlc_hw->band->bandunit);
1729
1730 brcms_c_ucode_bsinit(wlc_hw);
1731
1732 wlc_phy_init(wlc_hw->band->pi, chanspec);
1733
1734 brcms_c_ucode_txant_set(wlc_hw);
1735
1736 /*
1737 * cwmin is band-specific, update hardware
1738 * with value for current band
1739 */
1740 brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
1741 brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
1742
1743 brcms_b_update_slot_timing(wlc_hw,
1744 wlc_hw->band->bandtype == BRCM_BAND_5G ?
1745 true : wlc_hw->shortslot);
1746
1747 /* write phytype and phyvers */
1748 brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
1749 brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
1750
1751 /*
1752 * initialize the txphyctl1 rate table since
1753 * shmem is shared between bands
1754 */
1755 brcms_upd_ofdm_pctl1_table(wlc_hw);
1756
1757 brcms_b_upd_synthpu(wlc_hw);
1758}
1759
1760/* Perform a soft reset of the PHY PLL */
1761void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
1762{
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001763 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
1764 ~0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001765 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001766 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1767 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001768 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001769 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1770 0x4, 4);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001771 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001772 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1773 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001774 udelay(1);
1775}
1776
1777/* light way to turn on phy clock without reset for NPHY only
1778 * refer to brcms_b_core_phy_clk for full version
1779 */
1780void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
1781{
1782 /* support(necessary for NPHY and HYPHY) only */
1783 if (!BRCMS_ISNPHY(wlc_hw->band))
1784 return;
1785
1786 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001787 brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001788 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001789 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001790
1791}
1792
1793void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
1794{
1795 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001796 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001797 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001798 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001799}
1800
1801void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
1802{
1803 struct brcms_phy_pub *pih = wlc_hw->band->pi;
1804 u32 phy_bw_clkbits;
1805 bool phy_in_reset = false;
1806
Seth Forsheeb353dda2012-11-15 08:08:03 -06001807 brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808
1809 if (pih == NULL)
1810 return;
1811
1812 phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
1813
1814 /* Specific reset sequence required for NPHY rev 3 and 4 */
1815 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
1816 NREV_LE(wlc_hw->band->phyrev, 4)) {
1817 /* Set the PHY bandwidth */
Arend van Spriela8779e42011-12-08 15:06:58 -08001818 brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001819
1820 udelay(1);
1821
1822 /* Perform a soft reset of the PHY PLL */
1823 brcms_b_core_phypll_reset(wlc_hw);
1824
1825 /* reset the PHY */
Arend van Spriela8779e42011-12-08 15:06:58 -08001826 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
1827 (SICF_PRST | SICF_PCLKE));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 phy_in_reset = true;
1829 } else {
Arend van Spriela8779e42011-12-08 15:06:58 -08001830 brcms_b_core_ioctl(wlc_hw,
1831 (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
1832 (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001833 }
1834
1835 udelay(2);
1836 brcms_b_core_phy_clk(wlc_hw, ON);
1837
1838 if (pih)
1839 wlc_phy_anacore(pih, ON);
1840}
1841
1842/* switch to and initialize new band */
1843static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
1844 u16 chanspec) {
1845 struct brcms_c_info *wlc = wlc_hw->wlc;
1846 u32 macintmask;
1847
1848 /* Enable the d11 core before accessing it */
Arend van Spriela8779e42011-12-08 15:06:58 -08001849 if (!bcma_core_is_enabled(wlc_hw->d11core)) {
1850 bcma_core_enable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001851 brcms_c_mctrl_reset(wlc_hw);
1852 }
1853
1854 macintmask = brcms_c_setband_inact(wlc, bandunit);
1855
1856 if (!wlc_hw->up)
1857 return;
1858
1859 brcms_b_core_phy_clk(wlc_hw, ON);
1860
1861 /* band-specific initializations */
1862 brcms_b_bsinit(wlc, chanspec);
1863
1864 /*
1865 * If there are any pending software interrupt bits,
1866 * then replace these with a harmless nonzero value
1867 * so brcms_c_dpc() will re-enable interrupts when done.
1868 */
1869 if (wlc->macintstatus)
1870 wlc->macintstatus = MI_DMAINT;
1871
1872 /* restore macintmask */
1873 brcms_intrsrestore(wlc->wl, macintmask);
1874
1875 /* ucode should still be suspended.. */
Arend van Spriel16d28122011-12-08 15:06:51 -08001876 WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
1877 MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001878}
1879
Arend van Spriel5b435de2011-10-05 13:19:03 +02001880static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
1881{
1882
1883 /* reject unsupported corerev */
1884 if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
1885 wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
1886 wlc_hw->corerev);
1887 return false;
1888 }
1889
1890 return true;
1891}
1892
1893/* Validate some board info parameters */
1894static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
1895{
1896 uint boardrev = wlc_hw->boardrev;
1897
1898 /* 4 bits each for board type, major, minor, and tiny version */
1899 uint brt = (boardrev & 0xf000) >> 12;
1900 uint b0 = (boardrev & 0xf00) >> 8;
1901 uint b1 = (boardrev & 0xf0) >> 4;
1902 uint b2 = boardrev & 0xf;
1903
1904 /* voards from other vendors are always considered valid */
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001905 if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001906 return true;
1907
1908 /* do some boardrev sanity checks when boardvendor is Broadcom */
1909 if (boardrev == 0)
1910 return false;
1911
1912 if (boardrev <= 0xff)
1913 return true;
1914
1915 if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
1916 || (b2 > 9))
1917 return false;
1918
1919 return true;
1920}
1921
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001922static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
Arend van Spriel5b435de2011-10-05 13:19:03 +02001923{
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001924 struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001925
1926 /* If macaddr exists, use it (Sromrev4, CIS, ...). */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001927 if (!is_zero_ether_addr(sprom->il0mac)) {
1928 memcpy(etheraddr, sprom->il0mac, 6);
1929 return;
1930 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001931
1932 if (wlc_hw->_nbands > 1)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001933 memcpy(etheraddr, sprom->et1mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001934 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001935 memcpy(etheraddr, sprom->il0mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001936}
1937
1938/* power both the pll and external oscillator on/off */
1939static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
1940{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001941 brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001942
1943 /*
1944 * dont power down if plldown is false or
1945 * we must poll hw radio disable
1946 */
1947 if (!want && wlc_hw->pllreq)
1948 return;
1949
Arend van Spriel5b435de2011-10-05 13:19:03 +02001950 wlc_hw->sbclk = want;
1951 if (!wlc_hw->sbclk) {
1952 wlc_hw->clk = false;
1953 if (wlc_hw->band && wlc_hw->band->pi)
1954 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
1955 }
1956}
1957
1958/*
1959 * Return true if radio is disabled, otherwise false.
1960 * hw radio disable signal is an external pin, users activate it asynchronously
1961 * this function could be called when driver is down and w/o clock
1962 * it operates on different registers depending on corerev and boardflag.
1963 */
1964static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
1965{
1966 bool v, clk, xtal;
Arend van Spriela8779e42011-12-08 15:06:58 -08001967 u32 flags = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001968
1969 xtal = wlc_hw->sbclk;
1970 if (!xtal)
1971 brcms_b_xtal(wlc_hw, ON);
1972
1973 /* may need to take core out of reset first */
1974 clk = wlc_hw->clk;
1975 if (!clk) {
1976 /*
1977 * mac no longer enables phyclk automatically when driver
1978 * accesses phyreg throughput mac. This can be skipped since
1979 * only mac reg is accessed below
1980 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02001981 if (D11REV_GE(wlc_hw->corerev, 18))
1982 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001983
1984 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08001985 * TODO: test suspend/resume
1986 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02001987 * AI chip doesn't restore bar0win2 on
1988 * hibernation/resume, need sw fixup
1989 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001990
Arend van Spriela8779e42011-12-08 15:06:58 -08001991 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001992 brcms_c_mctrl_reset(wlc_hw);
1993 }
1994
Arend van Spriel16d28122011-12-08 15:06:51 -08001995 v = ((bcma_read32(wlc_hw->d11core,
1996 D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001997
1998 /* put core back into reset */
1999 if (!clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08002000 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002001
2002 if (!xtal)
2003 brcms_b_xtal(wlc_hw, OFF);
2004
2005 return v;
2006}
2007
2008static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
2009{
2010 struct dma_pub *di = wlc_hw->di[fifo];
2011 return dma_rxreset(di);
2012}
2013
2014/* d11 core reset
2015 * ensure fask clock during reset
2016 * reset dma
2017 * reset d11(out of reset)
2018 * reset phy(out of reset)
2019 * clear software macintstatus for fresh new start
2020 * one testing hack wlc_hw->noreset will bypass the d11/phy reset
2021 */
2022void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
2023{
Arend van Spriel5b435de2011-10-05 13:19:03 +02002024 uint i;
2025 bool fastclk;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002026
2027 if (flags == BRCMS_USE_COREFLAGS)
2028 flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
2029
Seth Forsheeb353dda2012-11-15 08:08:03 -06002030 brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002031
Arend van Spriel5b435de2011-10-05 13:19:03 +02002032 /* request FAST clock if not on */
2033 fastclk = wlc_hw->forcefastclk;
2034 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002035 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002036
2037 /* reset the dma engines except first time thru */
Arend van Spriela8779e42011-12-08 15:06:58 -08002038 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002039 for (i = 0; i < NFIFO; i++)
2040 if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002041 brcms_err(wlc_hw->d11core, "wl%d: %s: "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002042 "dma_txreset[%d]: cannot stop dma\n",
2043 wlc_hw->unit, __func__, i);
2044
2045 if ((wlc_hw->di[RX_FIFO])
2046 && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002047 brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002048 "[%d]: cannot stop dma\n",
2049 wlc_hw->unit, __func__, RX_FIFO);
2050 }
2051 /* if noreset, just stop the psm and return */
2052 if (wlc_hw->noreset) {
2053 wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
2054 brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
2055 return;
2056 }
2057
2058 /*
2059 * mac no longer enables phyclk automatically when driver accesses
2060 * phyreg throughput mac, AND phy_reset is skipped at early stage when
2061 * band->pi is invalid. need to enable PHY CLK
2062 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02002063 if (D11REV_GE(wlc_hw->corerev, 18))
2064 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002065
2066 /*
2067 * reset the core
2068 * In chips with PMU, the fastclk request goes through d11 core
2069 * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
2070 *
2071 * This adds some delay and we can optimize it by also requesting
2072 * fastclk through chipcommon during this period if necessary. But
2073 * that has to work coordinate with other driver like mips/arm since
2074 * they may touch chipcommon as well.
2075 */
2076 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002077 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002078 wlc_hw->clk = true;
2079 if (wlc_hw->band && wlc_hw->band->pi)
2080 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
2081
2082 brcms_c_mctrl_reset(wlc_hw);
2083
Arend van Sprielb2ffec42011-12-08 15:06:45 -08002084 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002085 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002086
2087 brcms_b_phy_reset(wlc_hw);
2088
2089 /* turn on PHY_PLL */
2090 brcms_b_core_phypll_ctl(wlc_hw, true);
2091
2092 /* clear sw intstatus */
2093 wlc_hw->wlc->macintstatus = 0;
2094
2095 /* restore the clk setting */
2096 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002097 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002098}
2099
2100/* txfifo sizes needs to be modified(increased) since the newer cores
2101 * have more memory.
2102 */
2103static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
2104{
Arend van Spriel16d28122011-12-08 15:06:51 -08002105 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002106 u16 fifo_nu;
2107 u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
2108 u16 txfifo_def, txfifo_def1;
2109 u16 txfifo_cmd;
2110
2111 /* tx fifos start at TXFIFO_START_BLK from the Base address */
2112 txfifo_startblk = TXFIFO_START_BLK;
2113
2114 /* sequence of operations: reset fifo, set fifo size, reset fifo */
2115 for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
2116
2117 txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
2118 txfifo_def = (txfifo_startblk & 0xff) |
2119 (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
2120 txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
2121 ((((txfifo_endblk -
2122 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
2123 txfifo_cmd =
2124 TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
2125
Arend van Spriel16d28122011-12-08 15:06:51 -08002126 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
2127 bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
2128 bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002129
Arend van Spriel16d28122011-12-08 15:06:51 -08002130 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002131
2132 txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
2133 }
2134 /*
2135 * need to propagate to shm location to be in sync since ucode/hw won't
2136 * do this
2137 */
2138 brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
2139 wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
2140 brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
2141 wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
2142 brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
2143 ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
2144 xmtfifo_sz[TX_AC_BK_FIFO]));
2145 brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
2146 ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
2147 xmtfifo_sz[TX_BCMC_FIFO]));
2148}
2149
2150/* This function is used for changing the tsf frac register
2151 * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
2152 * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
2153 * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
2154 * HTPHY Formula is 2^26/freq(MHz) e.g.
2155 * For spuron2 - 126MHz -> 2^26/126 = 532610.0
2156 * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
2157 * For spuron: 123MHz -> 2^26/123 = 545600.5
2158 * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
2159 * For spur off: 120MHz -> 2^26/120 = 559240.5
2160 * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
2161 */
2162
2163void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
2164{
Arend van Spriel16d28122011-12-08 15:06:51 -08002165 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002166
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002167 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
2168 (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002169 if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002170 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
2171 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002172 } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002173 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
2174 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002175 } else { /* 120Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002176 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
2177 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002178 }
2179 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2180 if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002181 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
2182 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002183 } else { /* 80Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002184 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
2185 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002186 }
2187 }
2188}
2189
2190/* Initialize GPIOs that are controlled by D11 core */
2191static void brcms_c_gpio_init(struct brcms_c_info *wlc)
2192{
2193 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002194 u32 gc, gm;
2195
Arend van Spriel5b435de2011-10-05 13:19:03 +02002196 /* use GPIO select 0 to get all gpio signals from the gpio out reg */
2197 brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
2198
2199 /*
2200 * Common GPIO setup:
2201 * G0 = LED 0 = WLAN Activity
2202 * G1 = LED 1 = WLAN 2.4 GHz Radio State
2203 * G2 = LED 2 = WLAN 5 GHz Radio State
2204 * G4 = radio disable input (HI enabled, LO disabled)
2205 */
2206
2207 gc = gm = 0;
2208
2209 /* Allocate GPIOs for mimo antenna diversity feature */
2210 if (wlc_hw->antsel_type == ANTSEL_2x3) {
2211 /* Enable antenna diversity, use 2x3 mode */
2212 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2213 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2214 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
2215 MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
2216
2217 /* init superswitch control */
2218 wlc_phy_antsel_init(wlc_hw->band->pi, false);
2219
2220 } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
2221 gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
2222 /*
2223 * The board itself is powered by these GPIOs
2224 * (when not sending pattern) so set them high
2225 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002226 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
2227 (BOARD_GPIO_12 | BOARD_GPIO_13));
2228 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
2229 (BOARD_GPIO_12 | BOARD_GPIO_13));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002230
2231 /* Enable antenna diversity, use 2x4 mode */
2232 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2233 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2234 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
2235 BRCM_BAND_ALL);
2236
2237 /* Configure the desired clock to be 4Mhz */
2238 brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
2239 ANTSEL_CLKDIV_4MHZ);
2240 }
2241
2242 /*
2243 * gpio 9 controls the PA. ucode is responsible
2244 * for wiggling out and oe
2245 */
2246 if (wlc_hw->boardflags & BFL_PACTRL)
2247 gm |= gc |= BOARD_GPIO_PACTRL;
2248
2249 /* apply to gpiocontrol register */
Hauke Mehrtensfa0b8232012-04-29 02:50:34 +02002250 bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002251}
2252
2253static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
2254 const __le32 ucode[], const size_t nbytes)
2255{
Arend van Spriel16d28122011-12-08 15:06:51 -08002256 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002257 uint i;
2258 uint count;
2259
Seth Forsheeb353dda2012-11-15 08:08:03 -06002260 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002261
2262 count = (nbytes / sizeof(u32));
2263
Arend van Spriel16d28122011-12-08 15:06:51 -08002264 bcma_write32(core, D11REGOFFS(objaddr),
2265 OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
2266 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002267 for (i = 0; i < count; i++)
Arend van Spriel16d28122011-12-08 15:06:51 -08002268 bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002269
2270}
2271
2272static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
2273{
2274 struct brcms_c_info *wlc;
2275 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
2276
2277 wlc = wlc_hw->wlc;
2278
2279 if (wlc_hw->ucode_loaded)
2280 return;
2281
2282 if (D11REV_IS(wlc_hw->corerev, 23)) {
2283 if (BRCMS_ISNPHY(wlc_hw->band)) {
2284 brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
2285 ucode->bcm43xx_16_mimosz);
2286 wlc_hw->ucode_loaded = true;
2287 } else
Seth Forsheeb353dda2012-11-15 08:08:03 -06002288 brcms_err(wlc_hw->d11core,
2289 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002290 __func__, wlc_hw->unit, wlc_hw->corerev);
2291 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
2292 if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2293 brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
2294 ucode->bcm43xx_24_lcnsz);
2295 wlc_hw->ucode_loaded = true;
2296 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002297 brcms_err(wlc_hw->d11core,
2298 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002299 __func__, wlc_hw->unit, wlc_hw->corerev);
2300 }
2301 }
2302}
2303
2304void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
2305{
2306 /* update sw state */
2307 wlc_hw->bmac_phytxant = phytxant;
2308
2309 /* push to ucode if up */
2310 if (!wlc_hw->up)
2311 return;
2312 brcms_c_ucode_txant_set(wlc_hw);
2313
2314}
2315
2316u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
2317{
2318 return (u16) wlc_hw->wlc->stf->txant;
2319}
2320
2321void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
2322{
2323 wlc_hw->antsel_type = antsel_type;
2324
2325 /* Update the antsel type for phy module to use */
2326 wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
2327}
2328
2329static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
2330{
2331 bool fatal = false;
2332 uint unit;
2333 uint intstatus, idx;
Arend van Spriel16d28122011-12-08 15:06:51 -08002334 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002335
2336 unit = wlc_hw->unit;
2337
2338 for (idx = 0; idx < NFIFO; idx++) {
2339 /* read intstatus register and ignore any non-error bits */
2340 intstatus =
Arend van Spriel16d28122011-12-08 15:06:51 -08002341 bcma_read32(core,
2342 D11REGOFFS(intctrlregs[idx].intstatus)) &
2343 I_ERRORS;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002344 if (!intstatus)
2345 continue;
2346
2347 BCMMSG(wlc_hw->wlc->wiphy, "wl%d: intstatus%d 0x%x\n",
2348 unit, idx, intstatus);
2349
2350 if (intstatus & I_RO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002351 brcms_err(core, "wl%d: fifo %d: receive fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002352 "overflow\n", unit, idx);
2353 fatal = true;
2354 }
2355
2356 if (intstatus & I_PC) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002357 brcms_err(core, "wl%d: fifo %d: descriptor error\n",
2358 unit, idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002359 fatal = true;
2360 }
2361
2362 if (intstatus & I_PD) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002363 brcms_err(core, "wl%d: fifo %d: data error\n", unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002364 idx);
2365 fatal = true;
2366 }
2367
2368 if (intstatus & I_DE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002369 brcms_err(core, "wl%d: fifo %d: descriptor protocol "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002370 "error\n", unit, idx);
2371 fatal = true;
2372 }
2373
2374 if (intstatus & I_RU)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002375 brcms_err(core, "wl%d: fifo %d: receive descriptor "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002376 "underflow\n", idx, unit);
2377
2378 if (intstatus & I_XU) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002379 brcms_err(core, "wl%d: fifo %d: transmit fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002380 "underflow\n", idx, unit);
2381 fatal = true;
2382 }
2383
2384 if (fatal) {
Roland Vossenc261bdf2011-10-18 14:03:04 +02002385 brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002386 break;
2387 } else
Arend van Spriel16d28122011-12-08 15:06:51 -08002388 bcma_write32(core,
2389 D11REGOFFS(intctrlregs[idx].intstatus),
2390 intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002391 }
2392}
2393
2394void brcms_c_intrson(struct brcms_c_info *wlc)
2395{
2396 struct brcms_hardware *wlc_hw = wlc->hw;
2397 wlc->macintmask = wlc->defmacintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002398 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002399}
2400
Arend van Spriel5b435de2011-10-05 13:19:03 +02002401u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
2402{
2403 struct brcms_hardware *wlc_hw = wlc->hw;
2404 u32 macintmask;
2405
2406 if (!wlc_hw->clk)
2407 return 0;
2408
2409 macintmask = wlc->macintmask; /* isr can still happen */
2410
Arend van Spriel16d28122011-12-08 15:06:51 -08002411 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
2412 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002413 udelay(1); /* ensure int line is no longer driven */
2414 wlc->macintmask = 0;
2415
2416 /* return previous macintmask; resolve race between us and our isr */
2417 return wlc->macintstatus ? 0 : macintmask;
2418}
2419
2420void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2421{
2422 struct brcms_hardware *wlc_hw = wlc->hw;
2423 if (!wlc_hw->clk)
2424 return;
2425
2426 wlc->macintmask = macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002427 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002428}
2429
Roland Vossendc460122011-10-21 16:16:28 +02002430/* assumes that the d11 MAC is enabled */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002431static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
2432 uint tx_fifo)
2433{
2434 u8 fifo = 1 << tx_fifo;
2435
2436 /* Two clients of this code, 11h Quiet period and scanning. */
2437
2438 /* only suspend if not already suspended */
2439 if ((wlc_hw->suspended_fifos & fifo) == fifo)
2440 return;
2441
2442 /* force the core awake only if not already */
2443 if (wlc_hw->suspended_fifos == 0)
2444 brcms_c_ucode_wake_override_set(wlc_hw,
2445 BRCMS_WAKE_OVERRIDE_TXFIFO);
2446
2447 wlc_hw->suspended_fifos |= fifo;
2448
2449 if (wlc_hw->di[tx_fifo]) {
2450 /*
2451 * Suspending AMPDU transmissions in the middle can cause
2452 * underflow which may result in mismatch between ucode and
2453 * driver so suspend the mac before suspending the FIFO
2454 */
2455 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2456 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
2457
2458 dma_txsuspend(wlc_hw->di[tx_fifo]);
2459
2460 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2461 brcms_c_enable_mac(wlc_hw->wlc);
2462 }
2463}
2464
2465static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
2466 uint tx_fifo)
2467{
2468 /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
2469 * but need to be done here for PIO otherwise the watchdog will catch
2470 * the inconsistency and fire
2471 */
2472 /* Two clients of this code, 11h Quiet period and scanning. */
2473 if (wlc_hw->di[tx_fifo])
2474 dma_txresume(wlc_hw->di[tx_fifo]);
2475
2476 /* allow core to sleep again */
2477 if (wlc_hw->suspended_fifos == 0)
2478 return;
2479 else {
2480 wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
2481 if (wlc_hw->suspended_fifos == 0)
2482 brcms_c_ucode_wake_override_clear(wlc_hw,
2483 BRCMS_WAKE_OVERRIDE_TXFIFO);
2484 }
2485}
2486
Roland Vossena8bc4912011-10-21 16:16:25 +02002487/* precondition: requires the mac core to be enabled */
Roland Vossenc6c44892011-10-21 16:16:26 +02002488static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002489{
2490 static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
2491
Roland Vossenc6c44892011-10-21 16:16:26 +02002492 if (mute_tx) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002493 /* suspend tx fifos */
2494 brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
2495 brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
2496 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
2497 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
2498
2499 /* zero the address match register so we do not send ACKs */
2500 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
2501 null_ether_addr);
2502 } else {
2503 /* resume tx fifos */
2504 brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
2505 brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
2506 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
2507 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
2508
2509 /* Restore address */
2510 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
2511 wlc_hw->etheraddr);
2512 }
2513
Roland Vossenc6c44892011-10-21 16:16:26 +02002514 wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002515
Roland Vossenc6c44892011-10-21 16:16:26 +02002516 if (mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002517 brcms_c_ucode_mute_override_set(wlc_hw);
2518 else
2519 brcms_c_ucode_mute_override_clear(wlc_hw);
2520}
2521
Roland Vossendc460122011-10-21 16:16:28 +02002522void
2523brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
2524{
2525 brcms_b_mute(wlc->hw, mute_tx);
2526}
2527
Arend van Spriel5b435de2011-10-05 13:19:03 +02002528/*
2529 * Read and clear macintmask and macintstatus and intstatus registers.
2530 * This routine should be called with interrupts off
2531 * Return:
2532 * -1 if brcms_deviceremoved(wlc) evaluates to true;
2533 * 0 if the interrupt is not for us, or we are in some special cases;
2534 * device interrupt status bits otherwise.
2535 */
2536static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
2537{
2538 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002539 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002540 u32 macintstatus;
2541
2542 /* macintstatus includes a DMA interrupt summary bit */
Arend van Spriel16d28122011-12-08 15:06:51 -08002543 macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002544
2545 BCMMSG(wlc->wiphy, "wl%d: macintstatus: 0x%x\n", wlc_hw->unit,
2546 macintstatus);
2547
2548 /* detect cardbus removed, in power down(suspend) and in reset */
2549 if (brcms_deviceremoved(wlc))
2550 return -1;
2551
2552 /* brcms_deviceremoved() succeeds even when the core is still resetting,
2553 * handle that case here.
2554 */
2555 if (macintstatus == 0xffffffff)
2556 return 0;
2557
2558 /* defer unsolicited interrupts */
2559 macintstatus &= (in_isr ? wlc->macintmask : wlc->defmacintmask);
2560
2561 /* if not for us */
2562 if (macintstatus == 0)
2563 return 0;
2564
2565 /* interrupts are already turned off for CFE build
2566 * Caution: For CFE Turning off the interrupts again has some undesired
2567 * consequences
2568 */
2569 /* turn off the interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002570 bcma_write32(core, D11REGOFFS(macintmask), 0);
2571 (void)bcma_read32(core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002572 wlc->macintmask = 0;
2573
2574 /* clear device interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002575 bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002576
2577 /* MI_DMAINT is indication of non-zero intstatus */
2578 if (macintstatus & MI_DMAINT)
2579 /*
2580 * only fifo interrupt enabled is I_RI in
2581 * RX_FIFO. If MI_DMAINT is set, assume it
2582 * is set and clear the interrupt.
2583 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002584 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
2585 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002586
2587 return macintstatus;
2588}
2589
2590/* Update wlc->macintstatus and wlc->intstatus[]. */
2591/* Return true if they are updated successfully. false otherwise */
2592bool brcms_c_intrsupd(struct brcms_c_info *wlc)
2593{
2594 u32 macintstatus;
2595
2596 /* read and clear macintstatus and intstatus registers */
2597 macintstatus = wlc_intstatus(wlc, false);
2598
2599 /* device is removed */
2600 if (macintstatus == 0xffffffff)
2601 return false;
2602
2603 /* update interrupt status in software */
2604 wlc->macintstatus |= macintstatus;
2605
2606 return true;
2607}
2608
2609/*
2610 * First-level interrupt processing.
2611 * Return true if this was our interrupt, false otherwise.
2612 * *wantdpc will be set to true if further brcms_c_dpc() processing is required,
2613 * false otherwise.
2614 */
2615bool brcms_c_isr(struct brcms_c_info *wlc, bool *wantdpc)
2616{
2617 struct brcms_hardware *wlc_hw = wlc->hw;
2618 u32 macintstatus;
2619
2620 *wantdpc = false;
2621
2622 if (!wlc_hw->up || !wlc->macintmask)
2623 return false;
2624
2625 /* read and clear macintstatus and intstatus registers */
2626 macintstatus = wlc_intstatus(wlc, true);
2627
2628 if (macintstatus == 0xffffffff)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002629 brcms_err(wlc_hw->d11core,
2630 "DEVICEREMOVED detected in the ISR code path\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002631
2632 /* it is not for us */
2633 if (macintstatus == 0)
2634 return false;
2635
2636 *wantdpc = true;
2637
2638 /* save interrupt status bits */
2639 wlc->macintstatus = macintstatus;
2640
2641 return true;
2642
2643}
2644
2645void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
2646{
2647 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002648 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002649 u32 mc, mi;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002650
2651 BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
2652 wlc_hw->band->bandunit);
2653
2654 /*
2655 * Track overlapping suspend requests
2656 */
2657 wlc_hw->mac_suspend_depth++;
2658 if (wlc_hw->mac_suspend_depth > 1)
2659 return;
2660
2661 /* force the core awake */
2662 brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2663
Arend van Spriel16d28122011-12-08 15:06:51 -08002664 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002665
2666 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002667 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002668 __func__);
2669 brcms_down(wlc->wl);
2670 return;
2671 }
2672 WARN_ON(mc & MCTL_PSM_JMP_0);
2673 WARN_ON(!(mc & MCTL_PSM_RUN));
2674 WARN_ON(!(mc & MCTL_EN_MAC));
2675
Arend van Spriel16d28122011-12-08 15:06:51 -08002676 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002677 if (mi == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002678 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002679 __func__);
2680 brcms_down(wlc->wl);
2681 return;
2682 }
2683 WARN_ON(mi & MI_MACSSPNDD);
2684
2685 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
2686
Arend van Spriel16d28122011-12-08 15:06:51 -08002687 SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
Arend van Spriel5b435de2011-10-05 13:19:03 +02002688 BRCMS_MAX_MAC_SUSPEND);
2689
Arend van Spriel16d28122011-12-08 15:06:51 -08002690 if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002691 brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002692 " and MI_MACSSPNDD is still not on.\n",
2693 wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
Seth Forsheeb353dda2012-11-15 08:08:03 -06002694 brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002695 "psm_brc 0x%04x\n", wlc_hw->unit,
Arend van Spriel16d28122011-12-08 15:06:51 -08002696 bcma_read32(core, D11REGOFFS(psmdebug)),
2697 bcma_read32(core, D11REGOFFS(phydebug)),
2698 bcma_read16(core, D11REGOFFS(psm_brc)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002699 }
2700
Arend van Spriel16d28122011-12-08 15:06:51 -08002701 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002702 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002703 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002704 __func__);
2705 brcms_down(wlc->wl);
2706 return;
2707 }
2708 WARN_ON(mc & MCTL_PSM_JMP_0);
2709 WARN_ON(!(mc & MCTL_PSM_RUN));
2710 WARN_ON(mc & MCTL_EN_MAC);
2711}
2712
2713void brcms_c_enable_mac(struct brcms_c_info *wlc)
2714{
2715 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002716 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002717 u32 mc, mi;
2718
2719 BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
2720 wlc->band->bandunit);
2721
2722 /*
2723 * Track overlapping suspend requests
2724 */
2725 wlc_hw->mac_suspend_depth--;
2726 if (wlc_hw->mac_suspend_depth > 0)
2727 return;
2728
Arend van Spriel16d28122011-12-08 15:06:51 -08002729 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002730 WARN_ON(mc & MCTL_PSM_JMP_0);
2731 WARN_ON(mc & MCTL_EN_MAC);
2732 WARN_ON(!(mc & MCTL_PSM_RUN));
2733
2734 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
Arend van Spriel16d28122011-12-08 15:06:51 -08002735 bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002736
Arend van Spriel16d28122011-12-08 15:06:51 -08002737 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002738 WARN_ON(mc & MCTL_PSM_JMP_0);
2739 WARN_ON(!(mc & MCTL_EN_MAC));
2740 WARN_ON(!(mc & MCTL_PSM_RUN));
2741
Arend van Spriel16d28122011-12-08 15:06:51 -08002742 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002743 WARN_ON(mi & MI_MACSSPNDD);
2744
2745 brcms_c_ucode_wake_override_clear(wlc_hw,
2746 BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2747}
2748
2749void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
2750{
2751 wlc_hw->hw_stf_ss_opmode = stf_mode;
2752
2753 if (wlc_hw->clk)
2754 brcms_upd_ofdm_pctl1_table(wlc_hw);
2755}
2756
2757static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
2758{
Arend van Spriel16d28122011-12-08 15:06:51 -08002759 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002760 u32 w, val;
2761 struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2762
Arend van Spriel5b435de2011-10-05 13:19:03 +02002763 /* Validate dchip register access */
2764
Arend van Spriel16d28122011-12-08 15:06:51 -08002765 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2766 (void)bcma_read32(core, D11REGOFFS(objaddr));
2767 w = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002768
2769 /* Can we write and read back a 32bit register? */
Arend van Spriel16d28122011-12-08 15:06:51 -08002770 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2771 (void)bcma_read32(core, D11REGOFFS(objaddr));
2772 bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002773
Arend van Spriel16d28122011-12-08 15:06:51 -08002774 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2775 (void)bcma_read32(core, D11REGOFFS(objaddr));
2776 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002777 if (val != (u32) 0xaa5555aa) {
2778 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2779 "expected 0xaa5555aa\n", wlc_hw->unit, val);
2780 return false;
2781 }
2782
Arend van Spriel16d28122011-12-08 15:06:51 -08002783 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2784 (void)bcma_read32(core, D11REGOFFS(objaddr));
2785 bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002786
Arend van Spriel16d28122011-12-08 15:06:51 -08002787 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2788 (void)bcma_read32(core, D11REGOFFS(objaddr));
2789 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002790 if (val != (u32) 0x55aaaa55) {
2791 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2792 "expected 0x55aaaa55\n", wlc_hw->unit, val);
2793 return false;
2794 }
2795
Arend van Spriel16d28122011-12-08 15:06:51 -08002796 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2797 (void)bcma_read32(core, D11REGOFFS(objaddr));
2798 bcma_write32(core, D11REGOFFS(objdata), w);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002799
2800 /* clear CFPStart */
Arend van Spriel16d28122011-12-08 15:06:51 -08002801 bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002802
Arend van Spriel16d28122011-12-08 15:06:51 -08002803 w = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002804 if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
2805 (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
2806 wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
2807 "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
2808 (MCTL_IHR_EN | MCTL_WAKE),
2809 (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
2810 return false;
2811 }
2812
2813 return true;
2814}
2815
2816#define PHYPLL_WAIT_US 100000
2817
2818void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
2819{
Arend van Spriel16d28122011-12-08 15:06:51 -08002820 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002821 u32 tmp;
2822
Seth Forsheeb353dda2012-11-15 08:08:03 -06002823 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002824
2825 tmp = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002826
2827 if (on) {
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002828 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08002829 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2830 CCS_ERSRC_REQ_HT |
2831 CCS_ERSRC_REQ_D11PLL |
2832 CCS_ERSRC_REQ_PHYPLL);
2833 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
2834 CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002835 PHYPLL_WAIT_US);
2836
Arend van Spriel16d28122011-12-08 15:06:51 -08002837 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
2838 if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002839 brcms_err(core, "%s: turn on PHY PLL failed\n",
2840 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002841 } else {
Arend van Spriel16d28122011-12-08 15:06:51 -08002842 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2843 tmp | CCS_ERSRC_REQ_D11PLL |
2844 CCS_ERSRC_REQ_PHYPLL);
2845 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02002846 (CCS_ERSRC_AVAIL_D11PLL |
2847 CCS_ERSRC_AVAIL_PHYPLL)) !=
2848 (CCS_ERSRC_AVAIL_D11PLL |
2849 CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
2850
Arend van Spriel16d28122011-12-08 15:06:51 -08002851 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002852 if ((tmp &
2853 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2854 !=
2855 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002856 brcms_err(core, "%s: turn on PHY PLL failed\n",
2857 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002858 }
2859 } else {
2860 /*
2861 * Since the PLL may be shared, other cores can still
2862 * be requesting it; so we'll deassert the request but
2863 * not wait for status to comply.
2864 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002865 bcma_mask32(core, D11REGOFFS(clk_ctl_st),
2866 ~CCS_ERSRC_REQ_PHYPLL);
2867 (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002868 }
2869}
2870
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02002871static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002872{
2873 bool dev_gone;
2874
Seth Forsheeb353dda2012-11-15 08:08:03 -06002875 brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002876
2877 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
2878
2879 if (dev_gone)
2880 return;
2881
2882 if (wlc_hw->noreset)
2883 return;
2884
2885 /* radio off */
2886 wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
2887
2888 /* turn off analog core */
2889 wlc_phy_anacore(wlc_hw->band->pi, OFF);
2890
2891 /* turn off PHYPLL to save power */
2892 brcms_b_core_phypll_ctl(wlc_hw, false);
2893
2894 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002895 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002896 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
2897}
2898
2899static void brcms_c_flushqueues(struct brcms_c_info *wlc)
2900{
2901 struct brcms_hardware *wlc_hw = wlc->hw;
2902 uint i;
2903
2904 /* free any posted tx packets */
Seth Forsheee041f652012-11-15 08:07:56 -06002905 for (i = 0; i < NFIFO; i++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002906 if (wlc_hw->di[i]) {
2907 dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
Seth Forsheee041f652012-11-15 08:07:56 -06002908 if (i < TX_BCMC_FIFO)
2909 ieee80211_wake_queue(wlc->pub->ieee_hw,
2910 brcms_fifo_to_ac(i));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002911 }
Seth Forsheee041f652012-11-15 08:07:56 -06002912 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002913
2914 /* free any posted rx packets */
2915 dma_rxreclaim(wlc_hw->di[RX_FIFO]);
2916}
2917
2918static u16
2919brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
2920{
Arend van Spriel16d28122011-12-08 15:06:51 -08002921 struct bcma_device *core = wlc_hw->d11core;
2922 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002923
Arend van Spriel16d28122011-12-08 15:06:51 -08002924 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2925 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002926 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002927 objoff += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002928
Arend van Spriel16d28122011-12-08 15:06:51 -08002929 return bcma_read16(core, objoff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002930}
2931
2932static void
2933brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
2934 u32 sel)
2935{
Arend van Spriel16d28122011-12-08 15:06:51 -08002936 struct bcma_device *core = wlc_hw->d11core;
2937 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002938
Arend van Spriel16d28122011-12-08 15:06:51 -08002939 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2940 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002941 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002942 objoff += 2;
2943
2944 bcma_write16(core, objoff, v);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002945}
2946
2947/*
2948 * Read a single u16 from shared memory.
2949 * SHM 'offset' needs to be an even address
2950 */
2951u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
2952{
2953 return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
2954}
2955
2956/*
2957 * Write a single u16 to shared memory.
2958 * SHM 'offset' needs to be an even address
2959 */
2960void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
2961{
2962 brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
2963}
2964
2965/*
2966 * Copy a buffer to shared memory of specified type .
2967 * SHM 'offset' needs to be an even address and
2968 * Buffer length 'len' must be an even number of bytes
2969 * 'sel' selects the type of memory
2970 */
2971void
2972brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
2973 const void *buf, int len, u32 sel)
2974{
2975 u16 v;
2976 const u8 *p = (const u8 *)buf;
2977 int i;
2978
2979 if (len <= 0 || (offset & 1) || (len & 1))
2980 return;
2981
2982 for (i = 0; i < len; i += 2) {
2983 v = p[i] | (p[i + 1] << 8);
2984 brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
2985 }
2986}
2987
2988/*
2989 * Copy a piece of shared memory of specified type to a buffer .
2990 * SHM 'offset' needs to be an even address and
2991 * Buffer length 'len' must be an even number of bytes
2992 * 'sel' selects the type of memory
2993 */
2994void
2995brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
2996 int len, u32 sel)
2997{
2998 u16 v;
2999 u8 *p = (u8 *) buf;
3000 int i;
3001
3002 if (len <= 0 || (offset & 1) || (len & 1))
3003 return;
3004
3005 for (i = 0; i < len; i += 2) {
3006 v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
3007 p[i] = v & 0xFF;
3008 p[i + 1] = (v >> 8) & 0xFF;
3009 }
3010}
3011
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003012/* Copy a buffer to shared memory.
3013 * SHM 'offset' needs to be an even address and
3014 * Buffer length 'len' must be an even number of bytes
3015 */
3016static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
3017 const void *buf, int len)
3018{
3019 brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
3020}
3021
Arend van Spriel5b435de2011-10-05 13:19:03 +02003022static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
3023 u16 SRL, u16 LRL)
3024{
3025 wlc_hw->SRL = SRL;
3026 wlc_hw->LRL = LRL;
3027
3028 /* write retry limit to SCR, shouldn't need to suspend */
3029 if (wlc_hw->up) {
Arend van Spriel16d28122011-12-08 15:06:51 -08003030 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3031 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3032 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3033 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
3034 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3035 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3036 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3037 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003038 }
3039}
3040
3041static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
3042{
3043 if (set) {
3044 if (mboolisset(wlc_hw->pllreq, req_bit))
3045 return;
3046
3047 mboolset(wlc_hw->pllreq, req_bit);
3048
3049 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3050 if (!wlc_hw->sbclk)
3051 brcms_b_xtal(wlc_hw, ON);
3052 }
3053 } else {
3054 if (!mboolisset(wlc_hw->pllreq, req_bit))
3055 return;
3056
3057 mboolclr(wlc_hw->pllreq, req_bit);
3058
3059 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3060 if (wlc_hw->sbclk)
3061 brcms_b_xtal(wlc_hw, OFF);
3062 }
3063 }
3064}
3065
3066static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
3067{
3068 wlc_hw->antsel_avail = antsel_avail;
3069}
3070
3071/*
3072 * conditions under which the PM bit should be set in outgoing frames
3073 * and STAY_AWAKE is meaningful
3074 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003075static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003076{
3077 struct brcms_bss_cfg *cfg = wlc->bsscfg;
3078
3079 /* disallow PS when one of the following global conditions meets */
3080 if (!wlc->pub->associated)
3081 return false;
3082
3083 /* disallow PS when one of these meets when not scanning */
Alwin Beukersbe667662011-11-22 17:21:43 -08003084 if (wlc->filter_flags & FIF_PROMISC_IN_BSS)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003085 return false;
3086
3087 if (cfg->associated) {
3088 /*
3089 * disallow PS when one of the following
3090 * bsscfg specific conditions meets
3091 */
3092 if (!cfg->BSS)
3093 return false;
3094
3095 return false;
3096 }
3097
3098 return true;
3099}
3100
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003101static void brcms_c_statsupd(struct brcms_c_info *wlc)
3102{
3103 int i;
3104 struct macstat macstats;
Joe Perches8ae74652012-01-15 00:38:38 -08003105#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003106 u16 delta;
3107 u16 rxf0ovfl;
3108 u16 txfunfl[NFIFO];
Joe Perches8ae74652012-01-15 00:38:38 -08003109#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003110
3111 /* if driver down, make no sense to update stats */
3112 if (!wlc->pub->up)
3113 return;
3114
Joe Perches8ae74652012-01-15 00:38:38 -08003115#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003116 /* save last rx fifo 0 overflow count */
3117 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
3118
3119 /* save last tx fifo underflow count */
3120 for (i = 0; i < NFIFO; i++)
3121 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
Joe Perches8ae74652012-01-15 00:38:38 -08003122#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003123
3124 /* Read mac stats from contiguous shared memory */
3125 brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, &macstats,
3126 sizeof(struct macstat), OBJADDR_SHM_SEL);
3127
Joe Perches8ae74652012-01-15 00:38:38 -08003128#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003129 /* check for rx fifo 0 overflow */
3130 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
3131 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003132 brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003133 wlc->pub->unit, delta);
3134
3135 /* check for tx fifo underflows */
3136 for (i = 0; i < NFIFO; i++) {
3137 delta =
3138 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
3139 txfunfl[i]);
3140 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003141 brcms_err(wlc->hw->d11core,
3142 "wl%d: %u tx fifo %d underflows!\n",
3143 wlc->pub->unit, delta, i);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003144 }
Joe Perches8ae74652012-01-15 00:38:38 -08003145#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003146
3147 /* merge counters from dma module */
3148 for (i = 0; i < NFIFO; i++) {
3149 if (wlc->hw->di[i])
3150 dma_counterreset(wlc->hw->di[i]);
3151 }
3152}
3153
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154static void brcms_b_reset(struct brcms_hardware *wlc_hw)
3155{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156 /* reset the core */
3157 if (!brcms_deviceremoved(wlc_hw->wlc))
3158 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
3159
3160 /* purge the dma rings */
3161 brcms_c_flushqueues(wlc_hw->wlc);
3162}
3163
3164void brcms_c_reset(struct brcms_c_info *wlc)
3165{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003166 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003167
3168 /* slurp up hw mac counters before core reset */
3169 brcms_c_statsupd(wlc);
3170
3171 /* reset our snapshot of macstat counters */
3172 memset((char *)wlc->core->macstat_snapshot, 0,
3173 sizeof(struct macstat));
3174
3175 brcms_b_reset(wlc->hw);
3176}
3177
Arend van Spriel5b435de2011-10-05 13:19:03 +02003178void brcms_c_init_scb(struct scb *scb)
3179{
3180 int i;
3181
3182 memset(scb, 0, sizeof(struct scb));
3183 scb->flags = SCB_WMECAP | SCB_HTCAP;
3184 for (i = 0; i < NUMPRIO; i++) {
3185 scb->seqnum[i] = 0;
3186 scb->seqctl[i] = 0xFFFF;
3187 }
3188
3189 scb->seqctl_nonqos = 0xFFFF;
3190 scb->magic = SCB_MAGIC;
3191}
3192
3193/* d11 core init
3194 * reset PSM
3195 * download ucode/PCM
3196 * let ucode run to suspended
3197 * download ucode inits
3198 * config other core registers
3199 * init dma
3200 */
3201static void brcms_b_coreinit(struct brcms_c_info *wlc)
3202{
3203 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08003204 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003205 u32 sflags;
Arend van Spriel16d28122011-12-08 15:06:51 -08003206 u32 bcnint_us;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003207 uint i = 0;
3208 bool fifosz_fixup = false;
3209 int err = 0;
3210 u16 buf[NFIFO];
Arend van Spriel5b435de2011-10-05 13:19:03 +02003211 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
3212
Seth Forsheeb353dda2012-11-15 08:08:03 -06003213 brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003214
3215 /* reset PSM */
3216 brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
3217
3218 brcms_ucode_download(wlc_hw);
3219 /*
3220 * FIFOSZ fixup. driver wants to controls the fifo allocation.
3221 */
3222 fifosz_fixup = true;
3223
3224 /* let the PSM run to the suspended state, set mode to BSS STA */
Arend van Spriel16d28122011-12-08 15:06:51 -08003225 bcma_write32(core, D11REGOFFS(macintstatus), -1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003226 brcms_b_mctrl(wlc_hw, ~0,
3227 (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
3228
3229 /* wait for ucode to self-suspend after auto-init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003230 SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
3231 MI_MACSSPNDD) == 0), 1000 * 1000);
3232 if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003233 brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003234 "suspend!\n", wlc_hw->unit);
3235
3236 brcms_c_gpio_init(wlc);
3237
Arend van Spriela8779e42011-12-08 15:06:58 -08003238 sflags = bcma_aread32(core, BCMA_IOST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003239
3240 if (D11REV_IS(wlc_hw->corerev, 23)) {
3241 if (BRCMS_ISNPHY(wlc_hw->band))
3242 brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
3243 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003244 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003245 " %d\n", __func__, wlc_hw->unit,
3246 wlc_hw->corerev);
3247 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
3248 if (BRCMS_ISLCNPHY(wlc_hw->band))
3249 brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
3250 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003251 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003252 " %d\n", __func__, wlc_hw->unit,
3253 wlc_hw->corerev);
3254 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003255 brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003256 __func__, wlc_hw->unit, wlc_hw->corerev);
3257 }
3258
3259 /* For old ucode, txfifo sizes needs to be modified(increased) */
Joe Perches23677ce2012-02-09 11:17:23 +00003260 if (fifosz_fixup)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003261 brcms_b_corerev_fifofixup(wlc_hw);
3262
3263 /* check txfifo allocations match between ucode and driver */
3264 buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
3265 if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
3266 i = TX_AC_BE_FIFO;
3267 err = -1;
3268 }
3269 buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
3270 if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
3271 i = TX_AC_VI_FIFO;
3272 err = -1;
3273 }
3274 buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
3275 buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
3276 buf[TX_AC_BK_FIFO] &= 0xff;
3277 if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
3278 i = TX_AC_BK_FIFO;
3279 err = -1;
3280 }
3281 if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
3282 i = TX_AC_VO_FIFO;
3283 err = -1;
3284 }
3285 buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
3286 buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
3287 buf[TX_BCMC_FIFO] &= 0xff;
3288 if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
3289 i = TX_BCMC_FIFO;
3290 err = -1;
3291 }
3292 if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
3293 i = TX_ATIM_FIFO;
3294 err = -1;
3295 }
3296 if (err != 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003297 brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003298 " driver size %d index %d\n", buf[i],
3299 wlc_hw->xmtfifo_sz[i], i);
3300
3301 /* make sure we can still talk to the mac */
Arend van Spriel16d28122011-12-08 15:06:51 -08003302 WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003303
3304 /* band-specific inits done by wlc_bsinit() */
3305
3306 /* Set up frame burst size and antenna swap threshold init values */
3307 brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
3308 brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
3309
3310 /* enable one rx interrupt per received frame */
Arend van Spriel16d28122011-12-08 15:06:51 -08003311 bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003312
3313 /* set the station mode (BSS STA) */
3314 brcms_b_mctrl(wlc_hw,
3315 (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
3316 (MCTL_INFRA | MCTL_DISCARD_PMQ));
3317
3318 /* set up Beacon interval */
3319 bcnint_us = 0x8000 << 10;
Arend van Spriel16d28122011-12-08 15:06:51 -08003320 bcma_write32(core, D11REGOFFS(tsf_cfprep),
3321 (bcnint_us << CFPREP_CBI_SHIFT));
3322 bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
3323 bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003324
3325 /* write interrupt mask */
Arend van Spriel16d28122011-12-08 15:06:51 -08003326 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
3327 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003328
3329 /* allow the MAC to control the PHY clock (dynamic on/off) */
3330 brcms_b_macphyclk_set(wlc_hw, ON);
3331
3332 /* program dynamic clock control fast powerup delay register */
3333 wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -08003334 bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003335
3336 /* tell the ucode the corerev */
3337 brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
3338
3339 /* tell the ucode MAC capabilities */
3340 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
3341 (u16) (wlc_hw->machwcap & 0xffff));
3342 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
3343 (u16) ((wlc_hw->
3344 machwcap >> 16) & 0xffff));
3345
3346 /* write retry limits to SCR, this done after PSM init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003347 bcma_write32(core, D11REGOFFS(objaddr),
3348 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3349 (void)bcma_read32(core, D11REGOFFS(objaddr));
3350 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
3351 bcma_write32(core, D11REGOFFS(objaddr),
3352 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3353 (void)bcma_read32(core, D11REGOFFS(objaddr));
3354 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003355
3356 /* write rate fallback retry limits */
3357 brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
3358 brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
3359
Arend van Spriel16d28122011-12-08 15:06:51 -08003360 bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
3361 bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003362
3363 /* init the tx dma engines */
3364 for (i = 0; i < NFIFO; i++) {
3365 if (wlc_hw->di[i])
3366 dma_txinit(wlc_hw->di[i]);
3367 }
3368
3369 /* init the rx dma engine(s) and post receive buffers */
3370 dma_rxinit(wlc_hw->di[RX_FIFO]);
3371 dma_rxfill(wlc_hw->di[RX_FIFO]);
3372}
3373
3374void
Roland Vossena8bc4912011-10-21 16:16:25 +02003375static brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003376 u32 macintmask;
3377 bool fastclk;
3378 struct brcms_c_info *wlc = wlc_hw->wlc;
3379
Arend van Spriel5b435de2011-10-05 13:19:03 +02003380 /* request FAST clock if not on */
3381 fastclk = wlc_hw->forcefastclk;
3382 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003383 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003384
3385 /* disable interrupts */
3386 macintmask = brcms_intrsoff(wlc->wl);
3387
3388 /* set up the specified band and chanspec */
3389 brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
3390 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3391
3392 /* do one-time phy inits and calibration */
3393 wlc_phy_cal_init(wlc_hw->band->pi);
3394
3395 /* core-specific initialization */
3396 brcms_b_coreinit(wlc);
3397
Arend van Spriel5b435de2011-10-05 13:19:03 +02003398 /* band-specific inits */
3399 brcms_b_bsinit(wlc, chanspec);
3400
3401 /* restore macintmask */
3402 brcms_intrsrestore(wlc->wl, macintmask);
3403
3404 /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
3405 * is suspended and brcms_c_enable_mac() will clear this override bit.
3406 */
3407 mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
3408
3409 /*
3410 * initialize mac_suspend_depth to 1 to match ucode
3411 * initial suspended state
3412 */
3413 wlc_hw->mac_suspend_depth = 1;
3414
3415 /* restore the clk */
3416 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003417 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003418}
3419
3420static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
3421 u16 chanspec)
3422{
3423 /* Save our copy of the chanspec */
3424 wlc->chanspec = chanspec;
3425
3426 /* Set the chanspec and power limits for this locale */
3427 brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
3428
3429 if (wlc->stf->ss_algosel_auto)
3430 brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
3431 chanspec);
3432
3433 brcms_c_stf_ss_update(wlc, wlc->band);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003434}
Arend van Spriel5b435de2011-10-05 13:19:03 +02003435
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003436static void
3437brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
3438{
3439 brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
3440 wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
3441 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
3442 brcms_chspec_bw(wlc->default_bss->chanspec),
3443 wlc->stf->txstreams);
3444}
3445
3446/* derive wlc->band->basic_rate[] table from 'rateset' */
3447static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
3448 struct brcms_c_rateset *rateset)
3449{
3450 u8 rate;
3451 u8 mandatory;
3452 u8 cck_basic = 0;
3453 u8 ofdm_basic = 0;
3454 u8 *br = wlc->band->basic_rate;
3455 uint i;
3456
3457 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
3458 memset(br, 0, BRCM_MAXRATE + 1);
3459
3460 /* For each basic rate in the rates list, make an entry in the
3461 * best basic lookup.
3462 */
3463 for (i = 0; i < rateset->count; i++) {
3464 /* only make an entry for a basic rate */
3465 if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
3466 continue;
3467
3468 /* mask off basic bit */
3469 rate = (rateset->rates[i] & BRCMS_RATE_MASK);
3470
3471 if (rate > BRCM_MAXRATE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003472 brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003473 "invalid rate 0x%X in rate set\n",
3474 rateset->rates[i]);
3475 continue;
3476 }
3477
3478 br[rate] = rate;
3479 }
3480
3481 /* The rate lookup table now has non-zero entries for each
3482 * basic rate, equal to the basic rate: br[basicN] = basicN
3483 *
3484 * To look up the best basic rate corresponding to any
3485 * particular rate, code can use the basic_rate table
3486 * like this
3487 *
3488 * basic_rate = wlc->band->basic_rate[tx_rate]
3489 *
3490 * Make sure there is a best basic rate entry for
3491 * every rate by walking up the table from low rates
3492 * to high, filling in holes in the lookup table
3493 */
3494
3495 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
3496 rate = wlc->band->hw_rateset.rates[i];
3497
3498 if (br[rate] != 0) {
3499 /* This rate is a basic rate.
3500 * Keep track of the best basic rate so far by
3501 * modulation type.
3502 */
3503 if (is_ofdm_rate(rate))
3504 ofdm_basic = rate;
3505 else
3506 cck_basic = rate;
3507
3508 continue;
3509 }
3510
3511 /* This rate is not a basic rate so figure out the
3512 * best basic rate less than this rate and fill in
3513 * the hole in the table
3514 */
3515
3516 br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
3517
3518 if (br[rate] != 0)
3519 continue;
3520
3521 if (is_ofdm_rate(rate)) {
3522 /*
3523 * In 11g and 11a, the OFDM mandatory rates
3524 * are 6, 12, and 24 Mbps
3525 */
3526 if (rate >= BRCM_RATE_24M)
3527 mandatory = BRCM_RATE_24M;
3528 else if (rate >= BRCM_RATE_12M)
3529 mandatory = BRCM_RATE_12M;
3530 else
3531 mandatory = BRCM_RATE_6M;
3532 } else {
3533 /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
3534 mandatory = rate;
3535 }
3536
3537 br[rate] = mandatory;
3538 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003539}
3540
3541static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
3542 u16 chanspec)
3543{
3544 struct brcms_c_rateset default_rateset;
3545 uint parkband;
3546 uint i, band_order[2];
3547
Arend van Spriel5b435de2011-10-05 13:19:03 +02003548 /*
3549 * We might have been bandlocked during down and the chip
3550 * power-cycled (hibernate). Figure out the right band to park on
3551 */
3552 if (wlc->bandlocked || wlc->pub->_nbands == 1) {
3553 /* updated in brcms_c_bandlock() */
3554 parkband = wlc->band->bandunit;
3555 band_order[0] = band_order[1] = parkband;
3556 } else {
3557 /* park on the band of the specified chanspec */
3558 parkband = chspec_bandunit(chanspec);
3559
3560 /* order so that parkband initialize last */
3561 band_order[0] = parkband ^ 1;
3562 band_order[1] = parkband;
3563 }
3564
3565 /* make each band operational, software state init */
3566 for (i = 0; i < wlc->pub->_nbands; i++) {
3567 uint j = band_order[i];
3568
3569 wlc->band = wlc->bandstate[j];
3570
3571 brcms_default_rateset(wlc, &default_rateset);
3572
3573 /* fill in hw_rate */
3574 brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
3575 false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
3576 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
3577
3578 /* init basic rate lookup */
3579 brcms_c_rate_lookup_init(wlc, &default_rateset);
3580 }
3581
3582 /* sync up phy/radio chanspec */
3583 brcms_c_set_phy_chanspec(wlc, chanspec);
3584}
3585
Alwin Beukers02a588a2011-11-10 20:30:28 +01003586/*
Alwin Beukersbe667662011-11-22 17:21:43 -08003587 * Set or clear filtering related maccontrol bits based on
3588 * specified filter flags
Alwin Beukers02a588a2011-11-10 20:30:28 +01003589 */
Alwin Beukersbe667662011-11-22 17:21:43 -08003590void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003591{
Alwin Beukers02a588a2011-11-10 20:30:28 +01003592 u32 promisc_bits = 0;
3593
Alwin Beukersbe667662011-11-22 17:21:43 -08003594 wlc->filter_flags = filter_flags;
3595
3596 if (filter_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
3597 promisc_bits |= MCTL_PROMISC;
3598
3599 if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
Alwin Beukers02a588a2011-11-10 20:30:28 +01003600 promisc_bits |= MCTL_BCNS_PROMISC;
3601
Alwin Beukersbe667662011-11-22 17:21:43 -08003602 if (filter_flags & FIF_FCSFAIL)
3603 promisc_bits |= MCTL_KEEPBADFCS;
3604
3605 if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
3606 promisc_bits |= MCTL_KEEPCONTROL;
Alwin Beukers02a588a2011-11-10 20:30:28 +01003607
3608 brcms_b_mctrl(wlc->hw,
Alwin Beukersbe667662011-11-22 17:21:43 -08003609 MCTL_PROMISC | MCTL_BCNS_PROMISC |
3610 MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
3611 promisc_bits);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003612}
3613
Arend van Spriel5b435de2011-10-05 13:19:03 +02003614/*
3615 * ucode, hwmac update
3616 * Channel dependent updates for ucode and hw
3617 */
3618static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
3619{
3620 /* enable or disable any active IBSSs depending on whether or not
3621 * we are on the home channel
3622 */
3623 if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
3624 if (wlc->pub->associated) {
3625 /*
3626 * BMAC_NOTE: This is something that should be fixed
3627 * in ucode inits. I think that the ucode inits set
3628 * up the bcn templates and shm values with a bogus
3629 * beacon. This should not be done in the inits. If
3630 * ucode needs to set up a beacon for testing, the
3631 * test routines should write it down, not expect the
3632 * inits to populate a bogus beacon.
3633 */
3634 if (BRCMS_PHY_11N_CAP(wlc->band))
3635 brcms_b_write_shm(wlc->hw,
3636 M_BCN_TXTSF_OFFSET, 0);
3637 }
3638 } else {
3639 /* disable an active IBSS if we are not on the home channel */
3640 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003641}
3642
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003643static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
3644 u8 basic_rate)
3645{
3646 u8 phy_rate, index;
3647 u8 basic_phy_rate, basic_index;
3648 u16 dir_table, basic_table;
3649 u16 basic_ptr;
3650
3651 /* Shared memory address for the table we are reading */
3652 dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
3653
3654 /* Shared memory address for the table we are writing */
3655 basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
3656
3657 /*
3658 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
3659 * the index into the rate table.
3660 */
3661 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
3662 basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
3663 index = phy_rate & 0xf;
3664 basic_index = basic_phy_rate & 0xf;
3665
3666 /* Find the SHM pointer to the ACK rate entry by looking in the
3667 * Direct-map Table
3668 */
3669 basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
3670
3671 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
3672 * to the correct basic rate for the given incoming rate
3673 */
3674 brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
3675}
3676
3677static const struct brcms_c_rateset *
3678brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
3679{
3680 const struct brcms_c_rateset *rs_dflt;
3681
3682 if (BRCMS_PHY_11N_CAP(wlc->band)) {
3683 if (wlc->band->bandtype == BRCM_BAND_5G)
3684 rs_dflt = &ofdm_mimo_rates;
3685 else
3686 rs_dflt = &cck_ofdm_mimo_rates;
3687 } else if (wlc->band->gmode)
3688 rs_dflt = &cck_ofdm_rates;
3689 else
3690 rs_dflt = &cck_rates;
3691
3692 return rs_dflt;
3693}
3694
3695static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
3696{
3697 const struct brcms_c_rateset *rs_dflt;
3698 struct brcms_c_rateset rs;
3699 u8 rate, basic_rate;
3700 uint i;
3701
3702 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
3703
3704 brcms_c_rateset_copy(rs_dflt, &rs);
3705 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
3706
3707 /* walk the phy rate table and update SHM basic rate lookup table */
3708 for (i = 0; i < rs.count; i++) {
3709 rate = rs.rates[i] & BRCMS_RATE_MASK;
3710
3711 /* for a given rate brcms_basic_rate returns the rate at
3712 * which a response ACK/CTS should be sent.
3713 */
3714 basic_rate = brcms_basic_rate(wlc, rate);
3715 if (basic_rate == 0)
3716 /* This should only happen if we are using a
3717 * restricted rateset.
3718 */
3719 basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
3720
3721 brcms_c_write_rate_shm(wlc, rate, basic_rate);
3722 }
3723}
3724
Arend van Spriel5b435de2011-10-05 13:19:03 +02003725/* band-specific init */
3726static void brcms_c_bsinit(struct brcms_c_info *wlc)
3727{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003728 brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
3729 wlc->pub->unit, wlc->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003730
3731 /* write ucode ACK/CTS rate table */
3732 brcms_c_set_ratetable(wlc);
3733
3734 /* update some band specific mac configuration */
3735 brcms_c_ucode_mac_upd(wlc);
3736
3737 /* init antenna selection */
3738 brcms_c_antsel_init(wlc->asi);
3739
3740}
3741
3742/* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
3743static int
3744brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
3745 bool writeToShm)
3746{
3747 int idle_busy_ratio_x_16 = 0;
3748 uint offset =
3749 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
3750 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
3751 if (duty_cycle > 100 || duty_cycle < 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003752 brcms_err(wlc->hw->d11core,
3753 "wl%d: duty cycle value off limit\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003754 wlc->pub->unit);
3755 return -EINVAL;
3756 }
3757 if (duty_cycle)
3758 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
3759 /* Only write to shared memory when wl is up */
3760 if (writeToShm)
3761 brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
3762
3763 if (isOFDM)
3764 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
3765 else
3766 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
3767
3768 return 0;
3769}
3770
Arend van Spriel5b435de2011-10-05 13:19:03 +02003771/* push sw hps and wake state through hardware */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003772static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003773{
3774 u32 v1, v2;
3775 bool hps;
3776 bool awake_before;
3777
3778 hps = brcms_c_ps_allowed(wlc);
3779
3780 BCMMSG(wlc->wiphy, "wl%d: hps %d\n", wlc->pub->unit, hps);
3781
Arend van Spriel16d28122011-12-08 15:06:51 -08003782 v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003783 v2 = MCTL_WAKE;
3784 if (hps)
3785 v2 |= MCTL_HPS;
3786
3787 brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
3788
3789 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
3790
3791 if (!awake_before)
3792 brcms_b_wait_for_wake(wlc->hw);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003793}
3794
3795/*
3796 * Write this BSS config's MAC address to core.
3797 * Updates RXE match engine.
3798 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003799static int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003800{
3801 int err = 0;
3802 struct brcms_c_info *wlc = bsscfg->wlc;
3803
3804 /* enter the MAC addr into the RXE match registers */
3805 brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, bsscfg->cur_etheraddr);
3806
3807 brcms_c_ampdu_macaddr_upd(wlc);
3808
3809 return err;
3810}
3811
3812/* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
3813 * Updates RXE match engine.
3814 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003815static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003816{
3817 /* we need to update BSSID in RXE match registers */
3818 brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
3819}
3820
3821static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
3822{
3823 wlc_hw->shortslot = shortslot;
3824
3825 if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
3826 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
3827 brcms_b_update_slot_timing(wlc_hw, shortslot);
3828 brcms_c_enable_mac(wlc_hw->wlc);
3829 }
3830}
3831
3832/*
3833 * Suspend the the MAC and update the slot timing
3834 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
3835 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003836static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003837{
3838 /* use the override if it is set */
3839 if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
3840 shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
3841
3842 if (wlc->shortslot == shortslot)
3843 return;
3844
3845 wlc->shortslot = shortslot;
3846
3847 brcms_b_set_shortslot(wlc->hw, shortslot);
3848}
3849
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003850static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003851{
3852 if (wlc->home_chanspec != chanspec) {
3853 wlc->home_chanspec = chanspec;
3854
3855 if (wlc->bsscfg->associated)
3856 wlc->bsscfg->current_bss->chanspec = chanspec;
3857 }
3858}
3859
3860void
3861brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
Roland Vossenc6c44892011-10-21 16:16:26 +02003862 bool mute_tx, struct txpwr_limits *txpwr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003863{
3864 uint bandunit;
3865
3866 BCMMSG(wlc_hw->wlc->wiphy, "wl%d: 0x%x\n", wlc_hw->unit, chanspec);
3867
3868 wlc_hw->chanspec = chanspec;
3869
3870 /* Switch bands if necessary */
3871 if (wlc_hw->_nbands > 1) {
3872 bandunit = chspec_bandunit(chanspec);
3873 if (wlc_hw->band->bandunit != bandunit) {
3874 /* brcms_b_setband disables other bandunit,
3875 * use light band switch if not up yet
3876 */
3877 if (wlc_hw->up) {
3878 wlc_phy_chanspec_radio_set(wlc_hw->
3879 bandstate[bandunit]->
3880 pi, chanspec);
3881 brcms_b_setband(wlc_hw, bandunit, chanspec);
3882 } else {
3883 brcms_c_setxband(wlc_hw, bandunit);
3884 }
3885 }
3886 }
3887
Roland Vossenc6c44892011-10-21 16:16:26 +02003888 wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003889
3890 if (!wlc_hw->up) {
3891 if (wlc_hw->clk)
3892 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
3893 chanspec);
3894 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3895 } else {
3896 wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
3897 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
3898
3899 /* Update muting of the channel */
Roland Vossenc6c44892011-10-21 16:16:26 +02003900 brcms_b_mute(wlc_hw, mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003901 }
3902}
3903
3904/* switch to and initialize new band */
3905static void brcms_c_setband(struct brcms_c_info *wlc,
3906 uint bandunit)
3907{
3908 wlc->band = wlc->bandstate[bandunit];
3909
3910 if (!wlc->pub->up)
3911 return;
3912
3913 /* wait for at least one beacon before entering sleeping state */
3914 brcms_c_set_ps_ctrl(wlc);
3915
3916 /* band-specific initializations */
3917 brcms_c_bsinit(wlc);
3918}
3919
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003920static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003921{
3922 uint bandunit;
3923 bool switchband = false;
3924 u16 old_chanspec = wlc->chanspec;
3925
3926 if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003927 brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003928 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
3929 return;
3930 }
3931
3932 /* Switch bands if necessary */
3933 if (wlc->pub->_nbands > 1) {
3934 bandunit = chspec_bandunit(chanspec);
3935 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
3936 switchband = true;
3937 if (wlc->bandlocked) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003938 brcms_err(wlc->hw->d11core,
3939 "wl%d: %s: chspec %d band is locked!\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003940 wlc->pub->unit, __func__,
3941 CHSPEC_CHANNEL(chanspec));
3942 return;
3943 }
3944 /*
3945 * should the setband call come after the
3946 * brcms_b_chanspec() ? if the setband updates
3947 * (brcms_c_bsinit) use low level calls to inspect and
3948 * set state, the state inspected may be from the wrong
3949 * band, or the following brcms_b_set_chanspec() may
3950 * undo the work.
3951 */
3952 brcms_c_setband(wlc, bandunit);
3953 }
3954 }
3955
3956 /* sync up phy/radio chanspec */
3957 brcms_c_set_phy_chanspec(wlc, chanspec);
3958
3959 /* init antenna selection */
3960 if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
3961 brcms_c_antsel_init(wlc->asi);
3962
3963 /* Fix the hardware rateset based on bw.
3964 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
3965 */
3966 brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
3967 wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
3968 }
3969
3970 /* update some mac configuration since chanspec changed */
3971 brcms_c_ucode_mac_upd(wlc);
3972}
3973
Arend van Spriel5b435de2011-10-05 13:19:03 +02003974/*
3975 * This function changes the phytxctl for beacon based on current
3976 * beacon ratespec AND txant setting as per this table:
3977 * ratespec CCK ant = wlc->stf->txant
3978 * OFDM ant = 3
3979 */
3980void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
3981 u32 bcn_rspec)
3982{
3983 u16 phyctl;
3984 u16 phytxant = wlc->stf->phytxant;
3985 u16 mask = PHY_TXC_ANT_MASK;
3986
3987 /* for non-siso rates or default setting, use the available chains */
3988 if (BRCMS_PHY_11N_CAP(wlc->band))
3989 phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
3990
3991 phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
3992 phyctl = (phyctl & ~mask) | phytxant;
3993 brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
3994}
3995
3996/*
3997 * centralized protection config change function to simplify debugging, no
3998 * consistency checking this should be called only on changes to avoid overhead
3999 * in periodic function
4000 */
4001void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
4002{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004003 /*
4004 * Cannot use brcms_dbg_* here because this function is called
4005 * before wlc is sufficiently initialized.
4006 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02004007 BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
4008
4009 switch (idx) {
4010 case BRCMS_PROT_G_SPEC:
4011 wlc->protection->_g = (bool) val;
4012 break;
4013 case BRCMS_PROT_G_OVR:
4014 wlc->protection->g_override = (s8) val;
4015 break;
4016 case BRCMS_PROT_G_USER:
4017 wlc->protection->gmode_user = (u8) val;
4018 break;
4019 case BRCMS_PROT_OVERLAP:
4020 wlc->protection->overlap = (s8) val;
4021 break;
4022 case BRCMS_PROT_N_USER:
4023 wlc->protection->nmode_user = (s8) val;
4024 break;
4025 case BRCMS_PROT_N_CFG:
4026 wlc->protection->n_cfg = (s8) val;
4027 break;
4028 case BRCMS_PROT_N_CFG_OVR:
4029 wlc->protection->n_cfg_override = (s8) val;
4030 break;
4031 case BRCMS_PROT_N_NONGF:
4032 wlc->protection->nongf = (bool) val;
4033 break;
4034 case BRCMS_PROT_N_NONGF_OVR:
4035 wlc->protection->nongf_override = (s8) val;
4036 break;
4037 case BRCMS_PROT_N_PAM_OVR:
4038 wlc->protection->n_pam_override = (s8) val;
4039 break;
4040 case BRCMS_PROT_N_OBSS:
4041 wlc->protection->n_obss = (bool) val;
4042 break;
4043
4044 default:
4045 break;
4046 }
4047
4048}
4049
4050static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
4051{
4052 if (wlc->pub->up) {
4053 brcms_c_update_beacon(wlc);
4054 brcms_c_update_probe_resp(wlc, true);
4055 }
4056}
4057
4058static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
4059{
4060 wlc->stf->ldpc = val;
4061
4062 if (wlc->pub->up) {
4063 brcms_c_update_beacon(wlc);
4064 brcms_c_update_probe_resp(wlc, true);
4065 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
4066 }
4067}
4068
4069void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
4070 const struct ieee80211_tx_queue_params *params,
4071 bool suspend)
4072{
4073 int i;
4074 struct shm_acparams acp_shm;
4075 u16 *shm_entry;
4076
4077 /* Only apply params if the core is out of reset and has clocks */
4078 if (!wlc->clk) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004079 brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
4080 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004081 return;
4082 }
4083
4084 memset((char *)&acp_shm, 0, sizeof(struct shm_acparams));
4085 /* fill in shm ac params struct */
4086 acp_shm.txop = params->txop;
4087 /* convert from units of 32us to us for ucode */
4088 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
4089 EDCF_TXOP2USEC(acp_shm.txop);
4090 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
4091
Arend van Sprielb7eec422011-11-10 20:30:18 +01004092 if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
Arend van Spriel5b435de2011-10-05 13:19:03 +02004093 && acp_shm.aifs < EDCF_AIFSN_MAX)
4094 acp_shm.aifs++;
4095
4096 if (acp_shm.aifs < EDCF_AIFSN_MIN
4097 || acp_shm.aifs > EDCF_AIFSN_MAX) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004098 brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
Arend van Spriel5b435de2011-10-05 13:19:03 +02004099 "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
4100 } else {
4101 acp_shm.cwmin = params->cw_min;
4102 acp_shm.cwmax = params->cw_max;
4103 acp_shm.cwcur = acp_shm.cwmin;
4104 acp_shm.bslots =
Arend van Spriel16d28122011-12-08 15:06:51 -08004105 bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
4106 acp_shm.cwcur;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004107 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
4108 /* Indicate the new params to the ucode */
4109 acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
4110 wme_ac2fifo[aci] *
4111 M_EDCF_QLEN +
4112 M_EDCF_STATUS_OFF));
4113 acp_shm.status |= WME_STATUS_NEWAC;
4114
4115 /* Fill in shm acparam table */
4116 shm_entry = (u16 *) &acp_shm;
4117 for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
4118 brcms_b_write_shm(wlc->hw,
4119 M_EDCF_QINFO +
4120 wme_ac2fifo[aci] * M_EDCF_QLEN + i,
4121 *shm_entry++);
4122 }
4123
4124 if (suspend) {
4125 brcms_c_suspend_mac_and_wait(wlc);
4126 brcms_c_enable_mac(wlc);
4127 }
4128}
4129
Arend van Spriel094b1992011-10-18 14:03:07 +02004130static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004131{
4132 u16 aci;
4133 int i_ac;
4134 struct ieee80211_tx_queue_params txq_pars;
4135 static const struct edcf_acparam default_edcf_acparams[] = {
4136 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
4137 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
4138 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
4139 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
4140 }; /* ucode needs these parameters during its initialization */
4141 const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
4142
Arend van Sprielb7eec422011-11-10 20:30:18 +01004143 for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004144 /* find out which ac this set of params applies to */
4145 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
4146
4147 /* fill in shm ac params struct */
4148 txq_pars.txop = edcf_acp->TXOP;
4149 txq_pars.aifs = edcf_acp->ACI;
4150
4151 /* CWmin = 2^(ECWmin) - 1 */
4152 txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
4153 /* CWmax = 2^(ECWmax) - 1 */
4154 txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
4155 >> EDCF_ECWMAX_SHIFT);
4156 brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
4157 }
4158
4159 if (suspend) {
4160 brcms_c_suspend_mac_and_wait(wlc);
4161 brcms_c_enable_mac(wlc);
4162 }
4163}
4164
Arend van Spriel5b435de2011-10-05 13:19:03 +02004165static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
4166{
4167 /* Don't start the timer if HWRADIO feature is disabled */
4168 if (wlc->radio_monitor)
4169 return;
4170
4171 wlc->radio_monitor = true;
4172 brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004173 brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004174}
4175
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004176static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004177{
4178 if (!wlc->radio_monitor)
4179 return true;
4180
4181 wlc->radio_monitor = false;
4182 brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004183 return brcms_del_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004184}
4185
4186/* read hwdisable state and propagate to wlc flag */
4187static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
4188{
4189 if (wlc->pub->hw_off)
4190 return;
4191
4192 if (brcms_b_radio_read_hwdisabled(wlc->hw))
4193 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4194 else
4195 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4196}
4197
Arend van Spriel5b435de2011-10-05 13:19:03 +02004198/* update hwradio status and return it */
4199bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
4200{
4201 brcms_c_radio_hwdisable_upd(wlc);
4202
4203 return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
4204 true : false;
4205}
4206
4207/* periodical query hw radio button while driver is "down" */
4208static void brcms_c_radio_timer(void *arg)
4209{
4210 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4211
4212 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004213 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4214 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004215 brcms_down(wlc->wl);
4216 return;
4217 }
4218
Arend van Spriel5b435de2011-10-05 13:19:03 +02004219 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004220}
4221
4222/* common low-level watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004223static void brcms_b_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004224{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004225 struct brcms_hardware *wlc_hw = wlc->hw;
4226
Arend van Spriel5b435de2011-10-05 13:19:03 +02004227 if (!wlc_hw->up)
4228 return;
4229
4230 /* increment second count */
4231 wlc_hw->now++;
4232
4233 /* Check for FIFO error interrupts */
4234 brcms_b_fifoerrors(wlc_hw);
4235
4236 /* make sure RX dma has buffers */
4237 dma_rxfill(wlc->hw->di[RX_FIFO]);
4238
4239 wlc_phy_watchdog(wlc_hw->band->pi);
4240}
4241
4242/* common watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004243static void brcms_c_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004244{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004245 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004246
4247 if (!wlc->pub->up)
4248 return;
4249
4250 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004251 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4252 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004253 brcms_down(wlc->wl);
4254 return;
4255 }
4256
4257 /* increment second count */
4258 wlc->pub->now++;
4259
Arend van Spriel5b435de2011-10-05 13:19:03 +02004260 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004261 /* if radio is disable, driver may be down, quit here */
4262 if (wlc->pub->radio_disabled)
4263 return;
4264
4265 brcms_b_watchdog(wlc);
4266
4267 /*
4268 * occasionally sample mac stat counters to
4269 * detect 16-bit counter wrap
4270 */
4271 if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
4272 brcms_c_statsupd(wlc);
4273
4274 if (BRCMS_ISNPHY(wlc->band) &&
4275 ((wlc->pub->now - wlc->tempsense_lasttime) >=
4276 BRCMS_TEMPSENSE_PERIOD)) {
4277 wlc->tempsense_lasttime = wlc->pub->now;
4278 brcms_c_tempsense_upd(wlc);
4279 }
4280}
4281
4282static void brcms_c_watchdog_by_timer(void *arg)
4283{
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004284 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4285
4286 brcms_c_watchdog(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004287}
4288
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004289static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004290{
4291 wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
4292 wlc, "watchdog");
4293 if (!wlc->wdtimer) {
4294 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for wdtimer "
4295 "failed\n", unit);
4296 goto fail;
4297 }
4298
4299 wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
4300 wlc, "radio");
4301 if (!wlc->radio_timer) {
4302 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for radio_timer "
4303 "failed\n", unit);
4304 goto fail;
4305 }
4306
4307 return true;
4308
4309 fail:
4310 return false;
4311}
4312
4313/*
4314 * Initialize brcms_c_info default values ...
4315 * may get overrides later in this function
4316 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004317static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004318{
4319 int i;
4320
4321 /* Save our copy of the chanspec */
4322 wlc->chanspec = ch20mhz_chspec(1);
4323
4324 /* various 802.11g modes */
4325 wlc->shortslot = false;
4326 wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
4327
4328 brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
4329 brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
4330
4331 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
4332 BRCMS_PROTECTION_AUTO);
4333 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
4334 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
4335 BRCMS_PROTECTION_AUTO);
4336 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
4337 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
4338
4339 brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
4340 BRCMS_PROTECTION_CTL_OVERLAP);
4341
4342 /* 802.11g draft 4.0 NonERP elt advertisement */
4343 wlc->include_legacy_erp = true;
4344
4345 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
4346 wlc->stf->txant = ANT_TX_DEF;
4347
4348 wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
4349
4350 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
4351 for (i = 0; i < NFIFO; i++)
4352 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
4353 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
4354
4355 /* default rate fallback retry limits */
4356 wlc->SFBL = RETRY_SHORT_FB;
4357 wlc->LFBL = RETRY_LONG_FB;
4358
4359 /* default mac retry limits */
4360 wlc->SRL = RETRY_SHORT_DEF;
4361 wlc->LRL = RETRY_LONG_DEF;
4362
4363 /* WME QoS mode is Auto by default */
4364 wlc->pub->_ampdu = AMPDU_AGG_HOST;
4365 wlc->pub->bcmerror = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004366}
4367
4368static uint brcms_c_attach_module(struct brcms_c_info *wlc)
4369{
4370 uint err = 0;
4371 uint unit;
4372 unit = wlc->pub->unit;
4373
4374 wlc->asi = brcms_c_antsel_attach(wlc);
4375 if (wlc->asi == NULL) {
4376 wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
4377 "failed\n", unit);
4378 err = 44;
4379 goto fail;
4380 }
4381
4382 wlc->ampdu = brcms_c_ampdu_attach(wlc);
4383 if (wlc->ampdu == NULL) {
4384 wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
4385 "failed\n", unit);
4386 err = 50;
4387 goto fail;
4388 }
4389
4390 if ((brcms_c_stf_attach(wlc) != 0)) {
4391 wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
4392 "failed\n", unit);
4393 err = 68;
4394 goto fail;
4395 }
4396 fail:
4397 return err;
4398}
4399
4400struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
4401{
4402 return wlc->pub;
4403}
4404
4405/* low level attach
4406 * run backplane attach, init nvram
4407 * run phy attach
4408 * initialize software state for each core and band
4409 * put the whole chip in reset(driver down state), no clock
4410 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08004411static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
4412 uint unit, bool piomode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004413{
4414 struct brcms_hardware *wlc_hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004415 uint err = 0;
4416 uint j;
4417 bool wme = false;
4418 struct shared_phy_params sha_params;
4419 struct wiphy *wiphy = wlc->wiphy;
Arend van Sprielb63337a2011-12-08 15:06:47 -08004420 struct pci_dev *pcidev = core->bus->host_pci;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004421 struct ssb_sprom *sprom = &core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004422
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004423 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
Seth Forsheeb353dda2012-11-15 08:08:03 -06004424 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4425 pcidev->vendor,
4426 pcidev->device);
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004427 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06004428 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4429 core->bus->boardinfo.vendor,
4430 core->bus->boardinfo.type);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004431
4432 wme = true;
4433
4434 wlc_hw = wlc->hw;
4435 wlc_hw->wlc = wlc;
4436 wlc_hw->unit = unit;
4437 wlc_hw->band = wlc_hw->bandstate[0];
4438 wlc_hw->_piomode = piomode;
4439
4440 /* populate struct brcms_hardware with default values */
4441 brcms_b_info_init(wlc_hw);
4442
4443 /*
4444 * Do the hardware portion of the attach. Also initialize software
4445 * state that depends on the particular hardware we are running.
4446 */
Arend van Spriel28a53442011-12-08 15:06:49 -08004447 wlc_hw->sih = ai_attach(core->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004448 if (wlc_hw->sih == NULL) {
4449 wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
4450 unit);
4451 err = 11;
4452 goto fail;
4453 }
4454
4455 /* verify again the device is supported */
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02004456 if (!brcms_c_chipmatch(core)) {
4457 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
4458 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004459 err = 12;
4460 goto fail;
4461 }
4462
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004463 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
4464 wlc_hw->vendorid = pcidev->vendor;
4465 wlc_hw->deviceid = pcidev->device;
4466 } else {
4467 wlc_hw->vendorid = core->bus->boardinfo.vendor;
4468 wlc_hw->deviceid = core->bus->boardinfo.type;
4469 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004470
Arend van Spriel16d28122011-12-08 15:06:51 -08004471 wlc_hw->d11core = core;
4472 wlc_hw->corerev = core->id.rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004473
4474 /* validate chip, chiprev and corerev */
4475 if (!brcms_c_isgoodchip(wlc_hw)) {
4476 err = 13;
4477 goto fail;
4478 }
4479
4480 /* initialize power control registers */
4481 ai_clkctl_init(wlc_hw->sih);
4482
4483 /* request fastclock and force fastclock for the rest of attach
4484 * bring the d11 core out of reset.
4485 * For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
4486 * is still false; But it will be called again inside wlc_corereset,
4487 * after d11 is out of reset.
4488 */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004489 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004490 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4491
4492 if (!brcms_b_validate_chip_access(wlc_hw)) {
4493 wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
4494 "failed\n", unit);
4495 err = 14;
4496 goto fail;
4497 }
4498
4499 /* get the board rev, used just below */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004500 j = sprom->board_rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004501 /* promote srom boardrev of 0xFF to 1 */
4502 if (j == BOARDREV_PROMOTABLE)
4503 j = BOARDREV_PROMOTED;
4504 wlc_hw->boardrev = (u16) j;
4505 if (!brcms_c_validboardtype(wlc_hw)) {
4506 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004507 "board type (0x%x)" " or revision level (0x%x)\n",
4508 unit, ai_get_boardtype(wlc_hw->sih),
4509 wlc_hw->boardrev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004510 err = 15;
4511 goto fail;
4512 }
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004513 wlc_hw->sromrev = sprom->revision;
4514 wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
4515 wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004516
4517 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
4518 brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
4519
4520 /* check device id(srom, nvram etc.) to set bands */
4521 if (wlc_hw->deviceid == BCM43224_D11N_ID ||
4522 wlc_hw->deviceid == BCM43224_D11N_ID_VEN1)
4523 /* Dualband boards */
4524 wlc_hw->_nbands = 2;
4525 else
4526 wlc_hw->_nbands = 1;
4527
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004528 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
Arend van Spriel5b435de2011-10-05 13:19:03 +02004529 wlc_hw->_nbands = 1;
4530
4531 /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
4532 * unconditionally does the init of these values
4533 */
4534 wlc->vendorid = wlc_hw->vendorid;
4535 wlc->deviceid = wlc_hw->deviceid;
4536 wlc->pub->sih = wlc_hw->sih;
4537 wlc->pub->corerev = wlc_hw->corerev;
4538 wlc->pub->sromrev = wlc_hw->sromrev;
4539 wlc->pub->boardrev = wlc_hw->boardrev;
4540 wlc->pub->boardflags = wlc_hw->boardflags;
4541 wlc->pub->boardflags2 = wlc_hw->boardflags2;
4542 wlc->pub->_nbands = wlc_hw->_nbands;
4543
4544 wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
4545
4546 if (wlc_hw->physhim == NULL) {
4547 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
4548 "failed\n", unit);
4549 err = 25;
4550 goto fail;
4551 }
4552
4553 /* pass all the parameters to wlc_phy_shared_attach in one struct */
4554 sha_params.sih = wlc_hw->sih;
4555 sha_params.physhim = wlc_hw->physhim;
4556 sha_params.unit = unit;
4557 sha_params.corerev = wlc_hw->corerev;
4558 sha_params.vid = wlc_hw->vendorid;
4559 sha_params.did = wlc_hw->deviceid;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004560 sha_params.chip = ai_get_chip_id(wlc_hw->sih);
4561 sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
4562 sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004563 sha_params.sromrev = wlc_hw->sromrev;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004564 sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004565 sha_params.boardrev = wlc_hw->boardrev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004566 sha_params.boardflags = wlc_hw->boardflags;
4567 sha_params.boardflags2 = wlc_hw->boardflags2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004568
4569 /* alloc and save pointer to shared phy state area */
4570 wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
4571 if (!wlc_hw->phy_sh) {
4572 err = 16;
4573 goto fail;
4574 }
4575
4576 /* initialize software state for each core and band */
4577 for (j = 0; j < wlc_hw->_nbands; j++) {
4578 /*
4579 * band0 is always 2.4Ghz
4580 * band1, if present, is 5Ghz
4581 */
4582
4583 brcms_c_setxband(wlc_hw, j);
4584
4585 wlc_hw->band->bandunit = j;
4586 wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4587 wlc->band->bandunit = j;
4588 wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
Arend van Spriel3b758a62011-12-12 15:15:09 -08004589 wlc->core->coreidx = core->core_index;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004590
Arend van Spriel16d28122011-12-08 15:06:51 -08004591 wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004592 wlc_hw->machwcap_backup = wlc_hw->machwcap;
4593
4594 /* init tx fifo size */
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004595 WARN_ON((wlc_hw->corerev - XMTFIFOTBL_STARTREV) < 0 ||
4596 (wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
4597 ARRAY_SIZE(xmtfifo_sz));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004598 wlc_hw->xmtfifo_sz =
4599 xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004600 WARN_ON(!wlc_hw->xmtfifo_sz[0]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004601
4602 /* Get a phy for this band */
4603 wlc_hw->band->pi =
Arend van Spriel4b006b12011-12-08 15:06:54 -08004604 wlc_phy_attach(wlc_hw->phy_sh, core,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004605 wlc_hw->band->bandtype,
4606 wlc->wiphy);
4607 if (wlc_hw->band->pi == NULL) {
4608 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
4609 "attach failed\n", unit);
4610 err = 17;
4611 goto fail;
4612 }
4613
4614 wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
4615
4616 wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
4617 &wlc_hw->band->phyrev,
4618 &wlc_hw->band->radioid,
4619 &wlc_hw->band->radiorev);
4620 wlc_hw->band->abgphy_encore =
4621 wlc_phy_get_encore(wlc_hw->band->pi);
4622 wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
4623 wlc_hw->band->core_flags =
4624 wlc_phy_get_coreflags(wlc_hw->band->pi);
4625
4626 /* verify good phy_type & supported phy revision */
4627 if (BRCMS_ISNPHY(wlc_hw->band)) {
4628 if (NCONF_HAS(wlc_hw->band->phyrev))
4629 goto good_phy;
4630 else
4631 goto bad_phy;
4632 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
4633 if (LCNCONF_HAS(wlc_hw->band->phyrev))
4634 goto good_phy;
4635 else
4636 goto bad_phy;
4637 } else {
4638 bad_phy:
4639 wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
4640 "phy type/rev (%d/%d)\n", unit,
4641 wlc_hw->band->phytype, wlc_hw->band->phyrev);
4642 err = 18;
4643 goto fail;
4644 }
4645
4646 good_phy:
4647 /*
4648 * BMAC_NOTE: wlc->band->pi should not be set below and should
4649 * be done in the high level attach. However we can not make
4650 * that change until all low level access is changed to
4651 * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
4652 * keeping wlc_hw->band->pi as well for incremental update of
4653 * low level fns, and cut over low only init when all fns
4654 * updated.
4655 */
4656 wlc->band->pi = wlc_hw->band->pi;
4657 wlc->band->phytype = wlc_hw->band->phytype;
4658 wlc->band->phyrev = wlc_hw->band->phyrev;
4659 wlc->band->radioid = wlc_hw->band->radioid;
4660 wlc->band->radiorev = wlc_hw->band->radiorev;
4661
4662 /* default contention windows size limits */
4663 wlc_hw->band->CWmin = APHY_CWMIN;
4664 wlc_hw->band->CWmax = PHY_CWMAX;
4665
4666 if (!brcms_b_attach_dmapio(wlc, j, wme)) {
4667 err = 19;
4668 goto fail;
4669 }
4670 }
4671
4672 /* disable core to match driver "down" state */
4673 brcms_c_coredisable(wlc_hw);
4674
4675 /* Match driver "down" state */
4676 ai_pci_down(wlc_hw->sih);
4677
Arend van Spriel5b435de2011-10-05 13:19:03 +02004678 /* turn off pll and xtal to match driver "down" state */
4679 brcms_b_xtal(wlc_hw, OFF);
4680
4681 /* *******************************************************************
4682 * The hardware is in the DOWN state at this point. D11 core
4683 * or cores are in reset with clocks off, and the board PLLs
4684 * are off if possible.
4685 *
4686 * Beyond this point, wlc->sbclk == false and chip registers
4687 * should not be touched.
4688 *********************************************************************
4689 */
4690
4691 /* init etheraddr state variables */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004692 brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
4693
4694 if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
Arend van Spriel5b435de2011-10-05 13:19:03 +02004695 is_zero_ether_addr(wlc_hw->etheraddr)) {
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004696 wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
4697 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004698 err = 22;
4699 goto fail;
4700 }
4701
Seth Forsheeb353dda2012-11-15 08:08:03 -06004702 brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
4703 wlc_hw->deviceid, wlc_hw->_nbands,
4704 ai_get_boardtype(wlc_hw->sih));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004705
4706 return err;
4707
4708 fail:
4709 wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
4710 err);
4711 return err;
4712}
4713
4714static void brcms_c_attach_antgain_init(struct brcms_c_info *wlc)
4715{
4716 uint unit;
4717 unit = wlc->pub->unit;
4718
4719 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
4720 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
4721 wlc->band->antgain = 8;
4722 } else if (wlc->band->antgain == -1) {
4723 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4724 " srom, using 2dB\n", unit, __func__);
4725 wlc->band->antgain = 8;
4726 } else {
4727 s8 gain, fract;
4728 /* Older sroms specified gain in whole dbm only. In order
4729 * be able to specify qdbm granularity and remain backward
4730 * compatible the whole dbms are now encoded in only
4731 * low 6 bits and remaining qdbms are encoded in the hi 2 bits.
4732 * 6 bit signed number ranges from -32 - 31.
4733 *
4734 * Examples:
4735 * 0x1 = 1 db,
4736 * 0xc1 = 1.75 db (1 + 3 quarters),
4737 * 0x3f = -1 (-1 + 0 quarters),
4738 * 0x7f = -.75 (-1 + 1 quarters) = -3 qdbm.
4739 * 0xbf = -.50 (-1 + 2 quarters) = -2 qdbm.
4740 */
4741 gain = wlc->band->antgain & 0x3f;
4742 gain <<= 2; /* Sign extend */
4743 gain >>= 2;
4744 fract = (wlc->band->antgain & 0xc0) >> 6;
4745 wlc->band->antgain = 4 * gain + fract;
4746 }
4747}
4748
4749static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
4750{
4751 int aa;
4752 uint unit;
4753 int bandtype;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004754 struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004755
4756 unit = wlc->pub->unit;
4757 bandtype = wlc->band->bandtype;
4758
4759 /* get antennas available */
4760 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004761 aa = sprom->ant_available_a;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004762 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004763 aa = sprom->ant_available_bg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004764
4765 if ((aa < 1) || (aa > 15)) {
4766 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4767 " srom (0x%x), using 3\n", unit, __func__, aa);
4768 aa = 3;
4769 }
4770
4771 /* reset the defaults if we have a single antenna */
4772 if (aa == 1) {
4773 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
4774 wlc->stf->txant = ANT_TX_FORCE_0;
4775 } else if (aa == 2) {
4776 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
4777 wlc->stf->txant = ANT_TX_FORCE_1;
4778 } else {
4779 }
4780
4781 /* Compute Antenna Gain */
4782 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004783 wlc->band->antgain = sprom->antenna_gain.a1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004784 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004785 wlc->band->antgain = sprom->antenna_gain.a0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004786
4787 brcms_c_attach_antgain_init(wlc);
4788
4789 return true;
4790}
4791
4792static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
4793{
4794 u16 chanspec;
4795 struct brcms_band *band;
4796 struct brcms_bss_info *bi = wlc->default_bss;
4797
4798 /* init default and target BSS with some sane initial values */
4799 memset((char *)(bi), 0, sizeof(struct brcms_bss_info));
4800 bi->beacon_period = BEACON_INTERVAL_DEFAULT;
4801
4802 /* fill the default channel as the first valid channel
4803 * starting from the 2G channels
4804 */
4805 chanspec = ch20mhz_chspec(1);
4806 wlc->home_chanspec = bi->chanspec = chanspec;
4807
4808 /* find the band of our default channel */
4809 band = wlc->band;
4810 if (wlc->pub->_nbands > 1 &&
4811 band->bandunit != chspec_bandunit(chanspec))
4812 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
4813
4814 /* init bss rates to the band specific default rate set */
4815 brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
4816 band->bandtype, false, BRCMS_RATE_MASK_FULL,
4817 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
4818 brcms_chspec_bw(chanspec), wlc->stf->txstreams);
4819
4820 if (wlc->pub->_n_enab & SUPPORT_11N)
4821 bi->flags |= BRCMS_BSS_HT;
4822}
4823
Arend van Spriel5b435de2011-10-05 13:19:03 +02004824static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
4825{
4826 uint i;
4827 struct brcms_band *band;
4828
4829 for (i = 0; i < wlc->pub->_nbands; i++) {
4830 band = wlc->bandstate[i];
4831 if (band->bandtype == BRCM_BAND_5G) {
4832 if ((bwcap == BRCMS_N_BW_40ALL)
4833 || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
4834 band->mimo_cap_40 = true;
4835 else
4836 band->mimo_cap_40 = false;
4837 } else {
4838 if (bwcap == BRCMS_N_BW_40ALL)
4839 band->mimo_cap_40 = true;
4840 else
4841 band->mimo_cap_40 = false;
4842 }
4843 }
4844}
4845
Arend van Spriel5b435de2011-10-05 13:19:03 +02004846static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
4847{
4848 /* free timer state */
4849 if (wlc->wdtimer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004850 brcms_free_timer(wlc->wdtimer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004851 wlc->wdtimer = NULL;
4852 }
4853 if (wlc->radio_timer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004854 brcms_free_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004855 wlc->radio_timer = NULL;
4856 }
4857}
4858
4859static void brcms_c_detach_module(struct brcms_c_info *wlc)
4860{
4861 if (wlc->asi) {
4862 brcms_c_antsel_detach(wlc->asi);
4863 wlc->asi = NULL;
4864 }
4865
4866 if (wlc->ampdu) {
4867 brcms_c_ampdu_detach(wlc->ampdu);
4868 wlc->ampdu = NULL;
4869 }
4870
4871 brcms_c_stf_detach(wlc);
4872}
4873
4874/*
4875 * low level detach
4876 */
4877static int brcms_b_detach(struct brcms_c_info *wlc)
4878{
4879 uint i;
4880 struct brcms_hw_band *band;
4881 struct brcms_hardware *wlc_hw = wlc->hw;
4882 int callbacks;
4883
4884 callbacks = 0;
4885
Arend van Spriel5b435de2011-10-05 13:19:03 +02004886 brcms_b_detach_dmapio(wlc_hw);
4887
4888 band = wlc_hw->band;
4889 for (i = 0; i < wlc_hw->_nbands; i++) {
4890 if (band->pi) {
4891 /* Detach this band's phy */
4892 wlc_phy_detach(band->pi);
4893 band->pi = NULL;
4894 }
4895 band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
4896 }
4897
4898 /* Free shared phy state */
4899 kfree(wlc_hw->phy_sh);
4900
4901 wlc_phy_shim_detach(wlc_hw->physhim);
4902
4903 if (wlc_hw->sih) {
4904 ai_detach(wlc_hw->sih);
4905 wlc_hw->sih = NULL;
4906 }
4907
4908 return callbacks;
4909
4910}
4911
4912/*
4913 * Return a count of the number of driver callbacks still pending.
4914 *
4915 * General policy is that brcms_c_detach can only dealloc/free software states.
4916 * It can NOT touch hardware registers since the d11core may be in reset and
4917 * clock may not be available.
4918 * One exception is sb register access, which is possible if crystal is turned
4919 * on after "down" state, driver should avoid software timer with the exception
4920 * of radio_monitor.
4921 */
4922uint brcms_c_detach(struct brcms_c_info *wlc)
4923{
4924 uint callbacks = 0;
4925
4926 if (wlc == NULL)
4927 return 0;
4928
4929 BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
4930
4931 callbacks += brcms_b_detach(wlc);
4932
4933 /* delete software timers */
4934 if (!brcms_c_radio_monitor_stop(wlc))
4935 callbacks++;
4936
4937 brcms_c_channel_mgr_detach(wlc->cmi);
4938
4939 brcms_c_timers_deinit(wlc);
4940
4941 brcms_c_detach_module(wlc);
4942
Arend van Spriel5b435de2011-10-05 13:19:03 +02004943 brcms_c_detach_mfree(wlc);
4944 return callbacks;
4945}
4946
4947/* update state that depends on the current value of "ap" */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004948static void brcms_c_ap_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004949{
4950 /* STA-BSS; short capable */
4951 wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004952}
4953
Arend van Spriel5b435de2011-10-05 13:19:03 +02004954/* Initialize just the hardware when coming out of POR or S3/S5 system states */
4955static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
4956{
4957 if (wlc_hw->wlc->pub->hw_up)
4958 return;
4959
Seth Forsheeb353dda2012-11-15 08:08:03 -06004960 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004961
4962 /*
4963 * Enable pll and xtal, initialize the power control registers,
4964 * and force fastclock for the remainder of brcms_c_up().
4965 */
4966 brcms_b_xtal(wlc_hw, ON);
4967 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004968 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004969
Arend van Spriel5b435de2011-10-05 13:19:03 +02004970 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08004971 * TODO: test suspend/resume
4972 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02004973 * AI chip doesn't restore bar0win2 on
4974 * hibernation/resume, need sw fixup
4975 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02004976
4977 /*
4978 * Inform phy that a POR reset has occurred so
4979 * it does a complete phy init
4980 */
4981 wlc_phy_por_inform(wlc_hw->band->pi);
4982
4983 wlc_hw->ucode_loaded = false;
4984 wlc_hw->wlc->pub->hw_up = true;
4985
4986 if ((wlc_hw->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004987 && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004988 if (!
4989 (wlc_hw->boardrev >= 0x1250
4990 && (wlc_hw->boardflags & BFL_FEM_BT)))
4991 ai_epa_4313war(wlc_hw->sih);
4992 }
4993}
4994
4995static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
4996{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004997 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004998
4999 /*
5000 * Enable pll and xtal, initialize the power control registers,
5001 * and force fastclock for the remainder of brcms_c_up().
5002 */
5003 brcms_b_xtal(wlc_hw, ON);
5004 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005005 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005006
5007 /*
5008 * Configure pci/pcmcia here instead of in brcms_c_attach()
5009 * to allow mfg hotswap: down, hotswap (chip power cycle), up.
5010 */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02005011 bcma_core_pci_irq_ctl(&wlc_hw->d11core->bus->drv_pci[0], wlc_hw->d11core,
Hauke Mehrtensb30ee752012-04-29 02:50:32 +02005012 true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005013
5014 /*
5015 * Need to read the hwradio status here to cover the case where the
5016 * system is loaded with the hw radio disabled. We do not want to
5017 * bring the driver up in this case.
5018 */
5019 if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
5020 /* put SB PCI in down state again */
5021 ai_pci_down(wlc_hw->sih);
5022 brcms_b_xtal(wlc_hw, OFF);
5023 return -ENOMEDIUM;
5024 }
5025
5026 ai_pci_up(wlc_hw->sih);
5027
5028 /* reset the d11 core */
5029 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
5030
5031 return 0;
5032}
5033
5034static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
5035{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005036 wlc_hw->up = true;
5037 wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
5038
5039 /* FULLY enable dynamic power control and d11 core interrupt */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005040 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005041 brcms_intrson(wlc_hw->wlc->wl);
5042 return 0;
5043}
5044
5045/*
5046 * Write WME tunable parameters for retransmit/max rate
5047 * from wlc struct to ucode
5048 */
5049static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
5050{
5051 int ac;
5052
5053 /* Need clock to do this */
5054 if (!wlc->clk)
5055 return;
5056
Arend van Sprielb7eec422011-11-10 20:30:18 +01005057 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005058 brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
5059 wlc->wme_retries[ac]);
5060}
5061
5062/* make interface operational */
5063int brcms_c_up(struct brcms_c_info *wlc)
5064{
Seth Forshee91691292012-06-16 07:47:49 -05005065 struct ieee80211_channel *ch;
5066
Seth Forsheeb353dda2012-11-15 08:08:03 -06005067 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005068
5069 /* HW is turned off so don't try to access it */
5070 if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
5071 return -ENOMEDIUM;
5072
5073 if (!wlc->pub->hw_up) {
5074 brcms_b_hw_up(wlc->hw);
5075 wlc->pub->hw_up = true;
5076 }
5077
5078 if ((wlc->pub->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02005079 && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005080 if (wlc->pub->boardrev >= 0x1250
5081 && (wlc->pub->boardflags & BFL_FEM_BT))
5082 brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
5083 MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
5084 else
5085 brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
5086 MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
5087 }
5088
5089 /*
5090 * Need to read the hwradio status here to cover the case where the
5091 * system is loaded with the hw radio disabled. We do not want to bring
5092 * the driver up in this case. If radio is disabled, abort up, lower
5093 * power, start radio timer and return 0(for NDIS) don't call
5094 * radio_update to avoid looping brcms_c_up.
5095 *
5096 * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
5097 */
5098 if (!wlc->pub->radio_disabled) {
5099 int status = brcms_b_up_prep(wlc->hw);
5100 if (status == -ENOMEDIUM) {
5101 if (!mboolisset
5102 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
5103 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
5104 mboolset(wlc->pub->radio_disabled,
5105 WL_RADIO_HW_DISABLE);
5106
5107 if (bsscfg->enable && bsscfg->BSS)
Seth Forsheeb353dda2012-11-15 08:08:03 -06005108 brcms_err(wlc->hw->d11core,
5109 "wl%d: up: rfdisable -> "
Arend van Spriel5b435de2011-10-05 13:19:03 +02005110 "bsscfg_disable()\n",
5111 wlc->pub->unit);
5112 }
5113 }
5114 }
5115
5116 if (wlc->pub->radio_disabled) {
5117 brcms_c_radio_monitor_start(wlc);
5118 return 0;
5119 }
5120
5121 /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
5122 wlc->clk = true;
5123
5124 brcms_c_radio_monitor_stop(wlc);
5125
5126 /* Set EDCF hostflags */
5127 brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
5128
5129 brcms_init(wlc->wl);
5130 wlc->pub->up = true;
5131
5132 if (wlc->bandinit_pending) {
Seth Forshee91691292012-06-16 07:47:49 -05005133 ch = wlc->pub->ieee_hw->conf.channel;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005134 brcms_c_suspend_mac_and_wait(wlc);
Seth Forshee91691292012-06-16 07:47:49 -05005135 brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005136 wlc->bandinit_pending = false;
5137 brcms_c_enable_mac(wlc);
5138 }
5139
5140 brcms_b_up_finish(wlc->hw);
5141
5142 /* Program the TX wme params with the current settings */
5143 brcms_c_wme_retries_write(wlc);
5144
5145 /* start one second watchdog timer */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005146 brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005147 wlc->WDarmed = true;
5148
5149 /* ensure antenna config is up to date */
5150 brcms_c_stf_phy_txant_upd(wlc);
5151 /* ensure LDPC config is in sync */
5152 brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
5153
5154 return 0;
5155}
5156
5157static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
5158{
5159 uint callbacks = 0;
5160
5161 return callbacks;
5162}
5163
5164static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
5165{
5166 bool dev_gone;
5167 uint callbacks = 0;
5168
Arend van Spriel5b435de2011-10-05 13:19:03 +02005169 if (!wlc_hw->up)
5170 return callbacks;
5171
5172 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5173
5174 /* disable interrupts */
5175 if (dev_gone)
5176 wlc_hw->wlc->macintmask = 0;
5177 else {
5178 /* now disable interrupts */
5179 brcms_intrsoff(wlc_hw->wlc->wl);
5180
5181 /* ensure we're running on the pll clock again */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005182 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005183 }
5184 /* down phy at the last of this stage */
5185 callbacks += wlc_phy_down(wlc_hw->band->pi);
5186
5187 return callbacks;
5188}
5189
5190static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
5191{
5192 uint callbacks = 0;
5193 bool dev_gone;
5194
Arend van Spriel5b435de2011-10-05 13:19:03 +02005195 if (!wlc_hw->up)
5196 return callbacks;
5197
5198 wlc_hw->up = false;
5199 wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
5200
5201 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5202
5203 if (dev_gone) {
5204 wlc_hw->sbclk = false;
5205 wlc_hw->clk = false;
5206 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
5207
5208 /* reclaim any posted packets */
5209 brcms_c_flushqueues(wlc_hw->wlc);
5210 } else {
5211
5212 /* Reset and disable the core */
Arend van Spriela8779e42011-12-08 15:06:58 -08005213 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08005214 if (bcma_read32(wlc_hw->d11core,
5215 D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005216 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
5217 callbacks += brcms_reset(wlc_hw->wlc->wl);
5218 brcms_c_coredisable(wlc_hw);
5219 }
5220
5221 /* turn off primary xtal and pll */
5222 if (!wlc_hw->noreset) {
5223 ai_pci_down(wlc_hw->sih);
5224 brcms_b_xtal(wlc_hw, OFF);
5225 }
5226 }
5227
5228 return callbacks;
5229}
5230
5231/*
5232 * Mark the interface nonoperational, stop the software mechanisms,
5233 * disable the hardware, free any transient buffer state.
5234 * Return a count of the number of driver callbacks still pending.
5235 */
5236uint brcms_c_down(struct brcms_c_info *wlc)
5237{
5238
5239 uint callbacks = 0;
5240 int i;
5241 bool dev_gone = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005242
Seth Forsheeb353dda2012-11-15 08:08:03 -06005243 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005244
5245 /* check if we are already in the going down path */
5246 if (wlc->going_down) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005247 brcms_err(wlc->hw->d11core,
5248 "wl%d: %s: Driver going down so return\n",
5249 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005250 return 0;
5251 }
5252 if (!wlc->pub->up)
5253 return callbacks;
5254
Arend van Spriel5b435de2011-10-05 13:19:03 +02005255 wlc->going_down = true;
5256
5257 callbacks += brcms_b_bmac_down_prep(wlc->hw);
5258
5259 dev_gone = brcms_deviceremoved(wlc);
5260
5261 /* Call any registered down handlers */
5262 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5263 if (wlc->modulecb[i].down_fn)
5264 callbacks +=
5265 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
5266 }
5267
5268 /* cancel the watchdog timer */
5269 if (wlc->WDarmed) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005270 if (!brcms_del_timer(wlc->wdtimer))
Arend van Spriel5b435de2011-10-05 13:19:03 +02005271 callbacks++;
5272 wlc->WDarmed = false;
5273 }
5274 /* cancel all other timers */
5275 callbacks += brcms_c_down_del_timer(wlc);
5276
5277 wlc->pub->up = false;
5278
5279 wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
5280
Arend van Spriel5b435de2011-10-05 13:19:03 +02005281 callbacks += brcms_b_down_finish(wlc->hw);
5282
5283 /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
5284 wlc->clk = false;
5285
5286 wlc->going_down = false;
5287 return callbacks;
5288}
5289
5290/* Set the current gmode configuration */
5291int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
5292{
5293 int ret = 0;
5294 uint i;
5295 struct brcms_c_rateset rs;
5296 /* Default to 54g Auto */
5297 /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
5298 s8 shortslot = BRCMS_SHORTSLOT_AUTO;
5299 bool shortslot_restrict = false; /* Restrict association to stations
5300 * that support shortslot
5301 */
5302 bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
5303 /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
5304 int preamble = BRCMS_PLCP_LONG;
5305 bool preamble_restrict = false; /* Restrict association to stations
5306 * that support short preambles
5307 */
5308 struct brcms_band *band;
5309
5310 /* if N-support is enabled, allow Gmode set as long as requested
5311 * Gmode is not GMODE_LEGACY_B
5312 */
5313 if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
5314 return -ENOTSUPP;
5315
5316 /* verify that we are dealing with 2G band and grab the band pointer */
5317 if (wlc->band->bandtype == BRCM_BAND_2G)
5318 band = wlc->band;
5319 else if ((wlc->pub->_nbands > 1) &&
5320 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
5321 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5322 else
5323 return -EINVAL;
5324
Arend van Spriel5b435de2011-10-05 13:19:03 +02005325 /* update configuration value */
Joe Perches23677ce2012-02-09 11:17:23 +00005326 if (config)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005327 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
5328
5329 /* Clear rateset override */
5330 memset(&rs, 0, sizeof(struct brcms_c_rateset));
5331
5332 switch (gmode) {
5333 case GMODE_LEGACY_B:
5334 shortslot = BRCMS_SHORTSLOT_OFF;
5335 brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
5336
5337 break;
5338
5339 case GMODE_LRS:
5340 break;
5341
5342 case GMODE_AUTO:
5343 /* Accept defaults */
5344 break;
5345
5346 case GMODE_ONLY:
5347 ofdm_basic = true;
5348 preamble = BRCMS_PLCP_SHORT;
5349 preamble_restrict = true;
5350 break;
5351
5352 case GMODE_PERFORMANCE:
5353 shortslot = BRCMS_SHORTSLOT_ON;
5354 shortslot_restrict = true;
5355 ofdm_basic = true;
5356 preamble = BRCMS_PLCP_SHORT;
5357 preamble_restrict = true;
5358 break;
5359
5360 default:
5361 /* Error */
Seth Forsheeb353dda2012-11-15 08:08:03 -06005362 brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005363 wlc->pub->unit, __func__, gmode);
5364 return -ENOTSUPP;
5365 }
5366
5367 band->gmode = gmode;
5368
5369 wlc->shortslot_override = shortslot;
5370
5371 /* Use the default 11g rateset */
5372 if (!rs.count)
5373 brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
5374
5375 if (ofdm_basic) {
5376 for (i = 0; i < rs.count; i++) {
5377 if (rs.rates[i] == BRCM_RATE_6M
5378 || rs.rates[i] == BRCM_RATE_12M
5379 || rs.rates[i] == BRCM_RATE_24M)
5380 rs.rates[i] |= BRCMS_RATE_FLAG;
5381 }
5382 }
5383
5384 /* Set default bss rateset */
5385 wlc->default_bss->rateset.count = rs.count;
5386 memcpy(wlc->default_bss->rateset.rates, rs.rates,
5387 sizeof(wlc->default_bss->rateset.rates));
5388
5389 return ret;
5390}
5391
5392int brcms_c_set_nmode(struct brcms_c_info *wlc)
5393{
5394 uint i;
5395 s32 nmode = AUTO;
5396
5397 if (wlc->stf->txstreams == WL_11N_3x3)
5398 nmode = WL_11N_3x3;
5399 else
5400 nmode = WL_11N_2x2;
5401
5402 /* force GMODE_AUTO if NMODE is ON */
5403 brcms_c_set_gmode(wlc, GMODE_AUTO, true);
5404 if (nmode == WL_11N_3x3)
5405 wlc->pub->_n_enab = SUPPORT_HT;
5406 else
5407 wlc->pub->_n_enab = SUPPORT_11N;
5408 wlc->default_bss->flags |= BRCMS_BSS_HT;
5409 /* add the mcs rates to the default and hw ratesets */
5410 brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
5411 wlc->stf->txstreams);
5412 for (i = 0; i < wlc->pub->_nbands; i++)
5413 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
5414 wlc->default_bss->rateset.mcs, MCSSET_LEN);
5415
5416 return 0;
5417}
5418
5419static int
5420brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
5421 struct brcms_c_rateset *rs_arg)
5422{
5423 struct brcms_c_rateset rs, new;
5424 uint bandunit;
5425
5426 memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
5427
5428 /* check for bad count value */
5429 if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
5430 return -EINVAL;
5431
5432 /* try the current band */
5433 bandunit = wlc->band->bandunit;
5434 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5435 if (brcms_c_rate_hwrs_filter_sort_validate
5436 (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
5437 wlc->stf->txstreams))
5438 goto good;
5439
5440 /* try the other band */
5441 if (brcms_is_mband_unlocked(wlc)) {
5442 bandunit = OTHERBANDUNIT(wlc);
5443 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5444 if (brcms_c_rate_hwrs_filter_sort_validate(&new,
5445 &wlc->
5446 bandstate[bandunit]->
5447 hw_rateset, true,
5448 wlc->stf->txstreams))
5449 goto good;
5450 }
5451
5452 return -EBADE;
5453
5454 good:
5455 /* apply new rateset */
5456 memcpy(&wlc->default_bss->rateset, &new,
5457 sizeof(struct brcms_c_rateset));
5458 memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
5459 sizeof(struct brcms_c_rateset));
5460 return 0;
5461}
5462
5463static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
5464{
5465 u8 r;
5466 bool war = false;
5467
5468 if (wlc->bsscfg->associated)
5469 r = wlc->bsscfg->current_bss->rateset.rates[0];
5470 else
5471 r = wlc->default_bss->rateset.rates[0];
5472
5473 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
5474}
5475
5476int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
5477{
5478 u16 chspec = ch20mhz_chspec(channel);
5479
5480 if (channel < 0 || channel > MAXCHANNEL)
5481 return -EINVAL;
5482
5483 if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
5484 return -EINVAL;
5485
5486
5487 if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
5488 if (wlc->band->bandunit != chspec_bandunit(chspec))
5489 wlc->bandinit_pending = true;
5490 else
5491 wlc->bandinit_pending = false;
5492 }
5493
5494 wlc->default_bss->chanspec = chspec;
5495 /* brcms_c_BSSinit() will sanitize the rateset before
5496 * using it.. */
5497 if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
5498 brcms_c_set_home_chanspec(wlc, chspec);
5499 brcms_c_suspend_mac_and_wait(wlc);
5500 brcms_c_set_chanspec(wlc, chspec);
5501 brcms_c_enable_mac(wlc);
5502 }
5503 return 0;
5504}
5505
5506int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
5507{
5508 int ac;
5509
5510 if (srl < 1 || srl > RETRY_SHORT_MAX ||
5511 lrl < 1 || lrl > RETRY_SHORT_MAX)
5512 return -EINVAL;
5513
5514 wlc->SRL = srl;
5515 wlc->LRL = lrl;
5516
5517 brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
5518
Arend van Sprielb7eec422011-11-10 20:30:18 +01005519 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005520 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5521 EDCF_SHORT, wlc->SRL);
5522 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5523 EDCF_LONG, wlc->LRL);
5524 }
5525 brcms_c_wme_retries_write(wlc);
5526
5527 return 0;
5528}
5529
5530void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
5531 struct brcm_rateset *currs)
5532{
5533 struct brcms_c_rateset *rs;
5534
5535 if (wlc->pub->associated)
5536 rs = &wlc->bsscfg->current_bss->rateset;
5537 else
5538 rs = &wlc->default_bss->rateset;
5539
5540 /* Copy only legacy rateset section */
5541 currs->count = rs->count;
5542 memcpy(&currs->rates, &rs->rates, rs->count);
5543}
5544
5545int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
5546{
5547 struct brcms_c_rateset internal_rs;
5548 int bcmerror;
5549
5550 if (rs->count > BRCMS_NUMRATES)
5551 return -ENOBUFS;
5552
5553 memset(&internal_rs, 0, sizeof(struct brcms_c_rateset));
5554
5555 /* Copy only legacy rateset section */
5556 internal_rs.count = rs->count;
5557 memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
5558
5559 /* merge rateset coming in with the current mcsset */
5560 if (wlc->pub->_n_enab & SUPPORT_11N) {
5561 struct brcms_bss_info *mcsset_bss;
5562 if (wlc->bsscfg->associated)
5563 mcsset_bss = wlc->bsscfg->current_bss;
5564 else
5565 mcsset_bss = wlc->default_bss;
5566 memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
5567 MCSSET_LEN);
5568 }
5569
5570 bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
5571 if (!bcmerror)
5572 brcms_c_ofdm_rateset_war(wlc);
5573
5574 return bcmerror;
5575}
5576
5577int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
5578{
5579 if (period < DOT11_MIN_BEACON_PERIOD ||
5580 period > DOT11_MAX_BEACON_PERIOD)
5581 return -EINVAL;
5582
5583 wlc->default_bss->beacon_period = period;
5584 return 0;
5585}
5586
5587u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
5588{
5589 return wlc->band->phytype;
5590}
5591
5592void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
5593{
5594 wlc->shortslot_override = sslot_override;
5595
5596 /*
5597 * shortslot is an 11g feature, so no more work if we are
5598 * currently on the 5G band
5599 */
5600 if (wlc->band->bandtype == BRCM_BAND_5G)
5601 return;
5602
5603 if (wlc->pub->up && wlc->pub->associated) {
5604 /* let watchdog or beacon processing update shortslot */
5605 } else if (wlc->pub->up) {
5606 /* unassociated shortslot is off */
5607 brcms_c_switch_shortslot(wlc, false);
5608 } else {
5609 /* driver is down, so just update the brcms_c_info
5610 * value */
5611 if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
5612 wlc->shortslot = false;
5613 else
5614 wlc->shortslot =
5615 (wlc->shortslot_override ==
5616 BRCMS_SHORTSLOT_ON);
5617 }
5618}
5619
5620/*
5621 * register watchdog and down handlers.
5622 */
5623int brcms_c_module_register(struct brcms_pub *pub,
5624 const char *name, struct brcms_info *hdl,
5625 int (*d_fn)(void *handle))
5626{
5627 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5628 int i;
5629
5630 /* find an empty entry and just add, no duplication check! */
5631 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5632 if (wlc->modulecb[i].name[0] == '\0') {
5633 strncpy(wlc->modulecb[i].name, name,
5634 sizeof(wlc->modulecb[i].name) - 1);
5635 wlc->modulecb[i].hdl = hdl;
5636 wlc->modulecb[i].down_fn = d_fn;
5637 return 0;
5638 }
5639 }
5640
5641 return -ENOSR;
5642}
5643
5644/* unregister module callbacks */
5645int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
5646 struct brcms_info *hdl)
5647{
5648 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5649 int i;
5650
5651 if (wlc == NULL)
5652 return -ENODATA;
5653
5654 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5655 if (!strcmp(wlc->modulecb[i].name, name) &&
5656 (wlc->modulecb[i].hdl == hdl)) {
5657 memset(&wlc->modulecb[i], 0, sizeof(struct modulecb));
5658 return 0;
5659 }
5660 }
5661
5662 /* table not found! */
5663 return -ENODATA;
5664}
5665
Arend van Spriel5b435de2011-10-05 13:19:03 +02005666void brcms_c_print_txstatus(struct tx_status *txs)
5667{
Joe Perches18aad4f2012-01-15 00:38:42 -08005668 pr_debug("\ntxpkt (MPDU) Complete\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005669
Joe Perches18aad4f2012-01-15 00:38:42 -08005670 pr_debug("FrameID: %04x TxStatus: %04x\n", txs->frameid, txs->status);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005671
Joe Perches18aad4f2012-01-15 00:38:42 -08005672 pr_debug("[15:12] %d frame attempts\n",
5673 (txs->status & TX_STATUS_FRM_RTX_MASK) >>
5674 TX_STATUS_FRM_RTX_SHIFT);
5675 pr_debug(" [11:8] %d rts attempts\n",
5676 (txs->status & TX_STATUS_RTS_RTX_MASK) >>
5677 TX_STATUS_RTS_RTX_SHIFT);
5678 pr_debug(" [7] %d PM mode indicated\n",
5679 txs->status & TX_STATUS_PMINDCTD ? 1 : 0);
5680 pr_debug(" [6] %d intermediate status\n",
5681 txs->status & TX_STATUS_INTERMEDIATE ? 1 : 0);
5682 pr_debug(" [5] %d AMPDU\n",
5683 txs->status & TX_STATUS_AMPDU ? 1 : 0);
5684 pr_debug(" [4:2] %d Frame Suppressed Reason (%s)\n",
5685 (txs->status & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT,
5686 (const char *[]) {
5687 "None",
5688 "PMQ Entry",
5689 "Flush request",
5690 "Previous frag failure",
5691 "Channel mismatch",
5692 "Lifetime Expiry",
5693 "Underflow"
5694 } [(txs->status & TX_STATUS_SUPR_MASK) >>
5695 TX_STATUS_SUPR_SHIFT]);
5696 pr_debug(" [1] %d acked\n",
5697 txs->status & TX_STATUS_ACK_RCV ? 1 : 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005698
Joe Perches18aad4f2012-01-15 00:38:42 -08005699 pr_debug("LastTxTime: %04x Seq: %04x PHYTxStatus: %04x RxAckRSSI: %04x RxAckSQ: %04x\n",
5700 txs->lasttxtime, txs->sequence, txs->phyerr,
5701 (txs->ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT,
5702 (txs->ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005703}
5704
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005705static bool brcms_c_chipmatch_pci(struct bcma_device *core)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005706{
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005707 struct pci_dev *pcidev = core->bus->host_pci;
5708 u16 vendor = pcidev->vendor;
5709 u16 device = pcidev->device;
5710
Arend van Spriel5b435de2011-10-05 13:19:03 +02005711 if (vendor != PCI_VENDOR_ID_BROADCOM) {
Joe Perches02f77192012-01-15 00:38:44 -08005712 pr_err("unknown vendor id %04x\n", vendor);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005713 return false;
5714 }
5715
5716 if (device == BCM43224_D11N_ID_VEN1)
5717 return true;
5718 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
5719 return true;
5720 if (device == BCM4313_D11N2G_ID)
5721 return true;
5722 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
5723 return true;
5724
Joe Perches02f77192012-01-15 00:38:44 -08005725 pr_err("unknown device id %04x\n", device);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005726 return false;
5727}
5728
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005729static bool brcms_c_chipmatch_soc(struct bcma_device *core)
5730{
5731 struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
5732
5733 if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
5734 return true;
5735
5736 pr_err("unknown chip id %04x\n", chipinfo->id);
5737 return false;
5738}
5739
5740bool brcms_c_chipmatch(struct bcma_device *core)
5741{
5742 switch (core->bus->hosttype) {
5743 case BCMA_HOSTTYPE_PCI:
5744 return brcms_c_chipmatch_pci(core);
5745 case BCMA_HOSTTYPE_SOC:
5746 return brcms_c_chipmatch_soc(core);
5747 default:
5748 pr_err("unknown host type: %i\n", core->bus->hosttype);
5749 return false;
5750 }
5751}
5752
Joe Perches8ae74652012-01-15 00:38:38 -08005753#if defined(DEBUG)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005754void brcms_c_print_txdesc(struct d11txh *txh)
5755{
5756 u16 mtcl = le16_to_cpu(txh->MacTxControlLow);
5757 u16 mtch = le16_to_cpu(txh->MacTxControlHigh);
5758 u16 mfc = le16_to_cpu(txh->MacFrameControl);
5759 u16 tfest = le16_to_cpu(txh->TxFesTimeNormal);
5760 u16 ptcw = le16_to_cpu(txh->PhyTxControlWord);
5761 u16 ptcw_1 = le16_to_cpu(txh->PhyTxControlWord_1);
5762 u16 ptcw_1_Fbr = le16_to_cpu(txh->PhyTxControlWord_1_Fbr);
5763 u16 ptcw_1_Rts = le16_to_cpu(txh->PhyTxControlWord_1_Rts);
5764 u16 ptcw_1_FbrRts = le16_to_cpu(txh->PhyTxControlWord_1_FbrRts);
5765 u16 mainrates = le16_to_cpu(txh->MainRates);
5766 u16 xtraft = le16_to_cpu(txh->XtraFrameTypes);
5767 u8 *iv = txh->IV;
5768 u8 *ra = txh->TxFrameRA;
5769 u16 tfestfb = le16_to_cpu(txh->TxFesTimeFallback);
5770 u8 *rtspfb = txh->RTSPLCPFallback;
5771 u16 rtsdfb = le16_to_cpu(txh->RTSDurFallback);
5772 u8 *fragpfb = txh->FragPLCPFallback;
5773 u16 fragdfb = le16_to_cpu(txh->FragDurFallback);
5774 u16 mmodelen = le16_to_cpu(txh->MModeLen);
5775 u16 mmodefbrlen = le16_to_cpu(txh->MModeFbrLen);
5776 u16 tfid = le16_to_cpu(txh->TxFrameID);
5777 u16 txs = le16_to_cpu(txh->TxStatus);
5778 u16 mnmpdu = le16_to_cpu(txh->MaxNMpdus);
5779 u16 mabyte = le16_to_cpu(txh->MaxABytes_MRT);
5780 u16 mabyte_f = le16_to_cpu(txh->MaxABytes_FBR);
5781 u16 mmbyte = le16_to_cpu(txh->MinMBytes);
5782
5783 u8 *rtsph = txh->RTSPhyHeader;
5784 struct ieee80211_rts rts = txh->rts_frame;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005785
5786 /* add plcp header along with txh descriptor */
Joe Perchesc2e6d5a2012-01-15 00:38:43 -08005787 brcmu_dbg_hex_dump(txh, sizeof(struct d11txh) + 48,
5788 "Raw TxDesc + plcp header:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005789
Joe Perches18aad4f2012-01-15 00:38:42 -08005790 pr_debug("TxCtlLow: %04x ", mtcl);
5791 pr_debug("TxCtlHigh: %04x ", mtch);
5792 pr_debug("FC: %04x ", mfc);
5793 pr_debug("FES Time: %04x\n", tfest);
5794 pr_debug("PhyCtl: %04x%s ", ptcw,
Arend van Spriel5b435de2011-10-05 13:19:03 +02005795 (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
Joe Perches18aad4f2012-01-15 00:38:42 -08005796 pr_debug("PhyCtl_1: %04x ", ptcw_1);
5797 pr_debug("PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
5798 pr_debug("PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
5799 pr_debug("PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
5800 pr_debug("MainRates: %04x ", mainrates);
5801 pr_debug("XtraFrameTypes: %04x ", xtraft);
5802 pr_debug("\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005803
Arend van Spriel09c7dfa2011-10-18 14:03:10 +02005804 print_hex_dump_bytes("SecIV:", DUMP_PREFIX_OFFSET, iv, sizeof(txh->IV));
5805 print_hex_dump_bytes("RA:", DUMP_PREFIX_OFFSET,
5806 ra, sizeof(txh->TxFrameRA));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005807
Joe Perches18aad4f2012-01-15 00:38:42 -08005808 pr_debug("Fb FES Time: %04x ", tfestfb);
Arend van Spriel09c7dfa2011-10-18 14:03:10 +02005809 print_hex_dump_bytes("Fb RTS PLCP:", DUMP_PREFIX_OFFSET,
5810 rtspfb, sizeof(txh->RTSPLCPFallback));
Joe Perches18aad4f2012-01-15 00:38:42 -08005811 pr_debug("RTS DUR: %04x ", rtsdfb);
Arend van Spriel09c7dfa2011-10-18 14:03:10 +02005812 print_hex_dump_bytes("PLCP:", DUMP_PREFIX_OFFSET,
5813 fragpfb, sizeof(txh->FragPLCPFallback));
Joe Perches18aad4f2012-01-15 00:38:42 -08005814 pr_debug("DUR: %04x", fragdfb);
5815 pr_debug("\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005816
Joe Perches18aad4f2012-01-15 00:38:42 -08005817 pr_debug("MModeLen: %04x ", mmodelen);
5818 pr_debug("MModeFbrLen: %04x\n", mmodefbrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005819
Joe Perches18aad4f2012-01-15 00:38:42 -08005820 pr_debug("FrameID: %04x\n", tfid);
5821 pr_debug("TxStatus: %04x\n", txs);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005822
Joe Perches18aad4f2012-01-15 00:38:42 -08005823 pr_debug("MaxNumMpdu: %04x\n", mnmpdu);
5824 pr_debug("MaxAggbyte: %04x\n", mabyte);
5825 pr_debug("MaxAggbyte_fb: %04x\n", mabyte_f);
5826 pr_debug("MinByte: %04x\n", mmbyte);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005827
Arend van Spriel09c7dfa2011-10-18 14:03:10 +02005828 print_hex_dump_bytes("RTS PLCP:", DUMP_PREFIX_OFFSET,
5829 rtsph, sizeof(txh->RTSPhyHeader));
5830 print_hex_dump_bytes("RTS Frame:", DUMP_PREFIX_OFFSET,
5831 (u8 *)&rts, sizeof(txh->rts_frame));
Joe Perches18aad4f2012-01-15 00:38:42 -08005832 pr_debug("\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005833}
Joe Perches8ae74652012-01-15 00:38:38 -08005834#endif /* defined(DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02005835
Joe Perches8ae74652012-01-15 00:38:38 -08005836#if defined(DEBUG)
Arend van Spriel094b1992011-10-18 14:03:07 +02005837static int
Alwin Beukers44760652011-10-12 20:51:31 +02005838brcms_c_format_flags(const struct brcms_c_bit_desc *bd, u32 flags, char *buf,
Arend van Spriel094b1992011-10-18 14:03:07 +02005839 int len)
Alwin Beukers44760652011-10-12 20:51:31 +02005840{
5841 int i;
5842 char *p = buf;
5843 char hexstr[16];
5844 int slen = 0, nlen = 0;
5845 u32 bit;
5846 const char *name;
5847
5848 if (len < 2 || !buf)
5849 return 0;
5850
5851 buf[0] = '\0';
5852
5853 for (i = 0; flags != 0; i++) {
5854 bit = bd[i].bit;
5855 name = bd[i].name;
5856 if (bit == 0 && flags != 0) {
5857 /* print any unnamed bits */
5858 snprintf(hexstr, 16, "0x%X", flags);
5859 name = hexstr;
5860 flags = 0; /* exit loop */
5861 } else if ((flags & bit) == 0)
5862 continue;
5863 flags &= ~bit;
5864 nlen = strlen(name);
5865 slen += nlen;
5866 /* count btwn flag space */
5867 if (flags != 0)
5868 slen += 1;
5869 /* need NULL char as well */
5870 if (len <= slen)
5871 break;
5872 /* copy NULL char but don't count it */
5873 strncpy(p, name, nlen + 1);
5874 p += nlen;
5875 /* copy btwn flag space and NULL char */
5876 if (flags != 0)
5877 p += snprintf(p, 2, " ");
5878 len -= slen;
5879 }
5880
5881 /* indicate the str was too short */
5882 if (flags != 0) {
5883 if (len < 2)
5884 p -= 2 - len; /* overwrite last char */
5885 p += snprintf(p, 2, ">");
5886 }
5887
5888 return (int)(p - buf);
5889}
Joe Perches8ae74652012-01-15 00:38:38 -08005890#endif /* defined(DEBUG) */
Alwin Beukers44760652011-10-12 20:51:31 +02005891
Joe Perches8ae74652012-01-15 00:38:38 -08005892#if defined(DEBUG)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005893void brcms_c_print_rxh(struct d11rxhdr *rxh)
5894{
5895 u16 len = rxh->RxFrameSize;
5896 u16 phystatus_0 = rxh->PhyRxStatus_0;
5897 u16 phystatus_1 = rxh->PhyRxStatus_1;
5898 u16 phystatus_2 = rxh->PhyRxStatus_2;
5899 u16 phystatus_3 = rxh->PhyRxStatus_3;
5900 u16 macstatus1 = rxh->RxStatus1;
5901 u16 macstatus2 = rxh->RxStatus2;
5902 char flagstr[64];
5903 char lenbuf[20];
Alwin Beukers44760652011-10-12 20:51:31 +02005904 static const struct brcms_c_bit_desc macstat_flags[] = {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005905 {RXS_FCSERR, "FCSErr"},
5906 {RXS_RESPFRAMETX, "Reply"},
5907 {RXS_PBPRES, "PADDING"},
5908 {RXS_DECATMPT, "DeCr"},
5909 {RXS_DECERR, "DeCrErr"},
5910 {RXS_BCNSENT, "Bcn"},
5911 {0, NULL}
5912 };
5913
Joe Perchesc2e6d5a2012-01-15 00:38:43 -08005914 brcmu_dbg_hex_dump(rxh, sizeof(struct d11rxhdr), "Raw RxDesc:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02005915
Alwin Beukers44760652011-10-12 20:51:31 +02005916 brcms_c_format_flags(macstat_flags, macstatus1, flagstr, 64);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005917
5918 snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
5919
Joe Perches18aad4f2012-01-15 00:38:42 -08005920 pr_debug("RxFrameSize: %6s (%d)%s\n", lenbuf, len,
Arend van Spriel5b435de2011-10-05 13:19:03 +02005921 (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
Joe Perches18aad4f2012-01-15 00:38:42 -08005922 pr_debug("RxPHYStatus: %04x %04x %04x %04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005923 phystatus_0, phystatus_1, phystatus_2, phystatus_3);
Joe Perches18aad4f2012-01-15 00:38:42 -08005924 pr_debug("RxMACStatus: %x %s\n", macstatus1, flagstr);
5925 pr_debug("RXMACaggtype: %x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005926 (macstatus2 & RXS_AGGTYPE_MASK));
Joe Perches18aad4f2012-01-15 00:38:42 -08005927 pr_debug("RxTSFTime: %04x\n", rxh->RxTSFTime);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005928}
Joe Perches8ae74652012-01-15 00:38:38 -08005929#endif /* defined(DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02005930
5931u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
5932{
5933 u16 table_ptr;
5934 u8 phy_rate, index;
5935
5936 /* get the phy specific rate encoding for the PLCP SIGNAL field */
5937 if (is_ofdm_rate(rate))
5938 table_ptr = M_RT_DIRMAP_A;
5939 else
5940 table_ptr = M_RT_DIRMAP_B;
5941
5942 /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
5943 * the index into the rate table.
5944 */
5945 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
5946 index = phy_rate & 0xf;
5947
5948 /* Find the SHM pointer to the rate table entry by looking in the
5949 * Direct-map Table
5950 */
5951 return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
5952}
5953
Arend van Spriel5b435de2011-10-05 13:19:03 +02005954/*
5955 * bcmc_fid_generate:
5956 * Generate frame ID for a BCMC packet. The frag field is not used
5957 * for MC frames so is used as part of the sequence number.
5958 */
5959static inline u16
5960bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
5961 struct d11txh *txh)
5962{
5963 u16 frameid;
5964
5965 frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
5966 TXFID_QUEUE_MASK);
5967 frameid |=
5968 (((wlc->
5969 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5970 TX_BCMC_FIFO;
5971
5972 return frameid;
5973}
5974
5975static uint
5976brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
5977 u8 preamble_type)
5978{
5979 uint dur = 0;
5980
5981 BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d\n",
5982 wlc->pub->unit, rspec, preamble_type);
5983 /*
5984 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5985 * is less than or equal to the rate of the immediately previous
5986 * frame in the FES
5987 */
5988 rspec = brcms_basic_rate(wlc, rspec);
5989 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
5990 dur =
5991 brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5992 (DOT11_ACK_LEN + FCS_LEN));
5993 return dur;
5994}
5995
5996static uint
5997brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
5998 u8 preamble_type)
5999{
6000 BCMMSG(wlc->wiphy, "wl%d: ratespec 0x%x, preamble_type %d\n",
6001 wlc->pub->unit, rspec, preamble_type);
6002 return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
6003}
6004
6005static uint
6006brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
6007 u8 preamble_type)
6008{
6009 BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, "
Seth Forsheeb353dda2012-11-15 08:08:03 -06006010 "preamble_type %d\n", wlc->pub->unit, rspec, preamble_type);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006011 /*
6012 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
6013 * is less than or equal to the rate of the immediately previous
6014 * frame in the FES
6015 */
6016 rspec = brcms_basic_rate(wlc, rspec);
6017 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
6018 return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
6019 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
6020 FCS_LEN));
6021}
6022
6023/* brcms_c_compute_frame_dur()
6024 *
6025 * Calculate the 802.11 MAC header DUR field for MPDU
6026 * DUR for a single frame = 1 SIFS + 1 ACK
6027 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
6028 *
6029 * rate MPDU rate in unit of 500kbps
6030 * next_frag_len next MPDU length in bytes
6031 * preamble_type use short/GF or long/MM PLCP header
6032 */
6033static u16
6034brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
6035 u8 preamble_type, uint next_frag_len)
6036{
6037 u16 dur, sifs;
6038
6039 sifs = get_sifs(wlc->band);
6040
6041 dur = sifs;
6042 dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
6043
6044 if (next_frag_len) {
6045 /* Double the current DUR to get 2 SIFS + 2 ACKs */
6046 dur *= 2;
6047 /* add another SIFS and the frag time */
6048 dur += sifs;
6049 dur +=
6050 (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
6051 next_frag_len);
6052 }
6053 return dur;
6054}
6055
6056/* The opposite of brcms_c_calc_frame_time */
6057static uint
6058brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
6059 u8 preamble_type, uint dur)
6060{
6061 uint nsyms, mac_len, Ndps, kNdps;
6062 uint rate = rspec2rate(ratespec);
6063
6064 BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, dur %d\n",
6065 wlc->pub->unit, ratespec, preamble_type, dur);
6066
6067 if (is_mcs_rate(ratespec)) {
6068 uint mcs = ratespec & RSPEC_RATE_MASK;
6069 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
6070 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
6071 /* payload calculation matches that of regular ofdm */
6072 if (wlc->band->bandtype == BRCM_BAND_2G)
6073 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
6074 /* kNdbps = kbps * 4 */
6075 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
6076 rspec_issgi(ratespec)) * 4;
6077 nsyms = dur / APHY_SYMBOL_TIME;
6078 mac_len =
6079 ((nsyms * kNdps) -
6080 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
6081 } else if (is_ofdm_rate(ratespec)) {
6082 dur -= APHY_PREAMBLE_TIME;
6083 dur -= APHY_SIGNAL_TIME;
6084 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
6085 Ndps = rate * 2;
6086 nsyms = dur / APHY_SYMBOL_TIME;
6087 mac_len =
6088 ((nsyms * Ndps) -
6089 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
6090 } else {
6091 if (preamble_type & BRCMS_SHORT_PREAMBLE)
6092 dur -= BPHY_PLCP_SHORT_TIME;
6093 else
6094 dur -= BPHY_PLCP_TIME;
6095 mac_len = dur * rate;
6096 /* divide out factor of 2 in rate (1/2 mbps) */
6097 mac_len = mac_len / 8 / 2;
6098 }
6099 return mac_len;
6100}
6101
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006102/*
6103 * Return true if the specified rate is supported by the specified band.
6104 * BRCM_BAND_AUTO indicates the current band.
6105 */
6106static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
6107 bool verbose)
6108{
6109 struct brcms_c_rateset *hw_rateset;
6110 uint i;
6111
6112 if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
6113 hw_rateset = &wlc->band->hw_rateset;
6114 else if (wlc->pub->_nbands > 1)
6115 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
6116 else
6117 /* other band specified and we are a single band device */
6118 return false;
6119
6120 /* check if this is a mimo rate */
6121 if (is_mcs_rate(rspec)) {
6122 if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
6123 goto error;
6124
6125 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
6126 }
6127
6128 for (i = 0; i < hw_rateset->count; i++)
6129 if (hw_rateset->rates[i] == rspec2rate(rspec))
6130 return true;
6131 error:
6132 if (verbose)
Seth Forsheeb353dda2012-11-15 08:08:03 -06006133 brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006134 "not in hw_rateset\n", wlc->pub->unit, rspec);
6135
6136 return false;
6137}
6138
Arend van Spriel5b435de2011-10-05 13:19:03 +02006139static u32
6140mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
6141 u32 int_val)
6142{
Seth Forsheeb353dda2012-11-15 08:08:03 -06006143 struct bcma_device *core = wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006144 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
6145 u8 rate = int_val & NRATE_RATE_MASK;
6146 u32 rspec;
6147 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
6148 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
6149 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
6150 == NRATE_OVERRIDE_MCS_ONLY);
6151 int bcmerror = 0;
6152
6153 if (!ismcs)
6154 return (u32) rate;
6155
6156 /* validate the combination of rate/mcs/stf is allowed */
6157 if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
6158 /* mcs only allowed when nmode */
6159 if (stf > PHY_TXC1_MODE_SDM) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006160 brcms_err(core, "wl%d: %s: Invalid stf\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02006161 wlc->pub->unit, __func__);
6162 bcmerror = -EINVAL;
6163 goto done;
6164 }
6165
6166 /* mcs 32 is a special case, DUP mode 40 only */
6167 if (rate == 32) {
6168 if (!CHSPEC_IS40(wlc->home_chanspec) ||
6169 ((stf != PHY_TXC1_MODE_SISO)
6170 && (stf != PHY_TXC1_MODE_CDD))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006171 brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
6172 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006173 bcmerror = -EINVAL;
6174 goto done;
6175 }
6176 /* mcs > 7 must use stf SDM */
6177 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
6178 /* mcs > 7 must use stf SDM */
6179 if (stf != PHY_TXC1_MODE_SDM) {
6180 BCMMSG(wlc->wiphy, "wl%d: enabling "
6181 "SDM mode for mcs %d\n",
6182 wlc->pub->unit, rate);
6183 stf = PHY_TXC1_MODE_SDM;
6184 }
6185 } else {
6186 /*
6187 * MCS 0-7 may use SISO, CDD, and for
6188 * phy_rev >= 3 STBC
6189 */
6190 if ((stf > PHY_TXC1_MODE_STBC) ||
6191 (!BRCMS_STBC_CAP_PHY(wlc)
6192 && (stf == PHY_TXC1_MODE_STBC))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006193 brcms_err(core, "wl%d: %s: Invalid STBC\n",
6194 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006195 bcmerror = -EINVAL;
6196 goto done;
6197 }
6198 }
6199 } else if (is_ofdm_rate(rate)) {
6200 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006201 brcms_err(core, "wl%d: %s: Invalid OFDM\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02006202 wlc->pub->unit, __func__);
6203 bcmerror = -EINVAL;
6204 goto done;
6205 }
6206 } else if (is_cck_rate(rate)) {
6207 if ((cur_band->bandtype != BRCM_BAND_2G)
6208 || (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006209 brcms_err(core, "wl%d: %s: Invalid CCK\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02006210 wlc->pub->unit, __func__);
6211 bcmerror = -EINVAL;
6212 goto done;
6213 }
6214 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006215 brcms_err(core, "wl%d: %s: Unknown rate type\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02006216 wlc->pub->unit, __func__);
6217 bcmerror = -EINVAL;
6218 goto done;
6219 }
6220 /* make sure multiple antennae are available for non-siso rates */
6221 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006222 brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
Arend van Spriel5b435de2011-10-05 13:19:03 +02006223 "request\n", wlc->pub->unit, __func__);
6224 bcmerror = -EINVAL;
6225 goto done;
6226 }
6227
6228 rspec = rate;
6229 if (ismcs) {
6230 rspec |= RSPEC_MIMORATE;
6231 /* For STBC populate the STC field of the ratespec */
6232 if (stf == PHY_TXC1_MODE_STBC) {
6233 u8 stc;
6234 stc = 1; /* Nss for single stream is always 1 */
6235 rspec |= (stc << RSPEC_STC_SHIFT);
6236 }
6237 }
6238
6239 rspec |= (stf << RSPEC_STF_SHIFT);
6240
6241 if (override_mcs_only)
6242 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
6243
6244 if (issgi)
6245 rspec |= RSPEC_SHORT_GI;
6246
6247 if ((rate != 0)
6248 && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
6249 return rate;
6250
6251 return rspec;
6252done:
6253 return rate;
6254}
6255
6256/*
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006257 * Compute PLCP, but only requires actual rate and length of pkt.
6258 * Rate is given in the driver standard multiple of 500 kbps.
6259 * le is set for 11 Mbps rate if necessary.
6260 * Broken out for PRQ.
6261 */
6262
6263static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
6264 uint length, u8 *plcp)
6265{
6266 u16 usec = 0;
6267 u8 le = 0;
6268
6269 switch (rate_500) {
6270 case BRCM_RATE_1M:
6271 usec = length << 3;
6272 break;
6273 case BRCM_RATE_2M:
6274 usec = length << 2;
6275 break;
6276 case BRCM_RATE_5M5:
6277 usec = (length << 4) / 11;
6278 if ((length << 4) - (usec * 11) > 0)
6279 usec++;
6280 break;
6281 case BRCM_RATE_11M:
6282 usec = (length << 3) / 11;
6283 if ((length << 3) - (usec * 11) > 0) {
6284 usec++;
6285 if ((usec * 11) - (length << 3) >= 8)
6286 le = D11B_PLCP_SIGNAL_LE;
6287 }
6288 break;
6289
6290 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06006291 brcms_err(wlc->hw->d11core,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006292 "brcms_c_cck_plcp_set: unsupported rate %d\n",
6293 rate_500);
6294 rate_500 = BRCM_RATE_1M;
6295 usec = length << 3;
6296 break;
6297 }
6298 /* PLCP signal byte */
6299 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
6300 /* PLCP service byte */
6301 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
6302 /* PLCP length u16, little endian */
6303 plcp[2] = usec & 0xff;
6304 plcp[3] = (usec >> 8) & 0xff;
6305 /* PLCP CRC16 */
6306 plcp[4] = 0;
6307 plcp[5] = 0;
6308}
6309
6310/* Rate: 802.11 rate code, length: PSDU length in octets */
6311static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
6312{
6313 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
6314 plcp[0] = mcs;
6315 if (rspec_is40mhz(rspec) || (mcs == 32))
6316 plcp[0] |= MIMO_PLCP_40MHZ;
6317 BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
6318 plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
6319 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
6320 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
6321 plcp[5] = 0;
6322}
6323
6324/* Rate: 802.11 rate code, length: PSDU length in octets */
6325static void
6326brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
6327{
6328 u8 rate_signal;
6329 u32 tmp = 0;
6330 int rate = rspec2rate(rspec);
6331
6332 /*
6333 * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
6334 * transmitted first
6335 */
6336 rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
6337 memset(plcp, 0, D11_PHY_HDR_LEN);
6338 D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
6339
6340 tmp = (length & 0xfff) << 5;
6341 plcp[2] |= (tmp >> 16) & 0xff;
6342 plcp[1] |= (tmp >> 8) & 0xff;
6343 plcp[0] |= tmp & 0xff;
6344}
6345
6346/* Rate: 802.11 rate code, length: PSDU length in octets */
6347static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
6348 uint length, u8 *plcp)
6349{
6350 int rate = rspec2rate(rspec);
6351
6352 brcms_c_cck_plcp_set(wlc, rate, length, plcp);
6353}
6354
6355static void
6356brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
6357 uint length, u8 *plcp)
6358{
6359 if (is_mcs_rate(rspec))
6360 brcms_c_compute_mimo_plcp(rspec, length, plcp);
6361 else if (is_ofdm_rate(rspec))
6362 brcms_c_compute_ofdm_plcp(rspec, length, plcp);
6363 else
6364 brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
6365}
6366
6367/* brcms_c_compute_rtscts_dur()
6368 *
6369 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
6370 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
6371 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
6372 *
6373 * cts cts-to-self or rts/cts
6374 * rts_rate rts or cts rate in unit of 500kbps
6375 * rate next MPDU rate in unit of 500kbps
6376 * frame_len next MPDU frame length in bytes
6377 */
6378u16
6379brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
6380 u32 rts_rate,
6381 u32 frame_rate, u8 rts_preamble_type,
6382 u8 frame_preamble_type, uint frame_len, bool ba)
6383{
6384 u16 dur, sifs;
6385
6386 sifs = get_sifs(wlc->band);
6387
6388 if (!cts_only) {
6389 /* RTS/CTS */
6390 dur = 3 * sifs;
6391 dur +=
6392 (u16) brcms_c_calc_cts_time(wlc, rts_rate,
6393 rts_preamble_type);
6394 } else {
6395 /* CTS-TO-SELF */
6396 dur = 2 * sifs;
6397 }
6398
6399 dur +=
6400 (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
6401 frame_len);
6402 if (ba)
6403 dur +=
6404 (u16) brcms_c_calc_ba_time(wlc, frame_rate,
6405 BRCMS_SHORT_PREAMBLE);
6406 else
6407 dur +=
6408 (u16) brcms_c_calc_ack_time(wlc, frame_rate,
6409 frame_preamble_type);
6410 return dur;
6411}
6412
6413static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
6414{
6415 u16 phyctl1 = 0;
6416 u16 bw;
6417
6418 if (BRCMS_ISLCNPHY(wlc->band)) {
6419 bw = PHY_TXC1_BW_20MHZ;
6420 } else {
6421 bw = rspec_get_bw(rspec);
6422 /* 10Mhz is not supported yet */
6423 if (bw < PHY_TXC1_BW_20MHZ) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006424 brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006425 "not supported yet, set to 20L\n", bw);
6426 bw = PHY_TXC1_BW_20MHZ;
6427 }
6428 }
6429
6430 if (is_mcs_rate(rspec)) {
6431 uint mcs = rspec & RSPEC_RATE_MASK;
6432
6433 /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
6434 phyctl1 = rspec_phytxbyte2(rspec);
6435 /* set the upper byte of phyctl1 */
6436 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
6437 } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
6438 && !BRCMS_ISSSLPNPHY(wlc->band)) {
6439 /*
6440 * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
6441 * Data Rate. Eventually MIMOPHY would also be converted to
6442 * this format
6443 */
6444 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
6445 phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6446 } else { /* legacy OFDM/CCK */
6447 s16 phycfg;
6448 /* get the phyctl byte from rate phycfg table */
6449 phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
6450 if (phycfg == -1) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006451 brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006452 "legacy OFDM/CCK rate\n");
6453 phycfg = 0;
6454 }
6455 /* set the upper byte of phyctl1 */
6456 phyctl1 =
6457 (bw | (phycfg << 8) |
6458 (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6459 }
6460 return phyctl1;
6461}
6462
6463/*
Arend van Spriel5b435de2011-10-05 13:19:03 +02006464 * Add struct d11txh, struct cck_phy_hdr.
6465 *
6466 * 'p' data must start with 802.11 MAC header
6467 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
6468 *
6469 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
6470 *
6471 */
6472static u16
6473brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
6474 struct sk_buff *p, struct scb *scb, uint frag,
6475 uint nfrags, uint queue, uint next_frag_len)
6476{
6477 struct ieee80211_hdr *h;
6478 struct d11txh *txh;
6479 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
6480 int len, phylen, rts_phylen;
6481 u16 mch, phyctl, xfts, mainrates;
6482 u16 seq = 0, mcl = 0, status = 0, frameid = 0;
6483 u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6484 u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6485 bool use_rts = false;
6486 bool use_cts = false;
6487 bool use_rifs = false;
6488 bool short_preamble[2] = { false, false };
6489 u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6490 u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6491 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
6492 struct ieee80211_rts *rts = NULL;
6493 bool qos;
6494 uint ac;
6495 bool hwtkmic = false;
6496 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
6497#define ANTCFG_NONE 0xFF
6498 u8 antcfg = ANTCFG_NONE;
6499 u8 fbantcfg = ANTCFG_NONE;
6500 uint phyctl1_stf = 0;
6501 u16 durid = 0;
6502 struct ieee80211_tx_rate *txrate[2];
6503 int k;
6504 struct ieee80211_tx_info *tx_info;
6505 bool is_mcs;
6506 u16 mimo_txbw;
6507 u8 mimo_preamble_type;
6508
6509 /* locate 802.11 MAC header */
6510 h = (struct ieee80211_hdr *)(p->data);
6511 qos = ieee80211_is_data_qos(h->frame_control);
6512
6513 /* compute length of frame in bytes for use in PLCP computations */
Arend van Sprielad4d71f2011-11-10 20:30:26 +01006514 len = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006515 phylen = len + FCS_LEN;
6516
6517 /* Get tx_info */
6518 tx_info = IEEE80211_SKB_CB(p);
6519
6520 /* add PLCP */
6521 plcp = skb_push(p, D11_PHY_HDR_LEN);
6522
6523 /* add Broadcom tx descriptor header */
6524 txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
6525 memset(txh, 0, D11_TXH_LEN);
6526
6527 /* setup frameid */
6528 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
6529 /* non-AP STA should never use BCMC queue */
6530 if (queue == TX_BCMC_FIFO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006531 brcms_err(wlc->hw->d11core,
6532 "wl%d: %s: ASSERT queue == TX_BCMC!\n",
6533 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006534 frameid = bcmc_fid_generate(wlc, NULL, txh);
6535 } else {
6536 /* Increment the counter for first fragment */
6537 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
6538 scb->seqnum[p->priority]++;
6539
6540 /* extract fragment number from frame first */
6541 seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
6542 seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
6543 h->seq_ctrl = cpu_to_le16(seq);
6544
6545 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6546 (queue & TXFID_QUEUE_MASK);
6547 }
6548 }
6549 frameid |= queue & TXFID_QUEUE_MASK;
6550
6551 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
6552 if (ieee80211_is_beacon(h->frame_control))
6553 mcl |= TXC_IGNOREPMQ;
6554
6555 txrate[0] = tx_info->control.rates;
6556 txrate[1] = txrate[0] + 1;
6557
6558 /*
6559 * if rate control algorithm didn't give us a fallback
6560 * rate, use the primary rate
6561 */
6562 if (txrate[1]->idx < 0)
6563 txrate[1] = txrate[0];
6564
6565 for (k = 0; k < hw->max_rates; k++) {
6566 is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
6567 if (!is_mcs) {
6568 if ((txrate[k]->idx >= 0)
6569 && (txrate[k]->idx <
6570 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6571 rspec[k] =
6572 hw->wiphy->bands[tx_info->band]->
6573 bitrates[txrate[k]->idx].hw_value;
6574 short_preamble[k] =
6575 txrate[k]->
6576 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
6577 true : false;
6578 } else {
6579 rspec[k] = BRCM_RATE_1M;
6580 }
6581 } else {
6582 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
6583 NRATE_MCS_INUSE | txrate[k]->idx);
6584 }
6585
6586 /*
6587 * Currently only support same setting for primay and
6588 * fallback rates. Unify flags for each rate into a
6589 * single value for the frame
6590 */
6591 use_rts |=
6592 txrate[k]->
6593 flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
6594 use_cts |=
6595 txrate[k]->
6596 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
6597
6598
6599 /*
6600 * (1) RATE:
6601 * determine and validate primary rate
6602 * and fallback rates
6603 */
6604 if (!rspec_active(rspec[k])) {
6605 rspec[k] = BRCM_RATE_1M;
6606 } else {
6607 if (!is_multicast_ether_addr(h->addr1)) {
6608 /* set tx antenna config */
6609 brcms_c_antsel_antcfg_get(wlc->asi, false,
6610 false, 0, 0, &antcfg, &fbantcfg);
6611 }
6612 }
6613 }
6614
6615 phyctl1_stf = wlc->stf->ss_opmode;
6616
6617 if (wlc->pub->_n_enab & SUPPORT_11N) {
6618 for (k = 0; k < hw->max_rates; k++) {
6619 /*
6620 * apply siso/cdd to single stream mcs's or ofdm
6621 * if rspec is auto selected
6622 */
6623 if (((is_mcs_rate(rspec[k]) &&
6624 is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
6625 is_ofdm_rate(rspec[k]))
6626 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
6627 || !(rspec[k] & RSPEC_OVERRIDE))) {
6628 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
6629
6630 /* For SISO MCS use STBC if possible */
6631 if (is_mcs_rate(rspec[k])
6632 && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
6633 u8 stc;
6634
6635 /* Nss for single stream is always 1 */
6636 stc = 1;
6637 rspec[k] |= (PHY_TXC1_MODE_STBC <<
6638 RSPEC_STF_SHIFT) |
6639 (stc << RSPEC_STC_SHIFT);
6640 } else
6641 rspec[k] |=
6642 (phyctl1_stf << RSPEC_STF_SHIFT);
6643 }
6644
6645 /*
6646 * Is the phy configured to use 40MHZ frames? If
6647 * so then pick the desired txbw
6648 */
6649 if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
6650 /* default txbw is 20in40 SB */
6651 mimo_ctlchbw = mimo_txbw =
6652 CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
6653 wlc->band->pi))
6654 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
6655
6656 if (is_mcs_rate(rspec[k])) {
6657 /* mcs 32 must be 40b/w DUP */
6658 if ((rspec[k] & RSPEC_RATE_MASK)
6659 == 32) {
6660 mimo_txbw =
6661 PHY_TXC1_BW_40MHZ_DUP;
6662 /* use override */
6663 } else if (wlc->mimo_40txbw != AUTO)
6664 mimo_txbw = wlc->mimo_40txbw;
6665 /* else check if dst is using 40 Mhz */
6666 else if (scb->flags & SCB_IS40)
6667 mimo_txbw = PHY_TXC1_BW_40MHZ;
6668 } else if (is_ofdm_rate(rspec[k])) {
6669 if (wlc->ofdm_40txbw != AUTO)
6670 mimo_txbw = wlc->ofdm_40txbw;
6671 } else if (wlc->cck_40txbw != AUTO) {
6672 mimo_txbw = wlc->cck_40txbw;
6673 }
6674 } else {
6675 /*
6676 * mcs32 is 40 b/w only.
6677 * This is possible for probe packets on
6678 * a STA during SCAN
6679 */
6680 if ((rspec[k] & RSPEC_RATE_MASK) == 32)
6681 /* mcs 0 */
6682 rspec[k] = RSPEC_MIMORATE;
6683
6684 mimo_txbw = PHY_TXC1_BW_20MHZ;
6685 }
6686
6687 /* Set channel width */
6688 rspec[k] &= ~RSPEC_BW_MASK;
6689 if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
6690 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
6691 else
6692 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6693
6694 /* Disable short GI, not supported yet */
6695 rspec[k] &= ~RSPEC_SHORT_GI;
6696
6697 mimo_preamble_type = BRCMS_MM_PREAMBLE;
6698 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6699 mimo_preamble_type = BRCMS_GF_PREAMBLE;
6700
6701 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6702 && (!is_mcs_rate(rspec[k]))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006703 brcms_err(wlc->hw->d11core,
6704 "wl%d: %s: IEEE80211_TX_"
Arend van Spriel5b435de2011-10-05 13:19:03 +02006705 "RC_MCS != is_mcs_rate(rspec)\n",
6706 wlc->pub->unit, __func__);
6707 }
6708
6709 if (is_mcs_rate(rspec[k])) {
6710 preamble_type[k] = mimo_preamble_type;
6711
6712 /*
6713 * if SGI is selected, then forced mm
6714 * for single stream
6715 */
6716 if ((rspec[k] & RSPEC_SHORT_GI)
6717 && is_single_stream(rspec[k] &
6718 RSPEC_RATE_MASK))
6719 preamble_type[k] = BRCMS_MM_PREAMBLE;
6720 }
6721
6722 /* should be better conditionalized */
6723 if (!is_mcs_rate(rspec[0])
6724 && (tx_info->control.rates[0].
6725 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6726 preamble_type[k] = BRCMS_SHORT_PREAMBLE;
6727 }
6728 } else {
6729 for (k = 0; k < hw->max_rates; k++) {
6730 /* Set ctrlchbw as 20Mhz */
6731 rspec[k] &= ~RSPEC_BW_MASK;
6732 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6733
6734 /* for nphy, stf of ofdm frames must follow policies */
6735 if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
6736 rspec[k] &= ~RSPEC_STF_MASK;
6737 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6738 }
6739 }
6740 }
6741
6742 /* Reset these for use with AMPDU's */
6743 txrate[0]->count = 0;
6744 txrate[1]->count = 0;
6745
6746 /* (2) PROTECTION, may change rspec */
6747 if ((ieee80211_is_data(h->frame_control) ||
6748 ieee80211_is_mgmt(h->frame_control)) &&
6749 (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
6750 use_rts = true;
6751
6752 /* (3) PLCP: determine PLCP header and MAC duration,
6753 * fill struct d11txh */
6754 brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
6755 brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6756 memcpy(&txh->FragPLCPFallback,
6757 plcp_fallback, sizeof(txh->FragPLCPFallback));
6758
6759 /* Length field now put in CCK FBR CRC field */
6760 if (is_cck_rate(rspec[1])) {
6761 txh->FragPLCPFallback[4] = phylen & 0xff;
6762 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6763 }
6764
6765 /* MIMO-RATE: need validation ?? */
6766 mainrates = is_ofdm_rate(rspec[0]) ?
6767 D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
6768 plcp[0];
6769
6770 /* DUR field for main rate */
6771 if (!ieee80211_is_pspoll(h->frame_control) &&
6772 !is_multicast_ether_addr(h->addr1) && !use_rifs) {
6773 durid =
6774 brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6775 next_frag_len);
6776 h->duration_id = cpu_to_le16(durid);
6777 } else if (use_rifs) {
6778 /* NAV protect to end of next max packet size */
6779 durid =
6780 (u16) brcms_c_calc_frame_time(wlc, rspec[0],
6781 preamble_type[0],
6782 DOT11_MAX_FRAG_LEN);
6783 durid += RIFS_11N_TIME;
6784 h->duration_id = cpu_to_le16(durid);
6785 }
6786
6787 /* DUR field for fallback rate */
6788 if (ieee80211_is_pspoll(h->frame_control))
6789 txh->FragDurFallback = h->duration_id;
6790 else if (is_multicast_ether_addr(h->addr1) || use_rifs)
6791 txh->FragDurFallback = 0;
6792 else {
6793 durid = brcms_c_compute_frame_dur(wlc, rspec[1],
6794 preamble_type[1], next_frag_len);
6795 txh->FragDurFallback = cpu_to_le16(durid);
6796 }
6797
6798 /* (4) MAC-HDR: MacTxControlLow */
6799 if (frag == 0)
6800 mcl |= TXC_STARTMSDU;
6801
6802 if (!is_multicast_ether_addr(h->addr1))
6803 mcl |= TXC_IMMEDACK;
6804
6805 if (wlc->band->bandtype == BRCM_BAND_5G)
6806 mcl |= TXC_FREQBAND_5G;
6807
6808 if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
6809 mcl |= TXC_BW_40;
6810
6811 /* set AMIC bit if using hardware TKIP MIC */
6812 if (hwtkmic)
6813 mcl |= TXC_AMIC;
6814
6815 txh->MacTxControlLow = cpu_to_le16(mcl);
6816
6817 /* MacTxControlHigh */
6818 mch = 0;
6819
6820 /* Set fallback rate preamble type */
6821 if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
6822 (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
6823 if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
6824 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6825 }
6826
6827 /* MacFrameControl */
6828 memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
6829 txh->TxFesTimeNormal = cpu_to_le16(0);
6830
6831 txh->TxFesTimeFallback = cpu_to_le16(0);
6832
6833 /* TxFrameRA */
6834 memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
6835
6836 /* TxFrameID */
6837 txh->TxFrameID = cpu_to_le16(frameid);
6838
6839 /*
6840 * TxStatus, Note the case of recreating the first frag of a suppressed
6841 * frame then we may need to reset the retry cnt's via the status reg
6842 */
6843 txh->TxStatus = cpu_to_le16(status);
6844
6845 /*
6846 * extra fields for ucode AMPDU aggregation, the new fields are added to
6847 * the END of previous structure so that it's compatible in driver.
6848 */
6849 txh->MaxNMpdus = cpu_to_le16(0);
6850 txh->MaxABytes_MRT = cpu_to_le16(0);
6851 txh->MaxABytes_FBR = cpu_to_le16(0);
6852 txh->MinMBytes = cpu_to_le16(0);
6853
6854 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
6855 * furnish struct d11txh */
6856 /* RTS PLCP header and RTS frame */
6857 if (use_rts || use_cts) {
6858 if (use_rts && use_cts)
6859 use_cts = false;
6860
6861 for (k = 0; k < 2; k++) {
6862 rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
6863 false,
6864 mimo_ctlchbw);
6865 }
6866
6867 if (!is_ofdm_rate(rts_rspec[0]) &&
6868 !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
6869 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6870 rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
6871 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6872 }
6873
6874 if (!is_ofdm_rate(rts_rspec[1]) &&
6875 !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
6876 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6877 rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
6878 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6879 }
6880
6881 /* RTS/CTS additions to MacTxControlLow */
6882 if (use_cts) {
6883 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
6884 } else {
6885 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
6886 txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
6887 }
6888
6889 /* RTS PLCP header */
6890 rts_plcp = txh->RTSPhyHeader;
6891 if (use_cts)
6892 rts_phylen = DOT11_CTS_LEN + FCS_LEN;
6893 else
6894 rts_phylen = DOT11_RTS_LEN + FCS_LEN;
6895
6896 brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6897
6898 /* fallback rate version of RTS PLCP header */
6899 brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6900 rts_plcp_fallback);
6901 memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
6902 sizeof(txh->RTSPLCPFallback));
6903
6904 /* RTS frame fields... */
6905 rts = (struct ieee80211_rts *)&txh->rts_frame;
6906
6907 durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6908 rspec[0], rts_preamble_type[0],
6909 preamble_type[0], phylen, false);
6910 rts->duration = cpu_to_le16(durid);
6911 /* fallback rate version of RTS DUR field */
6912 durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
6913 rts_rspec[1], rspec[1],
6914 rts_preamble_type[1],
6915 preamble_type[1], phylen, false);
6916 txh->RTSDurFallback = cpu_to_le16(durid);
6917
6918 if (use_cts) {
6919 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6920 IEEE80211_STYPE_CTS);
6921
6922 memcpy(&rts->ra, &h->addr2, ETH_ALEN);
6923 } else {
6924 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6925 IEEE80211_STYPE_RTS);
6926
6927 memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
6928 }
6929
6930 /* mainrate
6931 * low 8 bits: main frag rate/mcs,
6932 * high 8 bits: rts/cts rate/mcs
6933 */
6934 mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
6935 D11A_PHY_HDR_GRATE(
6936 (struct ofdm_phy_hdr *) rts_plcp) :
6937 rts_plcp[0]) << 8;
6938 } else {
6939 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6940 memset((char *)&txh->rts_frame, 0,
6941 sizeof(struct ieee80211_rts));
6942 memset((char *)txh->RTSPLCPFallback, 0,
6943 sizeof(txh->RTSPLCPFallback));
6944 txh->RTSDurFallback = 0;
6945 }
6946
6947#ifdef SUPPORT_40MHZ
6948 /* add null delimiter count */
6949 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
6950 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6951 brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6952
6953#endif
6954
6955 /*
6956 * Now that RTS/RTS FB preamble types are updated, write
6957 * the final value
6958 */
6959 txh->MacTxControlHigh = cpu_to_le16(mch);
6960
6961 /*
6962 * MainRates (both the rts and frag plcp rates have
6963 * been calculated now)
6964 */
6965 txh->MainRates = cpu_to_le16(mainrates);
6966
6967 /* XtraFrameTypes */
6968 xfts = frametype(rspec[1], wlc->mimoft);
6969 xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6970 xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6971 xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
6972 XFTS_CHANNEL_SHIFT;
6973 txh->XtraFrameTypes = cpu_to_le16(xfts);
6974
6975 /* PhyTxControlWord */
6976 phyctl = frametype(rspec[0], wlc->mimoft);
6977 if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
6978 (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
6979 if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
6980 phyctl |= PHY_TXC_SHORT_HDR;
6981 }
6982
6983 /* phytxant is properly bit shifted */
6984 phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6985 txh->PhyTxControlWord = cpu_to_le16(phyctl);
6986
6987 /* PhyTxControlWord_1 */
6988 if (BRCMS_PHY_11N_CAP(wlc->band)) {
6989 u16 phyctl1 = 0;
6990
6991 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
6992 txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
6993 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
6994 txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
6995
6996 if (use_rts || use_cts) {
6997 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
6998 txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
6999 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
7000 txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
7001 }
7002
7003 /*
7004 * For mcs frames, if mixedmode(overloaded with long preamble)
7005 * is going to be set, fill in non-zero MModeLen and/or
7006 * MModeFbrLen it will be unnecessary if they are separated
7007 */
7008 if (is_mcs_rate(rspec[0]) &&
7009 (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
7010 u16 mmodelen =
7011 brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
7012 txh->MModeLen = cpu_to_le16(mmodelen);
7013 }
7014
7015 if (is_mcs_rate(rspec[1]) &&
7016 (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
7017 u16 mmodefbrlen =
7018 brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
7019 txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
7020 }
7021 }
7022
7023 ac = skb_get_queue_mapping(p);
7024 if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
7025 uint frag_dur, dur, dur_fallback;
7026
7027 /* WME: Update TXOP threshold */
7028 if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
7029 frag_dur =
7030 brcms_c_calc_frame_time(wlc, rspec[0],
7031 preamble_type[0], phylen);
7032
7033 if (rts) {
7034 /* 1 RTS or CTS-to-self frame */
7035 dur =
7036 brcms_c_calc_cts_time(wlc, rts_rspec[0],
7037 rts_preamble_type[0]);
7038 dur_fallback =
7039 brcms_c_calc_cts_time(wlc, rts_rspec[1],
7040 rts_preamble_type[1]);
7041 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
7042 dur += le16_to_cpu(rts->duration);
7043 dur_fallback +=
7044 le16_to_cpu(txh->RTSDurFallback);
7045 } else if (use_rifs) {
7046 dur = frag_dur;
7047 dur_fallback = 0;
7048 } else {
7049 /* frame + SIFS + ACK */
7050 dur = frag_dur;
7051 dur +=
7052 brcms_c_compute_frame_dur(wlc, rspec[0],
7053 preamble_type[0], 0);
7054
7055 dur_fallback =
7056 brcms_c_calc_frame_time(wlc, rspec[1],
7057 preamble_type[1],
7058 phylen);
7059 dur_fallback +=
7060 brcms_c_compute_frame_dur(wlc, rspec[1],
7061 preamble_type[1], 0);
7062 }
7063 /* NEED to set TxFesTimeNormal (hard) */
7064 txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
7065 /*
7066 * NEED to set fallback rate version of
7067 * TxFesTimeNormal (hard)
7068 */
7069 txh->TxFesTimeFallback =
7070 cpu_to_le16((u16) dur_fallback);
7071
7072 /*
7073 * update txop byte threshold (txop minus intraframe
7074 * overhead)
7075 */
7076 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
7077 uint newfragthresh;
7078
7079 newfragthresh =
7080 brcms_c_calc_frame_len(wlc,
7081 rspec[0], preamble_type[0],
7082 (wlc->edcf_txop[ac] -
7083 (dur - frag_dur)));
7084 /* range bound the fragthreshold */
7085 if (newfragthresh < DOT11_MIN_FRAG_LEN)
7086 newfragthresh =
7087 DOT11_MIN_FRAG_LEN;
7088 else if (newfragthresh >
7089 wlc->usr_fragthresh)
7090 newfragthresh =
7091 wlc->usr_fragthresh;
7092 /* update the fragthresh and do txc update */
7093 if (wlc->fragthresh[queue] !=
7094 (u16) newfragthresh)
7095 wlc->fragthresh[queue] =
7096 (u16) newfragthresh;
7097 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007098 brcms_err(wlc->hw->d11core,
7099 "wl%d: %s txop invalid "
Arend van Spriel5b435de2011-10-05 13:19:03 +02007100 "for rate %d\n",
7101 wlc->pub->unit, fifo_names[queue],
7102 rspec2rate(rspec[0]));
7103 }
7104
7105 if (dur > wlc->edcf_txop[ac])
Seth Forsheeb353dda2012-11-15 08:08:03 -06007106 brcms_err(wlc->hw->d11core,
7107 "wl%d: %s: %s txop "
Arend van Spriel5b435de2011-10-05 13:19:03 +02007108 "exceeded phylen %d/%d dur %d/%d\n",
7109 wlc->pub->unit, __func__,
7110 fifo_names[queue],
7111 phylen, wlc->fragthresh[queue],
7112 dur, wlc->edcf_txop[ac]);
7113 }
7114 }
7115
7116 return 0;
7117}
7118
Seth Forsheee041f652012-11-15 08:07:56 -06007119static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007120{
Seth Forsheee041f652012-11-15 08:07:56 -06007121 struct dma_pub *dma;
7122 int fifo, ret = -ENOSPC;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007123 struct d11txh *txh;
Seth Forsheee041f652012-11-15 08:07:56 -06007124 u16 frameid = INVALIDFID;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007125
Seth Forsheee041f652012-11-15 08:07:56 -06007126 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
7127 dma = wlc->hw->di[fifo];
7128 txh = (struct d11txh *)(skb->data);
7129
7130 if (dma->txavail == 0) {
7131 /*
7132 * We sometimes get a frame from mac80211 after stopping
7133 * the queues. This only ever seems to be a single frame
7134 * and is seems likely to be a race. TX_HEADROOM should
7135 * ensure that we have enough space to handle these stray
7136 * packets, so warn if there isn't. If we're out of space
7137 * in the tx ring and the tx queue isn't stopped then
7138 * we've really got a bug; warn loudly if that happens.
7139 */
Seth Forsheeb353dda2012-11-15 08:08:03 -06007140 brcms_warn(wlc->hw->d11core,
Seth Forsheee041f652012-11-15 08:07:56 -06007141 "Received frame for tx with no space in DMA ring\n");
7142 WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
7143 skb_get_queue_mapping(skb)));
7144 return -ENOSPC;
7145 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02007146
7147 /* When a BC/MC frame is being committed to the BCMC fifo
7148 * via DMA (NOT PIO), update ucode or BSS info as appropriate.
7149 */
7150 if (fifo == TX_BCMC_FIFO)
7151 frameid = le16_to_cpu(txh->TxFrameID);
7152
Arend van Spriel5b435de2011-10-05 13:19:03 +02007153 /* Commit BCMC sequence number in the SHM frame ID location */
7154 if (frameid != INVALIDFID) {
7155 /*
7156 * To inform the ucode of the last mcast frame posted
7157 * so that it can clear moredata bit
7158 */
7159 brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
7160 }
7161
Seth Forsheee041f652012-11-15 08:07:56 -06007162 ret = brcms_c_txfifo(wlc, fifo, skb);
7163 /*
7164 * The only reason for brcms_c_txfifo to fail is because
7165 * there weren't any DMA descriptors, but we've already
7166 * checked for that. So if it does fail yell loudly.
7167 */
7168 WARN_ON_ONCE(ret);
7169
7170 return ret;
7171}
7172
7173void brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
7174 struct ieee80211_hw *hw)
7175{
7176 uint fifo;
7177 struct scb *scb = &wlc->pri_scb;
7178
7179 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
7180 if (brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0))
7181 return;
7182 if (brcms_c_tx(wlc, sdu))
7183 dev_kfree_skb_any(sdu);
7184}
7185
7186int
7187brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
7188{
7189 struct dma_pub *dma = wlc->hw->di[fifo];
7190 int ret;
7191 u16 queue;
7192
7193 ret = dma_txfast(wlc, dma, p);
7194 if (ret < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007195 wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
Seth Forsheee041f652012-11-15 08:07:56 -06007196
7197 /*
7198 * Stop queue if DMA ring is full. Reserve some free descriptors,
7199 * as we sometimes receive a frame from mac80211 after the queues
7200 * are stopped.
7201 */
7202 queue = skb_get_queue_mapping(p);
7203 if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
7204 !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
7205 ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
7206
7207 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007208}
7209
Arend van Spriel5b435de2011-10-05 13:19:03 +02007210u32
7211brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
7212 bool use_rspec, u16 mimo_ctlchbw)
7213{
7214 u32 rts_rspec = 0;
7215
7216 if (use_rspec)
7217 /* use frame rate as rts rate */
7218 rts_rspec = rspec;
7219 else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
7220 /* Use 11Mbps as the g protection RTS target rate and fallback.
7221 * Use the brcms_basic_rate() lookup to find the best basic rate
7222 * under the target in case 11 Mbps is not Basic.
7223 * 6 and 9 Mbps are not usually selected by rate selection, but
7224 * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
7225 * is more robust.
7226 */
7227 rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
7228 else
7229 /* calculate RTS rate and fallback rate based on the frame rate
7230 * RTS must be sent at a basic rate since it is a
7231 * control frame, sec 9.6 of 802.11 spec
7232 */
7233 rts_rspec = brcms_basic_rate(wlc, rspec);
7234
7235 if (BRCMS_PHY_11N_CAP(wlc->band)) {
7236 /* set rts txbw to correct side band */
7237 rts_rspec &= ~RSPEC_BW_MASK;
7238
7239 /*
7240 * if rspec/rspec_fallback is 40MHz, then send RTS on both
7241 * 20MHz channel (DUP), otherwise send RTS on control channel
7242 */
7243 if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
7244 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
7245 else
7246 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
7247
7248 /* pick siso/cdd as default for ofdm */
7249 if (is_ofdm_rate(rts_rspec)) {
7250 rts_rspec &= ~RSPEC_STF_MASK;
7251 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
7252 }
7253 }
7254 return rts_rspec;
7255}
7256
Arend van Spriel5b435de2011-10-05 13:19:03 +02007257/* Update beacon listen interval in shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007258static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007259{
7260 /* wake up every DTIM is the default */
7261 if (wlc->bcn_li_dtim == 1)
7262 brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
7263 else
7264 brcms_b_write_shm(wlc->hw, M_BCN_LI,
7265 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
7266}
7267
7268static void
7269brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
7270 u32 *tsf_h_ptr)
7271{
Arend van Spriel16d28122011-12-08 15:06:51 -08007272 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007273
7274 /* read the tsf timer low, then high to get an atomic read */
Arend van Spriel16d28122011-12-08 15:06:51 -08007275 *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
7276 *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
Arend van Spriel5b435de2011-10-05 13:19:03 +02007277}
7278
7279/*
7280 * recover 64bit TSF value from the 16bit TSF value in the rx header
7281 * given the assumption that the TSF passed in header is within 65ms
7282 * of the current tsf.
7283 *
7284 * 6 5 4 4 3 2 1
7285 * 3.......6.......8.......0.......2.......4.......6.......8......0
7286 * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
7287 *
7288 * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
7289 * tsf_l is filled in by brcms_b_recv, which is done earlier in the
7290 * receive call sequence after rx interrupt. Only the higher 16 bits
7291 * are used. Finally, the tsf_h is read from the tsf register.
7292 */
7293static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
7294 struct d11rxhdr *rxh)
7295{
7296 u32 tsf_h, tsf_l;
7297 u16 rx_tsf_0_15, rx_tsf_16_31;
7298
7299 brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
7300
7301 rx_tsf_16_31 = (u16)(tsf_l >> 16);
7302 rx_tsf_0_15 = rxh->RxTSFTime;
7303
7304 /*
7305 * a greater tsf time indicates the low 16 bits of
7306 * tsf_l wrapped, so decrement the high 16 bits.
7307 */
7308 if ((u16)tsf_l < rx_tsf_0_15) {
7309 rx_tsf_16_31 -= 1;
7310 if (rx_tsf_16_31 == 0xffff)
7311 tsf_h -= 1;
7312 }
7313
7314 return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
7315}
7316
7317static void
7318prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7319 struct sk_buff *p,
7320 struct ieee80211_rx_status *rx_status)
7321{
7322 int preamble;
7323 int channel;
7324 u32 rspec;
7325 unsigned char *plcp;
7326
7327 /* fill in TSF and flag its presence */
7328 rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
7329 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
7330
7331 channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
7332
Johannes Berg858a4552012-07-24 17:35:57 +02007333 rx_status->band =
7334 channel > 14 ? IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
7335 rx_status->freq =
7336 ieee80211_channel_to_frequency(channel, rx_status->band);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007337
7338 rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
7339
7340 /* noise */
7341 /* qual */
7342 rx_status->antenna =
7343 (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
7344
7345 plcp = p->data;
7346
7347 rspec = brcms_c_compute_rspec(rxh, plcp);
7348 if (is_mcs_rate(rspec)) {
7349 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
7350 rx_status->flag |= RX_FLAG_HT;
7351 if (rspec_is40mhz(rspec))
7352 rx_status->flag |= RX_FLAG_40MHZ;
7353 } else {
7354 switch (rspec2rate(rspec)) {
7355 case BRCM_RATE_1M:
7356 rx_status->rate_idx = 0;
7357 break;
7358 case BRCM_RATE_2M:
7359 rx_status->rate_idx = 1;
7360 break;
7361 case BRCM_RATE_5M5:
7362 rx_status->rate_idx = 2;
7363 break;
7364 case BRCM_RATE_11M:
7365 rx_status->rate_idx = 3;
7366 break;
7367 case BRCM_RATE_6M:
7368 rx_status->rate_idx = 4;
7369 break;
7370 case BRCM_RATE_9M:
7371 rx_status->rate_idx = 5;
7372 break;
7373 case BRCM_RATE_12M:
7374 rx_status->rate_idx = 6;
7375 break;
7376 case BRCM_RATE_18M:
7377 rx_status->rate_idx = 7;
7378 break;
7379 case BRCM_RATE_24M:
7380 rx_status->rate_idx = 8;
7381 break;
7382 case BRCM_RATE_36M:
7383 rx_status->rate_idx = 9;
7384 break;
7385 case BRCM_RATE_48M:
7386 rx_status->rate_idx = 10;
7387 break;
7388 case BRCM_RATE_54M:
7389 rx_status->rate_idx = 11;
7390 break;
7391 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06007392 brcms_err(wlc->hw->d11core,
7393 "%s: Unknown rate\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007394 }
7395
7396 /*
7397 * For 5GHz, we should decrease the index as it is
7398 * a subset of the 2.4G rates. See bitrates field
7399 * of brcms_band_5GHz_nphy (in mac80211_if.c).
7400 */
7401 if (rx_status->band == IEEE80211_BAND_5GHZ)
7402 rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
7403
7404 /* Determine short preamble and rate_idx */
7405 preamble = 0;
7406 if (is_cck_rate(rspec)) {
7407 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
7408 rx_status->flag |= RX_FLAG_SHORTPRE;
7409 } else if (is_ofdm_rate(rspec)) {
7410 rx_status->flag |= RX_FLAG_SHORTPRE;
7411 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007412 brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007413 __func__);
7414 }
7415 }
7416
7417 if (plcp3_issgi(plcp[3]))
7418 rx_status->flag |= RX_FLAG_SHORT_GI;
7419
7420 if (rxh->RxStatus1 & RXS_DECERR) {
7421 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007422 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007423 __func__);
7424 }
7425 if (rxh->RxStatus1 & RXS_FCSERR) {
7426 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007427 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_FCS_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007428 __func__);
7429 }
7430}
7431
7432static void
7433brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7434 struct sk_buff *p)
7435{
7436 int len_mpdu;
7437 struct ieee80211_rx_status rx_status;
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007438 struct ieee80211_hdr *hdr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007439
7440 memset(&rx_status, 0, sizeof(rx_status));
7441 prep_mac80211_status(wlc, rxh, p, &rx_status);
7442
7443 /* mac header+body length, exclude CRC and plcp header */
7444 len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
7445 skb_pull(p, D11_PHY_HDR_LEN);
7446 __skb_trim(p, len_mpdu);
7447
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007448 /* unmute transmit */
7449 if (wlc->hw->suspended_fifos) {
7450 hdr = (struct ieee80211_hdr *)p->data;
7451 if (ieee80211_is_beacon(hdr->frame_control))
7452 brcms_b_mute(wlc->hw, false);
7453 }
7454
Arend van Spriel5b435de2011-10-05 13:19:03 +02007455 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7456 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7457}
7458
Arend van Spriel5b435de2011-10-05 13:19:03 +02007459/* calculate frame duration for Mixed-mode L-SIG spoofing, return
7460 * number of bytes goes in the length field
7461 *
7462 * Formula given by HT PHY Spec v 1.13
7463 * len = 3(nsyms + nstream + 3) - 3
7464 */
7465u16
7466brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
7467 uint mac_len)
7468{
7469 uint nsyms, len = 0, kNdps;
7470
7471 BCMMSG(wlc->wiphy, "wl%d: rate %d, len%d\n",
7472 wlc->pub->unit, rspec2rate(ratespec), mac_len);
7473
7474 if (is_mcs_rate(ratespec)) {
7475 uint mcs = ratespec & RSPEC_RATE_MASK;
7476 int tot_streams = (mcs_2_txstreams(mcs) + 1) +
7477 rspec_stc(ratespec);
7478
7479 /*
7480 * the payload duration calculation matches that
7481 * of regular ofdm
7482 */
7483 /* 1000Ndbps = kbps * 4 */
7484 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
7485 rspec_issgi(ratespec)) * 4;
7486
7487 if (rspec_stc(ratespec) == 0)
7488 nsyms =
7489 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7490 APHY_TAIL_NBITS) * 1000, kNdps);
7491 else
7492 /* STBC needs to have even number of symbols */
7493 nsyms =
7494 2 *
7495 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7496 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7497
7498 /* (+3) account for HT-SIG(2) and HT-STF(1) */
7499 nsyms += (tot_streams + 3);
7500 /*
7501 * 3 bytes/symbol @ legacy 6Mbps rate
7502 * (-3) excluding service bits and tail bits
7503 */
7504 len = (3 * nsyms) - 3;
7505 }
7506
7507 return (u16) len;
7508}
7509
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007510static void
7511brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007512{
7513 const struct brcms_c_rateset *rs_dflt;
7514 struct brcms_c_rateset rs;
7515 u8 rate;
7516 u16 entry_ptr;
7517 u8 plcp[D11_PHY_HDR_LEN];
7518 u16 dur, sifs;
7519 uint i;
7520
7521 sifs = get_sifs(wlc->band);
7522
7523 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
7524
7525 brcms_c_rateset_copy(rs_dflt, &rs);
7526 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7527
7528 /*
7529 * walk the phy rate table and update MAC core SHM
7530 * basic rate table entries
7531 */
7532 for (i = 0; i < rs.count; i++) {
7533 rate = rs.rates[i] & BRCMS_RATE_MASK;
7534
7535 entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
7536
7537 /* Calculate the Probe Response PLCP for the given rate */
7538 brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
7539
7540 /*
7541 * Calculate the duration of the Probe Response
7542 * frame plus SIFS for the MAC
7543 */
7544 dur = (u16) brcms_c_calc_frame_time(wlc, rate,
7545 BRCMS_LONG_PREAMBLE, frame_len);
7546 dur += sifs;
7547
7548 /* Update the SHM Rate Table entry Probe Response values */
7549 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
7550 (u16) (plcp[0] + (plcp[1] << 8)));
7551 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7552 (u16) (plcp[2] + (plcp[3] << 8)));
7553 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
7554 }
7555}
7556
7557/* Max buffering needed for beacon template/prb resp template is 142 bytes.
7558 *
7559 * PLCP header is 6 bytes.
7560 * 802.11 A3 header is 24 bytes.
7561 * Max beacon frame body template length is 112 bytes.
7562 * Max probe resp frame body template length is 110 bytes.
7563 *
7564 * *len on input contains the max length of the packet available.
7565 *
7566 * The *len value is set to the number of bytes in buf used, and starts
7567 * with the PLCP and included up to, but not including, the 4 byte FCS.
7568 */
7569static void
7570brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
7571 u32 bcn_rspec,
7572 struct brcms_bss_cfg *cfg, u16 *buf, int *len)
7573{
7574 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
7575 struct cck_phy_hdr *plcp;
7576 struct ieee80211_mgmt *h;
7577 int hdr_len, body_len;
7578
7579 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7580
7581 /* calc buffer size provided for frame body */
7582 body_len = *len - hdr_len;
7583 /* return actual size */
7584 *len = hdr_len + body_len;
7585
7586 /* format PHY and MAC headers */
7587 memset((char *)buf, 0, hdr_len);
7588
7589 plcp = (struct cck_phy_hdr *) buf;
7590
7591 /*
7592 * PLCP for Probe Response frames are filled in from
7593 * core's rate table
7594 */
7595 if (type == IEEE80211_STYPE_BEACON)
7596 /* fill in PLCP */
7597 brcms_c_compute_plcp(wlc, bcn_rspec,
7598 (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
7599 (u8 *) plcp);
7600
7601 /* "Regular" and 16 MBSS but not for 4 MBSS */
7602 /* Update the phytxctl for the beacon based on the rspec */
7603 brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7604
7605 h = (struct ieee80211_mgmt *)&plcp[1];
7606
7607 /* fill in 802.11 header */
7608 h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
7609
7610 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7611 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7612 if (type == IEEE80211_STYPE_BEACON)
7613 memcpy(&h->da, &ether_bcast, ETH_ALEN);
7614 memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
7615 memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
7616
7617 /* SEQ filled in by MAC */
7618}
7619
7620int brcms_c_get_header_len(void)
7621{
7622 return TXOFF;
7623}
7624
7625/*
7626 * Update all beacons for the system.
7627 */
7628void brcms_c_update_beacon(struct brcms_c_info *wlc)
7629{
7630 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7631
7632 if (bsscfg->up && !bsscfg->BSS)
7633 /* Clear the soft intmask */
7634 wlc->defmacintmask &= ~MI_BCNTPL;
7635}
7636
7637/* Write ssid into shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007638static void
7639brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007640{
7641 u8 *ssidptr = cfg->SSID;
7642 u16 base = M_SSID;
7643 u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
7644
7645 /* padding the ssid with zero and copy it into shm */
7646 memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
7647 memcpy(ssidbuf, ssidptr, cfg->SSID_len);
7648
7649 brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
7650 brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
7651}
7652
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007653static void
Arend van Spriel5b435de2011-10-05 13:19:03 +02007654brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
7655 struct brcms_bss_cfg *cfg,
7656 bool suspend)
7657{
7658 u16 prb_resp[BCN_TMPL_LEN / 2];
7659 int len = BCN_TMPL_LEN;
7660
7661 /*
7662 * write the probe response to hardware, or save in
7663 * the config structure
7664 */
7665
7666 /* create the probe response template */
7667 brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
7668 cfg, prb_resp, &len);
7669
7670 if (suspend)
7671 brcms_c_suspend_mac_and_wait(wlc);
7672
7673 /* write the probe response into the template region */
7674 brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7675 (len + 3) & ~3, prb_resp);
7676
7677 /* write the length of the probe response frame (+PLCP/-FCS) */
7678 brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
7679
7680 /* write the SSID and SSID length */
7681 brcms_c_shm_ssid_upd(wlc, cfg);
7682
7683 /*
7684 * Write PLCP headers and durations for probe response frames
7685 * at all rates. Use the actual frame length covered by the
7686 * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
7687 * by subtracting the PLCP len and adding the FCS.
7688 */
7689 len += (-D11_PHY_HDR_LEN + FCS_LEN);
7690 brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
7691
7692 if (suspend)
7693 brcms_c_enable_mac(wlc);
7694}
7695
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007696void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
7697{
7698 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7699
7700 /* update AP or IBSS probe responses */
7701 if (bsscfg->up && !bsscfg->BSS)
7702 brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
7703}
7704
Arend van Spriel5b435de2011-10-05 13:19:03 +02007705int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
7706 uint *blocks)
7707{
7708 if (fifo >= NFIFO)
7709 return -EINVAL;
7710
7711 *blocks = wlc_hw->xmtfifo_sz[fifo];
7712
7713 return 0;
7714}
7715
7716void
7717brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
7718 const u8 *addr)
7719{
7720 brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
7721 if (match_reg_offset == RCM_BSSID_OFFSET)
7722 memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
7723}
7724
Arend van Spriel5b435de2011-10-05 13:19:03 +02007725/*
7726 * Flag 'scan in progress' to withhold dynamic phy calibration
7727 */
7728void brcms_c_scan_start(struct brcms_c_info *wlc)
7729{
7730 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
7731}
7732
7733void brcms_c_scan_stop(struct brcms_c_info *wlc)
7734{
7735 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
7736}
7737
7738void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
7739{
7740 wlc->pub->associated = state;
7741 wlc->bsscfg->associated = state;
7742}
7743
7744/*
7745 * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
7746 * AMPDU traffic, packets pending in hardware have to be invalidated so that
7747 * when later on hardware releases them, they can be handled appropriately.
7748 */
7749void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
7750 struct ieee80211_sta *sta,
7751 void (*dma_callback_fn))
7752{
7753 struct dma_pub *dmah;
7754 int i;
7755 for (i = 0; i < NFIFO; i++) {
7756 dmah = hw->di[i];
7757 if (dmah != NULL)
7758 dma_walk_packets(dmah, dma_callback_fn, sta);
7759 }
7760}
7761
7762int brcms_c_get_curband(struct brcms_c_info *wlc)
7763{
7764 return wlc->band->bandunit;
7765}
7766
7767void brcms_c_wait_for_tx_completion(struct brcms_c_info *wlc, bool drop)
7768{
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007769 int timeout = 20;
Seth Forsheee041f652012-11-15 08:07:56 -06007770 int i;
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007771
Seth Forsheee041f652012-11-15 08:07:56 -06007772 /* Kick DMA to send any pending AMPDU */
7773 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
7774 if (wlc->hw->di[i])
7775 dma_txflush(wlc->hw->di[i]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007776
7777 /* wait for queue and DMA fifos to run dry */
Seth Forsheee041f652012-11-15 08:07:56 -06007778 while (brcms_txpktpendtot(wlc) > 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02007779 brcms_msleep(wlc->wl, 1);
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007780
7781 if (--timeout == 0)
7782 break;
7783 }
7784
7785 WARN_ON_ONCE(timeout == 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007786}
7787
7788void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
7789{
7790 wlc->bcn_li_bcn = interval;
7791 if (wlc->pub->up)
7792 brcms_c_bcn_li_upd(wlc);
7793}
7794
7795int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
7796{
7797 uint qdbm;
7798
7799 /* Remove override bit and clip to max qdbm value */
7800 qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
7801 return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
7802}
7803
7804int brcms_c_get_tx_power(struct brcms_c_info *wlc)
7805{
7806 uint qdbm;
7807 bool override;
7808
7809 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
7810
7811 /* Return qdbm units */
7812 return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
7813}
7814
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007815/* Process received frames */
7816/*
7817 * Return true if more frames need to be processed. false otherwise.
7818 * Param 'bound' indicates max. # frames to process before break out.
7819 */
7820static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
7821{
7822 struct d11rxhdr *rxh;
7823 struct ieee80211_hdr *h;
7824 uint len;
7825 bool is_amsdu;
7826
7827 BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
7828
7829 /* frame starts with rxhdr */
7830 rxh = (struct d11rxhdr *) (p->data);
7831
7832 /* strip off rxhdr */
7833 skb_pull(p, BRCMS_HWRXOFF);
7834
7835 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7836 if (rxh->RxStatus1 & RXS_PBPRES) {
7837 if (p->len < 2) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007838 brcms_err(wlc->hw->d11core,
7839 "wl%d: recv: rcvd runt of len %d\n",
7840 wlc->pub->unit, p->len);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007841 goto toss;
7842 }
7843 skb_pull(p, 2);
7844 }
7845
7846 h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
7847 len = p->len;
7848
7849 if (rxh->RxStatus1 & RXS_FCSERR) {
Alwin Beukersbe667662011-11-22 17:21:43 -08007850 if (!(wlc->filter_flags & FIF_FCSFAIL))
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007851 goto toss;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007852 }
7853
7854 /* check received pkt has at least frame control field */
7855 if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
7856 goto toss;
7857
7858 /* not supporting A-MSDU */
7859 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7860 if (is_amsdu)
7861 goto toss;
7862
7863 brcms_c_recvctl(wlc, rxh, p);
7864 return;
7865
7866 toss:
7867 brcmu_pkt_buf_free_skb(p);
7868}
7869
7870/* Process received frames */
7871/*
7872 * Return true if more frames need to be processed. false otherwise.
7873 * Param 'bound' indicates max. # frames to process before break out.
7874 */
7875static bool
7876brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
7877{
7878 struct sk_buff *p;
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007879 struct sk_buff *next = NULL;
7880 struct sk_buff_head recv_frames;
7881
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007882 uint n = 0;
7883 uint bound_limit = bound ? RXBND : -1;
7884
7885 BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007886 skb_queue_head_init(&recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007887
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007888 /* gather received frames */
7889 while (dma_rx(wlc_hw->di[fifo], &recv_frames)) {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007890
7891 /* !give others some time to run! */
7892 if (++n >= bound_limit)
7893 break;
7894 }
7895
7896 /* post more rbufs */
7897 dma_rxfill(wlc_hw->di[fifo]);
7898
7899 /* process each frame */
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007900 skb_queue_walk_safe(&recv_frames, p, next) {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007901 struct d11rxhdr_le *rxh_le;
7902 struct d11rxhdr *rxh;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007903
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007904 skb_unlink(p, &recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007905 rxh_le = (struct d11rxhdr_le *)p->data;
7906 rxh = (struct d11rxhdr *)p->data;
7907
7908 /* fixup rx header endianness */
7909 rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
7910 rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
7911 rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
7912 rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
7913 rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
7914 rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
7915 rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
7916 rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
7917 rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
7918 rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
7919 rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
7920
7921 brcms_c_recv(wlc_hw->wlc, p);
7922 }
7923
7924 return n >= bound_limit;
7925}
7926
7927/* second-level interrupt processing
7928 * Return true if another dpc needs to be re-scheduled. false otherwise.
7929 * Param 'bounded' indicates if applicable loops should be bounded.
7930 */
7931bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
7932{
7933 u32 macintstatus;
7934 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08007935 struct bcma_device *core = wlc_hw->d11core;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007936
7937 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007938 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007939 __func__);
7940 brcms_down(wlc->wl);
7941 return false;
7942 }
7943
7944 /* grab and clear the saved software intstatus bits */
7945 macintstatus = wlc->macintstatus;
7946 wlc->macintstatus = 0;
7947
7948 BCMMSG(wlc->wiphy, "wl%d: macintstatus 0x%x\n",
7949 wlc_hw->unit, macintstatus);
7950
7951 WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
7952
7953 /* tx status */
7954 if (macintstatus & MI_TFS) {
7955 bool fatal;
7956 if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
7957 wlc->macintstatus |= MI_TFS;
7958 if (fatal) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007959 brcms_err(core, "MI_TFS: fatal\n");
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007960 goto fatal;
7961 }
7962 }
7963
7964 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
7965 brcms_c_tbtt(wlc);
7966
7967 /* ATIM window end */
7968 if (macintstatus & MI_ATIMWINEND) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007969 brcms_dbg_info(core, "end of ATIM window\n");
Arend van Spriel16d28122011-12-08 15:06:51 -08007970 bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007971 wlc->qvalid = 0;
7972 }
7973
7974 /*
7975 * received data or control frame, MI_DMAINT is
7976 * indication of RX_FIFO interrupt
7977 */
7978 if (macintstatus & MI_DMAINT)
7979 if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
7980 wlc->macintstatus |= MI_DMAINT;
7981
7982 /* noise sample collected */
7983 if (macintstatus & MI_BG_NOISE)
7984 wlc_phy_noise_sample_intr(wlc_hw->band->pi);
7985
7986 if (macintstatus & MI_GP0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007987 brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007988 "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007989
7990 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007991 __func__, ai_get_chip_id(wlc_hw->sih),
7992 ai_get_chiprev(wlc_hw->sih));
Roland Vossenc261bdf2011-10-18 14:03:04 +02007993 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007994 }
7995
7996 /* gptimer timeout */
7997 if (macintstatus & MI_TO)
Arend van Spriel16d28122011-12-08 15:06:51 -08007998 bcma_write32(core, D11REGOFFS(gptimer), 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007999
8000 if (macintstatus & MI_RFDISABLE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06008001 brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
8002 " RF Disable Input\n", wlc_hw->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008003 brcms_rfkill_set_hw_state(wlc->wl);
8004 }
8005
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008006 /* it isn't done and needs to be resched if macintstatus is non-zero */
8007 return wlc->macintstatus != 0;
8008
8009 fatal:
Roland Vossenc261bdf2011-10-18 14:03:04 +02008010 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008011 return wlc->macintstatus != 0;
8012}
8013
Roland Vossendc460122011-10-21 16:16:28 +02008014void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008015{
Arend van Spriel16d28122011-12-08 15:06:51 -08008016 struct bcma_device *core = wlc->hw->d11core;
Seth Forshee91691292012-06-16 07:47:49 -05008017 struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008018 u16 chanspec;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008019
Seth Forsheeb353dda2012-11-15 08:08:03 -06008020 brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008021
Seth Forshee91691292012-06-16 07:47:49 -05008022 chanspec = ch20mhz_chspec(ch->hw_value);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008023
Roland Vossena8bc4912011-10-21 16:16:25 +02008024 brcms_b_init(wlc->hw, chanspec);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008025
8026 /* update beacon listen interval */
8027 brcms_c_bcn_li_upd(wlc);
8028
8029 /* write ethernet address to core */
8030 brcms_c_set_mac(wlc->bsscfg);
8031 brcms_c_set_bssid(wlc->bsscfg);
8032
8033 /* Update tsf_cfprep if associated and up */
8034 if (wlc->pub->associated && wlc->bsscfg->up) {
8035 u32 bi;
8036
8037 /* get beacon period and convert to uS */
8038 bi = wlc->bsscfg->current_bss->beacon_period << 10;
8039 /*
8040 * update since init path would reset
8041 * to default value
8042 */
Arend van Spriel16d28122011-12-08 15:06:51 -08008043 bcma_write32(core, D11REGOFFS(tsf_cfprep),
8044 bi << CFPREP_CBI_SHIFT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008045
8046 /* Update maccontrol PM related bits */
8047 brcms_c_set_ps_ctrl(wlc);
8048 }
8049
8050 brcms_c_bandinit_ordered(wlc, chanspec);
8051
8052 /* init probe response timeout */
8053 brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
8054
8055 /* init max burst txop (framebursting) */
8056 brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
8057 (wlc->
8058 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
8059
8060 /* initialize maximum allowed duty cycle */
8061 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
8062 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
8063
8064 /*
8065 * Update some shared memory locations related to
8066 * max AMPDU size allowed to received
8067 */
8068 brcms_c_ampdu_shm_upd(wlc->ampdu);
8069
8070 /* band-specific inits */
8071 brcms_c_bsinit(wlc);
8072
8073 /* Enable EDCF mode (while the MAC is suspended) */
Arend van Spriel16d28122011-12-08 15:06:51 -08008074 bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008075 brcms_c_edcf_setparams(wlc, false);
8076
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008077 /* read the ucode version if we have not yet done so */
8078 if (wlc->ucode_rev == 0) {
8079 wlc->ucode_rev =
8080 brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
8081 wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
8082 }
8083
8084 /* ..now really unleash hell (allow the MAC out of suspend) */
8085 brcms_c_enable_mac(wlc);
8086
Roland Vossena8bc4912011-10-21 16:16:25 +02008087 /* suspend the tx fifos and mute the phy for preism cac time */
8088 if (mute_tx)
Roland Vossenc6c44892011-10-21 16:16:26 +02008089 brcms_b_mute(wlc->hw, true);
Roland Vossena8bc4912011-10-21 16:16:25 +02008090
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008091 /* enable the RF Disable Delay timer */
Arend van Spriel16d28122011-12-08 15:06:51 -08008092 bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008093
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008094 /*
8095 * Initialize WME parameters; if they haven't been set by some other
8096 * mechanism (IOVar, etc) then read them from the hardware.
8097 */
8098 if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
8099 /* Uninitialized; read from HW */
8100 int ac;
8101
Arend van Sprielb7eec422011-11-10 20:30:18 +01008102 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008103 wlc->wme_retries[ac] =
8104 brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
8105 }
8106}
8107
8108/*
8109 * The common driver entry routine. Error codes should be unique
8110 */
8111struct brcms_c_info *
Arend van Sprielb63337a2011-12-08 15:06:47 -08008112brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
8113 bool piomode, uint *perr)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008114{
8115 struct brcms_c_info *wlc;
8116 uint err = 0;
8117 uint i, j;
8118 struct brcms_pub *pub;
8119
8120 /* allocate struct brcms_c_info state and its substructures */
Joe Perches2c208892012-06-04 12:44:17 +00008121 wlc = brcms_c_attach_malloc(unit, &err, 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008122 if (wlc == NULL)
8123 goto fail;
8124 wlc->wiphy = wl->wiphy;
8125 pub = wlc->pub;
8126
Joe Perches8ae74652012-01-15 00:38:38 -08008127#if defined(DEBUG)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008128 wlc_info_dbg = wlc;
8129#endif
8130
8131 wlc->band = wlc->bandstate[0];
8132 wlc->core = wlc->corestate;
8133 wlc->wl = wl;
8134 pub->unit = unit;
8135 pub->_piomode = piomode;
8136 wlc->bandinit_pending = false;
8137
8138 /* populate struct brcms_c_info with default values */
8139 brcms_c_info_init(wlc, unit);
8140
8141 /* update sta/ap related parameters */
8142 brcms_c_ap_upd(wlc);
8143
8144 /*
8145 * low level attach steps(all hw accesses go
8146 * inside, no more in rest of the attach)
8147 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08008148 err = brcms_b_attach(wlc, core, unit, piomode);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008149 if (err)
8150 goto fail;
8151
8152 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
8153
8154 pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
8155
8156 /* disable allowed duty cycle */
8157 wlc->tx_duty_cycle_ofdm = 0;
8158 wlc->tx_duty_cycle_cck = 0;
8159
8160 brcms_c_stf_phy_chain_calc(wlc);
8161
8162 /* txchain 1: txant 0, txchain 2: txant 1 */
8163 if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
8164 wlc->stf->txant = wlc->stf->hw_txchain - 1;
8165
8166 /* push to BMAC driver */
8167 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
8168 wlc->stf->hw_rxchain);
8169
8170 /* pull up some info resulting from the low attach */
8171 for (i = 0; i < NFIFO; i++)
8172 wlc->core->txavail[i] = wlc->hw->txavail[i];
8173
8174 memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
8175 memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
8176
8177 for (j = 0; j < wlc->pub->_nbands; j++) {
8178 wlc->band = wlc->bandstate[j];
8179
8180 if (!brcms_c_attach_stf_ant_init(wlc)) {
8181 err = 24;
8182 goto fail;
8183 }
8184
8185 /* default contention windows size limits */
8186 wlc->band->CWmin = APHY_CWMIN;
8187 wlc->band->CWmax = PHY_CWMAX;
8188
8189 /* init gmode value */
8190 if (wlc->band->bandtype == BRCM_BAND_2G) {
8191 wlc->band->gmode = GMODE_AUTO;
8192 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
8193 wlc->band->gmode);
8194 }
8195
8196 /* init _n_enab supported mode */
8197 if (BRCMS_PHY_11N_CAP(wlc->band)) {
8198 pub->_n_enab = SUPPORT_11N;
8199 brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
8200 ((pub->_n_enab ==
8201 SUPPORT_11N) ? WL_11N_2x2 :
8202 WL_11N_3x3));
8203 }
8204
8205 /* init per-band default rateset, depend on band->gmode */
8206 brcms_default_rateset(wlc, &wlc->band->defrateset);
8207
8208 /* fill in hw_rateset */
8209 brcms_c_rateset_filter(&wlc->band->defrateset,
8210 &wlc->band->hw_rateset, false,
8211 BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
8212 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
8213 }
8214
8215 /*
8216 * update antenna config due to
8217 * wlc->stf->txant/txchain/ant_rx_ovr change
8218 */
8219 brcms_c_stf_phy_txant_upd(wlc);
8220
8221 /* attach each modules */
8222 err = brcms_c_attach_module(wlc);
8223 if (err != 0)
8224 goto fail;
8225
8226 if (!brcms_c_timers_init(wlc, unit)) {
8227 wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
8228 __func__);
8229 err = 32;
8230 goto fail;
8231 }
8232
8233 /* depend on rateset, gmode */
8234 wlc->cmi = brcms_c_channel_mgr_attach(wlc);
8235 if (!wlc->cmi) {
8236 wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
8237 "\n", unit, __func__);
8238 err = 33;
8239 goto fail;
8240 }
8241
8242 /* init default when all parameters are ready, i.e. ->rateset */
8243 brcms_c_bss_default_init(wlc);
8244
8245 /*
8246 * Complete the wlc default state initializations..
8247 */
8248
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008249 wlc->bsscfg->wlc = wlc;
8250
8251 wlc->mimoft = FT_HT;
8252 wlc->mimo_40txbw = AUTO;
8253 wlc->ofdm_40txbw = AUTO;
8254 wlc->cck_40txbw = AUTO;
8255 brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
8256
8257 /* Set default values of SGI */
8258 if (BRCMS_SGI_CAP_PHY(wlc)) {
8259 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8260 BRCMS_N_SGI_40));
8261 } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
8262 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8263 BRCMS_N_SGI_40));
8264 } else {
8265 brcms_c_ht_update_sgi_rx(wlc, 0);
8266 }
8267
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008268 brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
8269
8270 if (perr)
8271 *perr = 0;
8272
8273 return wlc;
8274
8275 fail:
8276 wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
8277 unit, __func__, err);
8278 if (wlc)
8279 brcms_c_detach(wlc);
8280
8281 if (perr)
8282 *perr = err;
8283 return NULL;
8284}