blob: 080b54da6506dc6d7a2c62b68a97f597544e2af7 [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"
Seth Forsheecdf43522012-11-15 08:08:09 -060039#include "brcms_trace_events.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020040
Arend van Spriel5b435de2011-10-05 13:19:03 +020041/* watchdog timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020042#define TIMER_INTERVAL_WATCHDOG 1000
Arend van Spriel5b435de2011-10-05 13:19:03 +020043/* radio monitor timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020044#define TIMER_INTERVAL_RADIOCHK 800
Arend van Spriel5b435de2011-10-05 13:19:03 +020045
Arend van Spriel5b435de2011-10-05 13:19:03 +020046/* beacon interval, in unit of 1024TU */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020047#define BEACON_INTERVAL_DEFAULT 100
Arend van Spriel5b435de2011-10-05 13:19:03 +020048
49/* n-mode support capability */
50/* 2x2 includes both 1x1 & 2x2 devices
51 * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
52 * control it independently
53 */
54#define WL_11N_2x2 1
55#define WL_11N_3x3 3
56#define WL_11N_4x4 4
57
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020058#define EDCF_ACI_MASK 0x60
59#define EDCF_ACI_SHIFT 5
60#define EDCF_ECWMIN_MASK 0x0f
61#define EDCF_ECWMAX_SHIFT 4
62#define EDCF_AIFSN_MASK 0x0f
63#define EDCF_AIFSN_MAX 15
64#define EDCF_ECWMAX_MASK 0xf0
Arend van Spriel5b435de2011-10-05 13:19:03 +020065
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020066#define EDCF_AC_BE_TXOP_STA 0x0000
67#define EDCF_AC_BK_TXOP_STA 0x0000
68#define EDCF_AC_VO_ACI_STA 0x62
69#define EDCF_AC_VO_ECW_STA 0x32
70#define EDCF_AC_VI_ACI_STA 0x42
71#define EDCF_AC_VI_ECW_STA 0x43
72#define EDCF_AC_BK_ECW_STA 0xA4
73#define EDCF_AC_VI_TXOP_STA 0x005e
74#define EDCF_AC_VO_TXOP_STA 0x002f
75#define EDCF_AC_BE_ACI_STA 0x03
76#define EDCF_AC_BE_ECW_STA 0xA4
77#define EDCF_AC_BK_ACI_STA 0x27
78#define EDCF_AC_VO_TXOP_AP 0x002f
Arend van Spriel5b435de2011-10-05 13:19:03 +020079
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020080#define EDCF_TXOP2USEC(txop) ((txop) << 5)
81#define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
Arend van Spriel5b435de2011-10-05 13:19:03 +020082
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020083#define APHY_SYMBOL_TIME 4
84#define APHY_PREAMBLE_TIME 16
85#define APHY_SIGNAL_TIME 4
86#define APHY_SIFS_TIME 16
87#define APHY_SERVICE_NBITS 16
88#define APHY_TAIL_NBITS 6
89#define BPHY_SIFS_TIME 10
90#define BPHY_PLCP_SHORT_TIME 96
Arend van Spriel5b435de2011-10-05 13:19:03 +020091
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020092#define PREN_PREAMBLE 24
93#define PREN_MM_EXT 12
94#define PREN_PREAMBLE_EXT 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020095
96#define DOT11_MAC_HDR_LEN 24
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020097#define DOT11_ACK_LEN 10
98#define DOT11_BA_LEN 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#define DOT11_OFDM_SIGNAL_EXTENSION 6
100#define DOT11_MIN_FRAG_LEN 256
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200101#define DOT11_RTS_LEN 16
102#define DOT11_CTS_LEN 10
Arend van Spriel5b435de2011-10-05 13:19:03 +0200103#define DOT11_BA_BITMAP_LEN 128
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200104#define DOT11_MAXNUMFRAGS 16
Arend van Spriel5b435de2011-10-05 13:19:03 +0200105#define DOT11_MAX_FRAG_LEN 2346
106
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200107#define BPHY_PLCP_TIME 192
108#define RIFS_11N_TIME 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200109
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200110/* length of the BCN template area */
111#define BCN_TMPL_LEN 512
Arend van Spriel5b435de2011-10-05 13:19:03 +0200112
113/* brcms_bss_info flag bit values */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200114#define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200115
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200116/* chip rx buffer offset */
117#define BRCMS_HWRXOFF 38
Arend van Spriel5b435de2011-10-05 13:19:03 +0200118
119/* rfdisable delay timer 500 ms, runs of ALP clock */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200120#define RFDISABLE_DEFAULT 10000000
Arend van Spriel5b435de2011-10-05 13:19:03 +0200121
122#define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
123
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200124/* synthpu_dly times in us */
125#define SYNTHPU_DLY_APHY_US 3700
126#define SYNTHPU_DLY_BPHY_US 1050
127#define SYNTHPU_DLY_NPHY_US 2048
128#define SYNTHPU_DLY_LPPHY_US 300
Arend van Spriel5b435de2011-10-05 13:19:03 +0200129
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200130#define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200131
132/* Per-AC retry limit register definitions; uses defs.h bitfield macros */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200133#define EDCF_SHORT_S 0
134#define EDCF_SFB_S 4
135#define EDCF_LONG_S 8
136#define EDCF_LFB_S 12
137#define EDCF_SHORT_M BITFIELD_MASK(4)
138#define EDCF_SFB_M BITFIELD_MASK(4)
139#define EDCF_LONG_M BITFIELD_MASK(4)
140#define EDCF_LFB_M BITFIELD_MASK(4)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200141
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200142#define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
143#define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
144#define RETRY_LONG_DEF 4 /* Default Long retry count */
145#define RETRY_SHORT_FB 3 /* Short count for fb rate */
146#define RETRY_LONG_FB 2 /* Long count for fb rate */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200147
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200148#define APHY_CWMIN 15
149#define PHY_CWMAX 1023
Arend van Spriel5b435de2011-10-05 13:19:03 +0200150
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200151#define EDCF_AIFSN_MIN 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200152
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200153#define FRAGNUM_MASK 0xF
Arend van Spriel5b435de2011-10-05 13:19:03 +0200154
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200155#define APHY_SLOT_TIME 9
156#define BPHY_SLOT_TIME 20
Arend van Spriel5b435de2011-10-05 13:19:03 +0200157
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200158#define WL_SPURAVOID_OFF 0
159#define WL_SPURAVOID_ON1 1
160#define WL_SPURAVOID_ON2 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200161
162/* invalid core flags, use the saved coreflags */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200163#define BRCMS_USE_COREFLAGS 0xffffffff
Arend van Spriel5b435de2011-10-05 13:19:03 +0200164
165/* values for PLCPHdr_override */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200166#define BRCMS_PLCP_AUTO -1
167#define BRCMS_PLCP_SHORT 0
168#define BRCMS_PLCP_LONG 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200169
170/* values for g_protection_override and n_protection_override */
171#define BRCMS_PROTECTION_AUTO -1
172#define BRCMS_PROTECTION_OFF 0
173#define BRCMS_PROTECTION_ON 1
174#define BRCMS_PROTECTION_MMHDR_ONLY 2
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200175#define BRCMS_PROTECTION_CTS_ONLY 3
Arend van Spriel5b435de2011-10-05 13:19:03 +0200176
177/* values for g_protection_control and n_protection_control */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200178#define BRCMS_PROTECTION_CTL_OFF 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200179#define BRCMS_PROTECTION_CTL_LOCAL 1
180#define BRCMS_PROTECTION_CTL_OVERLAP 2
181
182/* values for n_protection */
183#define BRCMS_N_PROTECTION_OFF 0
184#define BRCMS_N_PROTECTION_OPTIONAL 1
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200185#define BRCMS_N_PROTECTION_20IN40 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200186#define BRCMS_N_PROTECTION_MIXEDMODE 3
187
188/* values for band specific 40MHz capabilities */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200189#define BRCMS_N_BW_20ALL 0
190#define BRCMS_N_BW_40ALL 1
191#define BRCMS_N_BW_20IN2G_40IN5G 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200192
193/* bitflags for SGI support (sgi_rx iovar) */
194#define BRCMS_N_SGI_20 0x01
195#define BRCMS_N_SGI_40 0x02
196
197/* defines used by the nrate iovar */
198/* MSC in use,indicates b0-6 holds an mcs */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200199#define NRATE_MCS_INUSE 0x00000080
Arend van Spriel5b435de2011-10-05 13:19:03 +0200200/* rate/mcs value */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200201#define NRATE_RATE_MASK 0x0000007f
Arend van Spriel5b435de2011-10-05 13:19:03 +0200202/* stf mode mask: siso, cdd, stbc, sdm */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200203#define NRATE_STF_MASK 0x0000ff00
Arend van Spriel5b435de2011-10-05 13:19:03 +0200204/* stf mode shift */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200205#define NRATE_STF_SHIFT 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200206/* bit indicate to override mcs only */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200207#define NRATE_OVERRIDE_MCS_ONLY 0x40000000
208#define NRATE_SGI_MASK 0x00800000 /* sgi mode */
209#define NRATE_SGI_SHIFT 23 /* sgi mode */
210#define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
211#define NRATE_LDPC_SHIFT 22 /* ldpc shift */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200212
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200213#define NRATE_STF_SISO 0 /* stf mode SISO */
214#define NRATE_STF_CDD 1 /* stf mode CDD */
215#define NRATE_STF_STBC 2 /* stf mode STBC */
216#define NRATE_STF_SDM 3 /* stf mode SDM */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200217
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200218#define MAX_DMA_SEGS 4
Arend van Spriel5b435de2011-10-05 13:19:03 +0200219
Seth Forshee75be3e22012-11-15 08:07:58 -0600220/* # of entries in Tx FIFO */
221#define NTXD 64
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222/* Max # of entries in Rx FIFO based on 4kb page size */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200223#define NRXD 256
Arend van Spriel5b435de2011-10-05 13:19:03 +0200224
Seth Forsheee041f652012-11-15 08:07:56 -0600225/* Amount of headroom to leave in Tx FIFO */
226#define TX_HEADROOM 4
227
Arend van Spriel5b435de2011-10-05 13:19:03 +0200228/* try to keep this # rbufs posted to the chip */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200229#define NRXBUFPOST 32
Arend van Spriel5b435de2011-10-05 13:19:03 +0200230
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200231/* max # frames to process in brcms_c_recv() */
232#define RXBND 8
233/* max # tx status to process in wlc_txstatus() */
234#define TXSBND 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235
Alwin Beukers44760652011-10-12 20:51:31 +0200236/* brcmu_format_flags() bit description structure */
237struct brcms_c_bit_desc {
238 u32 bit;
239 const char *name;
240};
241
Arend van Spriel5b435de2011-10-05 13:19:03 +0200242/*
243 * The following table lists the buffer memory allocated to xmt fifos in HW.
244 * the size is in units of 256bytes(one block), total size is HW dependent
245 * ucode has default fifo partition, sw can overwrite if necessary
246 *
247 * This is documented in twiki under the topic UcodeTxFifo. Please ensure
248 * the twiki is updated before making changes.
249 */
250
251/* Starting corerev for the fifo size table */
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200252#define XMTFIFOTBL_STARTREV 17
Arend van Spriel5b435de2011-10-05 13:19:03 +0200253
254struct d11init {
255 __le16 addr;
256 __le16 size;
257 __le32 value;
258};
259
Arend van Spriel5b435de2011-10-05 13:19:03 +0200260struct edcf_acparam {
261 u8 ACI;
262 u8 ECW;
263 u16 TXOP;
264} __packed;
265
Arend van Spriel5b435de2011-10-05 13:19:03 +0200266/* debug/trace */
Seth Forsheeb0341742012-11-15 08:08:01 -0600267uint brcm_msg_level;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200268
269/* TX FIFO number to WME/802.1E Access Category */
Arend van Sprielb7eec422011-11-10 20:30:18 +0100270static const u8 wme_fifo2ac[] = {
271 IEEE80211_AC_BK,
272 IEEE80211_AC_BE,
273 IEEE80211_AC_VI,
274 IEEE80211_AC_VO,
275 IEEE80211_AC_BE,
276 IEEE80211_AC_BE
277};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200278
Arend van Sprielb7eec422011-11-10 20:30:18 +0100279/* ieee80211 Access Category to TX FIFO number */
280static const u8 wme_ac2fifo[] = {
281 TX_AC_VO_FIFO,
282 TX_AC_VI_FIFO,
283 TX_AC_BE_FIFO,
284 TX_AC_BK_FIFO
285};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200286
Arend van Spriel5b435de2011-10-05 13:19:03 +0200287static const u16 xmtfifo_sz[][NFIFO] = {
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200288 /* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
289 {20, 192, 192, 21, 17, 5},
290 /* corerev 18: */
291 {0, 0, 0, 0, 0, 0},
292 /* corerev 19: */
293 {0, 0, 0, 0, 0, 0},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200294 /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
295 {20, 192, 192, 21, 17, 5},
296 /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
297 {9, 58, 22, 14, 14, 5},
298 /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
299 {20, 192, 192, 21, 17, 5},
300 /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
301 {20, 192, 192, 21, 17, 5},
302 /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
303 {9, 58, 22, 14, 14, 5},
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200304 /* corerev 25: */
305 {0, 0, 0, 0, 0, 0},
306 /* corerev 26: */
307 {0, 0, 0, 0, 0, 0},
308 /* corerev 27: */
309 {0, 0, 0, 0, 0, 0},
310 /* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
311 {9, 58, 22, 14, 14, 5},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200312};
313
Joe Perches8ae74652012-01-15 00:38:38 -0800314#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200315static const char * const fifo_names[] = {
316 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
317#else
318static const char fifo_names[6][0];
319#endif
320
Joe Perches8ae74652012-01-15 00:38:38 -0800321#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200322/* pointer to most recently allocated wl/wlc */
323static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
324#endif
325
Seth Forshee32d0f122012-11-15 08:07:55 -0600326/* Mapping of ieee80211 AC numbers to tx fifos */
327static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
328 [IEEE80211_AC_VO] = TX_AC_VO_FIFO,
329 [IEEE80211_AC_VI] = TX_AC_VI_FIFO,
330 [IEEE80211_AC_BE] = TX_AC_BE_FIFO,
331 [IEEE80211_AC_BK] = TX_AC_BK_FIFO,
332};
333
Seth Forsheee041f652012-11-15 08:07:56 -0600334/* Mapping of tx fifos to ieee80211 AC numbers */
335static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
336 [TX_AC_BK_FIFO] = IEEE80211_AC_BK,
337 [TX_AC_BE_FIFO] = IEEE80211_AC_BE,
338 [TX_AC_VI_FIFO] = IEEE80211_AC_VI,
339 [TX_AC_VO_FIFO] = IEEE80211_AC_VO,
340};
341
Seth Forshee32d0f122012-11-15 08:07:55 -0600342static u8 brcms_ac_to_fifo(u8 ac)
343{
344 if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
345 return TX_AC_BE_FIFO;
346 return ac_to_fifo_mapping[ac];
347}
348
Seth Forsheee041f652012-11-15 08:07:56 -0600349static u8 brcms_fifo_to_ac(u8 fifo)
350{
351 if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
352 return IEEE80211_AC_BE;
353 return fifo_to_ac_mapping[fifo];
354}
355
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200356/* Find basic rate for a given rate */
357static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
358{
359 if (is_mcs_rate(rspec))
360 return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
361 .leg_ofdm];
362 return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
363}
364
365static u16 frametype(u32 rspec, u8 mimoframe)
366{
367 if (is_mcs_rate(rspec))
368 return mimoframe;
369 return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
370}
371
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200372/* currently the best mechanism for determining SIFS is the band in use */
373static u16 get_sifs(struct brcms_band *band)
374{
375 return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
376 BPHY_SIFS_TIME;
377}
378
379/*
380 * Detect Card removed.
381 * Even checking an sbconfig register read will not false trigger when the core
382 * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
383 * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
384 * reg with fixed 0/1 pattern (some platforms return all 0).
385 * If clocks are present, call the sb routine which will figure out if the
386 * device is removed.
387 */
388static bool brcms_deviceremoved(struct brcms_c_info *wlc)
389{
Arend van Spriel16d28122011-12-08 15:06:51 -0800390 u32 macctrl;
391
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200392 if (!wlc->hw->clk)
393 return ai_deviceremoved(wlc->hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -0800394 macctrl = bcma_read32(wlc->hw->d11core,
395 D11REGOFFS(maccontrol));
396 return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200397}
398
399/* sum the individual fifo tx pending packet counts */
Seth Forsheee041f652012-11-15 08:07:56 -0600400static int brcms_txpktpendtot(struct brcms_c_info *wlc)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200401{
Seth Forsheee041f652012-11-15 08:07:56 -0600402 int i;
403 int pending = 0;
404
405 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
406 if (wlc->hw->di[i])
407 pending += dma_txpending(wlc->hw->di[i]);
408 return pending;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200409}
410
411static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
412{
413 return wlc->pub->_nbands > 1 && !wlc->bandlocked;
414}
415
416static int brcms_chspec_bw(u16 chanspec)
417{
418 if (CHSPEC_IS40(chanspec))
419 return BRCMS_40_MHZ;
420 if (CHSPEC_IS20(chanspec))
421 return BRCMS_20_MHZ;
422
423 return BRCMS_10_MHZ;
424}
425
Arend van Spriel5b435de2011-10-05 13:19:03 +0200426static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
427{
428 if (cfg == NULL)
429 return;
430
431 kfree(cfg->current_bss);
432 kfree(cfg);
433}
434
435static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
436{
437 if (wlc == NULL)
438 return;
439
440 brcms_c_bsscfg_mfree(wlc->bsscfg);
441 kfree(wlc->pub);
442 kfree(wlc->modulecb);
443 kfree(wlc->default_bss);
444 kfree(wlc->protection);
445 kfree(wlc->stf);
446 kfree(wlc->bandstate[0]);
447 kfree(wlc->corestate->macstat_snapshot);
448 kfree(wlc->corestate);
449 kfree(wlc->hw->bandstate[0]);
450 kfree(wlc->hw);
451
452 /* free the wlc */
453 kfree(wlc);
454 wlc = NULL;
455}
456
457static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
458{
459 struct brcms_bss_cfg *cfg;
460
461 cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
462 if (cfg == NULL)
463 goto fail;
464
465 cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
466 if (cfg->current_bss == NULL)
467 goto fail;
468
469 return cfg;
470
471 fail:
472 brcms_c_bsscfg_mfree(cfg);
473 return NULL;
474}
475
476static struct brcms_c_info *
477brcms_c_attach_malloc(uint unit, uint *err, uint devid)
478{
479 struct brcms_c_info *wlc;
480
481 wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
482 if (wlc == NULL) {
483 *err = 1002;
484 goto fail;
485 }
486
487 /* allocate struct brcms_c_pub state structure */
488 wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
489 if (wlc->pub == NULL) {
490 *err = 1003;
491 goto fail;
492 }
493 wlc->pub->wlc = wlc;
494
495 /* allocate struct brcms_hardware state structure */
496
497 wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
498 if (wlc->hw == NULL) {
499 *err = 1005;
500 goto fail;
501 }
502 wlc->hw->wlc = wlc;
503
504 wlc->hw->bandstate[0] =
505 kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
506 if (wlc->hw->bandstate[0] == NULL) {
507 *err = 1006;
508 goto fail;
509 } else {
510 int i;
511
512 for (i = 1; i < MAXBANDS; i++)
513 wlc->hw->bandstate[i] = (struct brcms_hw_band *)
514 ((unsigned long)wlc->hw->bandstate[0] +
515 (sizeof(struct brcms_hw_band) * i));
516 }
517
518 wlc->modulecb =
519 kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
520 if (wlc->modulecb == NULL) {
521 *err = 1009;
522 goto fail;
523 }
524
525 wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
526 if (wlc->default_bss == NULL) {
527 *err = 1010;
528 goto fail;
529 }
530
531 wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
532 if (wlc->bsscfg == NULL) {
533 *err = 1011;
534 goto fail;
535 }
536
537 wlc->protection = kzalloc(sizeof(struct brcms_protection),
538 GFP_ATOMIC);
539 if (wlc->protection == NULL) {
540 *err = 1016;
541 goto fail;
542 }
543
544 wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
545 if (wlc->stf == NULL) {
546 *err = 1017;
547 goto fail;
548 }
549
550 wlc->bandstate[0] =
551 kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
552 if (wlc->bandstate[0] == NULL) {
553 *err = 1025;
554 goto fail;
555 } else {
556 int i;
557
558 for (i = 1; i < MAXBANDS; i++)
559 wlc->bandstate[i] = (struct brcms_band *)
560 ((unsigned long)wlc->bandstate[0]
561 + (sizeof(struct brcms_band)*i));
562 }
563
564 wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
565 if (wlc->corestate == NULL) {
566 *err = 1026;
567 goto fail;
568 }
569
570 wlc->corestate->macstat_snapshot =
571 kzalloc(sizeof(struct macstat), GFP_ATOMIC);
572 if (wlc->corestate->macstat_snapshot == NULL) {
573 *err = 1027;
574 goto fail;
575 }
576
577 return wlc;
578
579 fail:
580 brcms_c_detach_mfree(wlc);
581 return NULL;
582}
583
584/*
585 * Update the slot timing for standard 11b/g (20us slots)
586 * or shortslot 11g (9us slots)
587 * The PSM needs to be suspended for this call.
588 */
589static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
590 bool shortslot)
591{
Arend van Spriel16d28122011-12-08 15:06:51 -0800592 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200593
594 if (shortslot) {
595 /* 11g short slot: 11a timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800596 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200597 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
598 } else {
599 /* 11g long slot: 11b timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800600 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200601 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
602 }
603}
604
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200605/*
606 * calculate frame duration of a given rate and length, return
607 * time in usec unit
608 */
Arend van Spriel094b1992011-10-18 14:03:07 +0200609static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
610 u8 preamble_type, uint mac_len)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200611{
612 uint nsyms, dur = 0, Ndps, kNdps;
613 uint rate = rspec2rate(ratespec);
614
615 if (rate == 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600616 brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200617 wlc->pub->unit);
618 rate = BRCM_RATE_1M;
619 }
620
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200621 if (is_mcs_rate(ratespec)) {
622 uint mcs = ratespec & RSPEC_RATE_MASK;
623 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
624
625 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
626 if (preamble_type == BRCMS_MM_PREAMBLE)
627 dur += PREN_MM_EXT;
628 /* 1000Ndbps = kbps * 4 */
629 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
630 rspec_issgi(ratespec)) * 4;
631
632 if (rspec_stc(ratespec) == 0)
633 nsyms =
634 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
635 APHY_TAIL_NBITS) * 1000, kNdps);
636 else
637 /* STBC needs to have even number of symbols */
638 nsyms =
639 2 *
640 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
641 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
642
643 dur += APHY_SYMBOL_TIME * nsyms;
644 if (wlc->band->bandtype == BRCM_BAND_2G)
645 dur += DOT11_OFDM_SIGNAL_EXTENSION;
646 } else if (is_ofdm_rate(rate)) {
647 dur = APHY_PREAMBLE_TIME;
648 dur += APHY_SIGNAL_TIME;
649 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
650 Ndps = rate * 2;
651 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
652 nsyms =
653 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
654 Ndps);
655 dur += APHY_SYMBOL_TIME * nsyms;
656 if (wlc->band->bandtype == BRCM_BAND_2G)
657 dur += DOT11_OFDM_SIGNAL_EXTENSION;
658 } else {
659 /*
660 * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
661 * will divide out
662 */
663 mac_len = mac_len * 8 * 2;
664 /* calc ceiling of bits/rate = microseconds of air time */
665 dur = (mac_len + rate - 1) / rate;
666 if (preamble_type & BRCMS_SHORT_PREAMBLE)
667 dur += BPHY_PLCP_SHORT_TIME;
668 else
669 dur += BPHY_PLCP_TIME;
670 }
671 return dur;
672}
673
Arend van Spriel5b435de2011-10-05 13:19:03 +0200674static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
675 const struct d11init *inits)
676{
Arend van Spriel16d28122011-12-08 15:06:51 -0800677 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200678 int i;
Arend van Spriel16d28122011-12-08 15:06:51 -0800679 uint offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200680 u16 size;
681 u32 value;
682
Seth Forsheeb353dda2012-11-15 08:08:03 -0600683 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200684
Arend van Spriel5b435de2011-10-05 13:19:03 +0200685 for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
686 size = le16_to_cpu(inits[i].size);
Arend van Spriel16d28122011-12-08 15:06:51 -0800687 offset = le16_to_cpu(inits[i].addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200688 value = le32_to_cpu(inits[i].value);
689 if (size == 2)
Arend van Spriel16d28122011-12-08 15:06:51 -0800690 bcma_write16(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200691 else if (size == 4)
Arend van Spriel16d28122011-12-08 15:06:51 -0800692 bcma_write32(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200693 else
694 break;
695 }
696}
697
698static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
699{
700 u8 idx;
701 u16 addr[] = {
702 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
703 M_HOST_FLAGS5
704 };
705
706 for (idx = 0; idx < MHFMAX; idx++)
707 brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
708}
709
710static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
711{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200712 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
713
714 /* init microcode host flags */
715 brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
716
717 /* do band-specific ucode IHR, SHM, and SCR inits */
Hauke Mehrtens6f80f012012-12-07 00:35:53 +0100718 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200719 if (BRCMS_ISNPHY(wlc_hw->band))
720 brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
721 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600722 brcms_err(wlc_hw->d11core,
723 "%s: wl%d: unsupported phy in corerev %d\n",
724 __func__, wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200725 wlc_hw->corerev);
726 } else {
727 if (D11REV_IS(wlc_hw->corerev, 24)) {
728 if (BRCMS_ISLCNPHY(wlc_hw->band))
729 brcms_c_write_inits(wlc_hw,
730 ucode->d11lcn0bsinitvals24);
731 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600732 brcms_err(wlc_hw->d11core,
733 "%s: wl%d: unsupported phy in core rev %d\n",
734 __func__, wlc_hw->unit,
735 wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600737 brcms_err(wlc_hw->d11core,
738 "%s: wl%d: unsupported corerev %d\n",
739 __func__, wlc_hw->unit, wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200740 }
741 }
742}
743
Arend van Spriela8779e42011-12-08 15:06:58 -0800744static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
745{
746 struct bcma_device *core = wlc_hw->d11core;
747 u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
748
749 bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
750}
751
Arend van Spriel5b435de2011-10-05 13:19:03 +0200752static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
753{
Seth Forsheeb353dda2012-11-15 08:08:03 -0600754 brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200755
756 wlc_hw->phyclk = clk;
757
758 if (OFF == clk) { /* clear gmode bit, put phy into reset */
759
Arend van Spriela8779e42011-12-08 15:06:58 -0800760 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
761 (SICF_PRST | SICF_FGC));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200762 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800763 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 udelay(1);
765
766 } else { /* take phy out of reset */
767
Arend van Spriela8779e42011-12-08 15:06:58 -0800768 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200769 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800770 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771 udelay(1);
772
773 }
774}
775
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200776/* low-level band switch utility routine */
777static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
778{
Seth Forshee913911f2012-11-15 08:08:04 -0600779 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
780 bandunit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200781
782 wlc_hw->band = wlc_hw->bandstate[bandunit];
783
784 /*
785 * BMAC_NOTE:
786 * until we eliminate need for wlc->band refs in low level code
787 */
788 wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
789
790 /* set gmode core flag */
Arend van Spriela8779e42011-12-08 15:06:58 -0800791 if (wlc_hw->sbclk && !wlc_hw->noreset) {
792 u32 gmode = 0;
793
794 if (bandunit == 0)
795 gmode = SICF_GMODE;
796
797 brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
798 }
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200799}
800
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801/* switch to new band but leave it inactive */
802static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
803{
804 struct brcms_hardware *wlc_hw = wlc->hw;
805 u32 macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -0800806 u32 macctrl;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200807
Seth Forshee913911f2012-11-15 08:08:04 -0600808 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel16d28122011-12-08 15:06:51 -0800809 macctrl = bcma_read32(wlc_hw->d11core,
810 D11REGOFFS(maccontrol));
811 WARN_ON((macctrl & MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812
813 /* disable interrupts */
814 macintmask = brcms_intrsoff(wlc->wl);
815
816 /* radio off */
817 wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
818
819 brcms_b_core_phy_clk(wlc_hw, OFF);
820
821 brcms_c_setxband(wlc_hw, bandunit);
822
823 return macintmask;
824}
825
Arend van Spriel5b435de2011-10-05 13:19:03 +0200826/* process an individual struct tx_status */
827static bool
828brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
829{
Seth Forsheee041f652012-11-15 08:07:56 -0600830 struct sk_buff *p = NULL;
831 uint queue = NFIFO;
832 struct dma_pub *dma = NULL;
Seth Forsheecdf43522012-11-15 08:08:09 -0600833 struct d11txh *txh = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200834 struct scb *scb = NULL;
835 bool free_pdu;
836 int tx_rts, tx_frame_count, tx_rts_count;
837 uint totlen, supr_status;
838 bool lastframe;
839 struct ieee80211_hdr *h;
840 u16 mcl;
841 struct ieee80211_tx_info *tx_info;
842 struct ieee80211_tx_rate *txrate;
843 int i;
Seth Forsheee041f652012-11-15 08:07:56 -0600844 bool fatal = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200845
Seth Forsheecdf43522012-11-15 08:08:09 -0600846 trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
847 txs->frameid, txs->status, txs->lasttxtime,
848 txs->sequence, txs->phyerr, txs->ackphyrxsh);
849
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850 /* discard intermediate indications for ucode with one legitimate case:
851 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
852 * but the subsequent tx of DATA failed. so it will start rts/cts
853 * from the beginning (resetting the rts transmission count)
854 */
855 if (!(txs->status & TX_STATUS_AMPDU)
856 && (txs->status & TX_STATUS_INTERMEDIATE)) {
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600857 brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600858 fatal = false;
859 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860 }
861
862 queue = txs->frameid & TXFID_QUEUE_MASK;
Seth Forsheecdf43522012-11-15 08:08:09 -0600863 if (queue >= NFIFO) {
864 brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
Seth Forsheee041f652012-11-15 08:07:56 -0600865 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600866 }
Seth Forsheee041f652012-11-15 08:07:56 -0600867
868 dma = wlc->hw->di[queue];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200869
870 p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
Seth Forsheecdf43522012-11-15 08:08:09 -0600871 if (p == NULL) {
872 brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600873 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600874 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875
876 txh = (struct d11txh *) (p->data);
877 mcl = le16_to_cpu(txh->MacTxControlLow);
878
Seth Forsheecdf43522012-11-15 08:08:09 -0600879 if (txs->phyerr)
880 brcms_err(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
881 txs->phyerr, txh->MainRates);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200882
Seth Forsheecdf43522012-11-15 08:08:09 -0600883 if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
884 brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600885 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600886 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 tx_info = IEEE80211_SKB_CB(p);
888 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
889
Thomas Huehn644e8c02012-07-10 14:01:37 +0200890 if (tx_info->rate_driver_data[0])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200891 scb = &wlc->pri_scb;
892
893 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
894 brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
Seth Forsheee041f652012-11-15 08:07:56 -0600895 fatal = false;
896 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897 }
898
Seth Forsheecdf43522012-11-15 08:08:09 -0600899 /*
900 * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
901 * frames; this traces them for the rest.
902 */
903 trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
904
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905 supr_status = txs->status & TX_STATUS_SUPR_MASK;
Seth Forsheecdf43522012-11-15 08:08:09 -0600906 if (supr_status == TX_STATUS_SUPR_BADCH) {
907 unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600908 brcms_dbg_tx(wlc->hw->d11core,
Seth Forsheecdf43522012-11-15 08:08:09 -0600909 "Pkt tx suppressed, dest chan %u, current %d\n",
910 (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600911 CHSPEC_CHANNEL(wlc->default_bss->chanspec));
Seth Forsheecdf43522012-11-15 08:08:09 -0600912 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913
914 tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
915 tx_frame_count =
916 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
917 tx_rts_count =
918 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
919
920 lastframe = !ieee80211_has_morefrags(h->frame_control);
921
922 if (!lastframe) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600923 brcms_err(wlc->hw->d11core, "Not last frame!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200924 } else {
925 /*
926 * Set information to be consumed by Minstrel ht.
927 *
928 * The "fallback limit" is the number of tx attempts a given
929 * MPDU is sent at the "primary" rate. Tx attempts beyond that
930 * limit are sent at the "secondary" rate.
931 * A 'short frame' does not exceed RTS treshold.
932 */
933 u16 sfbl, /* Short Frame Rate Fallback Limit */
934 lfbl, /* Long Frame Rate Fallback Limit */
935 fbl;
936
Arend van Sprielb7eec422011-11-10 20:30:18 +0100937 if (queue < IEEE80211_NUM_ACS) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200938 sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
939 EDCF_SFB);
940 lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
941 EDCF_LFB);
942 } else {
943 sfbl = wlc->SFBL;
944 lfbl = wlc->LFBL;
945 }
946
947 txrate = tx_info->status.rates;
948 if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
949 fbl = lfbl;
950 else
951 fbl = sfbl;
952
953 ieee80211_tx_info_clear_status(tx_info);
954
955 if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
956 /*
957 * rate selection requested a fallback rate
958 * and we used it
959 */
960 txrate[0].count = fbl;
961 txrate[1].count = tx_frame_count - fbl;
962 } else {
963 /*
964 * rate selection did not request fallback rate, or
965 * we didn't need it
966 */
967 txrate[0].count = tx_frame_count;
968 /*
969 * rc80211_minstrel.c:minstrel_tx_status() expects
970 * unused rates to be marked with idx = -1
971 */
972 txrate[1].idx = -1;
973 txrate[1].count = 0;
974 }
975
976 /* clear the rest of the rates */
977 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
978 txrate[i].idx = -1;
979 txrate[i].count = 0;
980 }
981
982 if (txs->status & TX_STATUS_ACK_RCV)
983 tx_info->flags |= IEEE80211_TX_STAT_ACK;
984 }
985
Arend van Sprielad4d71f2011-11-10 20:30:26 +0100986 totlen = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200987 free_pdu = true;
988
Arend van Spriel5b435de2011-10-05 13:19:03 +0200989 if (lastframe) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200990 /* remove PLCP & Broadcom tx descriptor header */
991 skb_pull(p, D11_PHY_HDR_LEN);
992 skb_pull(p, D11_TXH_LEN);
993 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
994 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600995 brcms_err(wlc->hw->d11core,
996 "%s: Not last frame => not calling tx_status\n",
997 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200998 }
999
Seth Forsheee041f652012-11-15 08:07:56 -06001000 fatal = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001001
Seth Forsheee041f652012-11-15 08:07:56 -06001002 out:
Seth Forsheecdf43522012-11-15 08:08:09 -06001003 if (fatal) {
1004 if (txh)
1005 trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
1006 sizeof(*txh));
1007 if (p)
1008 brcmu_pkt_buf_free_skb(p);
1009 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001010
Seth Forsheee041f652012-11-15 08:07:56 -06001011 if (dma && queue < NFIFO) {
1012 u16 ac_queue = brcms_fifo_to_ac(queue);
1013 if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
1014 ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
1015 ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
1016 dma_kick_tx(dma);
1017 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001018
Seth Forsheee041f652012-11-15 08:07:56 -06001019 return fatal;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001020}
1021
1022/* process tx completion events in BMAC
1023 * Return true if more tx status need to be processed. false otherwise.
1024 */
1025static bool
1026brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
1027{
1028 bool morepending = false;
Arend van Spriel16d28122011-12-08 15:06:51 -08001029 struct bcma_device *core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001030 struct tx_status txstatus, *txs;
1031 u32 s1, s2;
1032 uint n = 0;
1033 /*
1034 * Param 'max_tx_num' indicates max. # tx status to process before
1035 * break out.
1036 */
1037 uint max_tx_num = bound ? TXSBND : -1;
1038
Arend van Spriel5b435de2011-10-05 13:19:03 +02001039 txs = &txstatus;
Arend van Spriel16d28122011-12-08 15:06:51 -08001040 core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001041 *fatal = false;
Arend van Spriel16d28122011-12-08 15:06:51 -08001042 s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001043 while (!(*fatal)
Arend van Spriel16d28122011-12-08 15:06:51 -08001044 && (s1 & TXS_V)) {
Piotr Haber57fe5042012-11-28 21:44:07 +01001045 /* !give others some time to run! */
1046 if (n >= max_tx_num) {
1047 morepending = true;
1048 break;
1049 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001050
1051 if (s1 == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001052 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
1053 __func__);
Piotr Haber57fe5042012-11-28 21:44:07 +01001054 *fatal = true;
1055 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001056 }
Arend van Spriel16d28122011-12-08 15:06:51 -08001057 s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001058
1059 txs->status = s1 & TXS_STATUS_MASK;
1060 txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
1061 txs->sequence = s2 & TXS_SEQ_MASK;
1062 txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
1063 txs->lasttxtime = 0;
1064
1065 *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
1066
Arend van Spriel16d28122011-12-08 15:06:51 -08001067 s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
Piotr Haber57fe5042012-11-28 21:44:07 +01001068 n++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001069 }
1070
1071 if (*fatal)
Piotr Haber57fe5042012-11-28 21:44:07 +01001072 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001073
Arend van Spriel5b435de2011-10-05 13:19:03 +02001074 return morepending;
1075}
1076
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001077static void brcms_c_tbtt(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001078{
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001079 if (!wlc->bsscfg->BSS)
1080 /*
1081 * DirFrmQ is now valid...defer setting until end
1082 * of ATIM window
1083 */
1084 wlc->qvalid |= MCMD_DIRFRMQVAL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001085}
1086
1087/* set initial host flags value */
1088static void
1089brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
1090{
1091 struct brcms_hardware *wlc_hw = wlc->hw;
1092
1093 memset(mhfs, 0, MHFMAX * sizeof(u16));
1094
1095 mhfs[MHF2] |= mhf2_init;
1096
1097 /* prohibit use of slowclock on multifunction boards */
1098 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
1099 mhfs[MHF1] |= MHF1_FORCEFASTCLK;
1100
1101 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
1102 mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
1103 mhfs[MHF1] |= MHF1_IQSWAP_WAR;
1104 }
1105}
1106
Arend van Spriele81da652011-12-08 15:06:53 -08001107static uint
1108dmareg(uint direction, uint fifonum)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001109{
1110 if (direction == DMA_TX)
Arend van Spriele81da652011-12-08 15:06:53 -08001111 return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
1112 return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001113}
1114
1115static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
1116{
1117 uint i;
1118 char name[8];
1119 /*
1120 * ucode host flag 2 needed for pio mode, independent of band and fifo
1121 */
1122 u16 pio_mhf2 = 0;
1123 struct brcms_hardware *wlc_hw = wlc->hw;
1124 uint unit = wlc_hw->unit;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001125
1126 /* name and offsets for dma_attach */
1127 snprintf(name, sizeof(name), "wl%d", unit);
1128
1129 if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
1130 int dma_attach_err = 0;
1131
1132 /*
1133 * FIFO 0
1134 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
1135 * RX: RX_FIFO (RX data packets)
1136 */
Seth Forsheee041f652012-11-15 08:07:56 -06001137 wlc_hw->di[0] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001138 (wme ? dmareg(DMA_TX, 0) : 0),
1139 dmareg(DMA_RX, 0),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001140 (wme ? NTXD : 0), NRXD,
1141 RXBUFSZ, -1, NRXBUFPOST,
Seth Forshee90123e02012-11-15 08:08:07 -06001142 BRCMS_HWRXOFF);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001143 dma_attach_err |= (NULL == wlc_hw->di[0]);
1144
1145 /*
1146 * FIFO 1
1147 * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
1148 * (legacy) TX_DATA_FIFO (TX data packets)
1149 * RX: UNUSED
1150 */
Seth Forsheee041f652012-11-15 08:07:56 -06001151 wlc_hw->di[1] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001152 dmareg(DMA_TX, 1), 0,
Seth Forshee90123e02012-11-15 08:08:07 -06001153 NTXD, 0, 0, -1, 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001154 dma_attach_err |= (NULL == wlc_hw->di[1]);
1155
1156 /*
1157 * FIFO 2
1158 * TX: TX_AC_VI_FIFO (TX AC Video data packets)
1159 * RX: UNUSED
1160 */
Seth Forsheee041f652012-11-15 08:07:56 -06001161 wlc_hw->di[2] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001162 dmareg(DMA_TX, 2), 0,
Seth Forshee90123e02012-11-15 08:08:07 -06001163 NTXD, 0, 0, -1, 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001164 dma_attach_err |= (NULL == wlc_hw->di[2]);
1165 /*
1166 * FIFO 3
1167 * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
1168 * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
1169 */
Seth Forsheee041f652012-11-15 08:07:56 -06001170 wlc_hw->di[3] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001171 dmareg(DMA_TX, 3),
1172 0, NTXD, 0, 0, -1,
Seth Forshee90123e02012-11-15 08:08:07 -06001173 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001174 dma_attach_err |= (NULL == wlc_hw->di[3]);
1175/* Cleaner to leave this as if with AP defined */
1176
1177 if (dma_attach_err) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001178 brcms_err(wlc_hw->d11core,
1179 "wl%d: wlc_attach: dma_attach failed\n",
1180 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001181 return false;
1182 }
1183
1184 /* get pointer to dma engine tx flow control variable */
1185 for (i = 0; i < NFIFO; i++)
1186 if (wlc_hw->di[i])
1187 wlc_hw->txavail[i] =
1188 (uint *) dma_getvar(wlc_hw->di[i],
1189 "&txavail");
1190 }
1191
1192 /* initial ucode host flags */
1193 brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
1194
1195 return true;
1196}
1197
1198static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
1199{
1200 uint j;
1201
1202 for (j = 0; j < NFIFO; j++) {
1203 if (wlc_hw->di[j]) {
1204 dma_detach(wlc_hw->di[j]);
1205 wlc_hw->di[j] = NULL;
1206 }
1207 }
1208}
1209
1210/*
1211 * Initialize brcms_c_info default values ...
1212 * may get overrides later in this function
1213 * BMAC_NOTES, move low out and resolve the dangling ones
1214 */
1215static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
1216{
1217 struct brcms_c_info *wlc = wlc_hw->wlc;
1218
1219 /* set default sw macintmask value */
1220 wlc->defmacintmask = DEF_MACINTMASK;
1221
1222 /* various 802.11g modes */
1223 wlc_hw->shortslot = false;
1224
1225 wlc_hw->SFBL = RETRY_SHORT_FB;
1226 wlc_hw->LFBL = RETRY_LONG_FB;
1227
1228 /* default mac retry limits */
1229 wlc_hw->SRL = RETRY_SHORT_DEF;
1230 wlc_hw->LRL = RETRY_LONG_DEF;
1231 wlc_hw->chanspec = ch20mhz_chspec(1);
1232}
1233
1234static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
1235{
1236 /* delay before first read of ucode state */
1237 udelay(40);
1238
1239 /* wait until ucode is no longer asleep */
1240 SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
1241 DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
1242}
1243
1244/* control chip clock to save power, enable dynamic clock or force fast clock */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001245static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001246{
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001247 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001248 /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
1249 * on backplane, but mac core will still run on ALP(not HT) when
1250 * it enters powersave mode, which means the FCA bit may not be
1251 * set. Should wakeup mac if driver wants it to run on HT.
1252 */
1253
1254 if (wlc_hw->clk) {
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001255 if (mode == BCMA_CLKMODE_FAST) {
Arend van Spriel16d28122011-12-08 15:06:51 -08001256 bcma_set32(wlc_hw->d11core,
1257 D11REGOFFS(clk_ctl_st),
1258 CCS_FORCEHT);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001259
1260 udelay(64);
1261
Arend van Spriel16d28122011-12-08 15:06:51 -08001262 SPINWAIT(
1263 ((bcma_read32(wlc_hw->d11core,
1264 D11REGOFFS(clk_ctl_st)) &
1265 CCS_HTAVAIL) == 0),
1266 PMU_MAX_TRANSITION_DLY);
1267 WARN_ON(!(bcma_read32(wlc_hw->d11core,
1268 D11REGOFFS(clk_ctl_st)) &
1269 CCS_HTAVAIL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001270 } else {
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001271 if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
Arend van Spriel16d28122011-12-08 15:06:51 -08001272 (bcma_read32(wlc_hw->d11core,
1273 D11REGOFFS(clk_ctl_st)) &
1274 (CCS_FORCEHT | CCS_HTAREQ)))
1275 SPINWAIT(
1276 ((bcma_read32(wlc_hw->d11core,
1277 offsetof(struct d11regs,
1278 clk_ctl_st)) &
1279 CCS_HTAVAIL) == 0),
1280 PMU_MAX_TRANSITION_DLY);
1281 bcma_mask32(wlc_hw->d11core,
1282 D11REGOFFS(clk_ctl_st),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001283 ~CCS_FORCEHT);
1284 }
1285 }
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001286 wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001287 } else {
1288
1289 /* old chips w/o PMU, force HT through cc,
1290 * then use FCA to verify mac is running fast clock
1291 */
1292
1293 wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
1294
1295 /* check fast clock is available (if core is not in reset) */
1296 if (wlc_hw->forcefastclk && wlc_hw->clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001297 WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02001298 SISF_FCLKA));
1299
1300 /*
1301 * keep the ucode wake bit on if forcefastclk is on since we
1302 * do not want ucode to put us back to slow clock when it dozes
1303 * for PM mode. Code below matches the wake override bit with
1304 * current forcefastclk state. Only setting bit in wake_override
1305 * instead of waking ucode immediately since old code had this
1306 * behavior. Older code set wlc->forcefastclk but only had the
1307 * wake happen if the wakup_ucode work (protected by an up
1308 * check) was executed just below.
1309 */
1310 if (wlc_hw->forcefastclk)
1311 mboolset(wlc_hw->wake_override,
1312 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1313 else
1314 mboolclr(wlc_hw->wake_override,
1315 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1316 }
1317}
1318
1319/* set or clear ucode host flag bits
1320 * it has an optimization for no-change write
1321 * it only writes through shared memory when the core has clock;
1322 * pre-CLK changes should use wlc_write_mhf to get around the optimization
1323 *
1324 *
1325 * bands values are: BRCM_BAND_AUTO <--- Current band only
1326 * BRCM_BAND_5G <--- 5G band only
1327 * BRCM_BAND_2G <--- 2G band only
1328 * BRCM_BAND_ALL <--- All bands
1329 */
1330void
1331brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
1332 int bands)
1333{
1334 u16 save;
1335 u16 addr[MHFMAX] = {
1336 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
1337 M_HOST_FLAGS5
1338 };
1339 struct brcms_hw_band *band;
1340
1341 if ((val & ~mask) || idx >= MHFMAX)
1342 return; /* error condition */
1343
1344 switch (bands) {
1345 /* Current band only or all bands,
1346 * then set the band to current band
1347 */
1348 case BRCM_BAND_AUTO:
1349 case BRCM_BAND_ALL:
1350 band = wlc_hw->band;
1351 break;
1352 case BRCM_BAND_5G:
1353 band = wlc_hw->bandstate[BAND_5G_INDEX];
1354 break;
1355 case BRCM_BAND_2G:
1356 band = wlc_hw->bandstate[BAND_2G_INDEX];
1357 break;
1358 default:
1359 band = NULL; /* error condition */
1360 }
1361
1362 if (band) {
1363 save = band->mhfs[idx];
1364 band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
1365
1366 /* optimization: only write through if changed, and
1367 * changed band is the current band
1368 */
1369 if (wlc_hw->clk && (band->mhfs[idx] != save)
1370 && (band == wlc_hw->band))
1371 brcms_b_write_shm(wlc_hw, addr[idx],
1372 (u16) band->mhfs[idx]);
1373 }
1374
1375 if (bands == BRCM_BAND_ALL) {
1376 wlc_hw->bandstate[0]->mhfs[idx] =
1377 (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
1378 wlc_hw->bandstate[1]->mhfs[idx] =
1379 (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
1380 }
1381}
1382
1383/* set the maccontrol register to desired reset state and
1384 * initialize the sw cache of the register
1385 */
1386static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
1387{
1388 /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
1389 wlc_hw->maccontrol = 0;
1390 wlc_hw->suspended_fifos = 0;
1391 wlc_hw->wake_override = 0;
1392 wlc_hw->mute_override = 0;
1393 brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
1394}
1395
1396/*
1397 * write the software state of maccontrol and
1398 * overrides to the maccontrol register
1399 */
1400static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
1401{
1402 u32 maccontrol = wlc_hw->maccontrol;
1403
1404 /* OR in the wake bit if overridden */
1405 if (wlc_hw->wake_override)
1406 maccontrol |= MCTL_WAKE;
1407
1408 /* set AP and INFRA bits for mute if needed */
1409 if (wlc_hw->mute_override) {
1410 maccontrol &= ~(MCTL_AP);
1411 maccontrol |= MCTL_INFRA;
1412 }
1413
Arend van Spriel16d28122011-12-08 15:06:51 -08001414 bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
1415 maccontrol);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001416}
1417
1418/* set or clear maccontrol bits */
1419void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
1420{
1421 u32 maccontrol;
1422 u32 new_maccontrol;
1423
1424 if (val & ~mask)
1425 return; /* error condition */
1426 maccontrol = wlc_hw->maccontrol;
1427 new_maccontrol = (maccontrol & ~mask) | val;
1428
1429 /* if the new maccontrol value is the same as the old, nothing to do */
1430 if (new_maccontrol == maccontrol)
1431 return;
1432
1433 /* something changed, cache the new value */
1434 wlc_hw->maccontrol = new_maccontrol;
1435
1436 /* write the new values with overrides applied */
1437 brcms_c_mctrl_write(wlc_hw);
1438}
1439
1440void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
1441 u32 override_bit)
1442{
1443 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
1444 mboolset(wlc_hw->wake_override, override_bit);
1445 return;
1446 }
1447
1448 mboolset(wlc_hw->wake_override, override_bit);
1449
1450 brcms_c_mctrl_write(wlc_hw);
1451 brcms_b_wait_for_wake(wlc_hw);
1452}
1453
1454void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
1455 u32 override_bit)
1456{
1457 mboolclr(wlc_hw->wake_override, override_bit);
1458
1459 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
1460 return;
1461
1462 brcms_c_mctrl_write(wlc_hw);
1463}
1464
1465/* When driver needs ucode to stop beaconing, it has to make sure that
1466 * MCTL_AP is clear and MCTL_INFRA is set
1467 * Mode MCTL_AP MCTL_INFRA
1468 * AP 1 1
1469 * STA 0 1 <--- This will ensure no beacons
1470 * IBSS 0 0
1471 */
1472static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
1473{
1474 wlc_hw->mute_override = 1;
1475
1476 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1477 * override, then there is no change to write
1478 */
1479 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1480 return;
1481
1482 brcms_c_mctrl_write(wlc_hw);
1483}
1484
1485/* Clear the override on AP and INFRA bits */
1486static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
1487{
1488 if (wlc_hw->mute_override == 0)
1489 return;
1490
1491 wlc_hw->mute_override = 0;
1492
1493 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1494 * override, then there is no change to write
1495 */
1496 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1497 return;
1498
1499 brcms_c_mctrl_write(wlc_hw);
1500}
1501
1502/*
1503 * Write a MAC address to the given match reg offset in the RXE match engine.
1504 */
1505static void
1506brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
1507 const u8 *addr)
1508{
Arend van Spriel16d28122011-12-08 15:06:51 -08001509 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001510 u16 mac_l;
1511 u16 mac_m;
1512 u16 mac_h;
1513
Seth Forshee5ce58bb2012-11-15 08:08:05 -06001514 brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001515
Arend van Spriel5b435de2011-10-05 13:19:03 +02001516 mac_l = addr[0] | (addr[1] << 8);
1517 mac_m = addr[2] | (addr[3] << 8);
1518 mac_h = addr[4] | (addr[5] << 8);
1519
1520 /* enter the MAC addr into the RXE match registers */
Arend van Spriel16d28122011-12-08 15:06:51 -08001521 bcma_write16(core, D11REGOFFS(rcm_ctl),
1522 RCM_INC_DATA | match_reg_offset);
1523 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
1524 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
1525 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526}
1527
1528void
1529brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
1530 void *buf)
1531{
Arend van Spriel16d28122011-12-08 15:06:51 -08001532 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533 u32 word;
1534 __le32 word_le;
1535 __be32 word_be;
1536 bool be_bit;
Seth Forsheeb353dda2012-11-15 08:08:03 -06001537 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001538
Arend van Spriel16d28122011-12-08 15:06:51 -08001539 bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001540
1541 /* if MCTL_BIGEND bit set in mac control register,
1542 * the chip swaps data in fifo, as well as data in
1543 * template ram
1544 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001545 be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001546
1547 while (len > 0) {
1548 memcpy(&word, buf, sizeof(u32));
1549
1550 if (be_bit) {
1551 word_be = cpu_to_be32(word);
1552 word = *(u32 *)&word_be;
1553 } else {
1554 word_le = cpu_to_le32(word);
1555 word = *(u32 *)&word_le;
1556 }
1557
Arend van Spriel16d28122011-12-08 15:06:51 -08001558 bcma_write32(core, D11REGOFFS(tplatewrdata), word);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001559
1560 buf = (u8 *) buf + sizeof(u32);
1561 len -= sizeof(u32);
1562 }
1563}
1564
1565static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
1566{
1567 wlc_hw->band->CWmin = newmin;
1568
Arend van Spriel16d28122011-12-08 15:06:51 -08001569 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1570 OBJADDR_SCR_SEL | S_DOT11_CWMIN);
1571 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1572 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001573}
1574
1575static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
1576{
1577 wlc_hw->band->CWmax = newmax;
1578
Arend van Spriel16d28122011-12-08 15:06:51 -08001579 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1580 OBJADDR_SCR_SEL | S_DOT11_CWMAX);
1581 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1582 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001583}
1584
1585void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
1586{
1587 bool fastclk;
1588
1589 /* request FAST clock if not on */
1590 fastclk = wlc_hw->forcefastclk;
1591 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001592 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001593
1594 wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
1595
1596 brcms_b_phy_reset(wlc_hw);
1597 wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
1598
1599 /* restore the clk */
1600 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001601 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001602}
1603
1604static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
1605{
1606 u16 v;
1607 struct brcms_c_info *wlc = wlc_hw->wlc;
1608 /* update SYNTHPU_DLY */
1609
1610 if (BRCMS_ISLCNPHY(wlc->band))
1611 v = SYNTHPU_DLY_LPPHY_US;
1612 else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
1613 v = SYNTHPU_DLY_NPHY_US;
1614 else
1615 v = SYNTHPU_DLY_BPHY_US;
1616
1617 brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
1618}
1619
1620static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
1621{
1622 u16 phyctl;
1623 u16 phytxant = wlc_hw->bmac_phytxant;
1624 u16 mask = PHY_TXC_ANT_MASK;
1625
1626 /* set the Probe Response frame phy control word */
1627 phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
1628 phyctl = (phyctl & ~mask) | phytxant;
1629 brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
1630
1631 /* set the Response (ACK/CTS) frame phy control word */
1632 phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
1633 phyctl = (phyctl & ~mask) | phytxant;
1634 brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
1635}
1636
1637static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
1638 u8 rate)
1639{
1640 uint i;
1641 u8 plcp_rate = 0;
1642 struct plcp_signal_rate_lookup {
1643 u8 rate;
1644 u8 signal_rate;
1645 };
1646 /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
1647 const struct plcp_signal_rate_lookup rate_lookup[] = {
1648 {BRCM_RATE_6M, 0xB},
1649 {BRCM_RATE_9M, 0xF},
1650 {BRCM_RATE_12M, 0xA},
1651 {BRCM_RATE_18M, 0xE},
1652 {BRCM_RATE_24M, 0x9},
1653 {BRCM_RATE_36M, 0xD},
1654 {BRCM_RATE_48M, 0x8},
1655 {BRCM_RATE_54M, 0xC}
1656 };
1657
1658 for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
1659 if (rate == rate_lookup[i].rate) {
1660 plcp_rate = rate_lookup[i].signal_rate;
1661 break;
1662 }
1663 }
1664
1665 /* Find the SHM pointer to the rate table entry by looking in the
1666 * Direct-map Table
1667 */
1668 return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
1669}
1670
1671static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
1672{
1673 u8 rate;
1674 u8 rates[8] = {
1675 BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
1676 BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
1677 };
1678 u16 entry_ptr;
1679 u16 pctl1;
1680 uint i;
1681
1682 if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
1683 return;
1684
1685 /* walk the phy rate table and update the entries */
1686 for (i = 0; i < ARRAY_SIZE(rates); i++) {
1687 rate = rates[i];
1688
1689 entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
1690
1691 /* read the SHM Rate Table entry OFDM PCTL1 values */
1692 pctl1 =
1693 brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
1694
1695 /* modify the value */
1696 pctl1 &= ~PHY_TXC1_MODE_MASK;
1697 pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
1698
1699 /* Update the SHM Rate Table entry OFDM PCTL1 values */
1700 brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
1701 pctl1);
1702 }
1703}
1704
1705/* band-specific init */
1706static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
1707{
1708 struct brcms_hardware *wlc_hw = wlc->hw;
1709
Seth Forshee913911f2012-11-15 08:08:04 -06001710 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
1711 wlc_hw->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001712
1713 brcms_c_ucode_bsinit(wlc_hw);
1714
1715 wlc_phy_init(wlc_hw->band->pi, chanspec);
1716
1717 brcms_c_ucode_txant_set(wlc_hw);
1718
1719 /*
1720 * cwmin is band-specific, update hardware
1721 * with value for current band
1722 */
1723 brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
1724 brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
1725
1726 brcms_b_update_slot_timing(wlc_hw,
1727 wlc_hw->band->bandtype == BRCM_BAND_5G ?
1728 true : wlc_hw->shortslot);
1729
1730 /* write phytype and phyvers */
1731 brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
1732 brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
1733
1734 /*
1735 * initialize the txphyctl1 rate table since
1736 * shmem is shared between bands
1737 */
1738 brcms_upd_ofdm_pctl1_table(wlc_hw);
1739
1740 brcms_b_upd_synthpu(wlc_hw);
1741}
1742
1743/* Perform a soft reset of the PHY PLL */
1744void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
1745{
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001746 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
1747 ~0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001748 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001749 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1750 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001751 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001752 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1753 0x4, 4);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001754 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001755 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1756 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001757 udelay(1);
1758}
1759
1760/* light way to turn on phy clock without reset for NPHY only
1761 * refer to brcms_b_core_phy_clk for full version
1762 */
1763void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
1764{
1765 /* support(necessary for NPHY and HYPHY) only */
1766 if (!BRCMS_ISNPHY(wlc_hw->band))
1767 return;
1768
1769 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001770 brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001771 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001772 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001773
1774}
1775
1776void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
1777{
1778 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001779 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001780 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001781 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001782}
1783
1784void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
1785{
1786 struct brcms_phy_pub *pih = wlc_hw->band->pi;
1787 u32 phy_bw_clkbits;
1788 bool phy_in_reset = false;
1789
Seth Forsheeb353dda2012-11-15 08:08:03 -06001790 brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001791
1792 if (pih == NULL)
1793 return;
1794
1795 phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
1796
1797 /* Specific reset sequence required for NPHY rev 3 and 4 */
1798 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
1799 NREV_LE(wlc_hw->band->phyrev, 4)) {
1800 /* Set the PHY bandwidth */
Arend van Spriela8779e42011-12-08 15:06:58 -08001801 brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001802
1803 udelay(1);
1804
1805 /* Perform a soft reset of the PHY PLL */
1806 brcms_b_core_phypll_reset(wlc_hw);
1807
1808 /* reset the PHY */
Arend van Spriela8779e42011-12-08 15:06:58 -08001809 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
1810 (SICF_PRST | SICF_PCLKE));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001811 phy_in_reset = true;
1812 } else {
Arend van Spriela8779e42011-12-08 15:06:58 -08001813 brcms_b_core_ioctl(wlc_hw,
1814 (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
1815 (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001816 }
1817
1818 udelay(2);
1819 brcms_b_core_phy_clk(wlc_hw, ON);
1820
1821 if (pih)
1822 wlc_phy_anacore(pih, ON);
1823}
1824
1825/* switch to and initialize new band */
1826static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
1827 u16 chanspec) {
1828 struct brcms_c_info *wlc = wlc_hw->wlc;
1829 u32 macintmask;
1830
1831 /* Enable the d11 core before accessing it */
Arend van Spriela8779e42011-12-08 15:06:58 -08001832 if (!bcma_core_is_enabled(wlc_hw->d11core)) {
1833 bcma_core_enable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001834 brcms_c_mctrl_reset(wlc_hw);
1835 }
1836
1837 macintmask = brcms_c_setband_inact(wlc, bandunit);
1838
1839 if (!wlc_hw->up)
1840 return;
1841
1842 brcms_b_core_phy_clk(wlc_hw, ON);
1843
1844 /* band-specific initializations */
1845 brcms_b_bsinit(wlc, chanspec);
1846
1847 /*
1848 * If there are any pending software interrupt bits,
1849 * then replace these with a harmless nonzero value
1850 * so brcms_c_dpc() will re-enable interrupts when done.
1851 */
1852 if (wlc->macintstatus)
1853 wlc->macintstatus = MI_DMAINT;
1854
1855 /* restore macintmask */
1856 brcms_intrsrestore(wlc->wl, macintmask);
1857
1858 /* ucode should still be suspended.. */
Arend van Spriel16d28122011-12-08 15:06:51 -08001859 WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
1860 MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001861}
1862
Arend van Spriel5b435de2011-10-05 13:19:03 +02001863static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
1864{
1865
1866 /* reject unsupported corerev */
1867 if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
1868 wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
1869 wlc_hw->corerev);
1870 return false;
1871 }
1872
1873 return true;
1874}
1875
1876/* Validate some board info parameters */
1877static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
1878{
1879 uint boardrev = wlc_hw->boardrev;
1880
1881 /* 4 bits each for board type, major, minor, and tiny version */
1882 uint brt = (boardrev & 0xf000) >> 12;
1883 uint b0 = (boardrev & 0xf00) >> 8;
1884 uint b1 = (boardrev & 0xf0) >> 4;
1885 uint b2 = boardrev & 0xf;
1886
1887 /* voards from other vendors are always considered valid */
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001888 if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001889 return true;
1890
1891 /* do some boardrev sanity checks when boardvendor is Broadcom */
1892 if (boardrev == 0)
1893 return false;
1894
1895 if (boardrev <= 0xff)
1896 return true;
1897
1898 if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
1899 || (b2 > 9))
1900 return false;
1901
1902 return true;
1903}
1904
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001905static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
Arend van Spriel5b435de2011-10-05 13:19:03 +02001906{
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001907 struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001908
1909 /* If macaddr exists, use it (Sromrev4, CIS, ...). */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001910 if (!is_zero_ether_addr(sprom->il0mac)) {
1911 memcpy(etheraddr, sprom->il0mac, 6);
1912 return;
1913 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001914
1915 if (wlc_hw->_nbands > 1)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001916 memcpy(etheraddr, sprom->et1mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001917 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001918 memcpy(etheraddr, sprom->il0mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001919}
1920
1921/* power both the pll and external oscillator on/off */
1922static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
1923{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001924 brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001925
1926 /*
1927 * dont power down if plldown is false or
1928 * we must poll hw radio disable
1929 */
1930 if (!want && wlc_hw->pllreq)
1931 return;
1932
Arend van Spriel5b435de2011-10-05 13:19:03 +02001933 wlc_hw->sbclk = want;
1934 if (!wlc_hw->sbclk) {
1935 wlc_hw->clk = false;
1936 if (wlc_hw->band && wlc_hw->band->pi)
1937 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
1938 }
1939}
1940
1941/*
1942 * Return true if radio is disabled, otherwise false.
1943 * hw radio disable signal is an external pin, users activate it asynchronously
1944 * this function could be called when driver is down and w/o clock
1945 * it operates on different registers depending on corerev and boardflag.
1946 */
1947static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
1948{
1949 bool v, clk, xtal;
Arend van Spriela8779e42011-12-08 15:06:58 -08001950 u32 flags = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001951
1952 xtal = wlc_hw->sbclk;
1953 if (!xtal)
1954 brcms_b_xtal(wlc_hw, ON);
1955
1956 /* may need to take core out of reset first */
1957 clk = wlc_hw->clk;
1958 if (!clk) {
1959 /*
1960 * mac no longer enables phyclk automatically when driver
1961 * accesses phyreg throughput mac. This can be skipped since
1962 * only mac reg is accessed below
1963 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02001964 if (D11REV_GE(wlc_hw->corerev, 18))
1965 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001966
1967 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08001968 * TODO: test suspend/resume
1969 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02001970 * AI chip doesn't restore bar0win2 on
1971 * hibernation/resume, need sw fixup
1972 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001973
Arend van Spriela8779e42011-12-08 15:06:58 -08001974 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001975 brcms_c_mctrl_reset(wlc_hw);
1976 }
1977
Arend van Spriel16d28122011-12-08 15:06:51 -08001978 v = ((bcma_read32(wlc_hw->d11core,
1979 D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001980
1981 /* put core back into reset */
1982 if (!clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001983 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001984
1985 if (!xtal)
1986 brcms_b_xtal(wlc_hw, OFF);
1987
1988 return v;
1989}
1990
1991static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
1992{
1993 struct dma_pub *di = wlc_hw->di[fifo];
1994 return dma_rxreset(di);
1995}
1996
1997/* d11 core reset
1998 * ensure fask clock during reset
1999 * reset dma
2000 * reset d11(out of reset)
2001 * reset phy(out of reset)
2002 * clear software macintstatus for fresh new start
2003 * one testing hack wlc_hw->noreset will bypass the d11/phy reset
2004 */
2005void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
2006{
Arend van Spriel5b435de2011-10-05 13:19:03 +02002007 uint i;
2008 bool fastclk;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002009
2010 if (flags == BRCMS_USE_COREFLAGS)
2011 flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
2012
Seth Forsheeb353dda2012-11-15 08:08:03 -06002013 brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002014
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015 /* request FAST clock if not on */
2016 fastclk = wlc_hw->forcefastclk;
2017 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002018 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002019
2020 /* reset the dma engines except first time thru */
Arend van Spriela8779e42011-12-08 15:06:58 -08002021 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002022 for (i = 0; i < NFIFO; i++)
2023 if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002024 brcms_err(wlc_hw->d11core, "wl%d: %s: "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002025 "dma_txreset[%d]: cannot stop dma\n",
2026 wlc_hw->unit, __func__, i);
2027
2028 if ((wlc_hw->di[RX_FIFO])
2029 && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002030 brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002031 "[%d]: cannot stop dma\n",
2032 wlc_hw->unit, __func__, RX_FIFO);
2033 }
2034 /* if noreset, just stop the psm and return */
2035 if (wlc_hw->noreset) {
2036 wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
2037 brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
2038 return;
2039 }
2040
2041 /*
2042 * mac no longer enables phyclk automatically when driver accesses
2043 * phyreg throughput mac, AND phy_reset is skipped at early stage when
2044 * band->pi is invalid. need to enable PHY CLK
2045 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02002046 if (D11REV_GE(wlc_hw->corerev, 18))
2047 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002048
2049 /*
2050 * reset the core
2051 * In chips with PMU, the fastclk request goes through d11 core
2052 * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
2053 *
2054 * This adds some delay and we can optimize it by also requesting
2055 * fastclk through chipcommon during this period if necessary. But
2056 * that has to work coordinate with other driver like mips/arm since
2057 * they may touch chipcommon as well.
2058 */
2059 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002060 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002061 wlc_hw->clk = true;
2062 if (wlc_hw->band && wlc_hw->band->pi)
2063 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
2064
2065 brcms_c_mctrl_reset(wlc_hw);
2066
Arend van Sprielb2ffec42011-12-08 15:06:45 -08002067 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002068 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002069
2070 brcms_b_phy_reset(wlc_hw);
2071
2072 /* turn on PHY_PLL */
2073 brcms_b_core_phypll_ctl(wlc_hw, true);
2074
2075 /* clear sw intstatus */
2076 wlc_hw->wlc->macintstatus = 0;
2077
2078 /* restore the clk setting */
2079 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002080 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002081}
2082
2083/* txfifo sizes needs to be modified(increased) since the newer cores
2084 * have more memory.
2085 */
2086static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
2087{
Arend van Spriel16d28122011-12-08 15:06:51 -08002088 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002089 u16 fifo_nu;
2090 u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
2091 u16 txfifo_def, txfifo_def1;
2092 u16 txfifo_cmd;
2093
2094 /* tx fifos start at TXFIFO_START_BLK from the Base address */
2095 txfifo_startblk = TXFIFO_START_BLK;
2096
2097 /* sequence of operations: reset fifo, set fifo size, reset fifo */
2098 for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
2099
2100 txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
2101 txfifo_def = (txfifo_startblk & 0xff) |
2102 (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
2103 txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
2104 ((((txfifo_endblk -
2105 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
2106 txfifo_cmd =
2107 TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
2108
Arend van Spriel16d28122011-12-08 15:06:51 -08002109 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
2110 bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
2111 bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002112
Arend van Spriel16d28122011-12-08 15:06:51 -08002113 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002114
2115 txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
2116 }
2117 /*
2118 * need to propagate to shm location to be in sync since ucode/hw won't
2119 * do this
2120 */
2121 brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
2122 wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
2123 brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
2124 wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
2125 brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
2126 ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
2127 xmtfifo_sz[TX_AC_BK_FIFO]));
2128 brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
2129 ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
2130 xmtfifo_sz[TX_BCMC_FIFO]));
2131}
2132
2133/* This function is used for changing the tsf frac register
2134 * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
2135 * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
2136 * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
2137 * HTPHY Formula is 2^26/freq(MHz) e.g.
2138 * For spuron2 - 126MHz -> 2^26/126 = 532610.0
2139 * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
2140 * For spuron: 123MHz -> 2^26/123 = 545600.5
2141 * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
2142 * For spur off: 120MHz -> 2^26/120 = 559240.5
2143 * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
2144 */
2145
2146void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
2147{
Arend van Spriel16d28122011-12-08 15:06:51 -08002148 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002149
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002150 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
2151 (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002152 if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002153 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
2154 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002155 } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002156 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
2157 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002158 } else { /* 120Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002159 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
2160 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002161 }
2162 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2163 if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002164 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
2165 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002166 } else { /* 80Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002167 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
2168 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002169 }
2170 }
2171}
2172
2173/* Initialize GPIOs that are controlled by D11 core */
2174static void brcms_c_gpio_init(struct brcms_c_info *wlc)
2175{
2176 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002177 u32 gc, gm;
2178
Arend van Spriel5b435de2011-10-05 13:19:03 +02002179 /* use GPIO select 0 to get all gpio signals from the gpio out reg */
2180 brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
2181
2182 /*
2183 * Common GPIO setup:
2184 * G0 = LED 0 = WLAN Activity
2185 * G1 = LED 1 = WLAN 2.4 GHz Radio State
2186 * G2 = LED 2 = WLAN 5 GHz Radio State
2187 * G4 = radio disable input (HI enabled, LO disabled)
2188 */
2189
2190 gc = gm = 0;
2191
2192 /* Allocate GPIOs for mimo antenna diversity feature */
2193 if (wlc_hw->antsel_type == ANTSEL_2x3) {
2194 /* Enable antenna diversity, use 2x3 mode */
2195 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2196 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2197 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
2198 MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
2199
2200 /* init superswitch control */
2201 wlc_phy_antsel_init(wlc_hw->band->pi, false);
2202
2203 } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
2204 gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
2205 /*
2206 * The board itself is powered by these GPIOs
2207 * (when not sending pattern) so set them high
2208 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002209 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
2210 (BOARD_GPIO_12 | BOARD_GPIO_13));
2211 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
2212 (BOARD_GPIO_12 | BOARD_GPIO_13));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002213
2214 /* Enable antenna diversity, use 2x4 mode */
2215 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2216 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2217 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
2218 BRCM_BAND_ALL);
2219
2220 /* Configure the desired clock to be 4Mhz */
2221 brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
2222 ANTSEL_CLKDIV_4MHZ);
2223 }
2224
2225 /*
2226 * gpio 9 controls the PA. ucode is responsible
2227 * for wiggling out and oe
2228 */
2229 if (wlc_hw->boardflags & BFL_PACTRL)
2230 gm |= gc |= BOARD_GPIO_PACTRL;
2231
2232 /* apply to gpiocontrol register */
Hauke Mehrtensfa0b8232012-04-29 02:50:34 +02002233 bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002234}
2235
2236static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
2237 const __le32 ucode[], const size_t nbytes)
2238{
Arend van Spriel16d28122011-12-08 15:06:51 -08002239 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002240 uint i;
2241 uint count;
2242
Seth Forsheeb353dda2012-11-15 08:08:03 -06002243 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002244
2245 count = (nbytes / sizeof(u32));
2246
Arend van Spriel16d28122011-12-08 15:06:51 -08002247 bcma_write32(core, D11REGOFFS(objaddr),
2248 OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
2249 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002250 for (i = 0; i < count; i++)
Arend van Spriel16d28122011-12-08 15:06:51 -08002251 bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002252
2253}
2254
2255static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
2256{
2257 struct brcms_c_info *wlc;
2258 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
2259
2260 wlc = wlc_hw->wlc;
2261
2262 if (wlc_hw->ucode_loaded)
2263 return;
2264
Hauke Mehrtens6f80f012012-12-07 00:35:53 +01002265 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002266 if (BRCMS_ISNPHY(wlc_hw->band)) {
2267 brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
2268 ucode->bcm43xx_16_mimosz);
2269 wlc_hw->ucode_loaded = true;
2270 } else
Seth Forsheeb353dda2012-11-15 08:08:03 -06002271 brcms_err(wlc_hw->d11core,
2272 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002273 __func__, wlc_hw->unit, wlc_hw->corerev);
2274 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
2275 if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2276 brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
2277 ucode->bcm43xx_24_lcnsz);
2278 wlc_hw->ucode_loaded = true;
2279 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002280 brcms_err(wlc_hw->d11core,
2281 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002282 __func__, wlc_hw->unit, wlc_hw->corerev);
2283 }
2284 }
2285}
2286
2287void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
2288{
2289 /* update sw state */
2290 wlc_hw->bmac_phytxant = phytxant;
2291
2292 /* push to ucode if up */
2293 if (!wlc_hw->up)
2294 return;
2295 brcms_c_ucode_txant_set(wlc_hw);
2296
2297}
2298
2299u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
2300{
2301 return (u16) wlc_hw->wlc->stf->txant;
2302}
2303
2304void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
2305{
2306 wlc_hw->antsel_type = antsel_type;
2307
2308 /* Update the antsel type for phy module to use */
2309 wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
2310}
2311
2312static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
2313{
2314 bool fatal = false;
2315 uint unit;
2316 uint intstatus, idx;
Arend van Spriel16d28122011-12-08 15:06:51 -08002317 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002318
2319 unit = wlc_hw->unit;
2320
2321 for (idx = 0; idx < NFIFO; idx++) {
2322 /* read intstatus register and ignore any non-error bits */
2323 intstatus =
Arend van Spriel16d28122011-12-08 15:06:51 -08002324 bcma_read32(core,
2325 D11REGOFFS(intctrlregs[idx].intstatus)) &
2326 I_ERRORS;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002327 if (!intstatus)
2328 continue;
2329
Seth Forshee229a41d2012-11-15 08:08:06 -06002330 brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
2331 unit, idx, intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002332
2333 if (intstatus & I_RO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002334 brcms_err(core, "wl%d: fifo %d: receive fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002335 "overflow\n", unit, idx);
2336 fatal = true;
2337 }
2338
2339 if (intstatus & I_PC) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002340 brcms_err(core, "wl%d: fifo %d: descriptor error\n",
2341 unit, idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002342 fatal = true;
2343 }
2344
2345 if (intstatus & I_PD) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002346 brcms_err(core, "wl%d: fifo %d: data error\n", unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002347 idx);
2348 fatal = true;
2349 }
2350
2351 if (intstatus & I_DE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002352 brcms_err(core, "wl%d: fifo %d: descriptor protocol "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002353 "error\n", unit, idx);
2354 fatal = true;
2355 }
2356
2357 if (intstatus & I_RU)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002358 brcms_err(core, "wl%d: fifo %d: receive descriptor "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002359 "underflow\n", idx, unit);
2360
2361 if (intstatus & I_XU) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002362 brcms_err(core, "wl%d: fifo %d: transmit fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002363 "underflow\n", idx, unit);
2364 fatal = true;
2365 }
2366
2367 if (fatal) {
Roland Vossenc261bdf2011-10-18 14:03:04 +02002368 brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002369 break;
2370 } else
Arend van Spriel16d28122011-12-08 15:06:51 -08002371 bcma_write32(core,
2372 D11REGOFFS(intctrlregs[idx].intstatus),
2373 intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002374 }
2375}
2376
2377void brcms_c_intrson(struct brcms_c_info *wlc)
2378{
2379 struct brcms_hardware *wlc_hw = wlc->hw;
2380 wlc->macintmask = wlc->defmacintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002381 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002382}
2383
Arend van Spriel5b435de2011-10-05 13:19:03 +02002384u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
2385{
2386 struct brcms_hardware *wlc_hw = wlc->hw;
2387 u32 macintmask;
2388
2389 if (!wlc_hw->clk)
2390 return 0;
2391
2392 macintmask = wlc->macintmask; /* isr can still happen */
2393
Arend van Spriel16d28122011-12-08 15:06:51 -08002394 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
2395 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002396 udelay(1); /* ensure int line is no longer driven */
2397 wlc->macintmask = 0;
2398
2399 /* return previous macintmask; resolve race between us and our isr */
2400 return wlc->macintstatus ? 0 : macintmask;
2401}
2402
2403void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2404{
2405 struct brcms_hardware *wlc_hw = wlc->hw;
2406 if (!wlc_hw->clk)
2407 return;
2408
2409 wlc->macintmask = macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002410 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002411}
2412
Roland Vossendc460122011-10-21 16:16:28 +02002413/* assumes that the d11 MAC is enabled */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002414static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
2415 uint tx_fifo)
2416{
2417 u8 fifo = 1 << tx_fifo;
2418
2419 /* Two clients of this code, 11h Quiet period and scanning. */
2420
2421 /* only suspend if not already suspended */
2422 if ((wlc_hw->suspended_fifos & fifo) == fifo)
2423 return;
2424
2425 /* force the core awake only if not already */
2426 if (wlc_hw->suspended_fifos == 0)
2427 brcms_c_ucode_wake_override_set(wlc_hw,
2428 BRCMS_WAKE_OVERRIDE_TXFIFO);
2429
2430 wlc_hw->suspended_fifos |= fifo;
2431
2432 if (wlc_hw->di[tx_fifo]) {
2433 /*
2434 * Suspending AMPDU transmissions in the middle can cause
2435 * underflow which may result in mismatch between ucode and
2436 * driver so suspend the mac before suspending the FIFO
2437 */
2438 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2439 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
2440
2441 dma_txsuspend(wlc_hw->di[tx_fifo]);
2442
2443 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2444 brcms_c_enable_mac(wlc_hw->wlc);
2445 }
2446}
2447
2448static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
2449 uint tx_fifo)
2450{
2451 /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
2452 * but need to be done here for PIO otherwise the watchdog will catch
2453 * the inconsistency and fire
2454 */
2455 /* Two clients of this code, 11h Quiet period and scanning. */
2456 if (wlc_hw->di[tx_fifo])
2457 dma_txresume(wlc_hw->di[tx_fifo]);
2458
2459 /* allow core to sleep again */
2460 if (wlc_hw->suspended_fifos == 0)
2461 return;
2462 else {
2463 wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
2464 if (wlc_hw->suspended_fifos == 0)
2465 brcms_c_ucode_wake_override_clear(wlc_hw,
2466 BRCMS_WAKE_OVERRIDE_TXFIFO);
2467 }
2468}
2469
Roland Vossena8bc4912011-10-21 16:16:25 +02002470/* precondition: requires the mac core to be enabled */
Roland Vossenc6c44892011-10-21 16:16:26 +02002471static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002472{
2473 static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Arend van Sprielb180b102013-01-02 15:22:36 +01002474 u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002475
Roland Vossenc6c44892011-10-21 16:16:26 +02002476 if (mute_tx) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002477 /* suspend tx fifos */
2478 brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
2479 brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
2480 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
2481 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
2482
2483 /* zero the address match register so we do not send ACKs */
Arend van Sprielb180b102013-01-02 15:22:36 +01002484 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002485 } else {
2486 /* resume tx fifos */
2487 brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
2488 brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
2489 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
2490 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
2491
2492 /* Restore address */
Arend van Sprielb180b102013-01-02 15:22:36 +01002493 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002494 }
2495
Roland Vossenc6c44892011-10-21 16:16:26 +02002496 wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002497
Roland Vossenc6c44892011-10-21 16:16:26 +02002498 if (mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002499 brcms_c_ucode_mute_override_set(wlc_hw);
2500 else
2501 brcms_c_ucode_mute_override_clear(wlc_hw);
2502}
2503
Roland Vossendc460122011-10-21 16:16:28 +02002504void
2505brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
2506{
2507 brcms_b_mute(wlc->hw, mute_tx);
2508}
2509
Arend van Spriel5b435de2011-10-05 13:19:03 +02002510/*
2511 * Read and clear macintmask and macintstatus and intstatus registers.
2512 * This routine should be called with interrupts off
2513 * Return:
2514 * -1 if brcms_deviceremoved(wlc) evaluates to true;
2515 * 0 if the interrupt is not for us, or we are in some special cases;
2516 * device interrupt status bits otherwise.
2517 */
2518static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
2519{
2520 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002521 struct bcma_device *core = wlc_hw->d11core;
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002522 u32 macintstatus, mask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002523
2524 /* macintstatus includes a DMA interrupt summary bit */
Arend van Spriel16d28122011-12-08 15:06:51 -08002525 macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002526 mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002527
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002528 trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002529
2530 /* detect cardbus removed, in power down(suspend) and in reset */
2531 if (brcms_deviceremoved(wlc))
2532 return -1;
2533
2534 /* brcms_deviceremoved() succeeds even when the core is still resetting,
2535 * handle that case here.
2536 */
2537 if (macintstatus == 0xffffffff)
2538 return 0;
2539
2540 /* defer unsolicited interrupts */
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002541 macintstatus &= mask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002542
2543 /* if not for us */
2544 if (macintstatus == 0)
2545 return 0;
2546
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547 /* turn off the interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002548 bcma_write32(core, D11REGOFFS(macintmask), 0);
2549 (void)bcma_read32(core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002550 wlc->macintmask = 0;
2551
2552 /* clear device interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002553 bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002554
2555 /* MI_DMAINT is indication of non-zero intstatus */
2556 if (macintstatus & MI_DMAINT)
2557 /*
2558 * only fifo interrupt enabled is I_RI in
2559 * RX_FIFO. If MI_DMAINT is set, assume it
2560 * is set and clear the interrupt.
2561 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002562 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
2563 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002564
2565 return macintstatus;
2566}
2567
2568/* Update wlc->macintstatus and wlc->intstatus[]. */
2569/* Return true if they are updated successfully. false otherwise */
2570bool brcms_c_intrsupd(struct brcms_c_info *wlc)
2571{
2572 u32 macintstatus;
2573
2574 /* read and clear macintstatus and intstatus registers */
2575 macintstatus = wlc_intstatus(wlc, false);
2576
2577 /* device is removed */
2578 if (macintstatus == 0xffffffff)
2579 return false;
2580
2581 /* update interrupt status in software */
2582 wlc->macintstatus |= macintstatus;
2583
2584 return true;
2585}
2586
2587/*
2588 * First-level interrupt processing.
Piotr Haber94d99022012-11-28 21:44:06 +01002589 * Return true if this was our interrupt
2590 * and if further brcms_c_dpc() processing is required,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002591 * false otherwise.
2592 */
Piotr Haber94d99022012-11-28 21:44:06 +01002593bool brcms_c_isr(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002594{
2595 struct brcms_hardware *wlc_hw = wlc->hw;
2596 u32 macintstatus;
2597
Arend van Spriel5b435de2011-10-05 13:19:03 +02002598 if (!wlc_hw->up || !wlc->macintmask)
2599 return false;
2600
2601 /* read and clear macintstatus and intstatus registers */
2602 macintstatus = wlc_intstatus(wlc, true);
2603
Piotr Haber94d99022012-11-28 21:44:06 +01002604 if (macintstatus == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002605 brcms_err(wlc_hw->d11core,
2606 "DEVICEREMOVED detected in the ISR code path\n");
Piotr Haber94d99022012-11-28 21:44:06 +01002607 return false;
2608 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002609
2610 /* it is not for us */
2611 if (macintstatus == 0)
2612 return false;
2613
Arend van Spriel5b435de2011-10-05 13:19:03 +02002614 /* save interrupt status bits */
2615 wlc->macintstatus = macintstatus;
2616
2617 return true;
2618
2619}
2620
2621void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
2622{
2623 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002624 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002625 u32 mc, mi;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002626
Seth Forshee913911f2012-11-15 08:08:04 -06002627 brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2628 wlc_hw->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002629
2630 /*
2631 * Track overlapping suspend requests
2632 */
2633 wlc_hw->mac_suspend_depth++;
2634 if (wlc_hw->mac_suspend_depth > 1)
2635 return;
2636
2637 /* force the core awake */
2638 brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2639
Arend van Spriel16d28122011-12-08 15:06:51 -08002640 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002641
2642 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002643 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 __func__);
2645 brcms_down(wlc->wl);
2646 return;
2647 }
2648 WARN_ON(mc & MCTL_PSM_JMP_0);
2649 WARN_ON(!(mc & MCTL_PSM_RUN));
2650 WARN_ON(!(mc & MCTL_EN_MAC));
2651
Arend van Spriel16d28122011-12-08 15:06:51 -08002652 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002653 if (mi == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002654 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002655 __func__);
2656 brcms_down(wlc->wl);
2657 return;
2658 }
2659 WARN_ON(mi & MI_MACSSPNDD);
2660
2661 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
2662
Arend van Spriel16d28122011-12-08 15:06:51 -08002663 SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
Arend van Spriel5b435de2011-10-05 13:19:03 +02002664 BRCMS_MAX_MAC_SUSPEND);
2665
Arend van Spriel16d28122011-12-08 15:06:51 -08002666 if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002667 brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002668 " and MI_MACSSPNDD is still not on.\n",
2669 wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
Seth Forsheeb353dda2012-11-15 08:08:03 -06002670 brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002671 "psm_brc 0x%04x\n", wlc_hw->unit,
Arend van Spriel16d28122011-12-08 15:06:51 -08002672 bcma_read32(core, D11REGOFFS(psmdebug)),
2673 bcma_read32(core, D11REGOFFS(phydebug)),
2674 bcma_read16(core, D11REGOFFS(psm_brc)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002675 }
2676
Arend van Spriel16d28122011-12-08 15:06:51 -08002677 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002678 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002679 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002680 __func__);
2681 brcms_down(wlc->wl);
2682 return;
2683 }
2684 WARN_ON(mc & MCTL_PSM_JMP_0);
2685 WARN_ON(!(mc & MCTL_PSM_RUN));
2686 WARN_ON(mc & MCTL_EN_MAC);
2687}
2688
2689void brcms_c_enable_mac(struct brcms_c_info *wlc)
2690{
2691 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002692 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002693 u32 mc, mi;
2694
Seth Forshee913911f2012-11-15 08:08:04 -06002695 brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2696 wlc->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002697
2698 /*
2699 * Track overlapping suspend requests
2700 */
2701 wlc_hw->mac_suspend_depth--;
2702 if (wlc_hw->mac_suspend_depth > 0)
2703 return;
2704
Arend van Spriel16d28122011-12-08 15:06:51 -08002705 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002706 WARN_ON(mc & MCTL_PSM_JMP_0);
2707 WARN_ON(mc & MCTL_EN_MAC);
2708 WARN_ON(!(mc & MCTL_PSM_RUN));
2709
2710 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
Arend van Spriel16d28122011-12-08 15:06:51 -08002711 bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002712
Arend van Spriel16d28122011-12-08 15:06:51 -08002713 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002714 WARN_ON(mc & MCTL_PSM_JMP_0);
2715 WARN_ON(!(mc & MCTL_EN_MAC));
2716 WARN_ON(!(mc & MCTL_PSM_RUN));
2717
Arend van Spriel16d28122011-12-08 15:06:51 -08002718 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002719 WARN_ON(mi & MI_MACSSPNDD);
2720
2721 brcms_c_ucode_wake_override_clear(wlc_hw,
2722 BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2723}
2724
2725void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
2726{
2727 wlc_hw->hw_stf_ss_opmode = stf_mode;
2728
2729 if (wlc_hw->clk)
2730 brcms_upd_ofdm_pctl1_table(wlc_hw);
2731}
2732
2733static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
2734{
Arend van Spriel16d28122011-12-08 15:06:51 -08002735 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002736 u32 w, val;
2737 struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2738
Arend van Spriel5b435de2011-10-05 13:19:03 +02002739 /* Validate dchip register access */
2740
Arend van Spriel16d28122011-12-08 15:06:51 -08002741 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2742 (void)bcma_read32(core, D11REGOFFS(objaddr));
2743 w = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002744
2745 /* Can we write and read back a 32bit register? */
Arend van Spriel16d28122011-12-08 15:06:51 -08002746 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2747 (void)bcma_read32(core, D11REGOFFS(objaddr));
2748 bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749
Arend van Spriel16d28122011-12-08 15:06:51 -08002750 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2751 (void)bcma_read32(core, D11REGOFFS(objaddr));
2752 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002753 if (val != (u32) 0xaa5555aa) {
2754 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2755 "expected 0xaa5555aa\n", wlc_hw->unit, val);
2756 return false;
2757 }
2758
Arend van Spriel16d28122011-12-08 15:06:51 -08002759 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2760 (void)bcma_read32(core, D11REGOFFS(objaddr));
2761 bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002762
Arend van Spriel16d28122011-12-08 15:06:51 -08002763 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2764 (void)bcma_read32(core, D11REGOFFS(objaddr));
2765 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002766 if (val != (u32) 0x55aaaa55) {
2767 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2768 "expected 0x55aaaa55\n", wlc_hw->unit, val);
2769 return false;
2770 }
2771
Arend van Spriel16d28122011-12-08 15:06:51 -08002772 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2773 (void)bcma_read32(core, D11REGOFFS(objaddr));
2774 bcma_write32(core, D11REGOFFS(objdata), w);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002775
2776 /* clear CFPStart */
Arend van Spriel16d28122011-12-08 15:06:51 -08002777 bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002778
Arend van Spriel16d28122011-12-08 15:06:51 -08002779 w = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002780 if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
2781 (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
2782 wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
2783 "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
2784 (MCTL_IHR_EN | MCTL_WAKE),
2785 (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
2786 return false;
2787 }
2788
2789 return true;
2790}
2791
2792#define PHYPLL_WAIT_US 100000
2793
2794void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
2795{
Arend van Spriel16d28122011-12-08 15:06:51 -08002796 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002797 u32 tmp;
2798
Seth Forsheeb353dda2012-11-15 08:08:03 -06002799 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002800
2801 tmp = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002802
2803 if (on) {
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002804 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08002805 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2806 CCS_ERSRC_REQ_HT |
2807 CCS_ERSRC_REQ_D11PLL |
2808 CCS_ERSRC_REQ_PHYPLL);
2809 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
2810 CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002811 PHYPLL_WAIT_US);
2812
Arend van Spriel16d28122011-12-08 15:06:51 -08002813 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
2814 if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002815 brcms_err(core, "%s: turn on PHY PLL failed\n",
2816 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002817 } else {
Arend van Spriel16d28122011-12-08 15:06:51 -08002818 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2819 tmp | CCS_ERSRC_REQ_D11PLL |
2820 CCS_ERSRC_REQ_PHYPLL);
2821 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02002822 (CCS_ERSRC_AVAIL_D11PLL |
2823 CCS_ERSRC_AVAIL_PHYPLL)) !=
2824 (CCS_ERSRC_AVAIL_D11PLL |
2825 CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
2826
Arend van Spriel16d28122011-12-08 15:06:51 -08002827 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002828 if ((tmp &
2829 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2830 !=
2831 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002832 brcms_err(core, "%s: turn on PHY PLL failed\n",
2833 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002834 }
2835 } else {
2836 /*
2837 * Since the PLL may be shared, other cores can still
2838 * be requesting it; so we'll deassert the request but
2839 * not wait for status to comply.
2840 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002841 bcma_mask32(core, D11REGOFFS(clk_ctl_st),
2842 ~CCS_ERSRC_REQ_PHYPLL);
2843 (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002844 }
2845}
2846
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02002847static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002848{
2849 bool dev_gone;
2850
Seth Forsheeb353dda2012-11-15 08:08:03 -06002851 brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002852
2853 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
2854
2855 if (dev_gone)
2856 return;
2857
2858 if (wlc_hw->noreset)
2859 return;
2860
2861 /* radio off */
2862 wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
2863
2864 /* turn off analog core */
2865 wlc_phy_anacore(wlc_hw->band->pi, OFF);
2866
2867 /* turn off PHYPLL to save power */
2868 brcms_b_core_phypll_ctl(wlc_hw, false);
2869
2870 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002871 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002872 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
2873}
2874
2875static void brcms_c_flushqueues(struct brcms_c_info *wlc)
2876{
2877 struct brcms_hardware *wlc_hw = wlc->hw;
2878 uint i;
2879
2880 /* free any posted tx packets */
Seth Forsheee041f652012-11-15 08:07:56 -06002881 for (i = 0; i < NFIFO; i++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002882 if (wlc_hw->di[i]) {
2883 dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
Seth Forsheee041f652012-11-15 08:07:56 -06002884 if (i < TX_BCMC_FIFO)
2885 ieee80211_wake_queue(wlc->pub->ieee_hw,
2886 brcms_fifo_to_ac(i));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002887 }
Seth Forsheee041f652012-11-15 08:07:56 -06002888 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002889
2890 /* free any posted rx packets */
2891 dma_rxreclaim(wlc_hw->di[RX_FIFO]);
2892}
2893
2894static u16
2895brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
2896{
Arend van Spriel16d28122011-12-08 15:06:51 -08002897 struct bcma_device *core = wlc_hw->d11core;
2898 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002899
Arend van Spriel16d28122011-12-08 15:06:51 -08002900 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2901 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002902 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002903 objoff += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002904
Arend van Spriel16d28122011-12-08 15:06:51 -08002905 return bcma_read16(core, objoff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002906}
2907
2908static void
2909brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
2910 u32 sel)
2911{
Arend van Spriel16d28122011-12-08 15:06:51 -08002912 struct bcma_device *core = wlc_hw->d11core;
2913 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002914
Arend van Spriel16d28122011-12-08 15:06:51 -08002915 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2916 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002917 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002918 objoff += 2;
2919
Hauke Mehrtens512ae052012-12-07 17:04:13 +01002920 bcma_wflush16(core, objoff, v);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002921}
2922
2923/*
2924 * Read a single u16 from shared memory.
2925 * SHM 'offset' needs to be an even address
2926 */
2927u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
2928{
2929 return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
2930}
2931
2932/*
2933 * Write a single u16 to shared memory.
2934 * SHM 'offset' needs to be an even address
2935 */
2936void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
2937{
2938 brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
2939}
2940
2941/*
2942 * Copy a buffer to shared memory of specified type .
2943 * SHM 'offset' needs to be an even address and
2944 * Buffer length 'len' must be an even number of bytes
2945 * 'sel' selects the type of memory
2946 */
2947void
2948brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
2949 const void *buf, int len, u32 sel)
2950{
2951 u16 v;
2952 const u8 *p = (const u8 *)buf;
2953 int i;
2954
2955 if (len <= 0 || (offset & 1) || (len & 1))
2956 return;
2957
2958 for (i = 0; i < len; i += 2) {
2959 v = p[i] | (p[i + 1] << 8);
2960 brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
2961 }
2962}
2963
2964/*
2965 * Copy a piece of shared memory of specified type to a buffer .
2966 * SHM 'offset' needs to be an even address and
2967 * Buffer length 'len' must be an even number of bytes
2968 * 'sel' selects the type of memory
2969 */
2970void
2971brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
2972 int len, u32 sel)
2973{
2974 u16 v;
2975 u8 *p = (u8 *) buf;
2976 int i;
2977
2978 if (len <= 0 || (offset & 1) || (len & 1))
2979 return;
2980
2981 for (i = 0; i < len; i += 2) {
2982 v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
2983 p[i] = v & 0xFF;
2984 p[i + 1] = (v >> 8) & 0xFF;
2985 }
2986}
2987
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02002988/* Copy a buffer to shared memory.
2989 * SHM 'offset' needs to be an even address and
2990 * Buffer length 'len' must be an even number of bytes
2991 */
2992static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
2993 const void *buf, int len)
2994{
2995 brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
2996}
2997
Arend van Spriel5b435de2011-10-05 13:19:03 +02002998static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
2999 u16 SRL, u16 LRL)
3000{
3001 wlc_hw->SRL = SRL;
3002 wlc_hw->LRL = LRL;
3003
3004 /* write retry limit to SCR, shouldn't need to suspend */
3005 if (wlc_hw->up) {
Arend van Spriel16d28122011-12-08 15:06:51 -08003006 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3007 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3008 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3009 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
3010 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3011 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3012 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3013 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003014 }
3015}
3016
3017static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
3018{
3019 if (set) {
3020 if (mboolisset(wlc_hw->pllreq, req_bit))
3021 return;
3022
3023 mboolset(wlc_hw->pllreq, req_bit);
3024
3025 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3026 if (!wlc_hw->sbclk)
3027 brcms_b_xtal(wlc_hw, ON);
3028 }
3029 } else {
3030 if (!mboolisset(wlc_hw->pllreq, req_bit))
3031 return;
3032
3033 mboolclr(wlc_hw->pllreq, req_bit);
3034
3035 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3036 if (wlc_hw->sbclk)
3037 brcms_b_xtal(wlc_hw, OFF);
3038 }
3039 }
3040}
3041
3042static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
3043{
3044 wlc_hw->antsel_avail = antsel_avail;
3045}
3046
3047/*
3048 * conditions under which the PM bit should be set in outgoing frames
3049 * and STAY_AWAKE is meaningful
3050 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003051static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003052{
3053 struct brcms_bss_cfg *cfg = wlc->bsscfg;
3054
3055 /* disallow PS when one of the following global conditions meets */
3056 if (!wlc->pub->associated)
3057 return false;
3058
3059 /* disallow PS when one of these meets when not scanning */
Alwin Beukersbe667662011-11-22 17:21:43 -08003060 if (wlc->filter_flags & FIF_PROMISC_IN_BSS)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003061 return false;
3062
3063 if (cfg->associated) {
3064 /*
3065 * disallow PS when one of the following
3066 * bsscfg specific conditions meets
3067 */
3068 if (!cfg->BSS)
3069 return false;
3070
3071 return false;
3072 }
3073
3074 return true;
3075}
3076
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003077static void brcms_c_statsupd(struct brcms_c_info *wlc)
3078{
3079 int i;
3080 struct macstat macstats;
Joe Perches8ae74652012-01-15 00:38:38 -08003081#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003082 u16 delta;
3083 u16 rxf0ovfl;
3084 u16 txfunfl[NFIFO];
Joe Perches8ae74652012-01-15 00:38:38 -08003085#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003086
3087 /* if driver down, make no sense to update stats */
3088 if (!wlc->pub->up)
3089 return;
3090
Joe Perches8ae74652012-01-15 00:38:38 -08003091#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003092 /* save last rx fifo 0 overflow count */
3093 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
3094
3095 /* save last tx fifo underflow count */
3096 for (i = 0; i < NFIFO; i++)
3097 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
Joe Perches8ae74652012-01-15 00:38:38 -08003098#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003099
3100 /* Read mac stats from contiguous shared memory */
3101 brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, &macstats,
3102 sizeof(struct macstat), OBJADDR_SHM_SEL);
3103
Joe Perches8ae74652012-01-15 00:38:38 -08003104#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003105 /* check for rx fifo 0 overflow */
3106 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
3107 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003108 brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003109 wlc->pub->unit, delta);
3110
3111 /* check for tx fifo underflows */
3112 for (i = 0; i < NFIFO; i++) {
3113 delta =
3114 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
3115 txfunfl[i]);
3116 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003117 brcms_err(wlc->hw->d11core,
3118 "wl%d: %u tx fifo %d underflows!\n",
3119 wlc->pub->unit, delta, i);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003120 }
Joe Perches8ae74652012-01-15 00:38:38 -08003121#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003122
3123 /* merge counters from dma module */
3124 for (i = 0; i < NFIFO; i++) {
3125 if (wlc->hw->di[i])
3126 dma_counterreset(wlc->hw->di[i]);
3127 }
3128}
3129
Arend van Spriel5b435de2011-10-05 13:19:03 +02003130static void brcms_b_reset(struct brcms_hardware *wlc_hw)
3131{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003132 /* reset the core */
3133 if (!brcms_deviceremoved(wlc_hw->wlc))
3134 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
3135
3136 /* purge the dma rings */
3137 brcms_c_flushqueues(wlc_hw->wlc);
3138}
3139
3140void brcms_c_reset(struct brcms_c_info *wlc)
3141{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003142 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003143
3144 /* slurp up hw mac counters before core reset */
3145 brcms_c_statsupd(wlc);
3146
3147 /* reset our snapshot of macstat counters */
3148 memset((char *)wlc->core->macstat_snapshot, 0,
3149 sizeof(struct macstat));
3150
3151 brcms_b_reset(wlc->hw);
3152}
3153
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154void brcms_c_init_scb(struct scb *scb)
3155{
3156 int i;
3157
3158 memset(scb, 0, sizeof(struct scb));
3159 scb->flags = SCB_WMECAP | SCB_HTCAP;
3160 for (i = 0; i < NUMPRIO; i++) {
3161 scb->seqnum[i] = 0;
3162 scb->seqctl[i] = 0xFFFF;
3163 }
3164
3165 scb->seqctl_nonqos = 0xFFFF;
3166 scb->magic = SCB_MAGIC;
3167}
3168
3169/* d11 core init
3170 * reset PSM
3171 * download ucode/PCM
3172 * let ucode run to suspended
3173 * download ucode inits
3174 * config other core registers
3175 * init dma
3176 */
3177static void brcms_b_coreinit(struct brcms_c_info *wlc)
3178{
3179 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08003180 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003181 u32 sflags;
Arend van Spriel16d28122011-12-08 15:06:51 -08003182 u32 bcnint_us;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003183 uint i = 0;
3184 bool fifosz_fixup = false;
3185 int err = 0;
3186 u16 buf[NFIFO];
Arend van Spriel5b435de2011-10-05 13:19:03 +02003187 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
3188
Seth Forsheeb353dda2012-11-15 08:08:03 -06003189 brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003190
3191 /* reset PSM */
3192 brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
3193
3194 brcms_ucode_download(wlc_hw);
3195 /*
3196 * FIFOSZ fixup. driver wants to controls the fifo allocation.
3197 */
3198 fifosz_fixup = true;
3199
3200 /* let the PSM run to the suspended state, set mode to BSS STA */
Arend van Spriel16d28122011-12-08 15:06:51 -08003201 bcma_write32(core, D11REGOFFS(macintstatus), -1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003202 brcms_b_mctrl(wlc_hw, ~0,
3203 (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
3204
3205 /* wait for ucode to self-suspend after auto-init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003206 SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
3207 MI_MACSSPNDD) == 0), 1000 * 1000);
3208 if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003209 brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003210 "suspend!\n", wlc_hw->unit);
3211
3212 brcms_c_gpio_init(wlc);
3213
Arend van Spriela8779e42011-12-08 15:06:58 -08003214 sflags = bcma_aread32(core, BCMA_IOST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003215
Hauke Mehrtens6f80f012012-12-07 00:35:53 +01003216 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003217 if (BRCMS_ISNPHY(wlc_hw->band))
3218 brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
3219 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003220 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003221 " %d\n", __func__, wlc_hw->unit,
3222 wlc_hw->corerev);
3223 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
3224 if (BRCMS_ISLCNPHY(wlc_hw->band))
3225 brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
3226 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003227 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003228 " %d\n", __func__, wlc_hw->unit,
3229 wlc_hw->corerev);
3230 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003231 brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003232 __func__, wlc_hw->unit, wlc_hw->corerev);
3233 }
3234
3235 /* For old ucode, txfifo sizes needs to be modified(increased) */
Joe Perches23677ce2012-02-09 11:17:23 +00003236 if (fifosz_fixup)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003237 brcms_b_corerev_fifofixup(wlc_hw);
3238
3239 /* check txfifo allocations match between ucode and driver */
3240 buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
3241 if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
3242 i = TX_AC_BE_FIFO;
3243 err = -1;
3244 }
3245 buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
3246 if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
3247 i = TX_AC_VI_FIFO;
3248 err = -1;
3249 }
3250 buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
3251 buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
3252 buf[TX_AC_BK_FIFO] &= 0xff;
3253 if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
3254 i = TX_AC_BK_FIFO;
3255 err = -1;
3256 }
3257 if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
3258 i = TX_AC_VO_FIFO;
3259 err = -1;
3260 }
3261 buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
3262 buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
3263 buf[TX_BCMC_FIFO] &= 0xff;
3264 if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
3265 i = TX_BCMC_FIFO;
3266 err = -1;
3267 }
3268 if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
3269 i = TX_ATIM_FIFO;
3270 err = -1;
3271 }
3272 if (err != 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003273 brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003274 " driver size %d index %d\n", buf[i],
3275 wlc_hw->xmtfifo_sz[i], i);
3276
3277 /* make sure we can still talk to the mac */
Arend van Spriel16d28122011-12-08 15:06:51 -08003278 WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003279
3280 /* band-specific inits done by wlc_bsinit() */
3281
3282 /* Set up frame burst size and antenna swap threshold init values */
3283 brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
3284 brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
3285
3286 /* enable one rx interrupt per received frame */
Arend van Spriel16d28122011-12-08 15:06:51 -08003287 bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003288
3289 /* set the station mode (BSS STA) */
3290 brcms_b_mctrl(wlc_hw,
3291 (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
3292 (MCTL_INFRA | MCTL_DISCARD_PMQ));
3293
3294 /* set up Beacon interval */
3295 bcnint_us = 0x8000 << 10;
Arend van Spriel16d28122011-12-08 15:06:51 -08003296 bcma_write32(core, D11REGOFFS(tsf_cfprep),
3297 (bcnint_us << CFPREP_CBI_SHIFT));
3298 bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
3299 bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003300
3301 /* write interrupt mask */
Arend van Spriel16d28122011-12-08 15:06:51 -08003302 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
3303 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003304
3305 /* allow the MAC to control the PHY clock (dynamic on/off) */
3306 brcms_b_macphyclk_set(wlc_hw, ON);
3307
3308 /* program dynamic clock control fast powerup delay register */
3309 wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -08003310 bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003311
3312 /* tell the ucode the corerev */
3313 brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
3314
3315 /* tell the ucode MAC capabilities */
3316 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
3317 (u16) (wlc_hw->machwcap & 0xffff));
3318 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
3319 (u16) ((wlc_hw->
3320 machwcap >> 16) & 0xffff));
3321
3322 /* write retry limits to SCR, this done after PSM init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003323 bcma_write32(core, D11REGOFFS(objaddr),
3324 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3325 (void)bcma_read32(core, D11REGOFFS(objaddr));
3326 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
3327 bcma_write32(core, D11REGOFFS(objaddr),
3328 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3329 (void)bcma_read32(core, D11REGOFFS(objaddr));
3330 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003331
3332 /* write rate fallback retry limits */
3333 brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
3334 brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
3335
Arend van Spriel16d28122011-12-08 15:06:51 -08003336 bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
3337 bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003338
3339 /* init the tx dma engines */
3340 for (i = 0; i < NFIFO; i++) {
3341 if (wlc_hw->di[i])
3342 dma_txinit(wlc_hw->di[i]);
3343 }
3344
3345 /* init the rx dma engine(s) and post receive buffers */
3346 dma_rxinit(wlc_hw->di[RX_FIFO]);
3347 dma_rxfill(wlc_hw->di[RX_FIFO]);
3348}
3349
3350void
Roland Vossena8bc4912011-10-21 16:16:25 +02003351static brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003352 u32 macintmask;
3353 bool fastclk;
3354 struct brcms_c_info *wlc = wlc_hw->wlc;
3355
Arend van Spriel5b435de2011-10-05 13:19:03 +02003356 /* request FAST clock if not on */
3357 fastclk = wlc_hw->forcefastclk;
3358 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003359 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003360
3361 /* disable interrupts */
3362 macintmask = brcms_intrsoff(wlc->wl);
3363
3364 /* set up the specified band and chanspec */
3365 brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
3366 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3367
3368 /* do one-time phy inits and calibration */
3369 wlc_phy_cal_init(wlc_hw->band->pi);
3370
3371 /* core-specific initialization */
3372 brcms_b_coreinit(wlc);
3373
Arend van Spriel5b435de2011-10-05 13:19:03 +02003374 /* band-specific inits */
3375 brcms_b_bsinit(wlc, chanspec);
3376
3377 /* restore macintmask */
3378 brcms_intrsrestore(wlc->wl, macintmask);
3379
3380 /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
3381 * is suspended and brcms_c_enable_mac() will clear this override bit.
3382 */
3383 mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
3384
3385 /*
3386 * initialize mac_suspend_depth to 1 to match ucode
3387 * initial suspended state
3388 */
3389 wlc_hw->mac_suspend_depth = 1;
3390
3391 /* restore the clk */
3392 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003393 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003394}
3395
3396static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
3397 u16 chanspec)
3398{
3399 /* Save our copy of the chanspec */
3400 wlc->chanspec = chanspec;
3401
3402 /* Set the chanspec and power limits for this locale */
3403 brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
3404
3405 if (wlc->stf->ss_algosel_auto)
3406 brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
3407 chanspec);
3408
3409 brcms_c_stf_ss_update(wlc, wlc->band);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003410}
Arend van Spriel5b435de2011-10-05 13:19:03 +02003411
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003412static void
3413brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
3414{
3415 brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
3416 wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
3417 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
3418 brcms_chspec_bw(wlc->default_bss->chanspec),
3419 wlc->stf->txstreams);
3420}
3421
3422/* derive wlc->band->basic_rate[] table from 'rateset' */
3423static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
3424 struct brcms_c_rateset *rateset)
3425{
3426 u8 rate;
3427 u8 mandatory;
3428 u8 cck_basic = 0;
3429 u8 ofdm_basic = 0;
3430 u8 *br = wlc->band->basic_rate;
3431 uint i;
3432
3433 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
3434 memset(br, 0, BRCM_MAXRATE + 1);
3435
3436 /* For each basic rate in the rates list, make an entry in the
3437 * best basic lookup.
3438 */
3439 for (i = 0; i < rateset->count; i++) {
3440 /* only make an entry for a basic rate */
3441 if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
3442 continue;
3443
3444 /* mask off basic bit */
3445 rate = (rateset->rates[i] & BRCMS_RATE_MASK);
3446
3447 if (rate > BRCM_MAXRATE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003448 brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003449 "invalid rate 0x%X in rate set\n",
3450 rateset->rates[i]);
3451 continue;
3452 }
3453
3454 br[rate] = rate;
3455 }
3456
3457 /* The rate lookup table now has non-zero entries for each
3458 * basic rate, equal to the basic rate: br[basicN] = basicN
3459 *
3460 * To look up the best basic rate corresponding to any
3461 * particular rate, code can use the basic_rate table
3462 * like this
3463 *
3464 * basic_rate = wlc->band->basic_rate[tx_rate]
3465 *
3466 * Make sure there is a best basic rate entry for
3467 * every rate by walking up the table from low rates
3468 * to high, filling in holes in the lookup table
3469 */
3470
3471 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
3472 rate = wlc->band->hw_rateset.rates[i];
3473
3474 if (br[rate] != 0) {
3475 /* This rate is a basic rate.
3476 * Keep track of the best basic rate so far by
3477 * modulation type.
3478 */
3479 if (is_ofdm_rate(rate))
3480 ofdm_basic = rate;
3481 else
3482 cck_basic = rate;
3483
3484 continue;
3485 }
3486
3487 /* This rate is not a basic rate so figure out the
3488 * best basic rate less than this rate and fill in
3489 * the hole in the table
3490 */
3491
3492 br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
3493
3494 if (br[rate] != 0)
3495 continue;
3496
3497 if (is_ofdm_rate(rate)) {
3498 /*
3499 * In 11g and 11a, the OFDM mandatory rates
3500 * are 6, 12, and 24 Mbps
3501 */
3502 if (rate >= BRCM_RATE_24M)
3503 mandatory = BRCM_RATE_24M;
3504 else if (rate >= BRCM_RATE_12M)
3505 mandatory = BRCM_RATE_12M;
3506 else
3507 mandatory = BRCM_RATE_6M;
3508 } else {
3509 /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
3510 mandatory = rate;
3511 }
3512
3513 br[rate] = mandatory;
3514 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003515}
3516
3517static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
3518 u16 chanspec)
3519{
3520 struct brcms_c_rateset default_rateset;
3521 uint parkband;
3522 uint i, band_order[2];
3523
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524 /*
3525 * We might have been bandlocked during down and the chip
3526 * power-cycled (hibernate). Figure out the right band to park on
3527 */
3528 if (wlc->bandlocked || wlc->pub->_nbands == 1) {
3529 /* updated in brcms_c_bandlock() */
3530 parkband = wlc->band->bandunit;
3531 band_order[0] = band_order[1] = parkband;
3532 } else {
3533 /* park on the band of the specified chanspec */
3534 parkband = chspec_bandunit(chanspec);
3535
3536 /* order so that parkband initialize last */
3537 band_order[0] = parkband ^ 1;
3538 band_order[1] = parkband;
3539 }
3540
3541 /* make each band operational, software state init */
3542 for (i = 0; i < wlc->pub->_nbands; i++) {
3543 uint j = band_order[i];
3544
3545 wlc->band = wlc->bandstate[j];
3546
3547 brcms_default_rateset(wlc, &default_rateset);
3548
3549 /* fill in hw_rate */
3550 brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
3551 false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
3552 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
3553
3554 /* init basic rate lookup */
3555 brcms_c_rate_lookup_init(wlc, &default_rateset);
3556 }
3557
3558 /* sync up phy/radio chanspec */
3559 brcms_c_set_phy_chanspec(wlc, chanspec);
3560}
3561
Alwin Beukers02a588a2011-11-10 20:30:28 +01003562/*
Alwin Beukersbe667662011-11-22 17:21:43 -08003563 * Set or clear filtering related maccontrol bits based on
3564 * specified filter flags
Alwin Beukers02a588a2011-11-10 20:30:28 +01003565 */
Alwin Beukersbe667662011-11-22 17:21:43 -08003566void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003567{
Alwin Beukers02a588a2011-11-10 20:30:28 +01003568 u32 promisc_bits = 0;
3569
Alwin Beukersbe667662011-11-22 17:21:43 -08003570 wlc->filter_flags = filter_flags;
3571
3572 if (filter_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
3573 promisc_bits |= MCTL_PROMISC;
3574
3575 if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
Alwin Beukers02a588a2011-11-10 20:30:28 +01003576 promisc_bits |= MCTL_BCNS_PROMISC;
3577
Alwin Beukersbe667662011-11-22 17:21:43 -08003578 if (filter_flags & FIF_FCSFAIL)
3579 promisc_bits |= MCTL_KEEPBADFCS;
3580
3581 if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
3582 promisc_bits |= MCTL_KEEPCONTROL;
Alwin Beukers02a588a2011-11-10 20:30:28 +01003583
3584 brcms_b_mctrl(wlc->hw,
Alwin Beukersbe667662011-11-22 17:21:43 -08003585 MCTL_PROMISC | MCTL_BCNS_PROMISC |
3586 MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
3587 promisc_bits);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003588}
3589
Arend van Spriel5b435de2011-10-05 13:19:03 +02003590/*
3591 * ucode, hwmac update
3592 * Channel dependent updates for ucode and hw
3593 */
3594static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
3595{
3596 /* enable or disable any active IBSSs depending on whether or not
3597 * we are on the home channel
3598 */
3599 if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
3600 if (wlc->pub->associated) {
3601 /*
3602 * BMAC_NOTE: This is something that should be fixed
3603 * in ucode inits. I think that the ucode inits set
3604 * up the bcn templates and shm values with a bogus
3605 * beacon. This should not be done in the inits. If
3606 * ucode needs to set up a beacon for testing, the
3607 * test routines should write it down, not expect the
3608 * inits to populate a bogus beacon.
3609 */
3610 if (BRCMS_PHY_11N_CAP(wlc->band))
3611 brcms_b_write_shm(wlc->hw,
3612 M_BCN_TXTSF_OFFSET, 0);
3613 }
3614 } else {
3615 /* disable an active IBSS if we are not on the home channel */
3616 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003617}
3618
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003619static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
3620 u8 basic_rate)
3621{
3622 u8 phy_rate, index;
3623 u8 basic_phy_rate, basic_index;
3624 u16 dir_table, basic_table;
3625 u16 basic_ptr;
3626
3627 /* Shared memory address for the table we are reading */
3628 dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
3629
3630 /* Shared memory address for the table we are writing */
3631 basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
3632
3633 /*
3634 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
3635 * the index into the rate table.
3636 */
3637 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
3638 basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
3639 index = phy_rate & 0xf;
3640 basic_index = basic_phy_rate & 0xf;
3641
3642 /* Find the SHM pointer to the ACK rate entry by looking in the
3643 * Direct-map Table
3644 */
3645 basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
3646
3647 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
3648 * to the correct basic rate for the given incoming rate
3649 */
3650 brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
3651}
3652
3653static const struct brcms_c_rateset *
3654brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
3655{
3656 const struct brcms_c_rateset *rs_dflt;
3657
3658 if (BRCMS_PHY_11N_CAP(wlc->band)) {
3659 if (wlc->band->bandtype == BRCM_BAND_5G)
3660 rs_dflt = &ofdm_mimo_rates;
3661 else
3662 rs_dflt = &cck_ofdm_mimo_rates;
3663 } else if (wlc->band->gmode)
3664 rs_dflt = &cck_ofdm_rates;
3665 else
3666 rs_dflt = &cck_rates;
3667
3668 return rs_dflt;
3669}
3670
3671static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
3672{
3673 const struct brcms_c_rateset *rs_dflt;
3674 struct brcms_c_rateset rs;
3675 u8 rate, basic_rate;
3676 uint i;
3677
3678 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
3679
3680 brcms_c_rateset_copy(rs_dflt, &rs);
3681 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
3682
3683 /* walk the phy rate table and update SHM basic rate lookup table */
3684 for (i = 0; i < rs.count; i++) {
3685 rate = rs.rates[i] & BRCMS_RATE_MASK;
3686
3687 /* for a given rate brcms_basic_rate returns the rate at
3688 * which a response ACK/CTS should be sent.
3689 */
3690 basic_rate = brcms_basic_rate(wlc, rate);
3691 if (basic_rate == 0)
3692 /* This should only happen if we are using a
3693 * restricted rateset.
3694 */
3695 basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
3696
3697 brcms_c_write_rate_shm(wlc, rate, basic_rate);
3698 }
3699}
3700
Arend van Spriel5b435de2011-10-05 13:19:03 +02003701/* band-specific init */
3702static void brcms_c_bsinit(struct brcms_c_info *wlc)
3703{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003704 brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
3705 wlc->pub->unit, wlc->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003706
3707 /* write ucode ACK/CTS rate table */
3708 brcms_c_set_ratetable(wlc);
3709
3710 /* update some band specific mac configuration */
3711 brcms_c_ucode_mac_upd(wlc);
3712
3713 /* init antenna selection */
3714 brcms_c_antsel_init(wlc->asi);
3715
3716}
3717
3718/* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
3719static int
3720brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
3721 bool writeToShm)
3722{
3723 int idle_busy_ratio_x_16 = 0;
3724 uint offset =
3725 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
3726 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
3727 if (duty_cycle > 100 || duty_cycle < 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003728 brcms_err(wlc->hw->d11core,
3729 "wl%d: duty cycle value off limit\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003730 wlc->pub->unit);
3731 return -EINVAL;
3732 }
3733 if (duty_cycle)
3734 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
3735 /* Only write to shared memory when wl is up */
3736 if (writeToShm)
3737 brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
3738
3739 if (isOFDM)
3740 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
3741 else
3742 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
3743
3744 return 0;
3745}
3746
Arend van Spriel5b435de2011-10-05 13:19:03 +02003747/* push sw hps and wake state through hardware */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003748static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003749{
3750 u32 v1, v2;
3751 bool hps;
3752 bool awake_before;
3753
3754 hps = brcms_c_ps_allowed(wlc);
3755
Seth Forshee913911f2012-11-15 08:08:04 -06003756 brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
3757 hps);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003758
Arend van Spriel16d28122011-12-08 15:06:51 -08003759 v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003760 v2 = MCTL_WAKE;
3761 if (hps)
3762 v2 |= MCTL_HPS;
3763
3764 brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
3765
3766 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
3767
3768 if (!awake_before)
3769 brcms_b_wait_for_wake(wlc->hw);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003770}
3771
3772/*
3773 * Write this BSS config's MAC address to core.
3774 * Updates RXE match engine.
3775 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003776static int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003777{
3778 int err = 0;
3779 struct brcms_c_info *wlc = bsscfg->wlc;
3780
3781 /* enter the MAC addr into the RXE match registers */
3782 brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, bsscfg->cur_etheraddr);
3783
3784 brcms_c_ampdu_macaddr_upd(wlc);
3785
3786 return err;
3787}
3788
3789/* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
3790 * Updates RXE match engine.
3791 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003792static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003793{
3794 /* we need to update BSSID in RXE match registers */
3795 brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
3796}
3797
3798static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
3799{
3800 wlc_hw->shortslot = shortslot;
3801
3802 if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
3803 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
3804 brcms_b_update_slot_timing(wlc_hw, shortslot);
3805 brcms_c_enable_mac(wlc_hw->wlc);
3806 }
3807}
3808
3809/*
3810 * Suspend the the MAC and update the slot timing
3811 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
3812 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003813static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003814{
3815 /* use the override if it is set */
3816 if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
3817 shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
3818
3819 if (wlc->shortslot == shortslot)
3820 return;
3821
3822 wlc->shortslot = shortslot;
3823
3824 brcms_b_set_shortslot(wlc->hw, shortslot);
3825}
3826
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003827static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003828{
3829 if (wlc->home_chanspec != chanspec) {
3830 wlc->home_chanspec = chanspec;
3831
3832 if (wlc->bsscfg->associated)
3833 wlc->bsscfg->current_bss->chanspec = chanspec;
3834 }
3835}
3836
3837void
3838brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
Roland Vossenc6c44892011-10-21 16:16:26 +02003839 bool mute_tx, struct txpwr_limits *txpwr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003840{
3841 uint bandunit;
3842
Seth Forshee913911f2012-11-15 08:08:04 -06003843 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
3844 chanspec);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003845
3846 wlc_hw->chanspec = chanspec;
3847
3848 /* Switch bands if necessary */
3849 if (wlc_hw->_nbands > 1) {
3850 bandunit = chspec_bandunit(chanspec);
3851 if (wlc_hw->band->bandunit != bandunit) {
3852 /* brcms_b_setband disables other bandunit,
3853 * use light band switch if not up yet
3854 */
3855 if (wlc_hw->up) {
3856 wlc_phy_chanspec_radio_set(wlc_hw->
3857 bandstate[bandunit]->
3858 pi, chanspec);
3859 brcms_b_setband(wlc_hw, bandunit, chanspec);
3860 } else {
3861 brcms_c_setxband(wlc_hw, bandunit);
3862 }
3863 }
3864 }
3865
Roland Vossenc6c44892011-10-21 16:16:26 +02003866 wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003867
3868 if (!wlc_hw->up) {
3869 if (wlc_hw->clk)
3870 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
3871 chanspec);
3872 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3873 } else {
3874 wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
3875 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
3876
3877 /* Update muting of the channel */
Roland Vossenc6c44892011-10-21 16:16:26 +02003878 brcms_b_mute(wlc_hw, mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003879 }
3880}
3881
3882/* switch to and initialize new band */
3883static void brcms_c_setband(struct brcms_c_info *wlc,
3884 uint bandunit)
3885{
3886 wlc->band = wlc->bandstate[bandunit];
3887
3888 if (!wlc->pub->up)
3889 return;
3890
3891 /* wait for at least one beacon before entering sleeping state */
3892 brcms_c_set_ps_ctrl(wlc);
3893
3894 /* band-specific initializations */
3895 brcms_c_bsinit(wlc);
3896}
3897
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003898static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003899{
3900 uint bandunit;
3901 bool switchband = false;
3902 u16 old_chanspec = wlc->chanspec;
3903
3904 if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003905 brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003906 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
3907 return;
3908 }
3909
3910 /* Switch bands if necessary */
3911 if (wlc->pub->_nbands > 1) {
3912 bandunit = chspec_bandunit(chanspec);
3913 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
3914 switchband = true;
3915 if (wlc->bandlocked) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003916 brcms_err(wlc->hw->d11core,
3917 "wl%d: %s: chspec %d band is locked!\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003918 wlc->pub->unit, __func__,
3919 CHSPEC_CHANNEL(chanspec));
3920 return;
3921 }
3922 /*
3923 * should the setband call come after the
3924 * brcms_b_chanspec() ? if the setband updates
3925 * (brcms_c_bsinit) use low level calls to inspect and
3926 * set state, the state inspected may be from the wrong
3927 * band, or the following brcms_b_set_chanspec() may
3928 * undo the work.
3929 */
3930 brcms_c_setband(wlc, bandunit);
3931 }
3932 }
3933
3934 /* sync up phy/radio chanspec */
3935 brcms_c_set_phy_chanspec(wlc, chanspec);
3936
3937 /* init antenna selection */
3938 if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
3939 brcms_c_antsel_init(wlc->asi);
3940
3941 /* Fix the hardware rateset based on bw.
3942 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
3943 */
3944 brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
3945 wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
3946 }
3947
3948 /* update some mac configuration since chanspec changed */
3949 brcms_c_ucode_mac_upd(wlc);
3950}
3951
Arend van Spriel5b435de2011-10-05 13:19:03 +02003952/*
3953 * This function changes the phytxctl for beacon based on current
3954 * beacon ratespec AND txant setting as per this table:
3955 * ratespec CCK ant = wlc->stf->txant
3956 * OFDM ant = 3
3957 */
3958void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
3959 u32 bcn_rspec)
3960{
3961 u16 phyctl;
3962 u16 phytxant = wlc->stf->phytxant;
3963 u16 mask = PHY_TXC_ANT_MASK;
3964
3965 /* for non-siso rates or default setting, use the available chains */
3966 if (BRCMS_PHY_11N_CAP(wlc->band))
3967 phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
3968
3969 phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
3970 phyctl = (phyctl & ~mask) | phytxant;
3971 brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
3972}
3973
3974/*
3975 * centralized protection config change function to simplify debugging, no
3976 * consistency checking this should be called only on changes to avoid overhead
3977 * in periodic function
3978 */
3979void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
3980{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003981 /*
3982 * Cannot use brcms_dbg_* here because this function is called
3983 * before wlc is sufficiently initialized.
3984 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003985 BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
3986
3987 switch (idx) {
3988 case BRCMS_PROT_G_SPEC:
3989 wlc->protection->_g = (bool) val;
3990 break;
3991 case BRCMS_PROT_G_OVR:
3992 wlc->protection->g_override = (s8) val;
3993 break;
3994 case BRCMS_PROT_G_USER:
3995 wlc->protection->gmode_user = (u8) val;
3996 break;
3997 case BRCMS_PROT_OVERLAP:
3998 wlc->protection->overlap = (s8) val;
3999 break;
4000 case BRCMS_PROT_N_USER:
4001 wlc->protection->nmode_user = (s8) val;
4002 break;
4003 case BRCMS_PROT_N_CFG:
4004 wlc->protection->n_cfg = (s8) val;
4005 break;
4006 case BRCMS_PROT_N_CFG_OVR:
4007 wlc->protection->n_cfg_override = (s8) val;
4008 break;
4009 case BRCMS_PROT_N_NONGF:
4010 wlc->protection->nongf = (bool) val;
4011 break;
4012 case BRCMS_PROT_N_NONGF_OVR:
4013 wlc->protection->nongf_override = (s8) val;
4014 break;
4015 case BRCMS_PROT_N_PAM_OVR:
4016 wlc->protection->n_pam_override = (s8) val;
4017 break;
4018 case BRCMS_PROT_N_OBSS:
4019 wlc->protection->n_obss = (bool) val;
4020 break;
4021
4022 default:
4023 break;
4024 }
4025
4026}
4027
4028static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
4029{
4030 if (wlc->pub->up) {
4031 brcms_c_update_beacon(wlc);
4032 brcms_c_update_probe_resp(wlc, true);
4033 }
4034}
4035
4036static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
4037{
4038 wlc->stf->ldpc = val;
4039
4040 if (wlc->pub->up) {
4041 brcms_c_update_beacon(wlc);
4042 brcms_c_update_probe_resp(wlc, true);
4043 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
4044 }
4045}
4046
4047void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
4048 const struct ieee80211_tx_queue_params *params,
4049 bool suspend)
4050{
4051 int i;
4052 struct shm_acparams acp_shm;
4053 u16 *shm_entry;
4054
4055 /* Only apply params if the core is out of reset and has clocks */
4056 if (!wlc->clk) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004057 brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
4058 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004059 return;
4060 }
4061
4062 memset((char *)&acp_shm, 0, sizeof(struct shm_acparams));
4063 /* fill in shm ac params struct */
4064 acp_shm.txop = params->txop;
4065 /* convert from units of 32us to us for ucode */
4066 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
4067 EDCF_TXOP2USEC(acp_shm.txop);
4068 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
4069
Arend van Sprielb7eec422011-11-10 20:30:18 +01004070 if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
Arend van Spriel5b435de2011-10-05 13:19:03 +02004071 && acp_shm.aifs < EDCF_AIFSN_MAX)
4072 acp_shm.aifs++;
4073
4074 if (acp_shm.aifs < EDCF_AIFSN_MIN
4075 || acp_shm.aifs > EDCF_AIFSN_MAX) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004076 brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
Arend van Spriel5b435de2011-10-05 13:19:03 +02004077 "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
4078 } else {
4079 acp_shm.cwmin = params->cw_min;
4080 acp_shm.cwmax = params->cw_max;
4081 acp_shm.cwcur = acp_shm.cwmin;
4082 acp_shm.bslots =
Arend van Spriel16d28122011-12-08 15:06:51 -08004083 bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
4084 acp_shm.cwcur;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004085 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
4086 /* Indicate the new params to the ucode */
4087 acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
4088 wme_ac2fifo[aci] *
4089 M_EDCF_QLEN +
4090 M_EDCF_STATUS_OFF));
4091 acp_shm.status |= WME_STATUS_NEWAC;
4092
4093 /* Fill in shm acparam table */
4094 shm_entry = (u16 *) &acp_shm;
4095 for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
4096 brcms_b_write_shm(wlc->hw,
4097 M_EDCF_QINFO +
4098 wme_ac2fifo[aci] * M_EDCF_QLEN + i,
4099 *shm_entry++);
4100 }
4101
4102 if (suspend) {
4103 brcms_c_suspend_mac_and_wait(wlc);
4104 brcms_c_enable_mac(wlc);
4105 }
4106}
4107
Arend van Spriel094b1992011-10-18 14:03:07 +02004108static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004109{
4110 u16 aci;
4111 int i_ac;
4112 struct ieee80211_tx_queue_params txq_pars;
4113 static const struct edcf_acparam default_edcf_acparams[] = {
4114 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
4115 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
4116 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
4117 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
4118 }; /* ucode needs these parameters during its initialization */
4119 const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
4120
Arend van Sprielb7eec422011-11-10 20:30:18 +01004121 for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004122 /* find out which ac this set of params applies to */
4123 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
4124
4125 /* fill in shm ac params struct */
4126 txq_pars.txop = edcf_acp->TXOP;
4127 txq_pars.aifs = edcf_acp->ACI;
4128
4129 /* CWmin = 2^(ECWmin) - 1 */
4130 txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
4131 /* CWmax = 2^(ECWmax) - 1 */
4132 txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
4133 >> EDCF_ECWMAX_SHIFT);
4134 brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
4135 }
4136
4137 if (suspend) {
4138 brcms_c_suspend_mac_and_wait(wlc);
4139 brcms_c_enable_mac(wlc);
4140 }
4141}
4142
Arend van Spriel5b435de2011-10-05 13:19:03 +02004143static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
4144{
4145 /* Don't start the timer if HWRADIO feature is disabled */
4146 if (wlc->radio_monitor)
4147 return;
4148
4149 wlc->radio_monitor = true;
4150 brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004151 brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004152}
4153
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004154static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004155{
4156 if (!wlc->radio_monitor)
4157 return true;
4158
4159 wlc->radio_monitor = false;
4160 brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004161 return brcms_del_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004162}
4163
4164/* read hwdisable state and propagate to wlc flag */
4165static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
4166{
4167 if (wlc->pub->hw_off)
4168 return;
4169
4170 if (brcms_b_radio_read_hwdisabled(wlc->hw))
4171 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4172 else
4173 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4174}
4175
Arend van Spriel5b435de2011-10-05 13:19:03 +02004176/* update hwradio status and return it */
4177bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
4178{
4179 brcms_c_radio_hwdisable_upd(wlc);
4180
4181 return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
4182 true : false;
4183}
4184
4185/* periodical query hw radio button while driver is "down" */
4186static void brcms_c_radio_timer(void *arg)
4187{
4188 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4189
4190 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004191 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4192 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004193 brcms_down(wlc->wl);
4194 return;
4195 }
4196
Arend van Spriel5b435de2011-10-05 13:19:03 +02004197 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004198}
4199
4200/* common low-level watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004201static void brcms_b_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004202{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004203 struct brcms_hardware *wlc_hw = wlc->hw;
4204
Arend van Spriel5b435de2011-10-05 13:19:03 +02004205 if (!wlc_hw->up)
4206 return;
4207
4208 /* increment second count */
4209 wlc_hw->now++;
4210
4211 /* Check for FIFO error interrupts */
4212 brcms_b_fifoerrors(wlc_hw);
4213
4214 /* make sure RX dma has buffers */
4215 dma_rxfill(wlc->hw->di[RX_FIFO]);
4216
4217 wlc_phy_watchdog(wlc_hw->band->pi);
4218}
4219
4220/* common watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004221static void brcms_c_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004222{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004223 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004224
4225 if (!wlc->pub->up)
4226 return;
4227
4228 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004229 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4230 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004231 brcms_down(wlc->wl);
4232 return;
4233 }
4234
4235 /* increment second count */
4236 wlc->pub->now++;
4237
Arend van Spriel5b435de2011-10-05 13:19:03 +02004238 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004239 /* if radio is disable, driver may be down, quit here */
4240 if (wlc->pub->radio_disabled)
4241 return;
4242
4243 brcms_b_watchdog(wlc);
4244
4245 /*
4246 * occasionally sample mac stat counters to
4247 * detect 16-bit counter wrap
4248 */
4249 if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
4250 brcms_c_statsupd(wlc);
4251
4252 if (BRCMS_ISNPHY(wlc->band) &&
4253 ((wlc->pub->now - wlc->tempsense_lasttime) >=
4254 BRCMS_TEMPSENSE_PERIOD)) {
4255 wlc->tempsense_lasttime = wlc->pub->now;
4256 brcms_c_tempsense_upd(wlc);
4257 }
4258}
4259
4260static void brcms_c_watchdog_by_timer(void *arg)
4261{
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004262 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4263
4264 brcms_c_watchdog(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004265}
4266
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004267static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004268{
4269 wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
4270 wlc, "watchdog");
4271 if (!wlc->wdtimer) {
4272 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for wdtimer "
4273 "failed\n", unit);
4274 goto fail;
4275 }
4276
4277 wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
4278 wlc, "radio");
4279 if (!wlc->radio_timer) {
4280 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for radio_timer "
4281 "failed\n", unit);
4282 goto fail;
4283 }
4284
4285 return true;
4286
4287 fail:
4288 return false;
4289}
4290
4291/*
4292 * Initialize brcms_c_info default values ...
4293 * may get overrides later in this function
4294 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004295static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004296{
4297 int i;
4298
4299 /* Save our copy of the chanspec */
4300 wlc->chanspec = ch20mhz_chspec(1);
4301
4302 /* various 802.11g modes */
4303 wlc->shortslot = false;
4304 wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
4305
4306 brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
4307 brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
4308
4309 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
4310 BRCMS_PROTECTION_AUTO);
4311 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
4312 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
4313 BRCMS_PROTECTION_AUTO);
4314 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
4315 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
4316
4317 brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
4318 BRCMS_PROTECTION_CTL_OVERLAP);
4319
4320 /* 802.11g draft 4.0 NonERP elt advertisement */
4321 wlc->include_legacy_erp = true;
4322
4323 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
4324 wlc->stf->txant = ANT_TX_DEF;
4325
4326 wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
4327
4328 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
4329 for (i = 0; i < NFIFO; i++)
4330 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
4331 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
4332
4333 /* default rate fallback retry limits */
4334 wlc->SFBL = RETRY_SHORT_FB;
4335 wlc->LFBL = RETRY_LONG_FB;
4336
4337 /* default mac retry limits */
4338 wlc->SRL = RETRY_SHORT_DEF;
4339 wlc->LRL = RETRY_LONG_DEF;
4340
4341 /* WME QoS mode is Auto by default */
4342 wlc->pub->_ampdu = AMPDU_AGG_HOST;
4343 wlc->pub->bcmerror = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004344}
4345
4346static uint brcms_c_attach_module(struct brcms_c_info *wlc)
4347{
4348 uint err = 0;
4349 uint unit;
4350 unit = wlc->pub->unit;
4351
4352 wlc->asi = brcms_c_antsel_attach(wlc);
4353 if (wlc->asi == NULL) {
4354 wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
4355 "failed\n", unit);
4356 err = 44;
4357 goto fail;
4358 }
4359
4360 wlc->ampdu = brcms_c_ampdu_attach(wlc);
4361 if (wlc->ampdu == NULL) {
4362 wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
4363 "failed\n", unit);
4364 err = 50;
4365 goto fail;
4366 }
4367
4368 if ((brcms_c_stf_attach(wlc) != 0)) {
4369 wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
4370 "failed\n", unit);
4371 err = 68;
4372 goto fail;
4373 }
4374 fail:
4375 return err;
4376}
4377
4378struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
4379{
4380 return wlc->pub;
4381}
4382
4383/* low level attach
4384 * run backplane attach, init nvram
4385 * run phy attach
4386 * initialize software state for each core and band
4387 * put the whole chip in reset(driver down state), no clock
4388 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08004389static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
4390 uint unit, bool piomode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004391{
4392 struct brcms_hardware *wlc_hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004393 uint err = 0;
4394 uint j;
4395 bool wme = false;
4396 struct shared_phy_params sha_params;
4397 struct wiphy *wiphy = wlc->wiphy;
Arend van Sprielb63337a2011-12-08 15:06:47 -08004398 struct pci_dev *pcidev = core->bus->host_pci;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004399 struct ssb_sprom *sprom = &core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004400
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004401 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
Seth Forsheeb353dda2012-11-15 08:08:03 -06004402 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4403 pcidev->vendor,
4404 pcidev->device);
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004405 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06004406 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4407 core->bus->boardinfo.vendor,
4408 core->bus->boardinfo.type);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004409
4410 wme = true;
4411
4412 wlc_hw = wlc->hw;
4413 wlc_hw->wlc = wlc;
4414 wlc_hw->unit = unit;
4415 wlc_hw->band = wlc_hw->bandstate[0];
4416 wlc_hw->_piomode = piomode;
4417
4418 /* populate struct brcms_hardware with default values */
4419 brcms_b_info_init(wlc_hw);
4420
4421 /*
4422 * Do the hardware portion of the attach. Also initialize software
4423 * state that depends on the particular hardware we are running.
4424 */
Arend van Spriel28a53442011-12-08 15:06:49 -08004425 wlc_hw->sih = ai_attach(core->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004426 if (wlc_hw->sih == NULL) {
4427 wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
4428 unit);
4429 err = 11;
4430 goto fail;
4431 }
4432
4433 /* verify again the device is supported */
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02004434 if (!brcms_c_chipmatch(core)) {
4435 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
4436 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004437 err = 12;
4438 goto fail;
4439 }
4440
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004441 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
4442 wlc_hw->vendorid = pcidev->vendor;
4443 wlc_hw->deviceid = pcidev->device;
4444 } else {
4445 wlc_hw->vendorid = core->bus->boardinfo.vendor;
4446 wlc_hw->deviceid = core->bus->boardinfo.type;
4447 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004448
Arend van Spriel16d28122011-12-08 15:06:51 -08004449 wlc_hw->d11core = core;
4450 wlc_hw->corerev = core->id.rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004451
4452 /* validate chip, chiprev and corerev */
4453 if (!brcms_c_isgoodchip(wlc_hw)) {
4454 err = 13;
4455 goto fail;
4456 }
4457
4458 /* initialize power control registers */
4459 ai_clkctl_init(wlc_hw->sih);
4460
4461 /* request fastclock and force fastclock for the rest of attach
4462 * bring the d11 core out of reset.
4463 * For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
4464 * is still false; But it will be called again inside wlc_corereset,
4465 * after d11 is out of reset.
4466 */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004467 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004468 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4469
4470 if (!brcms_b_validate_chip_access(wlc_hw)) {
4471 wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
4472 "failed\n", unit);
4473 err = 14;
4474 goto fail;
4475 }
4476
4477 /* get the board rev, used just below */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004478 j = sprom->board_rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004479 /* promote srom boardrev of 0xFF to 1 */
4480 if (j == BOARDREV_PROMOTABLE)
4481 j = BOARDREV_PROMOTED;
4482 wlc_hw->boardrev = (u16) j;
4483 if (!brcms_c_validboardtype(wlc_hw)) {
4484 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004485 "board type (0x%x)" " or revision level (0x%x)\n",
4486 unit, ai_get_boardtype(wlc_hw->sih),
4487 wlc_hw->boardrev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004488 err = 15;
4489 goto fail;
4490 }
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004491 wlc_hw->sromrev = sprom->revision;
4492 wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
4493 wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004494
4495 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
4496 brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
4497
4498 /* check device id(srom, nvram etc.) to set bands */
4499 if (wlc_hw->deviceid == BCM43224_D11N_ID ||
Hauke Mehrtens9dd4ea52012-12-07 17:10:03 +01004500 wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
4501 wlc_hw->deviceid == BCM43224_CHIP_ID)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004502 /* Dualband boards */
4503 wlc_hw->_nbands = 2;
4504 else
4505 wlc_hw->_nbands = 1;
4506
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004507 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
Arend van Spriel5b435de2011-10-05 13:19:03 +02004508 wlc_hw->_nbands = 1;
4509
4510 /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
4511 * unconditionally does the init of these values
4512 */
4513 wlc->vendorid = wlc_hw->vendorid;
4514 wlc->deviceid = wlc_hw->deviceid;
4515 wlc->pub->sih = wlc_hw->sih;
4516 wlc->pub->corerev = wlc_hw->corerev;
4517 wlc->pub->sromrev = wlc_hw->sromrev;
4518 wlc->pub->boardrev = wlc_hw->boardrev;
4519 wlc->pub->boardflags = wlc_hw->boardflags;
4520 wlc->pub->boardflags2 = wlc_hw->boardflags2;
4521 wlc->pub->_nbands = wlc_hw->_nbands;
4522
4523 wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
4524
4525 if (wlc_hw->physhim == NULL) {
4526 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
4527 "failed\n", unit);
4528 err = 25;
4529 goto fail;
4530 }
4531
4532 /* pass all the parameters to wlc_phy_shared_attach in one struct */
4533 sha_params.sih = wlc_hw->sih;
4534 sha_params.physhim = wlc_hw->physhim;
4535 sha_params.unit = unit;
4536 sha_params.corerev = wlc_hw->corerev;
4537 sha_params.vid = wlc_hw->vendorid;
4538 sha_params.did = wlc_hw->deviceid;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004539 sha_params.chip = ai_get_chip_id(wlc_hw->sih);
4540 sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
4541 sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004542 sha_params.sromrev = wlc_hw->sromrev;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004543 sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004544 sha_params.boardrev = wlc_hw->boardrev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004545 sha_params.boardflags = wlc_hw->boardflags;
4546 sha_params.boardflags2 = wlc_hw->boardflags2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004547
4548 /* alloc and save pointer to shared phy state area */
4549 wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
4550 if (!wlc_hw->phy_sh) {
4551 err = 16;
4552 goto fail;
4553 }
4554
4555 /* initialize software state for each core and band */
4556 for (j = 0; j < wlc_hw->_nbands; j++) {
4557 /*
4558 * band0 is always 2.4Ghz
4559 * band1, if present, is 5Ghz
4560 */
4561
4562 brcms_c_setxband(wlc_hw, j);
4563
4564 wlc_hw->band->bandunit = j;
4565 wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4566 wlc->band->bandunit = j;
4567 wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
Arend van Spriel3b758a62011-12-12 15:15:09 -08004568 wlc->core->coreidx = core->core_index;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004569
Arend van Spriel16d28122011-12-08 15:06:51 -08004570 wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004571 wlc_hw->machwcap_backup = wlc_hw->machwcap;
4572
4573 /* init tx fifo size */
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004574 WARN_ON((wlc_hw->corerev - XMTFIFOTBL_STARTREV) < 0 ||
4575 (wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
4576 ARRAY_SIZE(xmtfifo_sz));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004577 wlc_hw->xmtfifo_sz =
4578 xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004579 WARN_ON(!wlc_hw->xmtfifo_sz[0]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004580
4581 /* Get a phy for this band */
4582 wlc_hw->band->pi =
Arend van Spriel4b006b12011-12-08 15:06:54 -08004583 wlc_phy_attach(wlc_hw->phy_sh, core,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004584 wlc_hw->band->bandtype,
4585 wlc->wiphy);
4586 if (wlc_hw->band->pi == NULL) {
4587 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
4588 "attach failed\n", unit);
4589 err = 17;
4590 goto fail;
4591 }
4592
4593 wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
4594
4595 wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
4596 &wlc_hw->band->phyrev,
4597 &wlc_hw->band->radioid,
4598 &wlc_hw->band->radiorev);
4599 wlc_hw->band->abgphy_encore =
4600 wlc_phy_get_encore(wlc_hw->band->pi);
4601 wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
4602 wlc_hw->band->core_flags =
4603 wlc_phy_get_coreflags(wlc_hw->band->pi);
4604
4605 /* verify good phy_type & supported phy revision */
4606 if (BRCMS_ISNPHY(wlc_hw->band)) {
4607 if (NCONF_HAS(wlc_hw->band->phyrev))
4608 goto good_phy;
4609 else
4610 goto bad_phy;
4611 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
4612 if (LCNCONF_HAS(wlc_hw->band->phyrev))
4613 goto good_phy;
4614 else
4615 goto bad_phy;
4616 } else {
4617 bad_phy:
4618 wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
4619 "phy type/rev (%d/%d)\n", unit,
4620 wlc_hw->band->phytype, wlc_hw->band->phyrev);
4621 err = 18;
4622 goto fail;
4623 }
4624
4625 good_phy:
4626 /*
4627 * BMAC_NOTE: wlc->band->pi should not be set below and should
4628 * be done in the high level attach. However we can not make
4629 * that change until all low level access is changed to
4630 * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
4631 * keeping wlc_hw->band->pi as well for incremental update of
4632 * low level fns, and cut over low only init when all fns
4633 * updated.
4634 */
4635 wlc->band->pi = wlc_hw->band->pi;
4636 wlc->band->phytype = wlc_hw->band->phytype;
4637 wlc->band->phyrev = wlc_hw->band->phyrev;
4638 wlc->band->radioid = wlc_hw->band->radioid;
4639 wlc->band->radiorev = wlc_hw->band->radiorev;
4640
4641 /* default contention windows size limits */
4642 wlc_hw->band->CWmin = APHY_CWMIN;
4643 wlc_hw->band->CWmax = PHY_CWMAX;
4644
4645 if (!brcms_b_attach_dmapio(wlc, j, wme)) {
4646 err = 19;
4647 goto fail;
4648 }
4649 }
4650
4651 /* disable core to match driver "down" state */
4652 brcms_c_coredisable(wlc_hw);
4653
4654 /* Match driver "down" state */
4655 ai_pci_down(wlc_hw->sih);
4656
Arend van Spriel5b435de2011-10-05 13:19:03 +02004657 /* turn off pll and xtal to match driver "down" state */
4658 brcms_b_xtal(wlc_hw, OFF);
4659
4660 /* *******************************************************************
4661 * The hardware is in the DOWN state at this point. D11 core
4662 * or cores are in reset with clocks off, and the board PLLs
4663 * are off if possible.
4664 *
4665 * Beyond this point, wlc->sbclk == false and chip registers
4666 * should not be touched.
4667 *********************************************************************
4668 */
4669
4670 /* init etheraddr state variables */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004671 brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
4672
4673 if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
Arend van Spriel5b435de2011-10-05 13:19:03 +02004674 is_zero_ether_addr(wlc_hw->etheraddr)) {
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004675 wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
4676 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004677 err = 22;
4678 goto fail;
4679 }
4680
Seth Forsheeb353dda2012-11-15 08:08:03 -06004681 brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
4682 wlc_hw->deviceid, wlc_hw->_nbands,
4683 ai_get_boardtype(wlc_hw->sih));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004684
4685 return err;
4686
4687 fail:
4688 wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
4689 err);
4690 return err;
4691}
4692
4693static void brcms_c_attach_antgain_init(struct brcms_c_info *wlc)
4694{
4695 uint unit;
4696 unit = wlc->pub->unit;
4697
4698 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
4699 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
4700 wlc->band->antgain = 8;
4701 } else if (wlc->band->antgain == -1) {
4702 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4703 " srom, using 2dB\n", unit, __func__);
4704 wlc->band->antgain = 8;
4705 } else {
4706 s8 gain, fract;
4707 /* Older sroms specified gain in whole dbm only. In order
4708 * be able to specify qdbm granularity and remain backward
4709 * compatible the whole dbms are now encoded in only
4710 * low 6 bits and remaining qdbms are encoded in the hi 2 bits.
4711 * 6 bit signed number ranges from -32 - 31.
4712 *
4713 * Examples:
4714 * 0x1 = 1 db,
4715 * 0xc1 = 1.75 db (1 + 3 quarters),
4716 * 0x3f = -1 (-1 + 0 quarters),
4717 * 0x7f = -.75 (-1 + 1 quarters) = -3 qdbm.
4718 * 0xbf = -.50 (-1 + 2 quarters) = -2 qdbm.
4719 */
4720 gain = wlc->band->antgain & 0x3f;
4721 gain <<= 2; /* Sign extend */
4722 gain >>= 2;
4723 fract = (wlc->band->antgain & 0xc0) >> 6;
4724 wlc->band->antgain = 4 * gain + fract;
4725 }
4726}
4727
4728static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
4729{
4730 int aa;
4731 uint unit;
4732 int bandtype;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004733 struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004734
4735 unit = wlc->pub->unit;
4736 bandtype = wlc->band->bandtype;
4737
4738 /* get antennas available */
4739 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004740 aa = sprom->ant_available_a;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004741 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004742 aa = sprom->ant_available_bg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004743
4744 if ((aa < 1) || (aa > 15)) {
4745 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4746 " srom (0x%x), using 3\n", unit, __func__, aa);
4747 aa = 3;
4748 }
4749
4750 /* reset the defaults if we have a single antenna */
4751 if (aa == 1) {
4752 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
4753 wlc->stf->txant = ANT_TX_FORCE_0;
4754 } else if (aa == 2) {
4755 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
4756 wlc->stf->txant = ANT_TX_FORCE_1;
4757 } else {
4758 }
4759
4760 /* Compute Antenna Gain */
4761 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004762 wlc->band->antgain = sprom->antenna_gain.a1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004763 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004764 wlc->band->antgain = sprom->antenna_gain.a0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004765
4766 brcms_c_attach_antgain_init(wlc);
4767
4768 return true;
4769}
4770
4771static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
4772{
4773 u16 chanspec;
4774 struct brcms_band *band;
4775 struct brcms_bss_info *bi = wlc->default_bss;
4776
4777 /* init default and target BSS with some sane initial values */
4778 memset((char *)(bi), 0, sizeof(struct brcms_bss_info));
4779 bi->beacon_period = BEACON_INTERVAL_DEFAULT;
4780
4781 /* fill the default channel as the first valid channel
4782 * starting from the 2G channels
4783 */
4784 chanspec = ch20mhz_chspec(1);
4785 wlc->home_chanspec = bi->chanspec = chanspec;
4786
4787 /* find the band of our default channel */
4788 band = wlc->band;
4789 if (wlc->pub->_nbands > 1 &&
4790 band->bandunit != chspec_bandunit(chanspec))
4791 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
4792
4793 /* init bss rates to the band specific default rate set */
4794 brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
4795 band->bandtype, false, BRCMS_RATE_MASK_FULL,
4796 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
4797 brcms_chspec_bw(chanspec), wlc->stf->txstreams);
4798
4799 if (wlc->pub->_n_enab & SUPPORT_11N)
4800 bi->flags |= BRCMS_BSS_HT;
4801}
4802
Arend van Spriel5b435de2011-10-05 13:19:03 +02004803static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
4804{
4805 uint i;
4806 struct brcms_band *band;
4807
4808 for (i = 0; i < wlc->pub->_nbands; i++) {
4809 band = wlc->bandstate[i];
4810 if (band->bandtype == BRCM_BAND_5G) {
4811 if ((bwcap == BRCMS_N_BW_40ALL)
4812 || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
4813 band->mimo_cap_40 = true;
4814 else
4815 band->mimo_cap_40 = false;
4816 } else {
4817 if (bwcap == BRCMS_N_BW_40ALL)
4818 band->mimo_cap_40 = true;
4819 else
4820 band->mimo_cap_40 = false;
4821 }
4822 }
4823}
4824
Arend van Spriel5b435de2011-10-05 13:19:03 +02004825static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
4826{
4827 /* free timer state */
4828 if (wlc->wdtimer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004829 brcms_free_timer(wlc->wdtimer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004830 wlc->wdtimer = NULL;
4831 }
4832 if (wlc->radio_timer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004833 brcms_free_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004834 wlc->radio_timer = NULL;
4835 }
4836}
4837
4838static void brcms_c_detach_module(struct brcms_c_info *wlc)
4839{
4840 if (wlc->asi) {
4841 brcms_c_antsel_detach(wlc->asi);
4842 wlc->asi = NULL;
4843 }
4844
4845 if (wlc->ampdu) {
4846 brcms_c_ampdu_detach(wlc->ampdu);
4847 wlc->ampdu = NULL;
4848 }
4849
4850 brcms_c_stf_detach(wlc);
4851}
4852
4853/*
4854 * low level detach
4855 */
4856static int brcms_b_detach(struct brcms_c_info *wlc)
4857{
4858 uint i;
4859 struct brcms_hw_band *band;
4860 struct brcms_hardware *wlc_hw = wlc->hw;
4861 int callbacks;
4862
4863 callbacks = 0;
4864
Arend van Spriel5b435de2011-10-05 13:19:03 +02004865 brcms_b_detach_dmapio(wlc_hw);
4866
4867 band = wlc_hw->band;
4868 for (i = 0; i < wlc_hw->_nbands; i++) {
4869 if (band->pi) {
4870 /* Detach this band's phy */
4871 wlc_phy_detach(band->pi);
4872 band->pi = NULL;
4873 }
4874 band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
4875 }
4876
4877 /* Free shared phy state */
4878 kfree(wlc_hw->phy_sh);
4879
4880 wlc_phy_shim_detach(wlc_hw->physhim);
4881
4882 if (wlc_hw->sih) {
4883 ai_detach(wlc_hw->sih);
4884 wlc_hw->sih = NULL;
4885 }
4886
4887 return callbacks;
4888
4889}
4890
4891/*
4892 * Return a count of the number of driver callbacks still pending.
4893 *
4894 * General policy is that brcms_c_detach can only dealloc/free software states.
4895 * It can NOT touch hardware registers since the d11core may be in reset and
4896 * clock may not be available.
4897 * One exception is sb register access, which is possible if crystal is turned
4898 * on after "down" state, driver should avoid software timer with the exception
4899 * of radio_monitor.
4900 */
4901uint brcms_c_detach(struct brcms_c_info *wlc)
4902{
4903 uint callbacks = 0;
4904
4905 if (wlc == NULL)
4906 return 0;
4907
Arend van Spriel5b435de2011-10-05 13:19:03 +02004908 callbacks += brcms_b_detach(wlc);
4909
4910 /* delete software timers */
4911 if (!brcms_c_radio_monitor_stop(wlc))
4912 callbacks++;
4913
4914 brcms_c_channel_mgr_detach(wlc->cmi);
4915
4916 brcms_c_timers_deinit(wlc);
4917
4918 brcms_c_detach_module(wlc);
4919
Arend van Spriel5b435de2011-10-05 13:19:03 +02004920 brcms_c_detach_mfree(wlc);
4921 return callbacks;
4922}
4923
4924/* update state that depends on the current value of "ap" */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004925static void brcms_c_ap_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004926{
4927 /* STA-BSS; short capable */
4928 wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004929}
4930
Arend van Spriel5b435de2011-10-05 13:19:03 +02004931/* Initialize just the hardware when coming out of POR or S3/S5 system states */
4932static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
4933{
4934 if (wlc_hw->wlc->pub->hw_up)
4935 return;
4936
Seth Forsheeb353dda2012-11-15 08:08:03 -06004937 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004938
4939 /*
4940 * Enable pll and xtal, initialize the power control registers,
4941 * and force fastclock for the remainder of brcms_c_up().
4942 */
4943 brcms_b_xtal(wlc_hw, ON);
4944 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004945 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004946
Arend van Spriel5b435de2011-10-05 13:19:03 +02004947 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08004948 * TODO: test suspend/resume
4949 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02004950 * AI chip doesn't restore bar0win2 on
4951 * hibernation/resume, need sw fixup
4952 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02004953
4954 /*
4955 * Inform phy that a POR reset has occurred so
4956 * it does a complete phy init
4957 */
4958 wlc_phy_por_inform(wlc_hw->band->pi);
4959
4960 wlc_hw->ucode_loaded = false;
4961 wlc_hw->wlc->pub->hw_up = true;
4962
4963 if ((wlc_hw->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004964 && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004965 if (!
4966 (wlc_hw->boardrev >= 0x1250
4967 && (wlc_hw->boardflags & BFL_FEM_BT)))
4968 ai_epa_4313war(wlc_hw->sih);
4969 }
4970}
4971
4972static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
4973{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004974 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004975
4976 /*
4977 * Enable pll and xtal, initialize the power control registers,
4978 * and force fastclock for the remainder of brcms_c_up().
4979 */
4980 brcms_b_xtal(wlc_hw, ON);
4981 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004982 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004983
4984 /*
4985 * Configure pci/pcmcia here instead of in brcms_c_attach()
4986 * to allow mfg hotswap: down, hotswap (chip power cycle), up.
4987 */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004988 bcma_core_pci_irq_ctl(&wlc_hw->d11core->bus->drv_pci[0], wlc_hw->d11core,
Hauke Mehrtensb30ee752012-04-29 02:50:32 +02004989 true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004990
4991 /*
4992 * Need to read the hwradio status here to cover the case where the
4993 * system is loaded with the hw radio disabled. We do not want to
4994 * bring the driver up in this case.
4995 */
4996 if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
4997 /* put SB PCI in down state again */
4998 ai_pci_down(wlc_hw->sih);
4999 brcms_b_xtal(wlc_hw, OFF);
5000 return -ENOMEDIUM;
5001 }
5002
5003 ai_pci_up(wlc_hw->sih);
5004
5005 /* reset the d11 core */
5006 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
5007
5008 return 0;
5009}
5010
5011static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
5012{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005013 wlc_hw->up = true;
5014 wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
5015
5016 /* FULLY enable dynamic power control and d11 core interrupt */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005017 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005018 brcms_intrson(wlc_hw->wlc->wl);
5019 return 0;
5020}
5021
5022/*
5023 * Write WME tunable parameters for retransmit/max rate
5024 * from wlc struct to ucode
5025 */
5026static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
5027{
5028 int ac;
5029
5030 /* Need clock to do this */
5031 if (!wlc->clk)
5032 return;
5033
Arend van Sprielb7eec422011-11-10 20:30:18 +01005034 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005035 brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
5036 wlc->wme_retries[ac]);
5037}
5038
5039/* make interface operational */
5040int brcms_c_up(struct brcms_c_info *wlc)
5041{
Seth Forshee91691292012-06-16 07:47:49 -05005042 struct ieee80211_channel *ch;
5043
Seth Forsheeb353dda2012-11-15 08:08:03 -06005044 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005045
5046 /* HW is turned off so don't try to access it */
5047 if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
5048 return -ENOMEDIUM;
5049
5050 if (!wlc->pub->hw_up) {
5051 brcms_b_hw_up(wlc->hw);
5052 wlc->pub->hw_up = true;
5053 }
5054
5055 if ((wlc->pub->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02005056 && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005057 if (wlc->pub->boardrev >= 0x1250
5058 && (wlc->pub->boardflags & BFL_FEM_BT))
5059 brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
5060 MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
5061 else
5062 brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
5063 MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
5064 }
5065
5066 /*
5067 * Need to read the hwradio status here to cover the case where the
5068 * system is loaded with the hw radio disabled. We do not want to bring
5069 * the driver up in this case. If radio is disabled, abort up, lower
5070 * power, start radio timer and return 0(for NDIS) don't call
5071 * radio_update to avoid looping brcms_c_up.
5072 *
5073 * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
5074 */
5075 if (!wlc->pub->radio_disabled) {
5076 int status = brcms_b_up_prep(wlc->hw);
5077 if (status == -ENOMEDIUM) {
5078 if (!mboolisset
5079 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
5080 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
5081 mboolset(wlc->pub->radio_disabled,
5082 WL_RADIO_HW_DISABLE);
5083
5084 if (bsscfg->enable && bsscfg->BSS)
Seth Forsheeb353dda2012-11-15 08:08:03 -06005085 brcms_err(wlc->hw->d11core,
5086 "wl%d: up: rfdisable -> "
Arend van Spriel5b435de2011-10-05 13:19:03 +02005087 "bsscfg_disable()\n",
5088 wlc->pub->unit);
5089 }
5090 }
5091 }
5092
5093 if (wlc->pub->radio_disabled) {
5094 brcms_c_radio_monitor_start(wlc);
5095 return 0;
5096 }
5097
5098 /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
5099 wlc->clk = true;
5100
5101 brcms_c_radio_monitor_stop(wlc);
5102
5103 /* Set EDCF hostflags */
5104 brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
5105
5106 brcms_init(wlc->wl);
5107 wlc->pub->up = true;
5108
5109 if (wlc->bandinit_pending) {
Seth Forshee91691292012-06-16 07:47:49 -05005110 ch = wlc->pub->ieee_hw->conf.channel;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005111 brcms_c_suspend_mac_and_wait(wlc);
Seth Forshee91691292012-06-16 07:47:49 -05005112 brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005113 wlc->bandinit_pending = false;
5114 brcms_c_enable_mac(wlc);
5115 }
5116
5117 brcms_b_up_finish(wlc->hw);
5118
5119 /* Program the TX wme params with the current settings */
5120 brcms_c_wme_retries_write(wlc);
5121
5122 /* start one second watchdog timer */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005123 brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005124 wlc->WDarmed = true;
5125
5126 /* ensure antenna config is up to date */
5127 brcms_c_stf_phy_txant_upd(wlc);
5128 /* ensure LDPC config is in sync */
5129 brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
5130
5131 return 0;
5132}
5133
5134static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
5135{
5136 uint callbacks = 0;
5137
5138 return callbacks;
5139}
5140
5141static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
5142{
5143 bool dev_gone;
5144 uint callbacks = 0;
5145
Arend van Spriel5b435de2011-10-05 13:19:03 +02005146 if (!wlc_hw->up)
5147 return callbacks;
5148
5149 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5150
5151 /* disable interrupts */
5152 if (dev_gone)
5153 wlc_hw->wlc->macintmask = 0;
5154 else {
5155 /* now disable interrupts */
5156 brcms_intrsoff(wlc_hw->wlc->wl);
5157
5158 /* ensure we're running on the pll clock again */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005159 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005160 }
5161 /* down phy at the last of this stage */
5162 callbacks += wlc_phy_down(wlc_hw->band->pi);
5163
5164 return callbacks;
5165}
5166
5167static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
5168{
5169 uint callbacks = 0;
5170 bool dev_gone;
5171
Arend van Spriel5b435de2011-10-05 13:19:03 +02005172 if (!wlc_hw->up)
5173 return callbacks;
5174
5175 wlc_hw->up = false;
5176 wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
5177
5178 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5179
5180 if (dev_gone) {
5181 wlc_hw->sbclk = false;
5182 wlc_hw->clk = false;
5183 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
5184
5185 /* reclaim any posted packets */
5186 brcms_c_flushqueues(wlc_hw->wlc);
5187 } else {
5188
5189 /* Reset and disable the core */
Arend van Spriela8779e42011-12-08 15:06:58 -08005190 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08005191 if (bcma_read32(wlc_hw->d11core,
5192 D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005193 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
5194 callbacks += brcms_reset(wlc_hw->wlc->wl);
5195 brcms_c_coredisable(wlc_hw);
5196 }
5197
5198 /* turn off primary xtal and pll */
5199 if (!wlc_hw->noreset) {
5200 ai_pci_down(wlc_hw->sih);
5201 brcms_b_xtal(wlc_hw, OFF);
5202 }
5203 }
5204
5205 return callbacks;
5206}
5207
5208/*
5209 * Mark the interface nonoperational, stop the software mechanisms,
5210 * disable the hardware, free any transient buffer state.
5211 * Return a count of the number of driver callbacks still pending.
5212 */
5213uint brcms_c_down(struct brcms_c_info *wlc)
5214{
5215
5216 uint callbacks = 0;
5217 int i;
5218 bool dev_gone = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005219
Seth Forsheeb353dda2012-11-15 08:08:03 -06005220 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005221
5222 /* check if we are already in the going down path */
5223 if (wlc->going_down) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005224 brcms_err(wlc->hw->d11core,
5225 "wl%d: %s: Driver going down so return\n",
5226 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005227 return 0;
5228 }
5229 if (!wlc->pub->up)
5230 return callbacks;
5231
Arend van Spriel5b435de2011-10-05 13:19:03 +02005232 wlc->going_down = true;
5233
5234 callbacks += brcms_b_bmac_down_prep(wlc->hw);
5235
5236 dev_gone = brcms_deviceremoved(wlc);
5237
5238 /* Call any registered down handlers */
5239 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5240 if (wlc->modulecb[i].down_fn)
5241 callbacks +=
5242 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
5243 }
5244
5245 /* cancel the watchdog timer */
5246 if (wlc->WDarmed) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005247 if (!brcms_del_timer(wlc->wdtimer))
Arend van Spriel5b435de2011-10-05 13:19:03 +02005248 callbacks++;
5249 wlc->WDarmed = false;
5250 }
5251 /* cancel all other timers */
5252 callbacks += brcms_c_down_del_timer(wlc);
5253
5254 wlc->pub->up = false;
5255
5256 wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
5257
Arend van Spriel5b435de2011-10-05 13:19:03 +02005258 callbacks += brcms_b_down_finish(wlc->hw);
5259
5260 /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
5261 wlc->clk = false;
5262
5263 wlc->going_down = false;
5264 return callbacks;
5265}
5266
5267/* Set the current gmode configuration */
5268int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
5269{
5270 int ret = 0;
5271 uint i;
5272 struct brcms_c_rateset rs;
5273 /* Default to 54g Auto */
5274 /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
5275 s8 shortslot = BRCMS_SHORTSLOT_AUTO;
5276 bool shortslot_restrict = false; /* Restrict association to stations
5277 * that support shortslot
5278 */
5279 bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
5280 /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
5281 int preamble = BRCMS_PLCP_LONG;
5282 bool preamble_restrict = false; /* Restrict association to stations
5283 * that support short preambles
5284 */
5285 struct brcms_band *band;
5286
5287 /* if N-support is enabled, allow Gmode set as long as requested
5288 * Gmode is not GMODE_LEGACY_B
5289 */
5290 if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
5291 return -ENOTSUPP;
5292
5293 /* verify that we are dealing with 2G band and grab the band pointer */
5294 if (wlc->band->bandtype == BRCM_BAND_2G)
5295 band = wlc->band;
5296 else if ((wlc->pub->_nbands > 1) &&
5297 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
5298 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5299 else
5300 return -EINVAL;
5301
Arend van Spriel5b435de2011-10-05 13:19:03 +02005302 /* update configuration value */
Joe Perches23677ce2012-02-09 11:17:23 +00005303 if (config)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005304 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
5305
5306 /* Clear rateset override */
5307 memset(&rs, 0, sizeof(struct brcms_c_rateset));
5308
5309 switch (gmode) {
5310 case GMODE_LEGACY_B:
5311 shortslot = BRCMS_SHORTSLOT_OFF;
5312 brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
5313
5314 break;
5315
5316 case GMODE_LRS:
5317 break;
5318
5319 case GMODE_AUTO:
5320 /* Accept defaults */
5321 break;
5322
5323 case GMODE_ONLY:
5324 ofdm_basic = true;
5325 preamble = BRCMS_PLCP_SHORT;
5326 preamble_restrict = true;
5327 break;
5328
5329 case GMODE_PERFORMANCE:
5330 shortslot = BRCMS_SHORTSLOT_ON;
5331 shortslot_restrict = true;
5332 ofdm_basic = true;
5333 preamble = BRCMS_PLCP_SHORT;
5334 preamble_restrict = true;
5335 break;
5336
5337 default:
5338 /* Error */
Seth Forsheeb353dda2012-11-15 08:08:03 -06005339 brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005340 wlc->pub->unit, __func__, gmode);
5341 return -ENOTSUPP;
5342 }
5343
5344 band->gmode = gmode;
5345
5346 wlc->shortslot_override = shortslot;
5347
5348 /* Use the default 11g rateset */
5349 if (!rs.count)
5350 brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
5351
5352 if (ofdm_basic) {
5353 for (i = 0; i < rs.count; i++) {
5354 if (rs.rates[i] == BRCM_RATE_6M
5355 || rs.rates[i] == BRCM_RATE_12M
5356 || rs.rates[i] == BRCM_RATE_24M)
5357 rs.rates[i] |= BRCMS_RATE_FLAG;
5358 }
5359 }
5360
5361 /* Set default bss rateset */
5362 wlc->default_bss->rateset.count = rs.count;
5363 memcpy(wlc->default_bss->rateset.rates, rs.rates,
5364 sizeof(wlc->default_bss->rateset.rates));
5365
5366 return ret;
5367}
5368
5369int brcms_c_set_nmode(struct brcms_c_info *wlc)
5370{
5371 uint i;
5372 s32 nmode = AUTO;
5373
5374 if (wlc->stf->txstreams == WL_11N_3x3)
5375 nmode = WL_11N_3x3;
5376 else
5377 nmode = WL_11N_2x2;
5378
5379 /* force GMODE_AUTO if NMODE is ON */
5380 brcms_c_set_gmode(wlc, GMODE_AUTO, true);
5381 if (nmode == WL_11N_3x3)
5382 wlc->pub->_n_enab = SUPPORT_HT;
5383 else
5384 wlc->pub->_n_enab = SUPPORT_11N;
5385 wlc->default_bss->flags |= BRCMS_BSS_HT;
5386 /* add the mcs rates to the default and hw ratesets */
5387 brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
5388 wlc->stf->txstreams);
5389 for (i = 0; i < wlc->pub->_nbands; i++)
5390 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
5391 wlc->default_bss->rateset.mcs, MCSSET_LEN);
5392
5393 return 0;
5394}
5395
5396static int
5397brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
5398 struct brcms_c_rateset *rs_arg)
5399{
5400 struct brcms_c_rateset rs, new;
5401 uint bandunit;
5402
5403 memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
5404
5405 /* check for bad count value */
5406 if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
5407 return -EINVAL;
5408
5409 /* try the current band */
5410 bandunit = wlc->band->bandunit;
5411 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5412 if (brcms_c_rate_hwrs_filter_sort_validate
5413 (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
5414 wlc->stf->txstreams))
5415 goto good;
5416
5417 /* try the other band */
5418 if (brcms_is_mband_unlocked(wlc)) {
5419 bandunit = OTHERBANDUNIT(wlc);
5420 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5421 if (brcms_c_rate_hwrs_filter_sort_validate(&new,
5422 &wlc->
5423 bandstate[bandunit]->
5424 hw_rateset, true,
5425 wlc->stf->txstreams))
5426 goto good;
5427 }
5428
5429 return -EBADE;
5430
5431 good:
5432 /* apply new rateset */
5433 memcpy(&wlc->default_bss->rateset, &new,
5434 sizeof(struct brcms_c_rateset));
5435 memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
5436 sizeof(struct brcms_c_rateset));
5437 return 0;
5438}
5439
5440static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
5441{
5442 u8 r;
5443 bool war = false;
5444
5445 if (wlc->bsscfg->associated)
5446 r = wlc->bsscfg->current_bss->rateset.rates[0];
5447 else
5448 r = wlc->default_bss->rateset.rates[0];
5449
5450 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
5451}
5452
5453int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
5454{
5455 u16 chspec = ch20mhz_chspec(channel);
5456
5457 if (channel < 0 || channel > MAXCHANNEL)
5458 return -EINVAL;
5459
5460 if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
5461 return -EINVAL;
5462
5463
5464 if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
5465 if (wlc->band->bandunit != chspec_bandunit(chspec))
5466 wlc->bandinit_pending = true;
5467 else
5468 wlc->bandinit_pending = false;
5469 }
5470
5471 wlc->default_bss->chanspec = chspec;
5472 /* brcms_c_BSSinit() will sanitize the rateset before
5473 * using it.. */
5474 if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
5475 brcms_c_set_home_chanspec(wlc, chspec);
5476 brcms_c_suspend_mac_and_wait(wlc);
5477 brcms_c_set_chanspec(wlc, chspec);
5478 brcms_c_enable_mac(wlc);
5479 }
5480 return 0;
5481}
5482
5483int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
5484{
5485 int ac;
5486
5487 if (srl < 1 || srl > RETRY_SHORT_MAX ||
5488 lrl < 1 || lrl > RETRY_SHORT_MAX)
5489 return -EINVAL;
5490
5491 wlc->SRL = srl;
5492 wlc->LRL = lrl;
5493
5494 brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
5495
Arend van Sprielb7eec422011-11-10 20:30:18 +01005496 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005497 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5498 EDCF_SHORT, wlc->SRL);
5499 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5500 EDCF_LONG, wlc->LRL);
5501 }
5502 brcms_c_wme_retries_write(wlc);
5503
5504 return 0;
5505}
5506
5507void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
5508 struct brcm_rateset *currs)
5509{
5510 struct brcms_c_rateset *rs;
5511
5512 if (wlc->pub->associated)
5513 rs = &wlc->bsscfg->current_bss->rateset;
5514 else
5515 rs = &wlc->default_bss->rateset;
5516
5517 /* Copy only legacy rateset section */
5518 currs->count = rs->count;
5519 memcpy(&currs->rates, &rs->rates, rs->count);
5520}
5521
5522int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
5523{
5524 struct brcms_c_rateset internal_rs;
5525 int bcmerror;
5526
5527 if (rs->count > BRCMS_NUMRATES)
5528 return -ENOBUFS;
5529
5530 memset(&internal_rs, 0, sizeof(struct brcms_c_rateset));
5531
5532 /* Copy only legacy rateset section */
5533 internal_rs.count = rs->count;
5534 memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
5535
5536 /* merge rateset coming in with the current mcsset */
5537 if (wlc->pub->_n_enab & SUPPORT_11N) {
5538 struct brcms_bss_info *mcsset_bss;
5539 if (wlc->bsscfg->associated)
5540 mcsset_bss = wlc->bsscfg->current_bss;
5541 else
5542 mcsset_bss = wlc->default_bss;
5543 memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
5544 MCSSET_LEN);
5545 }
5546
5547 bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
5548 if (!bcmerror)
5549 brcms_c_ofdm_rateset_war(wlc);
5550
5551 return bcmerror;
5552}
5553
5554int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
5555{
Tim Gardner708eb542013-02-07 12:35:35 -07005556 if (period == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005557 return -EINVAL;
5558
5559 wlc->default_bss->beacon_period = period;
5560 return 0;
5561}
5562
5563u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
5564{
5565 return wlc->band->phytype;
5566}
5567
5568void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
5569{
5570 wlc->shortslot_override = sslot_override;
5571
5572 /*
5573 * shortslot is an 11g feature, so no more work if we are
5574 * currently on the 5G band
5575 */
5576 if (wlc->band->bandtype == BRCM_BAND_5G)
5577 return;
5578
5579 if (wlc->pub->up && wlc->pub->associated) {
5580 /* let watchdog or beacon processing update shortslot */
5581 } else if (wlc->pub->up) {
5582 /* unassociated shortslot is off */
5583 brcms_c_switch_shortslot(wlc, false);
5584 } else {
5585 /* driver is down, so just update the brcms_c_info
5586 * value */
5587 if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
5588 wlc->shortslot = false;
5589 else
5590 wlc->shortslot =
5591 (wlc->shortslot_override ==
5592 BRCMS_SHORTSLOT_ON);
5593 }
5594}
5595
5596/*
5597 * register watchdog and down handlers.
5598 */
5599int brcms_c_module_register(struct brcms_pub *pub,
5600 const char *name, struct brcms_info *hdl,
5601 int (*d_fn)(void *handle))
5602{
5603 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5604 int i;
5605
5606 /* find an empty entry and just add, no duplication check! */
5607 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5608 if (wlc->modulecb[i].name[0] == '\0') {
5609 strncpy(wlc->modulecb[i].name, name,
5610 sizeof(wlc->modulecb[i].name) - 1);
5611 wlc->modulecb[i].hdl = hdl;
5612 wlc->modulecb[i].down_fn = d_fn;
5613 return 0;
5614 }
5615 }
5616
5617 return -ENOSR;
5618}
5619
5620/* unregister module callbacks */
5621int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
5622 struct brcms_info *hdl)
5623{
5624 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5625 int i;
5626
5627 if (wlc == NULL)
5628 return -ENODATA;
5629
5630 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5631 if (!strcmp(wlc->modulecb[i].name, name) &&
5632 (wlc->modulecb[i].hdl == hdl)) {
5633 memset(&wlc->modulecb[i], 0, sizeof(struct modulecb));
5634 return 0;
5635 }
5636 }
5637
5638 /* table not found! */
5639 return -ENODATA;
5640}
5641
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005642static bool brcms_c_chipmatch_pci(struct bcma_device *core)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005643{
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005644 struct pci_dev *pcidev = core->bus->host_pci;
5645 u16 vendor = pcidev->vendor;
5646 u16 device = pcidev->device;
5647
Arend van Spriel5b435de2011-10-05 13:19:03 +02005648 if (vendor != PCI_VENDOR_ID_BROADCOM) {
Joe Perches02f77192012-01-15 00:38:44 -08005649 pr_err("unknown vendor id %04x\n", vendor);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005650 return false;
5651 }
5652
Hauke Mehrtens9dd4ea52012-12-07 17:10:03 +01005653 if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005654 return true;
5655 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
5656 return true;
5657 if (device == BCM4313_D11N2G_ID)
5658 return true;
5659 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
5660 return true;
5661
Joe Perches02f77192012-01-15 00:38:44 -08005662 pr_err("unknown device id %04x\n", device);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005663 return false;
5664}
5665
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005666static bool brcms_c_chipmatch_soc(struct bcma_device *core)
5667{
5668 struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
5669
5670 if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
5671 return true;
5672
5673 pr_err("unknown chip id %04x\n", chipinfo->id);
5674 return false;
5675}
5676
5677bool brcms_c_chipmatch(struct bcma_device *core)
5678{
5679 switch (core->bus->hosttype) {
5680 case BCMA_HOSTTYPE_PCI:
5681 return brcms_c_chipmatch_pci(core);
5682 case BCMA_HOSTTYPE_SOC:
5683 return brcms_c_chipmatch_soc(core);
5684 default:
5685 pr_err("unknown host type: %i\n", core->bus->hosttype);
5686 return false;
5687 }
5688}
5689
Arend van Spriel5b435de2011-10-05 13:19:03 +02005690u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
5691{
5692 u16 table_ptr;
5693 u8 phy_rate, index;
5694
5695 /* get the phy specific rate encoding for the PLCP SIGNAL field */
5696 if (is_ofdm_rate(rate))
5697 table_ptr = M_RT_DIRMAP_A;
5698 else
5699 table_ptr = M_RT_DIRMAP_B;
5700
5701 /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
5702 * the index into the rate table.
5703 */
5704 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
5705 index = phy_rate & 0xf;
5706
5707 /* Find the SHM pointer to the rate table entry by looking in the
5708 * Direct-map Table
5709 */
5710 return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
5711}
5712
Arend van Spriel5b435de2011-10-05 13:19:03 +02005713/*
5714 * bcmc_fid_generate:
5715 * Generate frame ID for a BCMC packet. The frag field is not used
5716 * for MC frames so is used as part of the sequence number.
5717 */
5718static inline u16
5719bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
5720 struct d11txh *txh)
5721{
5722 u16 frameid;
5723
5724 frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
5725 TXFID_QUEUE_MASK);
5726 frameid |=
5727 (((wlc->
5728 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5729 TX_BCMC_FIFO;
5730
5731 return frameid;
5732}
5733
5734static uint
5735brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
5736 u8 preamble_type)
5737{
5738 uint dur = 0;
5739
Arend van Spriel5b435de2011-10-05 13:19:03 +02005740 /*
5741 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5742 * is less than or equal to the rate of the immediately previous
5743 * frame in the FES
5744 */
5745 rspec = brcms_basic_rate(wlc, rspec);
5746 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
5747 dur =
5748 brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5749 (DOT11_ACK_LEN + FCS_LEN));
5750 return dur;
5751}
5752
5753static uint
5754brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
5755 u8 preamble_type)
5756{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005757 return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
5758}
5759
5760static uint
5761brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
5762 u8 preamble_type)
5763{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005764 /*
5765 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5766 * is less than or equal to the rate of the immediately previous
5767 * frame in the FES
5768 */
5769 rspec = brcms_basic_rate(wlc, rspec);
5770 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
5771 return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5772 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
5773 FCS_LEN));
5774}
5775
5776/* brcms_c_compute_frame_dur()
5777 *
5778 * Calculate the 802.11 MAC header DUR field for MPDU
5779 * DUR for a single frame = 1 SIFS + 1 ACK
5780 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5781 *
5782 * rate MPDU rate in unit of 500kbps
5783 * next_frag_len next MPDU length in bytes
5784 * preamble_type use short/GF or long/MM PLCP header
5785 */
5786static u16
5787brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
5788 u8 preamble_type, uint next_frag_len)
5789{
5790 u16 dur, sifs;
5791
5792 sifs = get_sifs(wlc->band);
5793
5794 dur = sifs;
5795 dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
5796
5797 if (next_frag_len) {
5798 /* Double the current DUR to get 2 SIFS + 2 ACKs */
5799 dur *= 2;
5800 /* add another SIFS and the frag time */
5801 dur += sifs;
5802 dur +=
5803 (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
5804 next_frag_len);
5805 }
5806 return dur;
5807}
5808
5809/* The opposite of brcms_c_calc_frame_time */
5810static uint
5811brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
5812 u8 preamble_type, uint dur)
5813{
5814 uint nsyms, mac_len, Ndps, kNdps;
5815 uint rate = rspec2rate(ratespec);
5816
Arend van Spriel5b435de2011-10-05 13:19:03 +02005817 if (is_mcs_rate(ratespec)) {
5818 uint mcs = ratespec & RSPEC_RATE_MASK;
5819 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
5820 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
5821 /* payload calculation matches that of regular ofdm */
5822 if (wlc->band->bandtype == BRCM_BAND_2G)
5823 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
5824 /* kNdbps = kbps * 4 */
5825 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
5826 rspec_issgi(ratespec)) * 4;
5827 nsyms = dur / APHY_SYMBOL_TIME;
5828 mac_len =
5829 ((nsyms * kNdps) -
5830 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
5831 } else if (is_ofdm_rate(ratespec)) {
5832 dur -= APHY_PREAMBLE_TIME;
5833 dur -= APHY_SIGNAL_TIME;
5834 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
5835 Ndps = rate * 2;
5836 nsyms = dur / APHY_SYMBOL_TIME;
5837 mac_len =
5838 ((nsyms * Ndps) -
5839 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
5840 } else {
5841 if (preamble_type & BRCMS_SHORT_PREAMBLE)
5842 dur -= BPHY_PLCP_SHORT_TIME;
5843 else
5844 dur -= BPHY_PLCP_TIME;
5845 mac_len = dur * rate;
5846 /* divide out factor of 2 in rate (1/2 mbps) */
5847 mac_len = mac_len / 8 / 2;
5848 }
5849 return mac_len;
5850}
5851
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02005852/*
5853 * Return true if the specified rate is supported by the specified band.
5854 * BRCM_BAND_AUTO indicates the current band.
5855 */
5856static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
5857 bool verbose)
5858{
5859 struct brcms_c_rateset *hw_rateset;
5860 uint i;
5861
5862 if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
5863 hw_rateset = &wlc->band->hw_rateset;
5864 else if (wlc->pub->_nbands > 1)
5865 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
5866 else
5867 /* other band specified and we are a single band device */
5868 return false;
5869
5870 /* check if this is a mimo rate */
5871 if (is_mcs_rate(rspec)) {
5872 if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
5873 goto error;
5874
5875 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
5876 }
5877
5878 for (i = 0; i < hw_rateset->count; i++)
5879 if (hw_rateset->rates[i] == rspec2rate(rspec))
5880 return true;
5881 error:
5882 if (verbose)
Seth Forsheeb353dda2012-11-15 08:08:03 -06005883 brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02005884 "not in hw_rateset\n", wlc->pub->unit, rspec);
5885
5886 return false;
5887}
5888
Arend van Spriel5b435de2011-10-05 13:19:03 +02005889static u32
5890mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
5891 u32 int_val)
5892{
Seth Forsheeb353dda2012-11-15 08:08:03 -06005893 struct bcma_device *core = wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005894 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
5895 u8 rate = int_val & NRATE_RATE_MASK;
5896 u32 rspec;
5897 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
5898 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
5899 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
5900 == NRATE_OVERRIDE_MCS_ONLY);
5901 int bcmerror = 0;
5902
5903 if (!ismcs)
5904 return (u32) rate;
5905
5906 /* validate the combination of rate/mcs/stf is allowed */
5907 if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
5908 /* mcs only allowed when nmode */
5909 if (stf > PHY_TXC1_MODE_SDM) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005910 brcms_err(core, "wl%d: %s: Invalid stf\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005911 wlc->pub->unit, __func__);
5912 bcmerror = -EINVAL;
5913 goto done;
5914 }
5915
5916 /* mcs 32 is a special case, DUP mode 40 only */
5917 if (rate == 32) {
5918 if (!CHSPEC_IS40(wlc->home_chanspec) ||
5919 ((stf != PHY_TXC1_MODE_SISO)
5920 && (stf != PHY_TXC1_MODE_CDD))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005921 brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
5922 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005923 bcmerror = -EINVAL;
5924 goto done;
5925 }
5926 /* mcs > 7 must use stf SDM */
5927 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
5928 /* mcs > 7 must use stf SDM */
5929 if (stf != PHY_TXC1_MODE_SDM) {
Seth Forshee913911f2012-11-15 08:08:04 -06005930 brcms_dbg_mac80211(core, "wl%d: enabling "
5931 "SDM mode for mcs %d\n",
5932 wlc->pub->unit, rate);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005933 stf = PHY_TXC1_MODE_SDM;
5934 }
5935 } else {
5936 /*
5937 * MCS 0-7 may use SISO, CDD, and for
5938 * phy_rev >= 3 STBC
5939 */
5940 if ((stf > PHY_TXC1_MODE_STBC) ||
5941 (!BRCMS_STBC_CAP_PHY(wlc)
5942 && (stf == PHY_TXC1_MODE_STBC))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005943 brcms_err(core, "wl%d: %s: Invalid STBC\n",
5944 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005945 bcmerror = -EINVAL;
5946 goto done;
5947 }
5948 }
5949 } else if (is_ofdm_rate(rate)) {
5950 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005951 brcms_err(core, "wl%d: %s: Invalid OFDM\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005952 wlc->pub->unit, __func__);
5953 bcmerror = -EINVAL;
5954 goto done;
5955 }
5956 } else if (is_cck_rate(rate)) {
5957 if ((cur_band->bandtype != BRCM_BAND_2G)
5958 || (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005959 brcms_err(core, "wl%d: %s: Invalid CCK\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005960 wlc->pub->unit, __func__);
5961 bcmerror = -EINVAL;
5962 goto done;
5963 }
5964 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005965 brcms_err(core, "wl%d: %s: Unknown rate type\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005966 wlc->pub->unit, __func__);
5967 bcmerror = -EINVAL;
5968 goto done;
5969 }
5970 /* make sure multiple antennae are available for non-siso rates */
5971 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005972 brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
Arend van Spriel5b435de2011-10-05 13:19:03 +02005973 "request\n", wlc->pub->unit, __func__);
5974 bcmerror = -EINVAL;
5975 goto done;
5976 }
5977
5978 rspec = rate;
5979 if (ismcs) {
5980 rspec |= RSPEC_MIMORATE;
5981 /* For STBC populate the STC field of the ratespec */
5982 if (stf == PHY_TXC1_MODE_STBC) {
5983 u8 stc;
5984 stc = 1; /* Nss for single stream is always 1 */
5985 rspec |= (stc << RSPEC_STC_SHIFT);
5986 }
5987 }
5988
5989 rspec |= (stf << RSPEC_STF_SHIFT);
5990
5991 if (override_mcs_only)
5992 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
5993
5994 if (issgi)
5995 rspec |= RSPEC_SHORT_GI;
5996
5997 if ((rate != 0)
5998 && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
5999 return rate;
6000
6001 return rspec;
6002done:
6003 return rate;
6004}
6005
6006/*
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006007 * Compute PLCP, but only requires actual rate and length of pkt.
6008 * Rate is given in the driver standard multiple of 500 kbps.
6009 * le is set for 11 Mbps rate if necessary.
6010 * Broken out for PRQ.
6011 */
6012
6013static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
6014 uint length, u8 *plcp)
6015{
6016 u16 usec = 0;
6017 u8 le = 0;
6018
6019 switch (rate_500) {
6020 case BRCM_RATE_1M:
6021 usec = length << 3;
6022 break;
6023 case BRCM_RATE_2M:
6024 usec = length << 2;
6025 break;
6026 case BRCM_RATE_5M5:
6027 usec = (length << 4) / 11;
6028 if ((length << 4) - (usec * 11) > 0)
6029 usec++;
6030 break;
6031 case BRCM_RATE_11M:
6032 usec = (length << 3) / 11;
6033 if ((length << 3) - (usec * 11) > 0) {
6034 usec++;
6035 if ((usec * 11) - (length << 3) >= 8)
6036 le = D11B_PLCP_SIGNAL_LE;
6037 }
6038 break;
6039
6040 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06006041 brcms_err(wlc->hw->d11core,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006042 "brcms_c_cck_plcp_set: unsupported rate %d\n",
6043 rate_500);
6044 rate_500 = BRCM_RATE_1M;
6045 usec = length << 3;
6046 break;
6047 }
6048 /* PLCP signal byte */
6049 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
6050 /* PLCP service byte */
6051 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
6052 /* PLCP length u16, little endian */
6053 plcp[2] = usec & 0xff;
6054 plcp[3] = (usec >> 8) & 0xff;
6055 /* PLCP CRC16 */
6056 plcp[4] = 0;
6057 plcp[5] = 0;
6058}
6059
6060/* Rate: 802.11 rate code, length: PSDU length in octets */
6061static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
6062{
6063 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
6064 plcp[0] = mcs;
6065 if (rspec_is40mhz(rspec) || (mcs == 32))
6066 plcp[0] |= MIMO_PLCP_40MHZ;
6067 BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
6068 plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
6069 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
6070 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
6071 plcp[5] = 0;
6072}
6073
6074/* Rate: 802.11 rate code, length: PSDU length in octets */
6075static void
6076brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
6077{
6078 u8 rate_signal;
6079 u32 tmp = 0;
6080 int rate = rspec2rate(rspec);
6081
6082 /*
6083 * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
6084 * transmitted first
6085 */
6086 rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
6087 memset(plcp, 0, D11_PHY_HDR_LEN);
6088 D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
6089
6090 tmp = (length & 0xfff) << 5;
6091 plcp[2] |= (tmp >> 16) & 0xff;
6092 plcp[1] |= (tmp >> 8) & 0xff;
6093 plcp[0] |= tmp & 0xff;
6094}
6095
6096/* Rate: 802.11 rate code, length: PSDU length in octets */
6097static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
6098 uint length, u8 *plcp)
6099{
6100 int rate = rspec2rate(rspec);
6101
6102 brcms_c_cck_plcp_set(wlc, rate, length, plcp);
6103}
6104
6105static void
6106brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
6107 uint length, u8 *plcp)
6108{
6109 if (is_mcs_rate(rspec))
6110 brcms_c_compute_mimo_plcp(rspec, length, plcp);
6111 else if (is_ofdm_rate(rspec))
6112 brcms_c_compute_ofdm_plcp(rspec, length, plcp);
6113 else
6114 brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
6115}
6116
6117/* brcms_c_compute_rtscts_dur()
6118 *
6119 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
6120 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
6121 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
6122 *
6123 * cts cts-to-self or rts/cts
6124 * rts_rate rts or cts rate in unit of 500kbps
6125 * rate next MPDU rate in unit of 500kbps
6126 * frame_len next MPDU frame length in bytes
6127 */
6128u16
6129brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
6130 u32 rts_rate,
6131 u32 frame_rate, u8 rts_preamble_type,
6132 u8 frame_preamble_type, uint frame_len, bool ba)
6133{
6134 u16 dur, sifs;
6135
6136 sifs = get_sifs(wlc->band);
6137
6138 if (!cts_only) {
6139 /* RTS/CTS */
6140 dur = 3 * sifs;
6141 dur +=
6142 (u16) brcms_c_calc_cts_time(wlc, rts_rate,
6143 rts_preamble_type);
6144 } else {
6145 /* CTS-TO-SELF */
6146 dur = 2 * sifs;
6147 }
6148
6149 dur +=
6150 (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
6151 frame_len);
6152 if (ba)
6153 dur +=
6154 (u16) brcms_c_calc_ba_time(wlc, frame_rate,
6155 BRCMS_SHORT_PREAMBLE);
6156 else
6157 dur +=
6158 (u16) brcms_c_calc_ack_time(wlc, frame_rate,
6159 frame_preamble_type);
6160 return dur;
6161}
6162
6163static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
6164{
6165 u16 phyctl1 = 0;
6166 u16 bw;
6167
6168 if (BRCMS_ISLCNPHY(wlc->band)) {
6169 bw = PHY_TXC1_BW_20MHZ;
6170 } else {
6171 bw = rspec_get_bw(rspec);
6172 /* 10Mhz is not supported yet */
6173 if (bw < PHY_TXC1_BW_20MHZ) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006174 brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006175 "not supported yet, set to 20L\n", bw);
6176 bw = PHY_TXC1_BW_20MHZ;
6177 }
6178 }
6179
6180 if (is_mcs_rate(rspec)) {
6181 uint mcs = rspec & RSPEC_RATE_MASK;
6182
6183 /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
6184 phyctl1 = rspec_phytxbyte2(rspec);
6185 /* set the upper byte of phyctl1 */
6186 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
6187 } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
6188 && !BRCMS_ISSSLPNPHY(wlc->band)) {
6189 /*
6190 * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
6191 * Data Rate. Eventually MIMOPHY would also be converted to
6192 * this format
6193 */
6194 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
6195 phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6196 } else { /* legacy OFDM/CCK */
6197 s16 phycfg;
6198 /* get the phyctl byte from rate phycfg table */
6199 phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
6200 if (phycfg == -1) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006201 brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006202 "legacy OFDM/CCK rate\n");
6203 phycfg = 0;
6204 }
6205 /* set the upper byte of phyctl1 */
6206 phyctl1 =
6207 (bw | (phycfg << 8) |
6208 (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6209 }
6210 return phyctl1;
6211}
6212
6213/*
Arend van Spriel5b435de2011-10-05 13:19:03 +02006214 * Add struct d11txh, struct cck_phy_hdr.
6215 *
6216 * 'p' data must start with 802.11 MAC header
6217 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
6218 *
6219 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
6220 *
6221 */
6222static u16
6223brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
6224 struct sk_buff *p, struct scb *scb, uint frag,
6225 uint nfrags, uint queue, uint next_frag_len)
6226{
6227 struct ieee80211_hdr *h;
6228 struct d11txh *txh;
6229 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
6230 int len, phylen, rts_phylen;
6231 u16 mch, phyctl, xfts, mainrates;
6232 u16 seq = 0, mcl = 0, status = 0, frameid = 0;
6233 u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6234 u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6235 bool use_rts = false;
6236 bool use_cts = false;
6237 bool use_rifs = false;
6238 bool short_preamble[2] = { false, false };
6239 u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6240 u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6241 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
6242 struct ieee80211_rts *rts = NULL;
6243 bool qos;
6244 uint ac;
6245 bool hwtkmic = false;
6246 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
6247#define ANTCFG_NONE 0xFF
6248 u8 antcfg = ANTCFG_NONE;
6249 u8 fbantcfg = ANTCFG_NONE;
6250 uint phyctl1_stf = 0;
6251 u16 durid = 0;
6252 struct ieee80211_tx_rate *txrate[2];
6253 int k;
6254 struct ieee80211_tx_info *tx_info;
6255 bool is_mcs;
6256 u16 mimo_txbw;
6257 u8 mimo_preamble_type;
6258
6259 /* locate 802.11 MAC header */
6260 h = (struct ieee80211_hdr *)(p->data);
6261 qos = ieee80211_is_data_qos(h->frame_control);
6262
6263 /* compute length of frame in bytes for use in PLCP computations */
Arend van Sprielad4d71f2011-11-10 20:30:26 +01006264 len = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006265 phylen = len + FCS_LEN;
6266
6267 /* Get tx_info */
6268 tx_info = IEEE80211_SKB_CB(p);
6269
6270 /* add PLCP */
6271 plcp = skb_push(p, D11_PHY_HDR_LEN);
6272
6273 /* add Broadcom tx descriptor header */
6274 txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
6275 memset(txh, 0, D11_TXH_LEN);
6276
6277 /* setup frameid */
6278 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
6279 /* non-AP STA should never use BCMC queue */
6280 if (queue == TX_BCMC_FIFO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006281 brcms_err(wlc->hw->d11core,
6282 "wl%d: %s: ASSERT queue == TX_BCMC!\n",
6283 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006284 frameid = bcmc_fid_generate(wlc, NULL, txh);
6285 } else {
6286 /* Increment the counter for first fragment */
6287 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
6288 scb->seqnum[p->priority]++;
6289
6290 /* extract fragment number from frame first */
6291 seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
6292 seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
6293 h->seq_ctrl = cpu_to_le16(seq);
6294
6295 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6296 (queue & TXFID_QUEUE_MASK);
6297 }
6298 }
6299 frameid |= queue & TXFID_QUEUE_MASK;
6300
6301 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
6302 if (ieee80211_is_beacon(h->frame_control))
6303 mcl |= TXC_IGNOREPMQ;
6304
6305 txrate[0] = tx_info->control.rates;
6306 txrate[1] = txrate[0] + 1;
6307
6308 /*
6309 * if rate control algorithm didn't give us a fallback
6310 * rate, use the primary rate
6311 */
6312 if (txrate[1]->idx < 0)
6313 txrate[1] = txrate[0];
6314
6315 for (k = 0; k < hw->max_rates; k++) {
6316 is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
6317 if (!is_mcs) {
6318 if ((txrate[k]->idx >= 0)
6319 && (txrate[k]->idx <
6320 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6321 rspec[k] =
6322 hw->wiphy->bands[tx_info->band]->
6323 bitrates[txrate[k]->idx].hw_value;
6324 short_preamble[k] =
6325 txrate[k]->
6326 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
6327 true : false;
6328 } else {
6329 rspec[k] = BRCM_RATE_1M;
6330 }
6331 } else {
6332 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
6333 NRATE_MCS_INUSE | txrate[k]->idx);
6334 }
6335
6336 /*
6337 * Currently only support same setting for primay and
6338 * fallback rates. Unify flags for each rate into a
6339 * single value for the frame
6340 */
6341 use_rts |=
6342 txrate[k]->
6343 flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
6344 use_cts |=
6345 txrate[k]->
6346 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
6347
6348
6349 /*
6350 * (1) RATE:
6351 * determine and validate primary rate
6352 * and fallback rates
6353 */
6354 if (!rspec_active(rspec[k])) {
6355 rspec[k] = BRCM_RATE_1M;
6356 } else {
6357 if (!is_multicast_ether_addr(h->addr1)) {
6358 /* set tx antenna config */
6359 brcms_c_antsel_antcfg_get(wlc->asi, false,
6360 false, 0, 0, &antcfg, &fbantcfg);
6361 }
6362 }
6363 }
6364
6365 phyctl1_stf = wlc->stf->ss_opmode;
6366
6367 if (wlc->pub->_n_enab & SUPPORT_11N) {
6368 for (k = 0; k < hw->max_rates; k++) {
6369 /*
6370 * apply siso/cdd to single stream mcs's or ofdm
6371 * if rspec is auto selected
6372 */
6373 if (((is_mcs_rate(rspec[k]) &&
6374 is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
6375 is_ofdm_rate(rspec[k]))
6376 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
6377 || !(rspec[k] & RSPEC_OVERRIDE))) {
6378 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
6379
6380 /* For SISO MCS use STBC if possible */
6381 if (is_mcs_rate(rspec[k])
6382 && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
6383 u8 stc;
6384
6385 /* Nss for single stream is always 1 */
6386 stc = 1;
6387 rspec[k] |= (PHY_TXC1_MODE_STBC <<
6388 RSPEC_STF_SHIFT) |
6389 (stc << RSPEC_STC_SHIFT);
6390 } else
6391 rspec[k] |=
6392 (phyctl1_stf << RSPEC_STF_SHIFT);
6393 }
6394
6395 /*
6396 * Is the phy configured to use 40MHZ frames? If
6397 * so then pick the desired txbw
6398 */
6399 if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
6400 /* default txbw is 20in40 SB */
6401 mimo_ctlchbw = mimo_txbw =
6402 CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
6403 wlc->band->pi))
6404 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
6405
6406 if (is_mcs_rate(rspec[k])) {
6407 /* mcs 32 must be 40b/w DUP */
6408 if ((rspec[k] & RSPEC_RATE_MASK)
6409 == 32) {
6410 mimo_txbw =
6411 PHY_TXC1_BW_40MHZ_DUP;
6412 /* use override */
6413 } else if (wlc->mimo_40txbw != AUTO)
6414 mimo_txbw = wlc->mimo_40txbw;
6415 /* else check if dst is using 40 Mhz */
6416 else if (scb->flags & SCB_IS40)
6417 mimo_txbw = PHY_TXC1_BW_40MHZ;
6418 } else if (is_ofdm_rate(rspec[k])) {
6419 if (wlc->ofdm_40txbw != AUTO)
6420 mimo_txbw = wlc->ofdm_40txbw;
6421 } else if (wlc->cck_40txbw != AUTO) {
6422 mimo_txbw = wlc->cck_40txbw;
6423 }
6424 } else {
6425 /*
6426 * mcs32 is 40 b/w only.
6427 * This is possible for probe packets on
6428 * a STA during SCAN
6429 */
6430 if ((rspec[k] & RSPEC_RATE_MASK) == 32)
6431 /* mcs 0 */
6432 rspec[k] = RSPEC_MIMORATE;
6433
6434 mimo_txbw = PHY_TXC1_BW_20MHZ;
6435 }
6436
6437 /* Set channel width */
6438 rspec[k] &= ~RSPEC_BW_MASK;
6439 if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
6440 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
6441 else
6442 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6443
6444 /* Disable short GI, not supported yet */
6445 rspec[k] &= ~RSPEC_SHORT_GI;
6446
6447 mimo_preamble_type = BRCMS_MM_PREAMBLE;
6448 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6449 mimo_preamble_type = BRCMS_GF_PREAMBLE;
6450
6451 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6452 && (!is_mcs_rate(rspec[k]))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006453 brcms_err(wlc->hw->d11core,
6454 "wl%d: %s: IEEE80211_TX_"
Arend van Spriel5b435de2011-10-05 13:19:03 +02006455 "RC_MCS != is_mcs_rate(rspec)\n",
6456 wlc->pub->unit, __func__);
6457 }
6458
6459 if (is_mcs_rate(rspec[k])) {
6460 preamble_type[k] = mimo_preamble_type;
6461
6462 /*
6463 * if SGI is selected, then forced mm
6464 * for single stream
6465 */
6466 if ((rspec[k] & RSPEC_SHORT_GI)
6467 && is_single_stream(rspec[k] &
6468 RSPEC_RATE_MASK))
6469 preamble_type[k] = BRCMS_MM_PREAMBLE;
6470 }
6471
6472 /* should be better conditionalized */
6473 if (!is_mcs_rate(rspec[0])
6474 && (tx_info->control.rates[0].
6475 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6476 preamble_type[k] = BRCMS_SHORT_PREAMBLE;
6477 }
6478 } else {
6479 for (k = 0; k < hw->max_rates; k++) {
6480 /* Set ctrlchbw as 20Mhz */
6481 rspec[k] &= ~RSPEC_BW_MASK;
6482 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6483
6484 /* for nphy, stf of ofdm frames must follow policies */
6485 if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
6486 rspec[k] &= ~RSPEC_STF_MASK;
6487 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6488 }
6489 }
6490 }
6491
6492 /* Reset these for use with AMPDU's */
6493 txrate[0]->count = 0;
6494 txrate[1]->count = 0;
6495
6496 /* (2) PROTECTION, may change rspec */
6497 if ((ieee80211_is_data(h->frame_control) ||
6498 ieee80211_is_mgmt(h->frame_control)) &&
6499 (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
6500 use_rts = true;
6501
6502 /* (3) PLCP: determine PLCP header and MAC duration,
6503 * fill struct d11txh */
6504 brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
6505 brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6506 memcpy(&txh->FragPLCPFallback,
6507 plcp_fallback, sizeof(txh->FragPLCPFallback));
6508
6509 /* Length field now put in CCK FBR CRC field */
6510 if (is_cck_rate(rspec[1])) {
6511 txh->FragPLCPFallback[4] = phylen & 0xff;
6512 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6513 }
6514
6515 /* MIMO-RATE: need validation ?? */
6516 mainrates = is_ofdm_rate(rspec[0]) ?
6517 D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
6518 plcp[0];
6519
6520 /* DUR field for main rate */
6521 if (!ieee80211_is_pspoll(h->frame_control) &&
6522 !is_multicast_ether_addr(h->addr1) && !use_rifs) {
6523 durid =
6524 brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6525 next_frag_len);
6526 h->duration_id = cpu_to_le16(durid);
6527 } else if (use_rifs) {
6528 /* NAV protect to end of next max packet size */
6529 durid =
6530 (u16) brcms_c_calc_frame_time(wlc, rspec[0],
6531 preamble_type[0],
6532 DOT11_MAX_FRAG_LEN);
6533 durid += RIFS_11N_TIME;
6534 h->duration_id = cpu_to_le16(durid);
6535 }
6536
6537 /* DUR field for fallback rate */
6538 if (ieee80211_is_pspoll(h->frame_control))
6539 txh->FragDurFallback = h->duration_id;
6540 else if (is_multicast_ether_addr(h->addr1) || use_rifs)
6541 txh->FragDurFallback = 0;
6542 else {
6543 durid = brcms_c_compute_frame_dur(wlc, rspec[1],
6544 preamble_type[1], next_frag_len);
6545 txh->FragDurFallback = cpu_to_le16(durid);
6546 }
6547
6548 /* (4) MAC-HDR: MacTxControlLow */
6549 if (frag == 0)
6550 mcl |= TXC_STARTMSDU;
6551
6552 if (!is_multicast_ether_addr(h->addr1))
6553 mcl |= TXC_IMMEDACK;
6554
6555 if (wlc->band->bandtype == BRCM_BAND_5G)
6556 mcl |= TXC_FREQBAND_5G;
6557
6558 if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
6559 mcl |= TXC_BW_40;
6560
6561 /* set AMIC bit if using hardware TKIP MIC */
6562 if (hwtkmic)
6563 mcl |= TXC_AMIC;
6564
6565 txh->MacTxControlLow = cpu_to_le16(mcl);
6566
6567 /* MacTxControlHigh */
6568 mch = 0;
6569
6570 /* Set fallback rate preamble type */
6571 if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
6572 (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
6573 if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
6574 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6575 }
6576
6577 /* MacFrameControl */
6578 memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
6579 txh->TxFesTimeNormal = cpu_to_le16(0);
6580
6581 txh->TxFesTimeFallback = cpu_to_le16(0);
6582
6583 /* TxFrameRA */
6584 memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
6585
6586 /* TxFrameID */
6587 txh->TxFrameID = cpu_to_le16(frameid);
6588
6589 /*
6590 * TxStatus, Note the case of recreating the first frag of a suppressed
6591 * frame then we may need to reset the retry cnt's via the status reg
6592 */
6593 txh->TxStatus = cpu_to_le16(status);
6594
6595 /*
6596 * extra fields for ucode AMPDU aggregation, the new fields are added to
6597 * the END of previous structure so that it's compatible in driver.
6598 */
6599 txh->MaxNMpdus = cpu_to_le16(0);
6600 txh->MaxABytes_MRT = cpu_to_le16(0);
6601 txh->MaxABytes_FBR = cpu_to_le16(0);
6602 txh->MinMBytes = cpu_to_le16(0);
6603
6604 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
6605 * furnish struct d11txh */
6606 /* RTS PLCP header and RTS frame */
6607 if (use_rts || use_cts) {
6608 if (use_rts && use_cts)
6609 use_cts = false;
6610
6611 for (k = 0; k < 2; k++) {
6612 rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
6613 false,
6614 mimo_ctlchbw);
6615 }
6616
6617 if (!is_ofdm_rate(rts_rspec[0]) &&
6618 !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
6619 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6620 rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
6621 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6622 }
6623
6624 if (!is_ofdm_rate(rts_rspec[1]) &&
6625 !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
6626 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6627 rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
6628 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6629 }
6630
6631 /* RTS/CTS additions to MacTxControlLow */
6632 if (use_cts) {
6633 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
6634 } else {
6635 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
6636 txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
6637 }
6638
6639 /* RTS PLCP header */
6640 rts_plcp = txh->RTSPhyHeader;
6641 if (use_cts)
6642 rts_phylen = DOT11_CTS_LEN + FCS_LEN;
6643 else
6644 rts_phylen = DOT11_RTS_LEN + FCS_LEN;
6645
6646 brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6647
6648 /* fallback rate version of RTS PLCP header */
6649 brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6650 rts_plcp_fallback);
6651 memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
6652 sizeof(txh->RTSPLCPFallback));
6653
6654 /* RTS frame fields... */
6655 rts = (struct ieee80211_rts *)&txh->rts_frame;
6656
6657 durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6658 rspec[0], rts_preamble_type[0],
6659 preamble_type[0], phylen, false);
6660 rts->duration = cpu_to_le16(durid);
6661 /* fallback rate version of RTS DUR field */
6662 durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
6663 rts_rspec[1], rspec[1],
6664 rts_preamble_type[1],
6665 preamble_type[1], phylen, false);
6666 txh->RTSDurFallback = cpu_to_le16(durid);
6667
6668 if (use_cts) {
6669 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6670 IEEE80211_STYPE_CTS);
6671
6672 memcpy(&rts->ra, &h->addr2, ETH_ALEN);
6673 } else {
6674 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6675 IEEE80211_STYPE_RTS);
6676
6677 memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
6678 }
6679
6680 /* mainrate
6681 * low 8 bits: main frag rate/mcs,
6682 * high 8 bits: rts/cts rate/mcs
6683 */
6684 mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
6685 D11A_PHY_HDR_GRATE(
6686 (struct ofdm_phy_hdr *) rts_plcp) :
6687 rts_plcp[0]) << 8;
6688 } else {
6689 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6690 memset((char *)&txh->rts_frame, 0,
6691 sizeof(struct ieee80211_rts));
6692 memset((char *)txh->RTSPLCPFallback, 0,
6693 sizeof(txh->RTSPLCPFallback));
6694 txh->RTSDurFallback = 0;
6695 }
6696
6697#ifdef SUPPORT_40MHZ
6698 /* add null delimiter count */
6699 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
6700 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6701 brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6702
6703#endif
6704
6705 /*
6706 * Now that RTS/RTS FB preamble types are updated, write
6707 * the final value
6708 */
6709 txh->MacTxControlHigh = cpu_to_le16(mch);
6710
6711 /*
6712 * MainRates (both the rts and frag plcp rates have
6713 * been calculated now)
6714 */
6715 txh->MainRates = cpu_to_le16(mainrates);
6716
6717 /* XtraFrameTypes */
6718 xfts = frametype(rspec[1], wlc->mimoft);
6719 xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6720 xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6721 xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
6722 XFTS_CHANNEL_SHIFT;
6723 txh->XtraFrameTypes = cpu_to_le16(xfts);
6724
6725 /* PhyTxControlWord */
6726 phyctl = frametype(rspec[0], wlc->mimoft);
6727 if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
6728 (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
6729 if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
6730 phyctl |= PHY_TXC_SHORT_HDR;
6731 }
6732
6733 /* phytxant is properly bit shifted */
6734 phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6735 txh->PhyTxControlWord = cpu_to_le16(phyctl);
6736
6737 /* PhyTxControlWord_1 */
6738 if (BRCMS_PHY_11N_CAP(wlc->band)) {
6739 u16 phyctl1 = 0;
6740
6741 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
6742 txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
6743 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
6744 txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
6745
6746 if (use_rts || use_cts) {
6747 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
6748 txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
6749 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
6750 txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
6751 }
6752
6753 /*
6754 * For mcs frames, if mixedmode(overloaded with long preamble)
6755 * is going to be set, fill in non-zero MModeLen and/or
6756 * MModeFbrLen it will be unnecessary if they are separated
6757 */
6758 if (is_mcs_rate(rspec[0]) &&
6759 (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
6760 u16 mmodelen =
6761 brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
6762 txh->MModeLen = cpu_to_le16(mmodelen);
6763 }
6764
6765 if (is_mcs_rate(rspec[1]) &&
6766 (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
6767 u16 mmodefbrlen =
6768 brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
6769 txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
6770 }
6771 }
6772
6773 ac = skb_get_queue_mapping(p);
6774 if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
6775 uint frag_dur, dur, dur_fallback;
6776
6777 /* WME: Update TXOP threshold */
6778 if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
6779 frag_dur =
6780 brcms_c_calc_frame_time(wlc, rspec[0],
6781 preamble_type[0], phylen);
6782
6783 if (rts) {
6784 /* 1 RTS or CTS-to-self frame */
6785 dur =
6786 brcms_c_calc_cts_time(wlc, rts_rspec[0],
6787 rts_preamble_type[0]);
6788 dur_fallback =
6789 brcms_c_calc_cts_time(wlc, rts_rspec[1],
6790 rts_preamble_type[1]);
6791 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
6792 dur += le16_to_cpu(rts->duration);
6793 dur_fallback +=
6794 le16_to_cpu(txh->RTSDurFallback);
6795 } else if (use_rifs) {
6796 dur = frag_dur;
6797 dur_fallback = 0;
6798 } else {
6799 /* frame + SIFS + ACK */
6800 dur = frag_dur;
6801 dur +=
6802 brcms_c_compute_frame_dur(wlc, rspec[0],
6803 preamble_type[0], 0);
6804
6805 dur_fallback =
6806 brcms_c_calc_frame_time(wlc, rspec[1],
6807 preamble_type[1],
6808 phylen);
6809 dur_fallback +=
6810 brcms_c_compute_frame_dur(wlc, rspec[1],
6811 preamble_type[1], 0);
6812 }
6813 /* NEED to set TxFesTimeNormal (hard) */
6814 txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
6815 /*
6816 * NEED to set fallback rate version of
6817 * TxFesTimeNormal (hard)
6818 */
6819 txh->TxFesTimeFallback =
6820 cpu_to_le16((u16) dur_fallback);
6821
6822 /*
6823 * update txop byte threshold (txop minus intraframe
6824 * overhead)
6825 */
6826 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6827 uint newfragthresh;
6828
6829 newfragthresh =
6830 brcms_c_calc_frame_len(wlc,
6831 rspec[0], preamble_type[0],
6832 (wlc->edcf_txop[ac] -
6833 (dur - frag_dur)));
6834 /* range bound the fragthreshold */
6835 if (newfragthresh < DOT11_MIN_FRAG_LEN)
6836 newfragthresh =
6837 DOT11_MIN_FRAG_LEN;
6838 else if (newfragthresh >
6839 wlc->usr_fragthresh)
6840 newfragthresh =
6841 wlc->usr_fragthresh;
6842 /* update the fragthresh and do txc update */
6843 if (wlc->fragthresh[queue] !=
6844 (u16) newfragthresh)
6845 wlc->fragthresh[queue] =
6846 (u16) newfragthresh;
6847 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006848 brcms_err(wlc->hw->d11core,
6849 "wl%d: %s txop invalid "
Arend van Spriel5b435de2011-10-05 13:19:03 +02006850 "for rate %d\n",
6851 wlc->pub->unit, fifo_names[queue],
6852 rspec2rate(rspec[0]));
6853 }
6854
6855 if (dur > wlc->edcf_txop[ac])
Seth Forsheeb353dda2012-11-15 08:08:03 -06006856 brcms_err(wlc->hw->d11core,
6857 "wl%d: %s: %s txop "
Arend van Spriel5b435de2011-10-05 13:19:03 +02006858 "exceeded phylen %d/%d dur %d/%d\n",
6859 wlc->pub->unit, __func__,
6860 fifo_names[queue],
6861 phylen, wlc->fragthresh[queue],
6862 dur, wlc->edcf_txop[ac]);
6863 }
6864 }
6865
6866 return 0;
6867}
6868
Seth Forsheee041f652012-11-15 08:07:56 -06006869static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
Arend van Spriel5b435de2011-10-05 13:19:03 +02006870{
Seth Forsheee041f652012-11-15 08:07:56 -06006871 struct dma_pub *dma;
6872 int fifo, ret = -ENOSPC;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006873 struct d11txh *txh;
Seth Forsheee041f652012-11-15 08:07:56 -06006874 u16 frameid = INVALIDFID;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006875
Seth Forsheee041f652012-11-15 08:07:56 -06006876 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
6877 dma = wlc->hw->di[fifo];
6878 txh = (struct d11txh *)(skb->data);
6879
6880 if (dma->txavail == 0) {
6881 /*
6882 * We sometimes get a frame from mac80211 after stopping
6883 * the queues. This only ever seems to be a single frame
6884 * and is seems likely to be a race. TX_HEADROOM should
6885 * ensure that we have enough space to handle these stray
6886 * packets, so warn if there isn't. If we're out of space
6887 * in the tx ring and the tx queue isn't stopped then
6888 * we've really got a bug; warn loudly if that happens.
6889 */
Seth Forsheeb353dda2012-11-15 08:08:03 -06006890 brcms_warn(wlc->hw->d11core,
Seth Forsheee041f652012-11-15 08:07:56 -06006891 "Received frame for tx with no space in DMA ring\n");
6892 WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
6893 skb_get_queue_mapping(skb)));
6894 return -ENOSPC;
6895 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02006896
6897 /* When a BC/MC frame is being committed to the BCMC fifo
6898 * via DMA (NOT PIO), update ucode or BSS info as appropriate.
6899 */
6900 if (fifo == TX_BCMC_FIFO)
6901 frameid = le16_to_cpu(txh->TxFrameID);
6902
Arend van Spriel5b435de2011-10-05 13:19:03 +02006903 /* Commit BCMC sequence number in the SHM frame ID location */
6904 if (frameid != INVALIDFID) {
6905 /*
6906 * To inform the ucode of the last mcast frame posted
6907 * so that it can clear moredata bit
6908 */
6909 brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
6910 }
6911
Seth Forsheee041f652012-11-15 08:07:56 -06006912 ret = brcms_c_txfifo(wlc, fifo, skb);
6913 /*
6914 * The only reason for brcms_c_txfifo to fail is because
6915 * there weren't any DMA descriptors, but we've already
6916 * checked for that. So if it does fail yell loudly.
6917 */
6918 WARN_ON_ONCE(ret);
6919
6920 return ret;
6921}
6922
Piotr Haberc4dea352012-11-28 21:44:05 +01006923bool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
Seth Forsheee041f652012-11-15 08:07:56 -06006924 struct ieee80211_hw *hw)
6925{
6926 uint fifo;
6927 struct scb *scb = &wlc->pri_scb;
6928
6929 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
Piotr Haberc4dea352012-11-28 21:44:05 +01006930 brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
6931 if (!brcms_c_tx(wlc, sdu))
6932 return true;
6933
6934 /* packet discarded */
6935 dev_kfree_skb_any(sdu);
6936 return false;
Seth Forsheee041f652012-11-15 08:07:56 -06006937}
6938
6939int
6940brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
6941{
6942 struct dma_pub *dma = wlc->hw->di[fifo];
6943 int ret;
6944 u16 queue;
6945
6946 ret = dma_txfast(wlc, dma, p);
6947 if (ret < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02006948 wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
Seth Forsheee041f652012-11-15 08:07:56 -06006949
6950 /*
6951 * Stop queue if DMA ring is full. Reserve some free descriptors,
6952 * as we sometimes receive a frame from mac80211 after the queues
6953 * are stopped.
6954 */
6955 queue = skb_get_queue_mapping(p);
6956 if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
6957 !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
6958 ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
6959
6960 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006961}
6962
Arend van Spriel5b435de2011-10-05 13:19:03 +02006963u32
6964brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
6965 bool use_rspec, u16 mimo_ctlchbw)
6966{
6967 u32 rts_rspec = 0;
6968
6969 if (use_rspec)
6970 /* use frame rate as rts rate */
6971 rts_rspec = rspec;
6972 else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
6973 /* Use 11Mbps as the g protection RTS target rate and fallback.
6974 * Use the brcms_basic_rate() lookup to find the best basic rate
6975 * under the target in case 11 Mbps is not Basic.
6976 * 6 and 9 Mbps are not usually selected by rate selection, but
6977 * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
6978 * is more robust.
6979 */
6980 rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
6981 else
6982 /* calculate RTS rate and fallback rate based on the frame rate
6983 * RTS must be sent at a basic rate since it is a
6984 * control frame, sec 9.6 of 802.11 spec
6985 */
6986 rts_rspec = brcms_basic_rate(wlc, rspec);
6987
6988 if (BRCMS_PHY_11N_CAP(wlc->band)) {
6989 /* set rts txbw to correct side band */
6990 rts_rspec &= ~RSPEC_BW_MASK;
6991
6992 /*
6993 * if rspec/rspec_fallback is 40MHz, then send RTS on both
6994 * 20MHz channel (DUP), otherwise send RTS on control channel
6995 */
6996 if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
6997 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
6998 else
6999 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
7000
7001 /* pick siso/cdd as default for ofdm */
7002 if (is_ofdm_rate(rts_rspec)) {
7003 rts_rspec &= ~RSPEC_STF_MASK;
7004 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
7005 }
7006 }
7007 return rts_rspec;
7008}
7009
Arend van Spriel5b435de2011-10-05 13:19:03 +02007010/* Update beacon listen interval in shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007011static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007012{
7013 /* wake up every DTIM is the default */
7014 if (wlc->bcn_li_dtim == 1)
7015 brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
7016 else
7017 brcms_b_write_shm(wlc->hw, M_BCN_LI,
7018 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
7019}
7020
7021static void
7022brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
7023 u32 *tsf_h_ptr)
7024{
Arend van Spriel16d28122011-12-08 15:06:51 -08007025 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007026
7027 /* read the tsf timer low, then high to get an atomic read */
Arend van Spriel16d28122011-12-08 15:06:51 -08007028 *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
7029 *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
Arend van Spriel5b435de2011-10-05 13:19:03 +02007030}
7031
7032/*
7033 * recover 64bit TSF value from the 16bit TSF value in the rx header
7034 * given the assumption that the TSF passed in header is within 65ms
7035 * of the current tsf.
7036 *
7037 * 6 5 4 4 3 2 1
7038 * 3.......6.......8.......0.......2.......4.......6.......8......0
7039 * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
7040 *
7041 * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
7042 * tsf_l is filled in by brcms_b_recv, which is done earlier in the
7043 * receive call sequence after rx interrupt. Only the higher 16 bits
7044 * are used. Finally, the tsf_h is read from the tsf register.
7045 */
7046static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
7047 struct d11rxhdr *rxh)
7048{
7049 u32 tsf_h, tsf_l;
7050 u16 rx_tsf_0_15, rx_tsf_16_31;
7051
7052 brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
7053
7054 rx_tsf_16_31 = (u16)(tsf_l >> 16);
7055 rx_tsf_0_15 = rxh->RxTSFTime;
7056
7057 /*
7058 * a greater tsf time indicates the low 16 bits of
7059 * tsf_l wrapped, so decrement the high 16 bits.
7060 */
7061 if ((u16)tsf_l < rx_tsf_0_15) {
7062 rx_tsf_16_31 -= 1;
7063 if (rx_tsf_16_31 == 0xffff)
7064 tsf_h -= 1;
7065 }
7066
7067 return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
7068}
7069
7070static void
7071prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7072 struct sk_buff *p,
7073 struct ieee80211_rx_status *rx_status)
7074{
7075 int preamble;
7076 int channel;
7077 u32 rspec;
7078 unsigned char *plcp;
7079
7080 /* fill in TSF and flag its presence */
7081 rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
Thomas Pedersenf4bda332012-11-13 10:46:27 -08007082 rx_status->flag |= RX_FLAG_MACTIME_START;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007083
7084 channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
7085
Johannes Berg858a4552012-07-24 17:35:57 +02007086 rx_status->band =
7087 channel > 14 ? IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
7088 rx_status->freq =
7089 ieee80211_channel_to_frequency(channel, rx_status->band);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007090
7091 rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
7092
7093 /* noise */
7094 /* qual */
7095 rx_status->antenna =
7096 (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
7097
7098 plcp = p->data;
7099
7100 rspec = brcms_c_compute_rspec(rxh, plcp);
7101 if (is_mcs_rate(rspec)) {
7102 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
7103 rx_status->flag |= RX_FLAG_HT;
7104 if (rspec_is40mhz(rspec))
7105 rx_status->flag |= RX_FLAG_40MHZ;
7106 } else {
7107 switch (rspec2rate(rspec)) {
7108 case BRCM_RATE_1M:
7109 rx_status->rate_idx = 0;
7110 break;
7111 case BRCM_RATE_2M:
7112 rx_status->rate_idx = 1;
7113 break;
7114 case BRCM_RATE_5M5:
7115 rx_status->rate_idx = 2;
7116 break;
7117 case BRCM_RATE_11M:
7118 rx_status->rate_idx = 3;
7119 break;
7120 case BRCM_RATE_6M:
7121 rx_status->rate_idx = 4;
7122 break;
7123 case BRCM_RATE_9M:
7124 rx_status->rate_idx = 5;
7125 break;
7126 case BRCM_RATE_12M:
7127 rx_status->rate_idx = 6;
7128 break;
7129 case BRCM_RATE_18M:
7130 rx_status->rate_idx = 7;
7131 break;
7132 case BRCM_RATE_24M:
7133 rx_status->rate_idx = 8;
7134 break;
7135 case BRCM_RATE_36M:
7136 rx_status->rate_idx = 9;
7137 break;
7138 case BRCM_RATE_48M:
7139 rx_status->rate_idx = 10;
7140 break;
7141 case BRCM_RATE_54M:
7142 rx_status->rate_idx = 11;
7143 break;
7144 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06007145 brcms_err(wlc->hw->d11core,
7146 "%s: Unknown rate\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007147 }
7148
7149 /*
7150 * For 5GHz, we should decrease the index as it is
7151 * a subset of the 2.4G rates. See bitrates field
7152 * of brcms_band_5GHz_nphy (in mac80211_if.c).
7153 */
7154 if (rx_status->band == IEEE80211_BAND_5GHZ)
7155 rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
7156
7157 /* Determine short preamble and rate_idx */
7158 preamble = 0;
7159 if (is_cck_rate(rspec)) {
7160 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
7161 rx_status->flag |= RX_FLAG_SHORTPRE;
7162 } else if (is_ofdm_rate(rspec)) {
7163 rx_status->flag |= RX_FLAG_SHORTPRE;
7164 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007165 brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007166 __func__);
7167 }
7168 }
7169
7170 if (plcp3_issgi(plcp[3]))
7171 rx_status->flag |= RX_FLAG_SHORT_GI;
7172
7173 if (rxh->RxStatus1 & RXS_DECERR) {
7174 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007175 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007176 __func__);
7177 }
7178 if (rxh->RxStatus1 & RXS_FCSERR) {
7179 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007180 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_FCS_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007181 __func__);
7182 }
7183}
7184
7185static void
7186brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7187 struct sk_buff *p)
7188{
7189 int len_mpdu;
7190 struct ieee80211_rx_status rx_status;
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007191 struct ieee80211_hdr *hdr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007192
7193 memset(&rx_status, 0, sizeof(rx_status));
7194 prep_mac80211_status(wlc, rxh, p, &rx_status);
7195
7196 /* mac header+body length, exclude CRC and plcp header */
7197 len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
7198 skb_pull(p, D11_PHY_HDR_LEN);
7199 __skb_trim(p, len_mpdu);
7200
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007201 /* unmute transmit */
7202 if (wlc->hw->suspended_fifos) {
7203 hdr = (struct ieee80211_hdr *)p->data;
7204 if (ieee80211_is_beacon(hdr->frame_control))
7205 brcms_b_mute(wlc->hw, false);
7206 }
7207
Arend van Spriel5b435de2011-10-05 13:19:03 +02007208 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7209 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7210}
7211
Arend van Spriel5b435de2011-10-05 13:19:03 +02007212/* calculate frame duration for Mixed-mode L-SIG spoofing, return
7213 * number of bytes goes in the length field
7214 *
7215 * Formula given by HT PHY Spec v 1.13
7216 * len = 3(nsyms + nstream + 3) - 3
7217 */
7218u16
7219brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
7220 uint mac_len)
7221{
7222 uint nsyms, len = 0, kNdps;
7223
Arend van Spriel5b435de2011-10-05 13:19:03 +02007224 if (is_mcs_rate(ratespec)) {
7225 uint mcs = ratespec & RSPEC_RATE_MASK;
7226 int tot_streams = (mcs_2_txstreams(mcs) + 1) +
7227 rspec_stc(ratespec);
7228
7229 /*
7230 * the payload duration calculation matches that
7231 * of regular ofdm
7232 */
7233 /* 1000Ndbps = kbps * 4 */
7234 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
7235 rspec_issgi(ratespec)) * 4;
7236
7237 if (rspec_stc(ratespec) == 0)
7238 nsyms =
7239 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7240 APHY_TAIL_NBITS) * 1000, kNdps);
7241 else
7242 /* STBC needs to have even number of symbols */
7243 nsyms =
7244 2 *
7245 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7246 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7247
7248 /* (+3) account for HT-SIG(2) and HT-STF(1) */
7249 nsyms += (tot_streams + 3);
7250 /*
7251 * 3 bytes/symbol @ legacy 6Mbps rate
7252 * (-3) excluding service bits and tail bits
7253 */
7254 len = (3 * nsyms) - 3;
7255 }
7256
7257 return (u16) len;
7258}
7259
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007260static void
7261brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007262{
7263 const struct brcms_c_rateset *rs_dflt;
7264 struct brcms_c_rateset rs;
7265 u8 rate;
7266 u16 entry_ptr;
7267 u8 plcp[D11_PHY_HDR_LEN];
7268 u16 dur, sifs;
7269 uint i;
7270
7271 sifs = get_sifs(wlc->band);
7272
7273 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
7274
7275 brcms_c_rateset_copy(rs_dflt, &rs);
7276 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7277
7278 /*
7279 * walk the phy rate table and update MAC core SHM
7280 * basic rate table entries
7281 */
7282 for (i = 0; i < rs.count; i++) {
7283 rate = rs.rates[i] & BRCMS_RATE_MASK;
7284
7285 entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
7286
7287 /* Calculate the Probe Response PLCP for the given rate */
7288 brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
7289
7290 /*
7291 * Calculate the duration of the Probe Response
7292 * frame plus SIFS for the MAC
7293 */
7294 dur = (u16) brcms_c_calc_frame_time(wlc, rate,
7295 BRCMS_LONG_PREAMBLE, frame_len);
7296 dur += sifs;
7297
7298 /* Update the SHM Rate Table entry Probe Response values */
7299 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
7300 (u16) (plcp[0] + (plcp[1] << 8)));
7301 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7302 (u16) (plcp[2] + (plcp[3] << 8)));
7303 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
7304 }
7305}
7306
7307/* Max buffering needed for beacon template/prb resp template is 142 bytes.
7308 *
7309 * PLCP header is 6 bytes.
7310 * 802.11 A3 header is 24 bytes.
7311 * Max beacon frame body template length is 112 bytes.
7312 * Max probe resp frame body template length is 110 bytes.
7313 *
7314 * *len on input contains the max length of the packet available.
7315 *
7316 * The *len value is set to the number of bytes in buf used, and starts
7317 * with the PLCP and included up to, but not including, the 4 byte FCS.
7318 */
7319static void
7320brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
7321 u32 bcn_rspec,
7322 struct brcms_bss_cfg *cfg, u16 *buf, int *len)
7323{
7324 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
7325 struct cck_phy_hdr *plcp;
7326 struct ieee80211_mgmt *h;
7327 int hdr_len, body_len;
7328
7329 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7330
7331 /* calc buffer size provided for frame body */
7332 body_len = *len - hdr_len;
7333 /* return actual size */
7334 *len = hdr_len + body_len;
7335
7336 /* format PHY and MAC headers */
7337 memset((char *)buf, 0, hdr_len);
7338
7339 plcp = (struct cck_phy_hdr *) buf;
7340
7341 /*
7342 * PLCP for Probe Response frames are filled in from
7343 * core's rate table
7344 */
7345 if (type == IEEE80211_STYPE_BEACON)
7346 /* fill in PLCP */
7347 brcms_c_compute_plcp(wlc, bcn_rspec,
7348 (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
7349 (u8 *) plcp);
7350
7351 /* "Regular" and 16 MBSS but not for 4 MBSS */
7352 /* Update the phytxctl for the beacon based on the rspec */
7353 brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7354
7355 h = (struct ieee80211_mgmt *)&plcp[1];
7356
7357 /* fill in 802.11 header */
7358 h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
7359
7360 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7361 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7362 if (type == IEEE80211_STYPE_BEACON)
7363 memcpy(&h->da, &ether_bcast, ETH_ALEN);
7364 memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
7365 memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
7366
7367 /* SEQ filled in by MAC */
7368}
7369
7370int brcms_c_get_header_len(void)
7371{
7372 return TXOFF;
7373}
7374
7375/*
7376 * Update all beacons for the system.
7377 */
7378void brcms_c_update_beacon(struct brcms_c_info *wlc)
7379{
7380 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7381
7382 if (bsscfg->up && !bsscfg->BSS)
7383 /* Clear the soft intmask */
7384 wlc->defmacintmask &= ~MI_BCNTPL;
7385}
7386
7387/* Write ssid into shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007388static void
7389brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007390{
7391 u8 *ssidptr = cfg->SSID;
7392 u16 base = M_SSID;
7393 u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
7394
7395 /* padding the ssid with zero and copy it into shm */
7396 memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
7397 memcpy(ssidbuf, ssidptr, cfg->SSID_len);
7398
7399 brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
7400 brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
7401}
7402
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007403static void
Arend van Spriel5b435de2011-10-05 13:19:03 +02007404brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
7405 struct brcms_bss_cfg *cfg,
7406 bool suspend)
7407{
7408 u16 prb_resp[BCN_TMPL_LEN / 2];
7409 int len = BCN_TMPL_LEN;
7410
7411 /*
7412 * write the probe response to hardware, or save in
7413 * the config structure
7414 */
7415
7416 /* create the probe response template */
7417 brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
7418 cfg, prb_resp, &len);
7419
7420 if (suspend)
7421 brcms_c_suspend_mac_and_wait(wlc);
7422
7423 /* write the probe response into the template region */
7424 brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7425 (len + 3) & ~3, prb_resp);
7426
7427 /* write the length of the probe response frame (+PLCP/-FCS) */
7428 brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
7429
7430 /* write the SSID and SSID length */
7431 brcms_c_shm_ssid_upd(wlc, cfg);
7432
7433 /*
7434 * Write PLCP headers and durations for probe response frames
7435 * at all rates. Use the actual frame length covered by the
7436 * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
7437 * by subtracting the PLCP len and adding the FCS.
7438 */
7439 len += (-D11_PHY_HDR_LEN + FCS_LEN);
7440 brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
7441
7442 if (suspend)
7443 brcms_c_enable_mac(wlc);
7444}
7445
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007446void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
7447{
7448 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7449
7450 /* update AP or IBSS probe responses */
7451 if (bsscfg->up && !bsscfg->BSS)
7452 brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
7453}
7454
Arend van Spriel5b435de2011-10-05 13:19:03 +02007455int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
7456 uint *blocks)
7457{
7458 if (fifo >= NFIFO)
7459 return -EINVAL;
7460
7461 *blocks = wlc_hw->xmtfifo_sz[fifo];
7462
7463 return 0;
7464}
7465
7466void
7467brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
7468 const u8 *addr)
7469{
7470 brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
7471 if (match_reg_offset == RCM_BSSID_OFFSET)
7472 memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
7473}
7474
Arend van Spriel5b435de2011-10-05 13:19:03 +02007475/*
7476 * Flag 'scan in progress' to withhold dynamic phy calibration
7477 */
7478void brcms_c_scan_start(struct brcms_c_info *wlc)
7479{
7480 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
7481}
7482
7483void brcms_c_scan_stop(struct brcms_c_info *wlc)
7484{
7485 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
7486}
7487
7488void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
7489{
7490 wlc->pub->associated = state;
7491 wlc->bsscfg->associated = state;
7492}
7493
7494/*
7495 * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
7496 * AMPDU traffic, packets pending in hardware have to be invalidated so that
7497 * when later on hardware releases them, they can be handled appropriately.
7498 */
7499void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
7500 struct ieee80211_sta *sta,
7501 void (*dma_callback_fn))
7502{
7503 struct dma_pub *dmah;
7504 int i;
7505 for (i = 0; i < NFIFO; i++) {
7506 dmah = hw->di[i];
7507 if (dmah != NULL)
7508 dma_walk_packets(dmah, dma_callback_fn, sta);
7509 }
7510}
7511
7512int brcms_c_get_curband(struct brcms_c_info *wlc)
7513{
7514 return wlc->band->bandunit;
7515}
7516
7517void brcms_c_wait_for_tx_completion(struct brcms_c_info *wlc, bool drop)
7518{
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007519 int timeout = 20;
Seth Forsheee041f652012-11-15 08:07:56 -06007520 int i;
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007521
Seth Forsheee041f652012-11-15 08:07:56 -06007522 /* Kick DMA to send any pending AMPDU */
7523 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
7524 if (wlc->hw->di[i])
7525 dma_txflush(wlc->hw->di[i]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007526
7527 /* wait for queue and DMA fifos to run dry */
Seth Forsheee041f652012-11-15 08:07:56 -06007528 while (brcms_txpktpendtot(wlc) > 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02007529 brcms_msleep(wlc->wl, 1);
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007530
7531 if (--timeout == 0)
7532 break;
7533 }
7534
7535 WARN_ON_ONCE(timeout == 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007536}
7537
7538void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
7539{
7540 wlc->bcn_li_bcn = interval;
7541 if (wlc->pub->up)
7542 brcms_c_bcn_li_upd(wlc);
7543}
7544
7545int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
7546{
7547 uint qdbm;
7548
7549 /* Remove override bit and clip to max qdbm value */
7550 qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
7551 return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
7552}
7553
7554int brcms_c_get_tx_power(struct brcms_c_info *wlc)
7555{
7556 uint qdbm;
7557 bool override;
7558
7559 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
7560
7561 /* Return qdbm units */
7562 return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
7563}
7564
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007565/* Process received frames */
7566/*
7567 * Return true if more frames need to be processed. false otherwise.
7568 * Param 'bound' indicates max. # frames to process before break out.
7569 */
7570static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
7571{
7572 struct d11rxhdr *rxh;
7573 struct ieee80211_hdr *h;
7574 uint len;
7575 bool is_amsdu;
7576
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007577 /* frame starts with rxhdr */
7578 rxh = (struct d11rxhdr *) (p->data);
7579
7580 /* strip off rxhdr */
7581 skb_pull(p, BRCMS_HWRXOFF);
7582
7583 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7584 if (rxh->RxStatus1 & RXS_PBPRES) {
7585 if (p->len < 2) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007586 brcms_err(wlc->hw->d11core,
7587 "wl%d: recv: rcvd runt of len %d\n",
7588 wlc->pub->unit, p->len);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007589 goto toss;
7590 }
7591 skb_pull(p, 2);
7592 }
7593
7594 h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
7595 len = p->len;
7596
7597 if (rxh->RxStatus1 & RXS_FCSERR) {
Alwin Beukersbe667662011-11-22 17:21:43 -08007598 if (!(wlc->filter_flags & FIF_FCSFAIL))
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007599 goto toss;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007600 }
7601
7602 /* check received pkt has at least frame control field */
7603 if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
7604 goto toss;
7605
7606 /* not supporting A-MSDU */
7607 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7608 if (is_amsdu)
7609 goto toss;
7610
7611 brcms_c_recvctl(wlc, rxh, p);
7612 return;
7613
7614 toss:
7615 brcmu_pkt_buf_free_skb(p);
7616}
7617
7618/* Process received frames */
7619/*
7620 * Return true if more frames need to be processed. false otherwise.
7621 * Param 'bound' indicates max. # frames to process before break out.
7622 */
7623static bool
7624brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
7625{
7626 struct sk_buff *p;
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007627 struct sk_buff *next = NULL;
7628 struct sk_buff_head recv_frames;
7629
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007630 uint n = 0;
7631 uint bound_limit = bound ? RXBND : -1;
Geert Uytterhoevenc2397bb2012-12-22 22:07:14 +01007632 bool morepending = false;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007633
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007634 skb_queue_head_init(&recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007635
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007636 /* gather received frames */
Piotr Haber57fe5042012-11-28 21:44:07 +01007637 do {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007638 /* !give others some time to run! */
Piotr Haber57fe5042012-11-28 21:44:07 +01007639 if (n >= bound_limit)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007640 break;
Piotr Haber57fe5042012-11-28 21:44:07 +01007641
7642 morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
7643 n++;
7644 } while (morepending);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007645
7646 /* post more rbufs */
7647 dma_rxfill(wlc_hw->di[fifo]);
7648
7649 /* process each frame */
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007650 skb_queue_walk_safe(&recv_frames, p, next) {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007651 struct d11rxhdr_le *rxh_le;
7652 struct d11rxhdr *rxh;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007653
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007654 skb_unlink(p, &recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007655 rxh_le = (struct d11rxhdr_le *)p->data;
7656 rxh = (struct d11rxhdr *)p->data;
7657
7658 /* fixup rx header endianness */
7659 rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
7660 rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
7661 rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
7662 rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
7663 rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
7664 rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
7665 rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
7666 rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
7667 rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
7668 rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
7669 rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
7670
7671 brcms_c_recv(wlc_hw->wlc, p);
7672 }
7673
Piotr Haber57fe5042012-11-28 21:44:07 +01007674 return morepending;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007675}
7676
7677/* second-level interrupt processing
7678 * Return true if another dpc needs to be re-scheduled. false otherwise.
7679 * Param 'bounded' indicates if applicable loops should be bounded.
7680 */
7681bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
7682{
7683 u32 macintstatus;
7684 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08007685 struct bcma_device *core = wlc_hw->d11core;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007686
7687 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007688 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007689 __func__);
7690 brcms_down(wlc->wl);
7691 return false;
7692 }
7693
7694 /* grab and clear the saved software intstatus bits */
7695 macintstatus = wlc->macintstatus;
7696 wlc->macintstatus = 0;
7697
Seth Forshee229a41d2012-11-15 08:08:06 -06007698 brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
7699 wlc_hw->unit, macintstatus);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007700
7701 WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
7702
7703 /* tx status */
7704 if (macintstatus & MI_TFS) {
7705 bool fatal;
7706 if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
7707 wlc->macintstatus |= MI_TFS;
7708 if (fatal) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007709 brcms_err(core, "MI_TFS: fatal\n");
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007710 goto fatal;
7711 }
7712 }
7713
7714 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
7715 brcms_c_tbtt(wlc);
7716
7717 /* ATIM window end */
7718 if (macintstatus & MI_ATIMWINEND) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007719 brcms_dbg_info(core, "end of ATIM window\n");
Arend van Spriel16d28122011-12-08 15:06:51 -08007720 bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007721 wlc->qvalid = 0;
7722 }
7723
7724 /*
7725 * received data or control frame, MI_DMAINT is
7726 * indication of RX_FIFO interrupt
7727 */
7728 if (macintstatus & MI_DMAINT)
7729 if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
7730 wlc->macintstatus |= MI_DMAINT;
7731
7732 /* noise sample collected */
7733 if (macintstatus & MI_BG_NOISE)
7734 wlc_phy_noise_sample_intr(wlc_hw->band->pi);
7735
7736 if (macintstatus & MI_GP0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007737 brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007738 "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007739
7740 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007741 __func__, ai_get_chip_id(wlc_hw->sih),
7742 ai_get_chiprev(wlc_hw->sih));
Roland Vossenc261bdf2011-10-18 14:03:04 +02007743 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007744 }
7745
7746 /* gptimer timeout */
7747 if (macintstatus & MI_TO)
Arend van Spriel16d28122011-12-08 15:06:51 -08007748 bcma_write32(core, D11REGOFFS(gptimer), 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007749
7750 if (macintstatus & MI_RFDISABLE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007751 brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
7752 " RF Disable Input\n", wlc_hw->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007753 brcms_rfkill_set_hw_state(wlc->wl);
7754 }
7755
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007756 /* it isn't done and needs to be resched if macintstatus is non-zero */
7757 return wlc->macintstatus != 0;
7758
7759 fatal:
Roland Vossenc261bdf2011-10-18 14:03:04 +02007760 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007761 return wlc->macintstatus != 0;
7762}
7763
Roland Vossendc460122011-10-21 16:16:28 +02007764void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007765{
Arend van Spriel16d28122011-12-08 15:06:51 -08007766 struct bcma_device *core = wlc->hw->d11core;
Seth Forshee91691292012-06-16 07:47:49 -05007767 struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007768 u16 chanspec;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007769
Seth Forsheeb353dda2012-11-15 08:08:03 -06007770 brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007771
Seth Forshee91691292012-06-16 07:47:49 -05007772 chanspec = ch20mhz_chspec(ch->hw_value);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007773
Roland Vossena8bc4912011-10-21 16:16:25 +02007774 brcms_b_init(wlc->hw, chanspec);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007775
7776 /* update beacon listen interval */
7777 brcms_c_bcn_li_upd(wlc);
7778
7779 /* write ethernet address to core */
7780 brcms_c_set_mac(wlc->bsscfg);
7781 brcms_c_set_bssid(wlc->bsscfg);
7782
7783 /* Update tsf_cfprep if associated and up */
7784 if (wlc->pub->associated && wlc->bsscfg->up) {
7785 u32 bi;
7786
7787 /* get beacon period and convert to uS */
7788 bi = wlc->bsscfg->current_bss->beacon_period << 10;
7789 /*
7790 * update since init path would reset
7791 * to default value
7792 */
Arend van Spriel16d28122011-12-08 15:06:51 -08007793 bcma_write32(core, D11REGOFFS(tsf_cfprep),
7794 bi << CFPREP_CBI_SHIFT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007795
7796 /* Update maccontrol PM related bits */
7797 brcms_c_set_ps_ctrl(wlc);
7798 }
7799
7800 brcms_c_bandinit_ordered(wlc, chanspec);
7801
7802 /* init probe response timeout */
7803 brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
7804
7805 /* init max burst txop (framebursting) */
7806 brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
7807 (wlc->
7808 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
7809
7810 /* initialize maximum allowed duty cycle */
7811 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
7812 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
7813
7814 /*
7815 * Update some shared memory locations related to
7816 * max AMPDU size allowed to received
7817 */
7818 brcms_c_ampdu_shm_upd(wlc->ampdu);
7819
7820 /* band-specific inits */
7821 brcms_c_bsinit(wlc);
7822
7823 /* Enable EDCF mode (while the MAC is suspended) */
Arend van Spriel16d28122011-12-08 15:06:51 -08007824 bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007825 brcms_c_edcf_setparams(wlc, false);
7826
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007827 /* read the ucode version if we have not yet done so */
7828 if (wlc->ucode_rev == 0) {
7829 wlc->ucode_rev =
7830 brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
7831 wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
7832 }
7833
7834 /* ..now really unleash hell (allow the MAC out of suspend) */
7835 brcms_c_enable_mac(wlc);
7836
Roland Vossena8bc4912011-10-21 16:16:25 +02007837 /* suspend the tx fifos and mute the phy for preism cac time */
7838 if (mute_tx)
Roland Vossenc6c44892011-10-21 16:16:26 +02007839 brcms_b_mute(wlc->hw, true);
Roland Vossena8bc4912011-10-21 16:16:25 +02007840
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007841 /* enable the RF Disable Delay timer */
Arend van Spriel16d28122011-12-08 15:06:51 -08007842 bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007843
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007844 /*
7845 * Initialize WME parameters; if they haven't been set by some other
7846 * mechanism (IOVar, etc) then read them from the hardware.
7847 */
7848 if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
7849 /* Uninitialized; read from HW */
7850 int ac;
7851
Arend van Sprielb7eec422011-11-10 20:30:18 +01007852 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007853 wlc->wme_retries[ac] =
7854 brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
7855 }
7856}
7857
7858/*
7859 * The common driver entry routine. Error codes should be unique
7860 */
7861struct brcms_c_info *
Arend van Sprielb63337a2011-12-08 15:06:47 -08007862brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
7863 bool piomode, uint *perr)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007864{
7865 struct brcms_c_info *wlc;
7866 uint err = 0;
7867 uint i, j;
7868 struct brcms_pub *pub;
7869
7870 /* allocate struct brcms_c_info state and its substructures */
Joe Perches2c208892012-06-04 12:44:17 +00007871 wlc = brcms_c_attach_malloc(unit, &err, 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007872 if (wlc == NULL)
7873 goto fail;
7874 wlc->wiphy = wl->wiphy;
7875 pub = wlc->pub;
7876
Joe Perches8ae74652012-01-15 00:38:38 -08007877#if defined(DEBUG)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007878 wlc_info_dbg = wlc;
7879#endif
7880
7881 wlc->band = wlc->bandstate[0];
7882 wlc->core = wlc->corestate;
7883 wlc->wl = wl;
7884 pub->unit = unit;
7885 pub->_piomode = piomode;
7886 wlc->bandinit_pending = false;
7887
7888 /* populate struct brcms_c_info with default values */
7889 brcms_c_info_init(wlc, unit);
7890
7891 /* update sta/ap related parameters */
7892 brcms_c_ap_upd(wlc);
7893
7894 /*
7895 * low level attach steps(all hw accesses go
7896 * inside, no more in rest of the attach)
7897 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08007898 err = brcms_b_attach(wlc, core, unit, piomode);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007899 if (err)
7900 goto fail;
7901
7902 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
7903
7904 pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
7905
7906 /* disable allowed duty cycle */
7907 wlc->tx_duty_cycle_ofdm = 0;
7908 wlc->tx_duty_cycle_cck = 0;
7909
7910 brcms_c_stf_phy_chain_calc(wlc);
7911
7912 /* txchain 1: txant 0, txchain 2: txant 1 */
7913 if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
7914 wlc->stf->txant = wlc->stf->hw_txchain - 1;
7915
7916 /* push to BMAC driver */
7917 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
7918 wlc->stf->hw_rxchain);
7919
7920 /* pull up some info resulting from the low attach */
7921 for (i = 0; i < NFIFO; i++)
7922 wlc->core->txavail[i] = wlc->hw->txavail[i];
7923
7924 memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7925 memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7926
7927 for (j = 0; j < wlc->pub->_nbands; j++) {
7928 wlc->band = wlc->bandstate[j];
7929
7930 if (!brcms_c_attach_stf_ant_init(wlc)) {
7931 err = 24;
7932 goto fail;
7933 }
7934
7935 /* default contention windows size limits */
7936 wlc->band->CWmin = APHY_CWMIN;
7937 wlc->band->CWmax = PHY_CWMAX;
7938
7939 /* init gmode value */
7940 if (wlc->band->bandtype == BRCM_BAND_2G) {
7941 wlc->band->gmode = GMODE_AUTO;
7942 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
7943 wlc->band->gmode);
7944 }
7945
7946 /* init _n_enab supported mode */
7947 if (BRCMS_PHY_11N_CAP(wlc->band)) {
7948 pub->_n_enab = SUPPORT_11N;
7949 brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
7950 ((pub->_n_enab ==
7951 SUPPORT_11N) ? WL_11N_2x2 :
7952 WL_11N_3x3));
7953 }
7954
7955 /* init per-band default rateset, depend on band->gmode */
7956 brcms_default_rateset(wlc, &wlc->band->defrateset);
7957
7958 /* fill in hw_rateset */
7959 brcms_c_rateset_filter(&wlc->band->defrateset,
7960 &wlc->band->hw_rateset, false,
7961 BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
7962 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
7963 }
7964
7965 /*
7966 * update antenna config due to
7967 * wlc->stf->txant/txchain/ant_rx_ovr change
7968 */
7969 brcms_c_stf_phy_txant_upd(wlc);
7970
7971 /* attach each modules */
7972 err = brcms_c_attach_module(wlc);
7973 if (err != 0)
7974 goto fail;
7975
7976 if (!brcms_c_timers_init(wlc, unit)) {
7977 wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
7978 __func__);
7979 err = 32;
7980 goto fail;
7981 }
7982
7983 /* depend on rateset, gmode */
7984 wlc->cmi = brcms_c_channel_mgr_attach(wlc);
7985 if (!wlc->cmi) {
7986 wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
7987 "\n", unit, __func__);
7988 err = 33;
7989 goto fail;
7990 }
7991
7992 /* init default when all parameters are ready, i.e. ->rateset */
7993 brcms_c_bss_default_init(wlc);
7994
7995 /*
7996 * Complete the wlc default state initializations..
7997 */
7998
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007999 wlc->bsscfg->wlc = wlc;
8000
8001 wlc->mimoft = FT_HT;
8002 wlc->mimo_40txbw = AUTO;
8003 wlc->ofdm_40txbw = AUTO;
8004 wlc->cck_40txbw = AUTO;
8005 brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
8006
8007 /* Set default values of SGI */
8008 if (BRCMS_SGI_CAP_PHY(wlc)) {
8009 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8010 BRCMS_N_SGI_40));
8011 } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
8012 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8013 BRCMS_N_SGI_40));
8014 } else {
8015 brcms_c_ht_update_sgi_rx(wlc, 0);
8016 }
8017
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008018 brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
8019
8020 if (perr)
8021 *perr = 0;
8022
8023 return wlc;
8024
8025 fail:
8026 wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
8027 unit, __func__, err);
8028 if (wlc)
8029 brcms_c_detach(wlc);
8030
8031 if (perr)
8032 *perr = err;
8033 return NULL;
8034}